Search in sources :

Example 61 with SerializationService

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

the class PortableTest method testPortableNestedInOthers.

@Test
public void testPortableNestedInOthers() {
    SerializationService serializationService = createSerializationService(1);
    Object o1 = new ComplexDataSerializable(new NamedPortable("test-portable", 137), new ByteArrayDataSerializable("test-data-serializable".getBytes()), new ByteArrayDataSerializable("test-data-serializable-2".getBytes()));
    Data data = serializationService.toData(o1);
    SerializationService serializationService2 = createSerializationService(2);
    Object o2 = serializationService2.toObject(data);
    assertEquals(o1, o2);
}
Also used : InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) QuickTest(com.hazelcast.test.annotation.QuickTest) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) Test(org.junit.Test)

Example 62 with SerializationService

use of com.hazelcast.internal.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();
                default:
                    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.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) Test(org.junit.Test)

Example 63 with SerializationService

use of com.hazelcast.internal.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();
                default:
                    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.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) QuickTest(com.hazelcast.test.annotation.QuickTest) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) Test(org.junit.Test)

Example 64 with SerializationService

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

the class PortableTest method testWriteObjectWithCustomSerializable.

@Test
public void testWriteObjectWithCustomSerializable() {
    SerializationConfig config = new SerializationConfig();
    SerializerConfig sc = new SerializerConfig().setImplementation(new CustomSerializationTest.FooXmlSerializer()).setTypeClass(CustomSerializationTest.Foo.class);
    config.addSerializerConfig(sc);
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setPortableVersion(1).addPortableFactory(PORTABLE_FACTORY_ID, new TestPortableFactory()).setConfig(config).build();
    CustomSerializationTest.Foo foo = new CustomSerializationTest.Foo("f");
    ObjectCarryingPortable objectCarryingPortable1 = new ObjectCarryingPortable(foo);
    Data data = serializationService.toData(objectCarryingPortable1);
    ObjectCarryingPortable objectCarryingPortable2 = serializationService.toObject(data);
    assertEquals(objectCarryingPortable1, objectCarryingPortable2);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializerConfig(com.hazelcast.config.SerializerConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) QuickTest(com.hazelcast.test.annotation.QuickTest) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) Test(org.junit.Test)

Example 65 with SerializationService

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

the class PortableTest method testWriteObjectWithPortable.

@Test
public void testWriteObjectWithPortable() {
    SerializationService serializationService = createSerializationService(1);
    NamedPortable namedPortable = new NamedPortable("name", 2);
    ObjectCarryingPortable objectCarryingPortable1 = new ObjectCarryingPortable(namedPortable);
    Data data = serializationService.toData(objectCarryingPortable1);
    ObjectCarryingPortable objectCarryingPortable2 = serializationService.toObject(data);
    assertEquals(objectCarryingPortable1, objectCarryingPortable2);
}
Also used : InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) QuickTest(com.hazelcast.test.annotation.QuickTest) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) Test(org.junit.Test)

Aggregations

SerializationService (com.hazelcast.internal.serialization.SerializationService)170 Data (com.hazelcast.internal.serialization.Data)117 Test (org.junit.Test)100 QuickTest (com.hazelcast.test.annotation.QuickTest)99 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)59 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)50 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)43 ArrayList (java.util.ArrayList)15 SerializationConfig (com.hazelcast.config.SerializationConfig)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)13 Node (com.hazelcast.instance.impl.Node)12 CustomSerializationTest (com.hazelcast.internal.serialization.impl.CustomSerializationTest)12 Accessors.getNode (com.hazelcast.test.Accessors.getNode)12 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)11 EmployeeDTO (example.serialization.EmployeeDTO)11 ExternalizableEmployeeDTO (example.serialization.ExternalizableEmployeeDTO)11 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)8 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8