Add a test case for externalizable

This commit is contained in:
Peter Lawrey
2018-06-05 11:32:48 +01:00
parent 39227e0d00
commit be351fc856
2 changed files with 64 additions and 0 deletions
@@ -0,0 +1,32 @@
package net.openhft.chronicle.map.externalizable;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class ExternalizableTest {
@Test
public void externalizable() throws IOException {
String path = OS.TARGET + "/test-" + System.nanoTime() + ".map";
new File(path).deleteOnExit();
try (ChronicleMap<Long, SomeClass> storage = ChronicleMapBuilder
.of(Long.class, SomeClass.class)
.averageValueSize(128)
.entries(128)
.createPersistedTo(new File(path))) {
SomeClass value = new SomeClass();
value.hits.add("one");
value.hits.add("two");
storage.put(1L, value);
SomeClass value2 = storage.get(1L);
assertEquals(value.hits, value2.hits);
}
}
}
@@ -0,0 +1,32 @@
package net.openhft.chronicle.map.externalizable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;
public class SomeClass implements Externalizable {
final List<String> hits = new ArrayList<>();
private long id;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeLong(id);
out.writeInt(hits.size());
for (int i = 0; i < hits.size(); i++) {
out.writeObject(hits.get(i));
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
id = in.readLong();
int size = in.readInt();
for (int i = 0; i < size; i++) {
hits.add((String) in.readObject());
}
}
}