Search in sources :

Example 66 with SerializationConfig

use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.

the class CompactStreamSerializerValueReaderSpecTest method executeTestScenario.

@Test
@SuppressWarnings("unchecked")
public void executeTestScenario() throws Exception {
    // handle result
    Object resultToMatch = expectedResult;
    if (expectedResult instanceof Class) {
        // expected exception case
        expected.expect(isA((Class) expectedResult));
    } else if (expectedResult instanceof List) {
        // just convenience -> if result is a list if will be compared to an array, so it has to be converted
        resultToMatch = ((List) resultToMatch).toArray();
    }
    // print test scenario for debug purposes
    // it makes debugging easier since all scenarios are generated
    printlnScenarioDescription(resultToMatch);
    SchemaService schemaService = CompactTestUtil.createInMemorySchemaService();
    SerializationConfig serializationConfig = new SerializationConfig();
    serializationConfig.setCompactSerializationConfig(new CompactSerializationConfig().setEnabled(true));
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
    Data data = ss.toData(inputObject);
    GenericRecordQueryReader reader = new GenericRecordQueryReader(ss.readAsInternalGenericRecord(data));
    Object result = reader.read(pathToRead);
    if (result instanceof MultiResult) {
        MultiResult multiResult = (MultiResult) result;
        if (multiResult.getResults().size() == 1 && multiResult.getResults().get(0) == null && multiResult.isNullEmptyTarget()) {
            // explode null in case of a single multi-result target result
            result = null;
        } else {
            // in case of multi result while invoking generic "read" method deal with the multi results
            result = ((MultiResult) result).getResults().toArray();
        }
    }
    assertThat(result, equalTo(resultToMatch));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) MultiResult(com.hazelcast.query.impl.getters.MultiResult) SchemaService(com.hazelcast.internal.serialization.impl.compact.SchemaService) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) GenericRecordQueryReader(com.hazelcast.internal.serialization.impl.GenericRecordQueryReader) GroupObject(com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.GroupObject) PrimitiveObject(com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.PrimitiveObject) NestedGroupObject(com.hazelcast.internal.serialization.impl.compact.reader.CompactValueReaderTestStructure.NestedGroupObject) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Data(com.hazelcast.internal.serialization.Data) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 67 with SerializationConfig

use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.

the class CompactStreamSerializerTest method testSchemaEvolution_fieldRemoved.

@Test
public void testSchemaEvolution_fieldRemoved() {
    SerializationConfig serializationConfig = new SerializationConfig();
    // Using this registration to mimic schema evolution. This is usage is not advised.
    serializationConfig.getCompactSerializationConfig().setEnabled(true).register(EmployeeDTO.class, EmployeeDTO.class.getName(), new CompactSerializer<EmployeeDTO>() {

        @Nonnull
        @Override
        public EmployeeDTO read(@Nonnull CompactReader in) {
            throw new UnsupportedOperationException("We will not read from here on this test");
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull EmployeeDTO object) {
            out.writeInt32("age", object.getAge());
        }
    });
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
    EmployeeDTO expected = new EmployeeDTO(20, 102310312);
    Data data = serializationService.toData(expected);
    SerializationService serializationService2 = createSerializationService();
    EmployeeDTO actual = serializationService2.toObject(data);
    assertEquals(expected.getAge(), actual.getAge());
    assertEquals(0, actual.getId());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) EmployeeDTO(example.serialization.EmployeeDTO) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) Nonnull(javax.annotation.Nonnull) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 68 with SerializationConfig

use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.

the class SerializationIssueTest 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.writeString(((DummyValue) v).s);
            out.writeInt(((DummyValue) v).k);
        }

        @Override
        public Object read(ObjectDataInput in) throws IOException {
            readCounter.incrementAndGet();
            return new DummyValue(in.readString(), 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 : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) StreamSerializer(com.hazelcast.nio.serialization.StreamSerializer) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 69 with SerializationConfig

use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.

the class SerializationIssueTest method testGlobalSerializer_withOverrideJavaSerializable.

@Test
public void testGlobalSerializer_withOverrideJavaSerializable() {
    GlobalSerializerConfig globalSerializerConfig = new GlobalSerializerConfig();
    globalSerializerConfig.setOverrideJavaSerialization(true);
    final AtomicInteger writeCounter = new AtomicInteger();
    final AtomicInteger readCounter = new AtomicInteger();
    final JavaSerializer javaSerializer = new JavaSerializer(true, false, null);
    SerializationConfig serializationConfig = new SerializationConfig().setGlobalSerializerConfig(globalSerializerConfig.setImplementation(new StreamSerializer<Object>() {

        @Override
        public void write(ObjectDataOutput out, Object v) throws IOException {
            writeCounter.incrementAndGet();
            if (v instanceof Serializable) {
                out.writeBoolean(true);
                javaSerializer.write(out, v);
            } else if (v instanceof DummyValue) {
                out.writeBoolean(false);
                out.writeString(((DummyValue) v).s);
                out.writeInt(((DummyValue) v).k);
            }
        }

        @Override
        public Object read(ObjectDataInput in) throws IOException {
            readCounter.incrementAndGet();
            boolean java = in.readBoolean();
            if (java) {
                return javaSerializer.read(in);
            }
            return new DummyValue(in.readString(), 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(2, 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(2, readCounter.get());
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) Serializable(java.io.Serializable) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) JavaSerializer(com.hazelcast.internal.serialization.impl.defaultserializers.JavaDefaultSerializers.JavaSerializer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamSerializer(com.hazelcast.nio.serialization.StreamSerializer) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 70 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() {
    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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SerializationConfig (com.hazelcast.config.SerializationConfig)73 Test (org.junit.Test)45 QuickTest (com.hazelcast.test.annotation.QuickTest)37 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)36 CompactSerializationConfig (com.hazelcast.config.CompactSerializationConfig)23 SerializerConfig (com.hazelcast.config.SerializerConfig)21 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)21 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)21 Data (com.hazelcast.internal.serialization.Data)18 SerializationService (com.hazelcast.internal.serialization.SerializationService)14 GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)11 ClientConfig (com.hazelcast.client.config.ClientConfig)8 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)7 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)7 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)6 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)6 SerializationService (com.hazelcast.spi.serialization.SerializationService)6 ExternalizableEmployeeDTO (example.serialization.ExternalizableEmployeeDTO)6 Config (com.hazelcast.config.Config)5