diff --git a/dubbo-serialization-extensions/dubbo-serialization-avro/src/test/java/org/apache/dubbo/common/serialize/avro/AvroObjectInputOutputTest.java b/dubbo-serialization-extensions/dubbo-serialization-avro/src/test/java/org/apache/dubbo/common/serialize/avro/AvroObjectInputOutputTest.java index 0ef1f3a9..5ede1fcd 100644 --- a/dubbo-serialization-extensions/dubbo-serialization-avro/src/test/java/org/apache/dubbo/common/serialize/avro/AvroObjectInputOutputTest.java +++ b/dubbo-serialization-extensions/dubbo-serialization-avro/src/test/java/org/apache/dubbo/common/serialize/avro/AvroObjectInputOutputTest.java @@ -186,7 +186,7 @@ public void testWriteReadObjectWithoutClass() throws IOException, ClassNotFoundE avroObjectOutput.flushBuffer(); pos.close(); - //这里会丢失所有信息 + //All the information is lost here Object result = avroObjectInput.readObject(); assertThat(result, not(nullValue())); diff --git a/dubbo-serialization-extensions/dubbo-serialization-fury/src/test/java/org/apache/dubbo/common/serialize/fury/FurySerializationTest.java b/dubbo-serialization-extensions/dubbo-serialization-fury/src/test/java/org/apache/dubbo/common/serialize/fury/FurySerializationTest.java new file mode 100644 index 00000000..2fe91102 --- /dev/null +++ b/dubbo-serialization-extensions/dubbo-serialization-fury/src/test/java/org/apache/dubbo/common/serialize/fury/FurySerializationTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.serialize.fury; + +import org.apache.dubbo.common.serialize.ObjectInput; +import org.apache.dubbo.common.serialize.ObjectOutput; +import org.apache.dubbo.common.serialize.fury.dubbo.FuryObjectInput; +import org.apache.dubbo.common.serialize.fury.dubbo.FuryObjectOutput; +import org.apache.dubbo.common.serialize.fury.dubbo.FurySerialization; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import static org.apache.dubbo.common.serialize.fury.dubbo.FurySerialization.FURY_SERIALIZATION_ID; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; + +public class FurySerializationTest { + private FurySerialization FurySerialization; + + @BeforeEach + public void setUp() { + this.FurySerialization = new FurySerialization(); + } + + @Test + public void testContentType() { + assertThat(FurySerialization.getContentType(), is("fury/consistent")); + } + + @Test + public void testContentTypeId() { + assertThat(FurySerialization.getContentTypeId(), is(FURY_SERIALIZATION_ID)); + } + + @Test + public void testObjectOutput() throws IOException { + ObjectOutput objectOutput = FurySerialization.serialize(null, mock(OutputStream.class)); + assertThat(objectOutput, Matchers.instanceOf(FuryObjectOutput.class)); + } + + @Test + public void testObjectInput() throws IOException { + ObjectInput objectInput = FurySerialization.deserialize(null, mock(InputStream.class)); + assertThat(objectInput, Matchers.instanceOf(FuryObjectInput.class)); + } +} diff --git a/dubbo-serialization-extensions/dubbo-serialization-kryo/src/test/java/org/apache/dubbo/common/serialize/kryo/KryObjectInputOutputTest.java b/dubbo-serialization-extensions/dubbo-serialization-kryo/src/test/java/org/apache/dubbo/common/serialize/kryo/KryObjectInputOutputTest.java new file mode 100644 index 00000000..c11b52b8 --- /dev/null +++ b/dubbo-serialization-extensions/dubbo-serialization-kryo/src/test/java/org/apache/dubbo/common/serialize/kryo/KryObjectInputOutputTest.java @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.serialize.kryo; + + +import org.apache.dubbo.common.serialize.model.Person; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.IsNull.nullValue; + + +public class KryObjectInputOutputTest { + private KryoObjectInput kryoObjectInput; + private KryoObjectOutput kryoObjectOutput; + + private PipedOutputStream pos; + private PipedInputStream pis; + + @BeforeEach + public void setup() throws IOException { + pis = new PipedInputStream(); + pos = new PipedOutputStream(); + pis.connect(pos); + + kryoObjectOutput = new KryoObjectOutput(pos); + kryoObjectInput = new KryoObjectInput(pis); + } + + @AfterEach + public void clean() throws IOException { + if (pos != null) { + pos.close(); + pos = null; + } + if (pis != null) { + pis.close(); + pis = null; + } + } + + @Test + public void testWriteReadBool() throws IOException, InterruptedException { + kryoObjectOutput.writeBool(true); + kryoObjectOutput.flushBuffer(); + pos.close(); + + boolean result = kryoObjectInput.readBool(); + assertThat(result, is(true)); + } + + @Test + public void testWriteReadByte() throws IOException { + kryoObjectOutput.writeByte((byte) 'a'); + kryoObjectOutput.flushBuffer(); + pos.close(); + + Byte result = kryoObjectInput.readByte(); + + assertThat(result, is((byte) 'a')); + } + + @Test + public void testWriteReadBytes() throws IOException { + kryoObjectOutput.writeBytes("123456".getBytes()); + kryoObjectOutput.flushBuffer(); + pos.close(); + + byte[] result = kryoObjectInput.readBytes(); + + assertThat(result, is("123456".getBytes())); + } + + @Test + public void testWriteReadShort() throws IOException { + kryoObjectOutput.writeShort((short) 1); + kryoObjectOutput.flushBuffer(); + pos.close(); + + short result = kryoObjectInput.readShort(); + + assertThat(result, is((short) 1)); + } + + @Test + public void testWriteReadInt() throws IOException { + kryoObjectOutput.writeInt(1); + kryoObjectOutput.flushBuffer(); + pos.close(); + + Integer result = kryoObjectInput.readInt(); + + assertThat(result, is(1)); + } + + @Test + public void testReadDouble() throws IOException { + kryoObjectOutput.writeDouble(3.14d); + kryoObjectOutput.flushBuffer(); + pos.close(); + + Double result = kryoObjectInput.readDouble(); + + assertThat(result, is(3.14d)); + } + + @Test + public void testReadLong() throws IOException { + kryoObjectOutput.writeLong(10L); + kryoObjectOutput.flushBuffer(); + pos.close(); + + Long result = kryoObjectInput.readLong(); + + assertThat(result, is(10L)); + } + + @Test + public void testWriteReadFloat() throws IOException { + kryoObjectOutput.writeFloat(1.66f); + kryoObjectOutput.flushBuffer(); + pos.close(); + + Float result = kryoObjectInput.readFloat(); + + assertThat(result, is(1.66F)); + } + + @Test + public void testWriteReadUTF() throws IOException { + kryoObjectOutput.writeUTF("wording"); + kryoObjectOutput.flushBuffer(); + pos.close(); + + String result = kryoObjectInput.readUTF(); + + assertThat(result, is("wording")); + } + + @Test + public void testWriteReadObject() throws IOException, ClassNotFoundException { + Person p = new Person(); + p.setAge(30); + p.setName("abc"); + + kryoObjectOutput.writeObject(p); + kryoObjectOutput.flushBuffer(); + pos.close(); + + Person result = kryoObjectInput.readObject(Person.class); + + assertThat(result, not(nullValue())); + assertThat(result.getName(), is("abc")); + assertThat(result.getAge(), is(30)); + } + + @Test + public void testWriteReadObjectWithoutClass() throws IOException, ClassNotFoundException { + Person p = new Person(); + p.setAge(30); + p.setName("abc"); + + kryoObjectOutput.writeObject(p); + kryoObjectOutput.flushBuffer(); + pos.close(); + + //All the information is lost here + Object result = kryoObjectInput.readObject(); + + assertThat(result, not(nullValue())); +// assertThat(result.getName(), is("abc")); +// assertThat(result.getAge(), is(30)); + } +} diff --git a/dubbo-serialization-extensions/dubbo-serialization-msgpack/src/test/java/org/apache/dubbo/common/serialize/msgpack/MsgpackSerializationTest.java b/dubbo-serialization-extensions/dubbo-serialization-msgpack/src/test/java/org/apache/dubbo/common/serialize/msgpack/MsgpackSerializationTest.java new file mode 100644 index 00000000..e081c2cb --- /dev/null +++ b/dubbo-serialization-extensions/dubbo-serialization-msgpack/src/test/java/org/apache/dubbo/common/serialize/msgpack/MsgpackSerializationTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.serialize.msgpack; + +import org.apache.dubbo.common.serialize.ObjectInput; +import org.apache.dubbo.common.serialize.ObjectOutput; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; + +public class MsgpackSerializationTest { + private MsgpackSerialization msgpackSerialization; + + @BeforeEach + public void setUp() { + this.msgpackSerialization = new MsgpackSerialization(); + } + + @Test + public void testContentType() { + assertThat(msgpackSerialization.getContentType(), is("text/json")); + } + + @Test + public void testContentTypeId() { + byte result = 27; + assertThat(msgpackSerialization.getContentTypeId(), is(result)); + } + + @Test + public void testObjectOutput() throws IOException { + ObjectOutput objectOutput = msgpackSerialization.serialize(null, mock(OutputStream.class)); + assertThat(objectOutput, Matchers.instanceOf(MsgpackObjectOutput.class)); + } + + @Test + public void testObjectInput() throws IOException { + ObjectInput objectInput = msgpackSerialization.deserialize(null, mock(InputStream.class)); + assertThat(objectInput, Matchers.instanceOf(MsgpackObjectInput.class)); + } +} diff --git a/dubbo-serialization-extensions/dubbo-serialization-native-hessian/src/test/java/org/apache/dubbo/serialize/hessian/NativeHessianObjectInputOutputTest.java b/dubbo-serialization-extensions/dubbo-serialization-native-hessian/src/test/java/org/apache/dubbo/serialize/hessian/NativeHessianObjectInputOutputTest.java new file mode 100644 index 00000000..a76b16fd --- /dev/null +++ b/dubbo-serialization-extensions/dubbo-serialization-native-hessian/src/test/java/org/apache/dubbo/serialize/hessian/NativeHessianObjectInputOutputTest.java @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.serialize.hessian; + + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.IsNull.nullValue; + + +public class NativeHessianObjectInputOutputTest { + private Hessian2ObjectInput hessian2ObjectInput; + private Hessian2ObjectOutput hessian2ObjectOutput; + + private PipedOutputStream pos; + private PipedInputStream pis; + + @BeforeEach + public void setup() throws IOException { + pis = new PipedInputStream(); + pos = new PipedOutputStream(); + pis.connect(pos); + + hessian2ObjectOutput = new Hessian2ObjectOutput(pos); + hessian2ObjectInput = new Hessian2ObjectInput(pis); + } + + @AfterEach + public void clean() throws IOException { + if (pos != null) { + pos.close(); + pos = null; + } + if (pis != null) { + pis.close(); + pis = null; + } + } + + @Test + public void testWriteReadBool() throws IOException, InterruptedException { + hessian2ObjectOutput.writeBool(true); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + boolean result = hessian2ObjectInput.readBool(); + assertThat(result, is(true)); + } + + @Test + public void testWriteReadByte() throws IOException { + hessian2ObjectOutput.writeByte((byte) 'a'); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + Byte result = hessian2ObjectInput.readByte(); + + assertThat(result, is((byte) 'a')); + } + + @Test + public void testWriteReadBytes() throws IOException { + hessian2ObjectOutput.writeBytes("123456".getBytes()); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + byte[] result = hessian2ObjectInput.readBytes(); + + assertThat(result, is("123456".getBytes())); + } + + @Test + public void testWriteReadShort() throws IOException { + hessian2ObjectOutput.writeShort((short) 1); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + short result = hessian2ObjectInput.readShort(); + + assertThat(result, is((short) 1)); + } + + @Test + public void testWriteReadInt() throws IOException { + hessian2ObjectOutput.writeInt(1); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + Integer result = hessian2ObjectInput.readInt(); + + assertThat(result, is(1)); + } + + @Test + public void testReadDouble() throws IOException { + hessian2ObjectOutput.writeDouble(3.14d); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + Double result = hessian2ObjectInput.readDouble(); + + assertThat(result, is(3.14d)); + } + + @Test + public void testReadLong() throws IOException { + hessian2ObjectOutput.writeLong(10L); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + Long result = hessian2ObjectInput.readLong(); + + assertThat(result, is(10L)); + } + + @Test + public void testWriteReadFloat() throws IOException { + hessian2ObjectOutput.writeFloat(1.66f); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + Float result = hessian2ObjectInput.readFloat(); + + assertThat(result, is(1.66F)); + } + + @Test + public void testWriteReadUTF() throws IOException { + hessian2ObjectOutput.writeUTF("wording"); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + String result = hessian2ObjectInput.readUTF(); + + assertThat(result, is("wording")); + } + + @Test + public void testWriteReadObject() throws IOException, ClassNotFoundException { + Person p = new Person(); + p.setAge(30); + p.setName("abc"); + + hessian2ObjectOutput.writeObject(p); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + Person result = hessian2ObjectInput.readObject(Person.class); + + assertThat(result, not(nullValue())); + assertThat(result.getName(), is("abc")); + assertThat(result.getAge(), is(30)); + } + + @Test + public void testWriteReadObjectWithoutClass() throws IOException, ClassNotFoundException { + Person p = new Person(); + p.setAge(30); + p.setName("abc"); + + hessian2ObjectOutput.writeObject(p); + hessian2ObjectOutput.flushBuffer(); + pos.close(); + + //All the information is lost here + Object result = hessian2ObjectInput.readObject(); + + assertThat(result, not(nullValue())); +// assertThat(result.getName(), is("abc")); +// assertThat(result.getAge(), is(30)); + } +} diff --git a/dubbo-serialization-extensions/dubbo-serialization-native-hessian/src/test/java/org/apache/dubbo/serialize/hessian/Person.java b/dubbo-serialization-extensions/dubbo-serialization-native-hessian/src/test/java/org/apache/dubbo/serialize/hessian/Person.java new file mode 100644 index 00000000..ea666452 --- /dev/null +++ b/dubbo-serialization-extensions/dubbo-serialization-native-hessian/src/test/java/org/apache/dubbo/serialize/hessian/Person.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.serialize.hessian; + +import java.io.Serializable; +import java.util.Arrays; + +public class Person implements Serializable { + byte oneByte = 123; + private String name = "name1"; + private int age = 11; + + private String[] value = {"value1", "value2"}; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public byte getOneByte() { + return oneByte; + } + + public void setOneByte(byte b) { + this.oneByte = b; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String[] getValue() { + return value; + } + + public void setValue(String[] value) { + this.value = value; + } + + @Override + public String toString() { + return String.format("Person name(%s) age(%d) byte(%s) [value=%s]", name, age, oneByte, Arrays.toString(value)); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + age; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + Arrays.hashCode(value); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Person other = (Person) obj; + if (age != other.age) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (!Arrays.equals(value, other.value)) + return false; + return true; + } +} diff --git a/dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufSerialization.java b/dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufSerialization.java index 12d2f658..53b69a0e 100644 --- a/dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufSerialization.java +++ b/dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufSerialization.java @@ -17,7 +17,6 @@ package org.apache.dubbo.common.serialize.protobuf.support; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.serialize.Constants; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; @@ -25,6 +24,8 @@ import java.io.InputStream; import java.io.OutputStream; +import static org.apache.dubbo.common.serialize.Constants.PROTOBUF_SERIALIZATION_ID; + /** *

* Currently, the Dubbo protocol / framework data, such as attachments, event data, etc., @@ -43,7 +44,7 @@ public class GenericProtobufSerialization implements Serialization { @Override public byte getContentTypeId() { - return Constants.PROTOBUF_SERIALIZATION_ID; + return PROTOBUF_SERIALIZATION_ID; } @Override