Search in sources :

Example 11 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast by hazelcast.

the class SerializationTest method testEmptyData.

@Test
public void testEmptyData() {
    SerializationConfig serializationConfig = new SerializationConfig().addSerializerConfig(new SerializerConfig().setTypeClass(SingletonValue.class).setImplementation(new StreamSerializer<SingletonValue>() {

        @Override
        public void write(ObjectDataOutput out, SingletonValue v) throws IOException {
        }

        @Override
        public SingletonValue read(ObjectDataInput in) throws IOException {
            return new SingletonValue();
        }

        @Override
        public int getTypeId() {
            return 123;
        }

        @Override
        public void destroy() {
        }
    }));
    SerializationService ss1 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
    Data data = ss1.toData(new SingletonValue());
    Assert.assertNotNull(data);
    SerializationService ss2 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
    Object o = ss2.toObject(data);
    Assert.assertEquals(new SingletonValue(), o);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) SerializerConfig(com.hazelcast.config.SerializerConfig) ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast by hazelcast.

the class SerializationTest 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);
    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.writeUTF(((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.readUTF(), 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 : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) Serializable(java.io.Serializable) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) JavaSerializer(com.hazelcast.internal.serialization.impl.JavaDefaultSerializers.JavaSerializer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 13 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast-jet by hazelcast.

the class DistributedLongSummaryStatisticsTest method readData.

@Test
public void readData() throws IOException {
    // Given
    DistributedLongSummaryStatistics stats = new DistributedLongSummaryStatistics();
    ObjectDataInput in = mock(ObjectDataInput.class);
    when(in.readLong()).thenReturn(1L).thenReturn(2L).thenReturn(3L).thenReturn(4L);
    // When
    stats.readData(in);
    // Then
    assertEquals(1, stats.getCount());
    assertEquals(2, stats.getSum());
    assertEquals(3, stats.getMin());
    assertEquals(4, stats.getMax());
}
Also used : ObjectDataInput(com.hazelcast.nio.ObjectDataInput) Test(org.junit.Test)

Example 14 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast-jet by hazelcast.

the class DistributedDoubleSummaryStatisticsTest method readData.

@Test
public void readData() throws IOException {
    // Given
    DistributedDoubleSummaryStatistics stats = new DistributedDoubleSummaryStatistics();
    ObjectDataInput in = mock(ObjectDataInput.class);
    when(in.readLong()).thenReturn(1L);
    when(in.readDouble()).thenReturn(2.0).thenReturn(3.0).thenReturn(4.0);
    // When
    stats.readData(in);
    // Then
    assertEquals(1, stats.getCount());
    assertEquals(2.0, stats.getSum(), 0.0);
    assertEquals(3.0, stats.getMin(), 0.0);
    assertEquals(4.0, stats.getMax(), 0.0);
}
Also used : ObjectDataInput(com.hazelcast.nio.ObjectDataInput) Test(org.junit.Test)

Example 15 with ObjectDataInput

use of com.hazelcast.nio.ObjectDataInput in project hazelcast-jet by hazelcast.

the class HashMapHook method createSerializer.

@Override
@SuppressWarnings("checkstyle:anoninnerlength")
public Serializer createSerializer() {
    return new StreamSerializer<HashMap>() {

        @Override
        public int getTypeId() {
            return SerializerHookConstants.HASH_MAP;
        }

        @Override
        public void destroy() {
        }

        @Override
        @SuppressWarnings("checkstyle:illegaltype")
        public void write(ObjectDataOutput out, HashMap map) throws IOException {
            out.writeInt(map.size());
            for (Object o : map.entrySet()) {
                Map.Entry e = (Map.Entry) o;
                out.writeObject(e.getKey());
                out.writeObject(e.getValue());
            }
        }

        @Override
        @SuppressWarnings("checkstyle:illegaltype")
        public HashMap read(ObjectDataInput in) throws IOException {
            int length = in.readInt();
            HashMap map = new HashMap();
            for (int i = 0; i < length; i++) {
                // noinspection unchecked
                map.put(in.readObject(), in.readObject());
            }
            return map;
        }
    };
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) HashMap(java.util.HashMap) StreamSerializer(com.hazelcast.nio.serialization.StreamSerializer) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ObjectDataInput (com.hazelcast.nio.ObjectDataInput)80 Test (org.junit.Test)35 QuickTest (com.hazelcast.test.annotation.QuickTest)32 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)24 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)17 FieldKind (com.hazelcast.nio.serialization.FieldKind)16 IOException (java.io.IOException)13 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)12 Nullable (javax.annotation.Nullable)9 GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)7 SerializationConfig (com.hazelcast.config.SerializationConfig)6 StreamSerializer (com.hazelcast.nio.serialization.StreamSerializer)6 Data (com.hazelcast.nio.serialization.Data)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Data (com.hazelcast.internal.serialization.Data)3 SerializationService (com.hazelcast.internal.serialization.SerializationService)3 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)3 HeapData (com.hazelcast.internal.serialization.impl.HeapData)3 SerializationService (com.hazelcast.spi.serialization.SerializationService)3 SerializerConfig (com.hazelcast.config.SerializerConfig)2