use of com.nokia.dempsy.serialization.kryo.KryoOptimizer in project Dempsy by Dempsy.
the class TestDefaultSerializer method testChildClassSerializationWithRegistrationAndOptimization.
@Test
public void testChildClassSerializationWithRegistrationAndOptimization() throws Throwable {
KryoSerializer<Object> ser = new KryoSerializer<Object>(defaultMock3Optimizer);
JavaSerializer<Object> serJ = new JavaSerializer<Object>();
KryoSerializer<Object> serR = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName(), 10));
KryoSerializer<Object> serRR = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName(), 10), new Registration(Mock3.class.getName(), 11));
KryoSerializer<Object> serRROb = new KryoSerializer<Object>(defaultMock3Optimizer, new Registration(MockClass.class.getName()), new Registration(Mock3.class.getName()));
KryoSerializer<Object> serRRO = new KryoSerializer<Object>(new Registration(MockClass.class.getName(), 10), new Registration(Mock3.class.getName(), 11), new Registration(UUID.class.getName(), 12));
serRRO.setKryoOptimizer(new KryoOptimizer() {
@Override
public void preRegister(Kryo kryo) {
kryo.setRegistrationRequired(true);
@SuppressWarnings("unchecked") FieldSerializer<MockClass> mockClassSer = (FieldSerializer<MockClass>) kryo.getSerializer(MockClass.class);
mockClassSer.setFieldsCanBeNull(false);
@SuppressWarnings("unchecked") FieldSerializer<Mock2> mock2Ser = (FieldSerializer<Mock2>) kryo.getSerializer(MockClass.class);
mock2Ser.setFixedFieldTypes(true);
mock2Ser.setFieldsCanBeNull(false);
}
@Override
public void postRegister(Kryo kryo) {
com.esotericsoftware.kryo.Registration reg = kryo.getRegistration(UUID.class);
reg.setSerializer(uuidSerializer);
}
});
Mock2 o = new Mock3(1, new MockClass(2, "Hello"));
byte[] data = ser.serialize(o);
byte[] dataJ = serJ.serialize(o);
byte[] dataR = serR.serialize(o);
byte[] dataRR = serRR.serialize(o);
byte[] dataRROb = serRROb.serialize(o);
byte[] dataRRO = serRRO.serialize(o);
assertTrue(dataJ.length > data.length);
assertTrue(dataR.length < data.length);
assertTrue(dataRR.length < dataR.length);
assertTrue(dataRROb.length == dataRR.length);
assertTrue(dataRRO.length <= dataRR.length);
Mock2 o2 = (Mock2) ser.deserialize(data);
assertEquals(1, o2.getInt());
assertEquals(new MockClass(2, "Hello"), o2.getMockClass());
assertTrue(o2 instanceof Mock3);
assertEquals(1, ((Mock3) o2).myI);
assertEquals(o, serR.deserialize(dataR));
assertEquals(o, serRR.deserialize(dataRR));
assertEquals(o, serRRO.deserialize(dataRRO));
}
Aggregations