Search in sources :

Example 91 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method test_BizExceptionNoDefaultConstructor.

@Test
public void test_BizExceptionNoDefaultConstructor() throws Exception {
    BizExceptionNoDefaultConstructor e = new BizExceptionNoDefaultConstructor("Hello");
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(e);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    Object read = deserialize.readObject();
    assertEquals("Hello", ((BizExceptionNoDefaultConstructor) read).getMessage());
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) BizExceptionNoDefaultConstructor(com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor) Test(org.junit.Test)

Example 92 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method test_MediaContent_WithType_badStream.

@Test
public void test_MediaContent_WithType_badStream() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(mediaContent);
    objectOutput.flushBuffer();
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    for (int i = 0; i < byteArray.length; i++) {
        if (i % 3 == 0) {
            byteArray[i] = (byte) ~byteArray[i];
        }
    }
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    try {
        ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
        // local variable, convenient for debug
        @SuppressWarnings("unused") Object read = deserialize.readObject(MediaContent.class);
        fail();
    } catch (IOException expected) {
        System.out.println(expected);
    }
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Test(org.junit.Test)

Example 93 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method assertObject.

@SuppressWarnings("unchecked")
<T> void assertObject(T data) throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertEquals(data, (T) deserialize.readObject());
    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException)

Example 94 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method test_shortArray_withType.

@Test
public void test_shortArray_withType() throws Exception {
    short[] data = new short[] { 37, 39, 12 };
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertArrayEquals(data, (short[]) deserialize.readObject(short[].class));
    try {
        deserialize.readObject(short[].class);
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Test(org.junit.Test)

Example 95 with ObjectOutput

use of com.alibaba.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method assertObjectArray.

// ================ Array Type ================
<T> void assertObjectArray(T[] data, Class<T[]> clazz) throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertArrayEquals(data, clazz.cast(deserialize.readObject()));
    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException)

Aggregations

ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)106 ObjectInput (com.alibaba.dubbo.common.serialize.ObjectInput)92 ByteArrayInputStream (java.io.ByteArrayInputStream)92 Test (org.junit.Test)90 IOException (java.io.IOException)59 Person (com.alibaba.dubbo.common.model.Person)7 NotSerializableException (java.io.NotSerializableException)7 HashMap (java.util.HashMap)5 BizException (com.alibaba.dubbo.common.model.BizException)4 BizExceptionNoDefaultConstructor (com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor)4 Serialization (com.alibaba.dubbo.common.serialize.Serialization)4 LinkedHashMap (java.util.LinkedHashMap)4 UnsafeByteArrayOutputStream (com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream)3 Cleanable (com.alibaba.dubbo.common.serialize.Cleanable)3 ChannelBufferOutputStream (com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream)3 ArrayList (java.util.ArrayList)3 URL (com.alibaba.dubbo.common.URL)2 MediaContent (com.alibaba.dubbo.common.model.media.MediaContent)2 BigPerson (com.alibaba.dubbo.common.model.person.BigPerson)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)2