Search in sources :

Example 51 with SerializationService

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

the class PortableTest method testWriteObject_withPortable.

@Test
public void testWriteObject_withPortable() {
    SerializationService ss = new DefaultSerializationServiceBuilder().addPortableFactory(PORTABLE_FACTORY_ID, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            return new NamedPortableV2();
        }
    }).build();
    SerializationService ss2 = new DefaultSerializationServiceBuilder().addPortableFactory(PORTABLE_FACTORY_ID, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            return new NamedPortable();
        }
    }).setPortableVersion(5).build();
    Object o1 = new ComplexDataSerializable(new NamedPortableV2("test", 123, 500), new ByteArrayDataSerializable(new byte[3]), null);
    Data data = ss.toData(o1);
    Object o2 = ss2.toObject(data);
    assertEquals(o1, o2);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 52 with SerializationService

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

the class PortableTest method testGenericPortable_whenMultipleTypesAreUsed.

@Test(expected = HazelcastSerializationException.class)
public void testGenericPortable_whenMultipleTypesAreUsed() {
    SerializationService ss = new DefaultSerializationServiceBuilder().addPortableFactory(1, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            switch(classId) {
                case ParentGenericPortable.CLASS_ID:
                    return new ParentGenericPortable();
                case ChildGenericPortable1.CLASS_ID:
                    return new ChildGenericPortable1();
                case ChildGenericPortable2.CLASS_ID:
                    return new ChildGenericPortable2();
            }
            throw new IllegalArgumentException();
        }
    }).build();
    ss.toData(new ParentGenericPortable<ChildGenericPortable1>(new ChildGenericPortable1("aaa", "bbb")));
    Data data = ss.toData(new ParentGenericPortable<ChildGenericPortable2>(new ChildGenericPortable2("ccc")));
    ss.toObject(data);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 53 with SerializationService

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

the class PortableTest method testWriteRead_withNullPortableArray.

@Test
public void testWriteRead_withNullPortableArray() {
    int portableVersion = 1;
    ClassDefinitionBuilder builder0 = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, 1, portableVersion);
    ClassDefinitionBuilder builder1 = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, 2, portableVersion);
    builder0.addPortableArrayField("list", builder1.build());
    SerializationService ss = new DefaultSerializationServiceBuilder().setPortableVersion(portableVersion).addClassDefinition(builder0.build()).addClassDefinition(builder1.build()).build();
    Data data = ss.toData(new TestObject1());
    SerializationService ss2 = new DefaultSerializationServiceBuilder().addPortableFactory(1, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            switch(classId) {
                case 1:
                    return new TestObject1();
                case 2:
                    return new TestObject2();
            }
            return null;
        }
    }).build();
    Object object = ss2.toObject(data);
    assertNotNull(object);
    assertTrue(object instanceof TestObject1);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 54 with SerializationService

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

the class PortableTest method testWriteData_withPortable.

@Test
public void testWriteData_withPortable() {
    SerializationService ss = new DefaultSerializationServiceBuilder().addPortableFactory(PORTABLE_FACTORY_ID, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            return new NamedPortableV2();
        }
    }).build();
    SerializationService ss2 = new DefaultSerializationServiceBuilder().addPortableFactory(PORTABLE_FACTORY_ID, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            return new NamedPortable();
        }
    }).setPortableVersion(5).build();
    Portable p1 = new NamedPortableV2("test", 456, 500);
    Object o1 = new DataDataSerializable(ss.toData(p1));
    Data data = ss.toData(o1);
    DataDataSerializable o2 = ss2.toObject(data);
    assertEquals(o1, o2);
    Portable p2 = ss2.toObject(o2.data);
    assertEquals(p1, p2);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 55 with SerializationService

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

the class PortableTest method testRawDataWithoutRegistering.

@Test
public void testRawDataWithoutRegistering() {
    final SerializationService serializationService = createSerializationService(1);
    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 : InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) 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