Search in sources :

Example 1 with JavaSerializer

use of com.hazelcast.internal.serialization.impl.defaultserializers.JavaDefaultSerializers.JavaSerializer 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)

Aggregations

GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)1 SerializationConfig (com.hazelcast.config.SerializationConfig)1 Data (com.hazelcast.internal.serialization.Data)1 SerializationService (com.hazelcast.internal.serialization.SerializationService)1 JavaSerializer (com.hazelcast.internal.serialization.impl.defaultserializers.JavaDefaultSerializers.JavaSerializer)1 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)1 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)1 DataSerializable (com.hazelcast.nio.serialization.DataSerializable)1 StreamSerializer (com.hazelcast.nio.serialization.StreamSerializer)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 Serializable (java.io.Serializable)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1