Search in sources :

Example 96 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo-spi-extensions by apache.

the class AbstractSerializationTest method test_Byte.

@Test
public void test_Byte() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeByte((byte) 123);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertEquals((byte) 123, deserialize.readByte());
    try {
        deserialize.readByte();
        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 97 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo-spi-extensions by apache.

the class AbstractSerializationTest method test_LoopReference.

@Test
public void test_LoopReference() throws Exception {
    assertTimeout(ofMillis(3000), () -> {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("k1", "v1");
        map.put("self", map);
        ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
        objectOutput.writeObject(map);
        objectOutput.flushBuffer();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
        @SuppressWarnings("unchecked") Map<String, Object> output = (Map<String, Object>) deserialize.readObject();
        assertEquals("v1", output.get("k1"));
        assertSame(output, output.get("self"));
    });
}
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)

Example 98 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo-spi-extensions by apache.

the class AbstractSerializationTest method test_Bool.

@Test
public void test_Bool() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeBool(false);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertFalse(deserialize.readBool());
    try {
        deserialize.readBool();
        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 99 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo-spi-extensions by apache.

the class AbstractSerializationTest method test_Integer.

@Test
public void test_Integer() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeInt(1);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    int i = deserialize.readInt();
    assertEquals(1, i);
    try {
        deserialize.readInt();
        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 100 with ObjectOutput

use of org.apache.dubbo.common.serialize.ObjectOutput in project dubbo-spi-extensions by apache.

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)

Aggregations

ObjectOutput (org.apache.dubbo.common.serialize.ObjectOutput)256 Test (org.junit.jupiter.api.Test)224 ByteArrayInputStream (java.io.ByteArrayInputStream)203 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)203 IOException (java.io.IOException)122 NotSerializableException (java.io.NotSerializableException)21 Person (org.apache.dubbo.common.serialize.model.Person)21 OutputStream (java.io.OutputStream)16 HashMap (java.util.HashMap)14 AbstractSerializationPersonFailTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonFailTest)14 AbstractSerializationPersonOkTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonOkTest)14 Serialization (org.apache.dubbo.common.serialize.Serialization)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 UnsafeByteArrayOutputStream (org.apache.dubbo.common.io.UnsafeByteArrayOutputStream)8 BigPerson (org.apache.dubbo.common.serialize.model.person.BigPerson)8 ArrayList (java.util.ArrayList)6 Cleanable (org.apache.dubbo.common.serialize.Cleanable)6 BizException (org.apache.dubbo.common.serialize.model.BizException)6 BizExceptionNoDefaultConstructor (org.apache.dubbo.common.serialize.model.BizExceptionNoDefaultConstructor)6 ChannelBufferOutputStream (org.apache.dubbo.remoting.buffer.ChannelBufferOutputStream)6