Search in sources :

Example 56 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class Hessian2SerializationTest method test_longArray_withType.

@Test
public void test_longArray_withType() throws Exception {
    long[] data = new long[] { 234, 0, -1 };
    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, (long[]) deserialize.readObject());
    try {
        deserialize.readObject(long[].class);
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
// NOTE: Hessian2 throws ArrayIndexOutOfBoundsException instead of IOException, let's live with this.
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Test(org.junit.jupiter.api.Test) AbstractSerializationPersonFailTest(org.apache.dubbo.common.serialize.base.AbstractSerializationPersonFailTest)

Example 57 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method test_BizException_WithType.

@Test
public void test_BizException_WithType() throws Exception {
    BizException e = new BizException("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(BizException.class);
    assertEquals("Hello", ((BizException) read).getMessage());
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) BizException(org.apache.dubbo.common.serialize.model.BizException) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Test(org.junit.jupiter.api.Test)

Example 58 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method test_boolArray_withType.

@Test
public void test_boolArray_withType() throws Exception {
    boolean[] data = new boolean[] { true, false, true };
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject(boolean[].class)));
    try {
        deserialize.readObject(boolean[].class);
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 59 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method test_MediaContent_badStream.

// abnormal case
@Test
public void test_MediaContent_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();
        fail();
    } catch (IOException expected) {
        System.out.println(expected);
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 60 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo by alibaba.

the class AbstractSerializationTest method test_intArray.

@Test
public void test_intArray() throws Exception {
    int[] data = new int[] { 234, 0, -1 };
    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, (int[]) deserialize.readObject());
    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectOutput (org.apache.dubbo.common.serialize.ObjectOutput)94 Test (org.junit.jupiter.api.Test)80 ByteArrayInputStream (java.io.ByteArrayInputStream)73 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)73 IOException (java.io.IOException)42 NotSerializableException (java.io.NotSerializableException)7 AbstractSerializationPersonFailTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonFailTest)7 AbstractSerializationPersonOkTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonOkTest)7 Person (org.apache.dubbo.common.serialize.model.Person)7 OutputStream (java.io.OutputStream)6 HashMap (java.util.HashMap)6 Serialization (org.apache.dubbo.common.serialize.Serialization)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 UnsafeByteArrayOutputStream (org.apache.dubbo.common.io.UnsafeByteArrayOutputStream)4 ArrayList (java.util.ArrayList)3 Cleanable (org.apache.dubbo.common.serialize.Cleanable)3 BigPerson (org.apache.dubbo.common.serialize.model.person.BigPerson)3 ChannelBufferOutputStream (org.apache.dubbo.remoting.buffer.ChannelBufferOutputStream)3 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2