Search in sources :

Example 11 with ObjectInput

use of com.alibaba.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);
    assertEquals(false, 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(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) MediaContent(com.alibaba.dubbo.common.model.media.MediaContent) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) BigPerson(com.alibaba.dubbo.common.model.person.BigPerson) Test(org.junit.Test)

Example 12 with ObjectInput

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

the class AbstractSerializationTest method test_BytesRange.

@Test
public void test_BytesRange() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeBytes("123中华人民共和国-新疆维吾尔自治区".getBytes(), 1, 9);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    byte[] expectedArray = new byte[9];
    System.arraycopy("123中华人民共和国-新疆维吾尔自治区".getBytes(), 1, expectedArray, 0, expectedArray.length);
    assertArrayEquals(expectedArray, deserialize.readBytes());
    try {
        deserialize.readBytes();
        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 13 with ObjectInput

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

the class AbstractSerializationTest method test_LinkedHashMap.

@Test
public void test_LinkedHashMap() throws Exception {
    LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
    data.put("1", "a");
    data.put("2", "b");
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    Object read = deserialize.readObject();
    assertTrue(read instanceof LinkedHashMap);
    @SuppressWarnings("unchecked") String key1 = ((LinkedHashMap<String, String>) read).entrySet().iterator().next().getKey();
    assertEquals("1", key1);
    assertEquals(data, read);
    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) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 14 with ObjectInput

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

the class AbstractSerializationTest method test_URL_mutable_withType.

// ================ final field test ================
@Test
public void test_URL_mutable_withType() throws Exception {
    URL data = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan&noValue");
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    URL actual = (URL) deserialize.readObject(URL.class);
    assertEquals(data, actual);
    assertEquals(data.getParameters(), actual.getParameters());
    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) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 15 with ObjectInput

use of com.alibaba.dubbo.common.serialize.ObjectInput 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(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) BizException(com.alibaba.dubbo.common.model.BizException) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Aggregations

ObjectInput (com.alibaba.dubbo.common.serialize.ObjectInput)97 ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)92 ByteArrayInputStream (java.io.ByteArrayInputStream)92 Test (org.junit.Test)83 IOException (java.io.IOException)59 BizException (com.alibaba.dubbo.common.model.BizException)4 BizExceptionNoDefaultConstructor (com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor)4 LinkedHashMap (java.util.LinkedHashMap)4 Cleanable (com.alibaba.dubbo.common.serialize.Cleanable)3 HashMap (java.util.HashMap)3 Map (java.util.Map)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 Serialization (com.alibaba.dubbo.common.serialize.Serialization)2 Request (com.alibaba.dubbo.remoting.exchange.Request)2 Response (com.alibaba.dubbo.remoting.exchange.Response)2 JSONException (com.alibaba.fastjson.JSONException)2 ChannelBufferInputStream (com.alibaba.dubbo.remoting.buffer.ChannelBufferInputStream)1 Invocation (com.alibaba.dubbo.rpc.Invocation)1