Search in sources :

Example 36 with ObjectDataOutput

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

the class InvalidRawDataPortable method writePortable.

@Override
public void writePortable(PortableWriter writer) throws IOException {
    writer.writeLong("l", l);
    final ObjectDataOutput output = writer.getRawDataOutput();
    output.writeInt(k);
    output.writeString(s);
    writer.writeCharArray("c", c);
    output.writeObject(sds);
    writer.writePortable("p", p);
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput)

Example 37 with ObjectDataOutput

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

the class SerializationUtilTest method serialize.

private byte[] serialize(Boolean b) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectDataOutput out = createObjectDataOutputStream(bout, mockSs);
    SerializationUtil.writeNullableBoolean(out, b);
    return bout.toByteArray();
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 38 with ObjectDataOutput

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

the class CachePartitionLostListenerTest method test_cachePartitionEventData_serialization.

@Test
public void test_cachePartitionEventData_serialization() throws IOException {
    CachePartitionEventData cachePartitionEventData = new CachePartitionEventData("cacheName", 1, null);
    ObjectDataOutput output = mock(ObjectDataOutput.class, withSettings().extraInterfaces(DataWriter.class));
    cachePartitionEventData.writeData(output);
    verify(output).writeString("cacheName");
    verify(output).writeInt(1);
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) CachePartitionEventData(com.hazelcast.cache.impl.CachePartitionEventData) DataWriter(com.hazelcast.internal.nio.DataWriter) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) AbstractPartitionLostListenerTest(com.hazelcast.partition.AbstractPartitionLostListenerTest) Test(org.junit.Test)

Example 39 with ObjectDataOutput

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

the class DataSerializableSerializationTest method testClarifiedExceptionsForUnsupportedClassTypes.

@Test
public void testClarifiedExceptionsForUnsupportedClassTypes() {
    class LocalClass implements DataSerializable {

        @Override
        public void writeData(ObjectDataOutput out) {
        }

        @Override
        public void readData(ObjectDataInput in) {
        }
    }
    DataSerializable anonymousInstance = new DataSerializable() {

        @Override
        public void writeData(ObjectDataOutput out) {
        }

        @Override
        public void readData(ObjectDataInput in) {
        }
    };
    DataSerializable[] throwingInstances = { new LocalClass(), anonymousInstance, new NonStaticMemberClass() };
    for (DataSerializable throwingInstance : throwingInstances) {
        try {
            ss.toObject(ss.toData(throwingInstance));
        } catch (HazelcastSerializationException e) {
            assertInstanceOf(NoSuchMethodException.class, e.getCause());
            assertContains(e.getCause().getMessage(), "can't conform to DataSerializable");
            assertInstanceOf(NoSuchMethodException.class, e.getCause().getCause());
            continue;
        }
        fail("deserialization of '" + throwingInstance.getClass() + "' is expected to fail");
    }
    for (DataSerializable throwingInstance : throwingInstances) {
        try {
            ss.toObject(ss.toData(throwingInstance), throwingInstance.getClass());
        } catch (HazelcastSerializationException e) {
            assertInstanceOf(InstantiationException.class, e.getCause());
            assertContains(e.getCause().getMessage(), "can't conform to DataSerializable");
            assertInstanceOf(InstantiationException.class, e.getCause().getCause());
            continue;
        }
        fail("deserialization of '" + throwingInstance.getClass() + "' is expected to fail");
    }
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 40 with ObjectDataOutput

use of com.hazelcast.nio.ObjectDataOutput 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());
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) StreamSerializer(com.hazelcast.nio.serialization.StreamSerializer) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)43 Test (org.junit.Test)15 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)12 QuickTest (com.hazelcast.test.annotation.QuickTest)12 GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)7 SerializationConfig (com.hazelcast.config.SerializationConfig)6 StreamSerializer (com.hazelcast.nio.serialization.StreamSerializer)6 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)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 Data (com.hazelcast.nio.serialization.Data)3 SerializationService (com.hazelcast.spi.serialization.SerializationService)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 SerializerConfig (com.hazelcast.config.SerializerConfig)2 DataWriter (com.hazelcast.internal.nio.DataWriter)2 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)2 DataSerializable (com.hazelcast.nio.serialization.DataSerializable)2