Search in sources :

Example 16 with SerializationConfig

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());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 17 with SerializationConfig

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);
}
Also used : SerializerConfig(com.hazelcast.config.SerializerConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 18 with SerializationConfig

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);
}
Also used : SerializerConfig(com.hazelcast.config.SerializerConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 19 with SerializationConfig

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));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationConfig(com.hazelcast.config.SerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 20 with SerializationConfig

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();
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationConfig(com.hazelcast.config.SerializationConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SerializationConfig (com.hazelcast.config.SerializationConfig)24 QuickTest (com.hazelcast.test.annotation.QuickTest)18 Test (org.junit.Test)18 SerializerConfig (com.hazelcast.config.SerializerConfig)14 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)11 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 SerializationService (com.hazelcast.spi.serialization.SerializationService)6 ClientConfig (com.hazelcast.client.config.ClientConfig)5 GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)4 HeapData (com.hazelcast.internal.serialization.impl.HeapData)3 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)3 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)3 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)3 Config (com.hazelcast.config.Config)2 PartitioningStrategy (com.hazelcast.core.PartitioningStrategy)2 SerializationServiceBuilder (com.hazelcast.internal.serialization.SerializationServiceBuilder)2 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)2 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)2