Search in sources :

Example 81 with ObjectOutput

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

the class AbstractProtobufSerializationTest 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 (Exception expected) {
        expected.printStackTrace();
    }
}
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 82 with ObjectOutput

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

the class AbstractProtobufSerializationTest method test_Bool_Multi.

@Test
public void test_Bool_Multi() throws Exception {
    boolean[] array = new boolean[100];
    for (int i = 0; i < array.length; i++) {
        array[i] = random.nextBoolean();
    }
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    for (boolean b : array) {
        objectOutput.writeBool(b);
    }
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    for (boolean b : array) {
        assertEquals(b, deserialize.readBool());
    }
    try {
        deserialize.readBool();
        fail();
    } catch (Exception expected) {
        expected.printStackTrace();
    }
}
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 83 with ObjectOutput

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

the class AbstractProtobufSerializationTest method test_Double.

// ================== Util methods ==================
@Test
public void test_Double() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeDouble(1.28);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertEquals(1.28, deserialize.readDouble());
    try {
        deserialize.readDouble();
        fail();
    } catch (Exception expected) {
        expected.printStackTrace();
    }
}
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 84 with ObjectOutput

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

the class AbstractProtobufSerializationTest method testPbMap.

/**
 * Special test case
 * Dubbo protocol will directly writes native map (Invocation.attachments) using protobuf.
 * this should definitely be fixed but not done yet.
 */
@Test
public void testPbMap() throws Exception {
    Map<String, Object> attachments = new HashMap<>();
    attachments.put("key", "value");
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeAttachments(attachments);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput objectInput = serialization.deserialize(url, byteArrayInputStream);
    Map<String, Object> derializedAttachments = objectInput.readAttachments();
    assertEquals(attachments, derializedAttachments);
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Test(org.junit.jupiter.api.Test)

Example 85 with ObjectOutput

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

the class AbstractProtobufSerializationTest method testPbNormal.

@Test
public void testPbNormal() throws Exception {
    ProtobufUtils.marshaller(GooglePB.PBRequestType.getDefaultInstance());
    GooglePB.PBRequestType request = buildPbMessage();
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(request);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput objectInput = serialization.deserialize(url, byteArrayInputStream);
    GooglePB.PBRequestType derializedRequest = objectInput.readObject(GooglePB.PBRequestType.class);
    assertEquals(request, derializedRequest);
}
Also used : GooglePB(org.apache.dubbo.common.serialize.protobuf.support.model.GooglePB) ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) 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