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));
}
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());
}
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());
}
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());
}
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);
}
Aggregations