Search in sources :

Example 21 with ObjectInput

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

the class AbstractSerializationTest method test_Long.

@Test
public void test_Long() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeLong(123L);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertEquals(123L, deserialize.readLong());
    try {
        deserialize.readLong();
        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 22 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput 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(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) BizExceptionNoDefaultConstructor(org.apache.dubbo.common.serialize.model.BizExceptionNoDefaultConstructor) Test(org.junit.jupiter.api.Test)

Example 23 with ObjectInput

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

the class AbstractSerializationTest method test_Byte_Multi.

@Test
public void test_Byte_Multi() throws Exception {
    byte[] array = new byte[100];
    random.nextBytes(array);
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    for (byte b : array) {
        objectOutput.writeByte(b);
    }
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    for (byte b : array) {
        assertEquals(b, 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 24 with ObjectInput

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

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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 25 with ObjectInput

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

the class AbstractSerializationTest method test_MultiObject_WithType.

@Test
public void test_MultiObject_WithType() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeBool(false);
    objectOutput.writeObject(bigPerson);
    objectOutput.writeByte((byte) 23);
    objectOutput.writeObject(mediaContent);
    objectOutput.writeInt(-23);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertFalse(deserialize.readBool());
    assertEquals(bigPerson, deserialize.readObject(BigPerson.class));
    assertEquals((byte) 23, deserialize.readByte());
    assertEquals(mediaContent, deserialize.readObject(MediaContent.class));
    assertEquals(-23, deserialize.readInt());
    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) MediaContent(org.apache.dubbo.common.serialize.model.media.MediaContent) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) BigPerson(org.apache.dubbo.common.serialize.model.person.BigPerson) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)84 ByteArrayInputStream (java.io.ByteArrayInputStream)76 Test (org.junit.jupiter.api.Test)74 ObjectOutput (org.apache.dubbo.common.serialize.ObjectOutput)73 IOException (java.io.IOException)42 AbstractSerializationPersonFailTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonFailTest)7 AbstractSerializationPersonOkTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonOkTest)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)3 Cleanable (org.apache.dubbo.common.serialize.Cleanable)3 BigPerson (org.apache.dubbo.common.serialize.model.person.BigPerson)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)2 URL (org.apache.dubbo.common.URL)2 Serialization (org.apache.dubbo.common.serialize.Serialization)2 BizException (org.apache.dubbo.common.serialize.model.BizException)2 BizExceptionNoDefaultConstructor (org.apache.dubbo.common.serialize.model.BizExceptionNoDefaultConstructor)2 Request (org.apache.dubbo.remoting.exchange.Request)2 Response (org.apache.dubbo.remoting.exchange.Response)2