Search in sources :

Example 36 with ObjectInput

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

the class FstSerializationTest 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());
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput)

Example 37 with ObjectInput

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

the class FstSerializationTest method test_LoopReference.

@Test(timeout = 3000)
public void test_LoopReference() throws Exception {
    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(com.alibaba.dubbo.common.serialize.ObjectOutput) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 38 with ObjectInput

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

the class FstSerializationTest method test_charArray.

@Test
public void test_charArray() throws Exception {
    char[] data = new char[] { 'a', '中', '无' };
    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, (char[]) 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) Test(org.junit.Test)

Example 39 with ObjectInput

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

the class FstSerializationTest method test_Bytes.

@Test
public void test_Bytes() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeBytes("123中华人民共和国".getBytes());
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertArrayEquals("123中华人民共和国".getBytes(), deserialize.readBytes());
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Example 40 with ObjectInput

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

the class FstSerializationTest method test_MultiObject.

@Test
public void test_MultiObject() 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());
    assertEquals((byte) 23, deserialize.readByte());
    assertEquals(mediaContent, deserialize.readObject());
    assertEquals(-23, deserialize.readInt());
    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) 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