Search in sources :

Example 41 with SerializationService

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

Example 42 with SerializationService

use of com.hazelcast.spi.serialization.SerializationService in project hazelcast by hazelcast.

the class SerializationTest method testCompressionOnSerializables.

/**
     * Ensures that SerializationService correctly handles compressed Serializables,
     * using a Properties object as a test case.
     */
@Test
public void testCompressionOnSerializables() {
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setEnableCompression(true).build();
    long key = 1, value = 5000;
    Properties properties = new Properties();
    properties.put(key, value);
    Data data = serializationService.toData(properties);
    Properties output = serializationService.toObject(data);
    assertEquals(value, output.get(key));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) Properties(java.util.Properties) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 43 with SerializationService

use of com.hazelcast.spi.serialization.SerializationService in project hazelcast by hazelcast.

the class SerializationTest method testDynamicProxySerialization_withContextClassLoader.

@Test
public void testDynamicProxySerialization_withContextClassLoader() {
    ClassLoader oldContextLoader = Thread.currentThread().getContextClassLoader();
    try {
        ClassLoader current = getClass().getClassLoader();
        DynamicProxyTestClassLoader cl = new DynamicProxyTestClassLoader(current);
        Thread.currentThread().setContextClassLoader(cl);
        SerializationService ss = new DefaultSerializationServiceBuilder().setClassLoader(cl).build();
        IObjectA oa = (IObjectA) Proxy.newProxyInstance(current, new Class[] { IObjectA.class }, DummyInvocationHandler.INSTANCE);
        Data data = ss.toData(oa);
        Object o = ss.toObject(data);
        Assert.assertSame("context classloader is not used", cl, o.getClass().getClassLoader());
        try {
            IObjectA.class.cast(o);
            Assert.fail("the serialized object should not be castable");
        } catch (ClassCastException expected) {
        // expected
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldContextLoader);
    }
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 44 with SerializationService

use of com.hazelcast.spi.serialization.SerializationService in project hazelcast by hazelcast.

the class SerializationTest method testCompressionOnExternalizables.

/**
     * Ensures that SerializationService correctly handles compressed Serializables,
     * using a test-specific object as a test case.
     */
@Test
public void testCompressionOnExternalizables() {
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setEnableCompression(true).build();
    String test = "test";
    ExternalizableString ex = new ExternalizableString(test);
    Data data = serializationService.toData(ex);
    ExternalizableString actual = serializationService.toObject(data);
    assertEquals(test, actual.value);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 45 with SerializationService

use of com.hazelcast.spi.serialization.SerializationService in project hazelcast by hazelcast.

the class SerializationTest method testPartitionHash.

@Test
public void testPartitionHash() {
    PartitioningStrategy partitionStrategy = new PartitioningStrategy() {

        @Override
        public Object getPartitionKey(Object key) {
            return key.hashCode();
        }
    };
    SerializationService ss = new DefaultSerializationServiceBuilder().build();
    String obj = String.valueOf(System.nanoTime());
    Data dataWithPartitionHash = ss.toData(obj, partitionStrategy);
    Data dataWithOutPartitionHash = ss.toData(obj);
    assertTrue(dataWithPartitionHash.hasPartitionHash());
    assertNotEquals(dataWithPartitionHash.hashCode(), dataWithPartitionHash.getPartitionHash());
    assertFalse(dataWithOutPartitionHash.hasPartitionHash());
    assertEquals(dataWithOutPartitionHash.hashCode(), dataWithOutPartitionHash.getPartitionHash());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) PartitioningStrategy(com.hazelcast.core.PartitioningStrategy) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SerializationService (com.hazelcast.spi.serialization.SerializationService)120 Test (org.junit.Test)59 QuickTest (com.hazelcast.test.annotation.QuickTest)56 Data (com.hazelcast.nio.serialization.Data)54 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)46 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)28 ParallelTest (com.hazelcast.test.annotation.ParallelTest)22 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)18 HeapData (com.hazelcast.internal.serialization.impl.HeapData)17 Map (java.util.Map)15 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)10 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)9 ClientDelegatingFuture (com.hazelcast.client.util.ClientDelegatingFuture)8 Node (com.hazelcast.instance.Node)7 SerializationConfig (com.hazelcast.config.SerializationConfig)6 ICacheService (com.hazelcast.cache.impl.ICacheService)5 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)5 Record (com.hazelcast.map.impl.record.Record)5 ICacheRecordStore (com.hazelcast.cache.impl.ICacheRecordStore)3