Search in sources :

Example 1 with GenericRecordBuilder

use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.

the class CompactNullablePrimitiveInteroperabilityTest method testWriteNullableReadPrimitive.

@Test
public void testWriteNullableReadPrimitive() {
    GenericRecordBuilder builder = compact("test");
    builder.setNullableBoolean("boolean", true);
    builder.setNullableInt8("byte", (byte) 4);
    builder.setNullableInt16("short", (short) 6);
    builder.setNullableInt32("int", 8);
    builder.setNullableInt64("long", 4444L);
    builder.setNullableFloat32("float", 8321.321F);
    builder.setNullableFloat64("double", 41231.32);
    builder.setArrayOfNullableBoolean("booleans", new Boolean[] { true, false });
    builder.setArrayOfNullableInt8("bytes", new Byte[] { 1, 2 });
    builder.setArrayOfNullableInt16("shorts", new Short[] { 1, 2 });
    builder.setArrayOfNullableInt32("ints", new Integer[] { 1, 8 });
    builder.setArrayOfNullableInt64("longs", new Long[] { 1L, 4444L });
    builder.setArrayOfNullableFloat32("floats", new Float[] { 1F, 8321.321F });
    builder.setArrayOfNullableFloat64("doubles", new Double[] { 41231.32, 2.0 });
    GenericRecord record = builder.build();
    assertTrue(record instanceof DeserializedGenericRecord);
    assertReadAsPrimitive(record);
    SerializationService serializationService = createSerializationService();
    Data data = serializationService.toData(record);
    GenericRecord serializedRecord = serializationService.toObject(data);
    assertTrue(serializedRecord instanceof CompactInternalGenericRecord);
    assertReadAsPrimitive(serializedRecord);
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with GenericRecordBuilder

use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.

the class CompactNullablePrimitiveInteroperabilityTest method testWriteNullReadPrimitiveThrowsException.

@Test
public void testWriteNullReadPrimitiveThrowsException() {
    GenericRecordBuilder builder = compact("test");
    builder.setNullableBoolean("boolean", null);
    builder.setNullableInt8("byte", null);
    builder.setNullableInt16("short", null);
    builder.setNullableInt32("int", null);
    builder.setNullableInt64("long", null);
    builder.setNullableFloat32("float", null);
    builder.setNullableFloat64("double", null);
    builder.setArrayOfNullableBoolean("booleans", new Boolean[] { null, false });
    builder.setArrayOfNullableInt8("bytes", new Byte[] { 1, null });
    builder.setArrayOfNullableInt16("shorts", new Short[] { null, 2 });
    builder.setArrayOfNullableInt32("ints", new Integer[] { 1, null });
    builder.setArrayOfNullableInt64("longs", new Long[] { null, 2L });
    builder.setArrayOfNullableFloat32("floats", new Float[] { null, 2F });
    builder.setArrayOfNullableFloat64("doubles", new Double[] { 1.0, null });
    GenericRecord record = builder.build();
    assertReadNullAsPrimitiveThrowsException(record);
    SerializationService serializationService = createSerializationService();
    Data data = serializationService.toData(record);
    GenericRecord serializedRecord = serializationService.toObject(data);
    assertTrue(serializedRecord instanceof CompactInternalGenericRecord);
    assertReadNullAsPrimitiveThrowsException(serializedRecord);
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with GenericRecordBuilder

use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.

the class CompactNullablePrimitiveInteroperabilityTest method testWritePrimitiveReadNullable.

@Test
public void testWritePrimitiveReadNullable() {
    GenericRecordBuilder builder = compact("test");
    builder.setBoolean("boolean", true);
    builder.setInt8("byte", (byte) 2);
    builder.setInt16("short", (short) 4);
    builder.setInt32("int", 8);
    builder.setInt64("long", 4444L);
    builder.setFloat32("float", 8321.321F);
    builder.setFloat64("double", 41231.32);
    builder.setArrayOfBoolean("booleans", new boolean[] { true, false });
    builder.setArrayOfInt8("bytes", new byte[] { 1, 2 });
    builder.setArrayOfInt16("shorts", new short[] { 1, 4 });
    builder.setArrayOfInt32("ints", new int[] { 1, 8 });
    builder.setArrayOfInt64("longs", new long[] { 1, 4444L });
    builder.setArrayOfFloat32("floats", new float[] { 1, 8321.321F });
    builder.setArrayOfFloat64("doubles", new double[] { 41231.32, 2 });
    GenericRecord record = builder.build();
    assertTrue(record instanceof DeserializedGenericRecord);
    assertReadAsNullable(record);
    SerializationService serializationService = createSerializationService();
    Data data = serializationService.toData(record);
    GenericRecord serializedRecord = serializationService.toObject(data);
    assertTrue(serializedRecord instanceof CompactInternalGenericRecord);
    assertReadAsNullable(serializedRecord);
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with GenericRecordBuilder

use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.

the class GenericRecordTest method testCloneObjectConvertedFromData.

@Test
public void testCloneObjectConvertedFromData() {
    SerializationService serializationService = createSerializationService();
    GenericRecordBuilder builder = compact("fooBarTypeName");
    builder.setInt32("foo", 1);
    assertTrue(trySetAndGetMessage("foo", 5, builder).startsWith("Field can only be written once"));
    builder.setInt64("bar", 1231L);
    GenericRecord expectedGenericRecord = builder.build();
    Data data = serializationService.toData(expectedGenericRecord);
    Object object = serializationService.toObject(data);
    GenericRecord genericRecord = (GenericRecord) object;
    GenericRecordBuilder cloneBuilder = genericRecord.cloneWithBuilder();
    cloneBuilder.setInt32("foo", 2);
    assertTrue(trySetAndGetMessage("foo", 5, cloneBuilder).startsWith("Field can only be written once"));
    assertTrue(trySetAndGetMessage("notExisting", 3, cloneBuilder).startsWith("Invalid field name"));
    GenericRecord clone = cloneBuilder.build();
    assertEquals(2, clone.getInt32("foo"));
    assertEquals(1231L, clone.getInt64("bar"));
}
Also used : InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) Data(com.hazelcast.internal.serialization.Data) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) CompactTestUtil.createCompactGenericRecord(com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createCompactGenericRecord) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with GenericRecordBuilder

use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.

the class GenericRecordTest method testUnsupportedMethods.

@Test
public void testUnsupportedMethods() {
    ClassDefinition namedPortableClassDefinition = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE).addStringField("name").addIntField("myint").build();
    GenericRecordBuilder builder = GenericRecordBuilder.portable(namedPortableClassDefinition).setString("name", "foo").setInt32("myint", 123);
    GenericRecord record = builder.build();
    assertThrows(UnsupportedOperationException.class, () -> builder.setNullableBoolean("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setNullableInt8("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setNullableInt16("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setNullableInt32("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setNullableInt64("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setNullableFloat32("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setNullableFloat64("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setArrayOfNullableBoolean("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setArrayOfNullableInt8("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setArrayOfNullableInt16("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setArrayOfNullableInt32("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setArrayOfNullableInt64("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setArrayOfNullableFloat32("name", null));
    assertThrows(UnsupportedOperationException.class, () -> builder.setArrayOfNullableFloat64("name", null));
    assertThrows(UnsupportedOperationException.class, () -> record.getNullableBoolean("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getNullableInt8("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getNullableInt16("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getNullableInt32("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getNullableInt64("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getNullableFloat32("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getNullableFloat64("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getArrayOfNullableBoolean("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getArrayOfNullableInt8("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getArrayOfNullableInt16("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getArrayOfNullableInt32("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getArrayOfNullableInt64("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getArrayOfNullableFloat32("name"));
    assertThrows(UnsupportedOperationException.class, () -> record.getArrayOfNullableFloat64("name"));
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

GenericRecordBuilder (com.hazelcast.nio.serialization.GenericRecordBuilder)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 QuickTest (com.hazelcast.test.annotation.QuickTest)15 Test (org.junit.Test)15 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)11 Data (com.hazelcast.internal.serialization.Data)7 SerializationService (com.hazelcast.internal.serialization.SerializationService)7 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)7 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)6 CompactTestUtil.createCompactGenericRecord (com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createCompactGenericRecord)5 InternalGenericRecord (com.hazelcast.internal.serialization.impl.InternalGenericRecord)4 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)3