-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow subclasses of DefaultBindableModel to be serialized (#20)
* Allow subclasses of DefaultBindableModel to be serialized + Bumps gradle version to 4.1 + Fixes up gitignore + Adds a test that uses FST serialization * remove volatile
- Loading branch information
1 parent
97f34f6
commit 358b773
Showing
6 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,10 @@ target/ | |
.*.swp | ||
*~ | ||
.settings | ||
build/ | ||
build/ | ||
out/ | ||
.classpath | ||
.project | ||
*.iml | ||
*.ipr | ||
*.iws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/com/palantir/ptoss/cinch/SerializableBindableModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.palantir.ptoss.cinch; | ||
|
||
import com.palantir.ptoss.cinch.core.DefaultBindableModel; | ||
|
||
import java.io.Serializable; | ||
|
||
public class SerializableBindableModel extends DefaultBindableModel implements Serializable { | ||
|
||
private final String data; | ||
|
||
public SerializableBindableModel(String data) { | ||
super(); | ||
this.data = data; | ||
} | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SerializableBindableModel{" + | ||
"data='" + data + '\'' + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
SerializableBindableModel that = (SerializableBindableModel) o; | ||
|
||
return data != null ? data.equals(that.data) : that.data == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return data != null ? data.hashCode() : 0; | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/com/palantir/ptoss/cinch/SerializableModelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.palantir.ptoss.cinch; | ||
|
||
import com.palantir.ptoss.cinch.core.Binding; | ||
import com.palantir.ptoss.cinch.core.ModelUpdate; | ||
import org.apache.commons.lang.SerializationUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.nustaq.serialization.FSTConfiguration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class SerializableModelTest { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(SerializableModelTest.class); | ||
private static FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); | ||
|
||
private final Binding testBinding = new Binding() { | ||
@Override | ||
public <T extends Enum<?> & ModelUpdate> void update(T... changed) { | ||
log.info("Ran update"); | ||
} | ||
}; | ||
|
||
@Test | ||
public void testJavaSerialize() { | ||
SerializableBindableModel model = new SerializableBindableModel("testing"); | ||
javaRoundTrip(model); | ||
} | ||
|
||
@Test | ||
public void testJavaSerializeWithBinding() { | ||
SerializableBindableModel model = new SerializableBindableModel("testing"); | ||
model.bind(testBinding); | ||
model.update(); | ||
SerializableBindableModel rehydrated = javaRoundTrip(model); | ||
rehydrated.bind(testBinding); | ||
rehydrated.update(); | ||
} | ||
|
||
private SerializableBindableModel javaRoundTrip(SerializableBindableModel model) { | ||
SerializableBindableModel rehydrated = (SerializableBindableModel) SerializationUtils.clone(model); | ||
Assert.assertEquals(model, rehydrated); | ||
return rehydrated; | ||
} | ||
|
||
@Test | ||
public void testFstSerialize() { | ||
SerializableBindableModel model = new SerializableBindableModel("testing"); | ||
fstRoundTrip(model); | ||
} | ||
|
||
@Test | ||
public void testFstSerializeWithBinding() { | ||
SerializableBindableModel model = new SerializableBindableModel("testing"); | ||
model.bind(testBinding); | ||
model.update(); | ||
SerializableBindableModel rehydrated = fstRoundTrip(model); | ||
rehydrated.bind(testBinding); | ||
rehydrated.update(); | ||
} | ||
|
||
private SerializableBindableModel fstRoundTrip(SerializableBindableModel model) { | ||
byte[] bytes = conf.asByteArray(model); | ||
SerializableBindableModel rehydrated = (SerializableBindableModel)conf.asObject(bytes); | ||
Assert.assertEquals(model, rehydrated); | ||
return rehydrated; | ||
} | ||
} |