use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.
the class SerializationTest method testGlobalSerializer_withoutOverrideJavaSerializable.
@Test
public void testGlobalSerializer_withoutOverrideJavaSerializable() {
GlobalSerializerConfig globalSerializerConfig = new GlobalSerializerConfig();
globalSerializerConfig.setOverrideJavaSerialization(false);
final AtomicInteger writeCounter = new AtomicInteger();
final AtomicInteger readCounter = new AtomicInteger();
SerializationConfig serializationConfig = new SerializationConfig().setGlobalSerializerConfig(globalSerializerConfig.setImplementation(new StreamSerializer<Object>() {
@Override
public void write(ObjectDataOutput out, Object v) throws IOException {
writeCounter.incrementAndGet();
out.writeUTF(((DummyValue) v).s);
out.writeInt(((DummyValue) v).k);
}
@Override
public Object read(ObjectDataInput in) throws IOException {
readCounter.incrementAndGet();
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(1, 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(1, readCounter.get());
}
use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.
the class SerializerHookLoaderTest method testLoad_typeException.
@Test(expected = HazelcastSerializationException.class)
public void testLoad_typeException() throws Exception {
SerializerConfig serializerConfig = new SerializerConfig();
serializerConfig.setClassName("com.hazelcast.internal.serialization.impl.TestSerializerHook$TestSerializer");
serializerConfig.setTypeClassName("NOT FOUND CLASS");
SerializationConfig serializationConfig = getConfig().getSerializationConfig();
serializationConfig.addSerializerConfig(serializerConfig);
new SerializerHookLoader(serializationConfig, classLoader);
}
use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.
the class SerializerHookLoaderTest method testLoad_implException.
@Test(expected = HazelcastSerializationException.class)
public void testLoad_implException() throws Exception {
SerializerConfig serializerConfig = new SerializerConfig();
serializerConfig.setClassName("NOT FOUND CLASS");
serializerConfig.setTypeClassName("com.hazelcast.nio.serialization.SampleIdentifiedDataSerializable");
SerializationConfig serializationConfig = getConfig().getSerializationConfig();
serializationConfig.addSerializerConfig(serializerConfig);
new SerializerHookLoader(serializationConfig, classLoader);
}
use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.
the class PortableTest method testClassDefinitionConfig.
@Test
public void testClassDefinitionConfig() throws Exception {
int portableVersion = 1;
SerializationConfig serializationConfig = new SerializationConfig();
serializationConfig.addPortableFactory(PORTABLE_FACTORY_ID, new TestPortableFactory());
serializationConfig.setPortableVersion(portableVersion);
serializationConfig.addClassDefinition(new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, TestSerializationConstants.RAW_DATA_PORTABLE, portableVersion).addLongField("l").addCharArrayField("c").addPortableField("p", createNamedPortableClassDefinition(portableVersion)).build()).addClassDefinition(new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE, portableVersion).addUTFField("name").addIntField("myint").build());
SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
RawDataPortable p = new RawDataPortable(System.currentTimeMillis(), "test chars".toCharArray(), new NamedPortable("named portable", 34567), 9876, "Testing raw portable", new ByteArrayDataSerializable("test bytes".getBytes()));
final Data data = serializationService.toData(p);
assertEquals(p, serializationService.toObject(data));
}
use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.
the class PortableTest method testClassDefinitionConfigWithErrors.
@Test
public void testClassDefinitionConfigWithErrors() throws Exception {
SerializationConfig serializationConfig = new SerializationConfig();
serializationConfig.addPortableFactory(PORTABLE_FACTORY_ID, new TestPortableFactory());
serializationConfig.setPortableVersion(1);
serializationConfig.addClassDefinition(new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, TestSerializationConstants.RAW_DATA_PORTABLE, 1).addLongField("l").addCharArrayField("c").addPortableField("p", createNamedPortableClassDefinition(1)).build());
try {
new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
fail("Should throw HazelcastSerializationException!");
} catch (HazelcastSerializationException ignored) {
}
new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setCheckClassDefErrors(false).build();
// -- OR --
serializationConfig.setCheckClassDefErrors(false);
new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
}
Aggregations