use of com.hazelcast.nio.ObjectDataInput in project hazelcast by hazelcast.
the class SerializationTest method testEmptyData.
@Test
public void testEmptyData() {
SerializationConfig serializationConfig = new SerializationConfig().addSerializerConfig(new SerializerConfig().setTypeClass(SingletonValue.class).setImplementation(new StreamSerializer<SingletonValue>() {
@Override
public void write(ObjectDataOutput out, SingletonValue v) throws IOException {
}
@Override
public SingletonValue read(ObjectDataInput in) throws IOException {
return new SingletonValue();
}
@Override
public int getTypeId() {
return 123;
}
@Override
public void destroy() {
}
}));
SerializationService ss1 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
Data data = ss1.toData(new SingletonValue());
Assert.assertNotNull(data);
SerializationService ss2 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
Object o = ss2.toObject(data);
Assert.assertEquals(new SingletonValue(), o);
}
use of com.hazelcast.nio.ObjectDataInput in project hazelcast by hazelcast.
the class SerializationTest method testGlobalSerializer_withOverrideJavaSerializable.
@Test
public void testGlobalSerializer_withOverrideJavaSerializable() {
GlobalSerializerConfig globalSerializerConfig = new GlobalSerializerConfig();
globalSerializerConfig.setOverrideJavaSerialization(true);
final AtomicInteger writeCounter = new AtomicInteger();
final AtomicInteger readCounter = new AtomicInteger();
final JavaSerializer javaSerializer = new JavaSerializer(true, false);
SerializationConfig serializationConfig = new SerializationConfig().setGlobalSerializerConfig(globalSerializerConfig.setImplementation(new StreamSerializer<Object>() {
@Override
public void write(ObjectDataOutput out, Object v) throws IOException {
writeCounter.incrementAndGet();
if (v instanceof Serializable) {
out.writeBoolean(true);
javaSerializer.write(out, v);
} else if (v instanceof DummyValue) {
out.writeBoolean(false);
out.writeUTF(((DummyValue) v).s);
out.writeInt(((DummyValue) v).k);
}
}
@Override
public Object read(ObjectDataInput in) throws IOException {
readCounter.incrementAndGet();
boolean java = in.readBoolean();
if (java) {
return javaSerializer.read(in);
}
return new DummyValue(in.readUTF(), in.readInt());
}
public int getTypeId() {
return 123;
}
public void destroy() {
}
}));
SerializationService ss1 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
DummyValue value = new DummyValue("test", 111);
Data data1 = ss1.toData(value);
Data data2 = ss1.toData(new Foo());
Assert.assertNotNull(data1);
Assert.assertNotNull(data2);
assertEquals(2, writeCounter.get());
SerializationService ss2 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
Object o1 = ss2.toObject(data1);
Object o2 = ss2.toObject(data2);
Assert.assertEquals(value, o1);
Assert.assertNotNull(o2);
assertEquals(2, readCounter.get());
}
use of com.hazelcast.nio.ObjectDataInput in project hazelcast-jet by hazelcast.
the class DistributedLongSummaryStatisticsTest method readData.
@Test
public void readData() throws IOException {
// Given
DistributedLongSummaryStatistics stats = new DistributedLongSummaryStatistics();
ObjectDataInput in = mock(ObjectDataInput.class);
when(in.readLong()).thenReturn(1L).thenReturn(2L).thenReturn(3L).thenReturn(4L);
// When
stats.readData(in);
// Then
assertEquals(1, stats.getCount());
assertEquals(2, stats.getSum());
assertEquals(3, stats.getMin());
assertEquals(4, stats.getMax());
}
use of com.hazelcast.nio.ObjectDataInput in project hazelcast-jet by hazelcast.
the class DistributedDoubleSummaryStatisticsTest method readData.
@Test
public void readData() throws IOException {
// Given
DistributedDoubleSummaryStatistics stats = new DistributedDoubleSummaryStatistics();
ObjectDataInput in = mock(ObjectDataInput.class);
when(in.readLong()).thenReturn(1L);
when(in.readDouble()).thenReturn(2.0).thenReturn(3.0).thenReturn(4.0);
// When
stats.readData(in);
// Then
assertEquals(1, stats.getCount());
assertEquals(2.0, stats.getSum(), 0.0);
assertEquals(3.0, stats.getMin(), 0.0);
assertEquals(4.0, stats.getMax(), 0.0);
}
use of com.hazelcast.nio.ObjectDataInput in project hazelcast-jet by hazelcast.
the class HashMapHook method createSerializer.
@Override
@SuppressWarnings("checkstyle:anoninnerlength")
public Serializer createSerializer() {
return new StreamSerializer<HashMap>() {
@Override
public int getTypeId() {
return SerializerHookConstants.HASH_MAP;
}
@Override
public void destroy() {
}
@Override
@SuppressWarnings("checkstyle:illegaltype")
public void write(ObjectDataOutput out, HashMap map) throws IOException {
out.writeInt(map.size());
for (Object o : map.entrySet()) {
Map.Entry e = (Map.Entry) o;
out.writeObject(e.getKey());
out.writeObject(e.getValue());
}
}
@Override
@SuppressWarnings("checkstyle:illegaltype")
public HashMap read(ObjectDataInput in) throws IOException {
int length = in.readInt();
HashMap map = new HashMap();
for (int i = 0; i < length; i++) {
// noinspection unchecked
map.put(in.readObject(), in.readObject());
}
return map;
}
};
}
Aggregations