Search in sources :

Example 11 with GenericRecordBuilder

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

the class GenericRecordBuilderTest method testWritingToFieldWithWrongType.

@Test
public void testWritingToFieldWithWrongType() {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE).addStringField("name").addIntField("myint").build();
    GenericRecordBuilder builder = GenericRecordBuilder.portable(classDefinition);
    assertThrows(HazelcastSerializationException.class, () -> builder.setInt32("name", 1));
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with GenericRecordBuilder

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

the class GenericRecordTest method testBuildFromObjectCreatedViaAPI.

@Test
public void testBuildFromObjectCreatedViaAPI() {
    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 genericRecord = builder.build();
    GenericRecordBuilder recordBuilder = genericRecord.newBuilder();
    recordBuilder.setInt32("foo", 2);
    assertTrue(trySetAndGetMessage("foo", 5, recordBuilder).startsWith("Field can only be written once"));
    assertTrue(trySetAndGetMessage("notExisting", 3, recordBuilder).startsWith("Invalid field name"));
    assertTrue(tryBuildAndGetMessage(recordBuilder).startsWith("Found an unset field"));
    recordBuilder.setInt64("bar", 100);
    GenericRecord newRecord = recordBuilder.build();
    assertEquals(2, newRecord.getInt32("foo"));
    assertEquals(100, newRecord.getInt64("bar"));
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) 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 13 with GenericRecordBuilder

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

the class GenericRecordTest method testCloneObjectCreatedViaAPI.

@Test
public void testCloneObjectCreatedViaAPI() {
    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 genericRecord = builder.build();
    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 : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) 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 14 with GenericRecordBuilder

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

the class GenericRecordTest method testBuildFromObjectConvertedFromData.

@Test
public void testBuildFromObjectConvertedFromData() {
    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 recordBuilder = genericRecord.newBuilder();
    recordBuilder.setInt32("foo", 2);
    assertTrue(trySetAndGetMessage("foo", 5, recordBuilder).startsWith("Field can only be written once"));
    assertTrue(trySetAndGetMessage("notExisting", 3, recordBuilder).startsWith("Invalid field name"));
    assertTrue(tryBuildAndGetMessage(recordBuilder).startsWith("Found an unset field"));
    recordBuilder.setInt64("bar", 100);
    GenericRecord newRecord = recordBuilder.build();
    assertEquals(2, newRecord.getInt32("foo"));
    assertEquals(100, newRecord.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 15 with GenericRecordBuilder

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

the class CompactNullablePrimitiveInteroperabilityTest method testReflectiveSerializer.

@Test
public void testReflectiveSerializer() {
    SerializationService serializationService = createSerializationService();
    // We set Nullable for primitives and primitives for Nullable fields on purpose on the generic record
    GenericRecordBuilder builder = compact(PrimitiveObject.class.getName());
    builder.setNullableBoolean("boolean_", true);
    builder.setNullableInt8("byte_", (byte) 2);
    builder.setNullableInt16("short_", (short) 4);
    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, 4 });
    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 });
    builder.setBoolean("nullableBoolean", true);
    builder.setInt8("nullableByte", (byte) 4);
    builder.setInt16("nullableShort", (short) 6);
    builder.setInt32("nullableInt", 8);
    builder.setInt64("nullableLong", 4444L);
    builder.setFloat32("nullableFloat", 8321.321F);
    builder.setFloat64("nullableDouble", 41231.32);
    builder.setArrayOfBoolean("nullableBooleans", new boolean[] { true, false });
    builder.setArrayOfInt8("nullableBytes", new byte[] { 1, 2 });
    builder.setArrayOfInt16("nullableShorts", new short[] { 1, 4 });
    builder.setArrayOfInt32("nullableInts", new int[] { 1, 8 });
    builder.setArrayOfInt64("nullableLongs", new long[] { 1L, 4444L });
    builder.setArrayOfFloat32("nullableFloats", new float[] { 1F, 8321.321F });
    builder.setArrayOfFloat64("nullableDoubles", new double[] { 41231.32, 2.0 });
    GenericRecord record = builder.build();
    Data data = serializationService.toData(record);
    PrimitiveObject primitiveObject = serializationService.toObject(data);
    assertEquals(true, primitiveObject.boolean_);
    assertEquals((byte) 2, primitiveObject.byte_);
    assertEquals((short) 4, primitiveObject.short_);
    assertEquals(8, primitiveObject.int_);
    assertEquals(4444L, primitiveObject.long_);
    assertEquals(8321.321F, primitiveObject.float_, 0);
    assertEquals(41231.32, primitiveObject.double_, 0);
    assertArrayEquals(new boolean[] { true, false }, primitiveObject.booleans);
    assertArrayEquals(new byte[] { 1, 2 }, primitiveObject.bytes);
    assertArrayEquals(new short[] { 1, 4 }, primitiveObject.shorts);
    assertArrayEquals(new int[] { 1, 8 }, primitiveObject.ints);
    assertArrayEquals(new long[] { 1L, 4444L }, primitiveObject.longs);
    assertArrayEquals(new float[] { 1F, 8321.321F }, primitiveObject.floats, 0);
    assertArrayEquals(new double[] { 41231.32, 2.0 }, primitiveObject.doubles, 0);
    assertEquals(true, primitiveObject.nullableBoolean);
    assertEquals(Byte.valueOf((byte) 4), primitiveObject.nullableByte);
    assertEquals(Short.valueOf((short) 6), primitiveObject.nullableShort);
    assertEquals(Integer.valueOf(8), primitiveObject.nullableInt);
    assertEquals(Long.valueOf(4444L), primitiveObject.nullableLong);
    assertEquals(Float.valueOf(8321.321F), primitiveObject.nullableFloat);
    assertEquals(Double.valueOf(41231.32), primitiveObject.nullableDouble);
    assertArrayEquals(new Boolean[] { true, false }, primitiveObject.nullableBooleans);
    assertArrayEquals(new Byte[] { 1, 2 }, primitiveObject.nullableBytes);
    assertArrayEquals(new Short[] { 1, 4 }, primitiveObject.nullableShorts);
    assertArrayEquals(new Integer[] { 1, 8 }, primitiveObject.nullableInts);
    assertArrayEquals(new Long[] { 1L, 4444L }, primitiveObject.nullableLongs);
    assertArrayEquals(new Float[] { 1F, 8321.321F }, primitiveObject.nullableFloats);
    assertArrayEquals(new Double[] { 41231.32, 2.0 }, primitiveObject.nullableDoubles);
}
Also used : SerializationService(com.hazelcast.internal.serialization.SerializationService) GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) 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)

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