From 58847570655cdfe9fade6a4d1fa48612bcf3df26 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 13 Feb 2018 10:11:35 +0800 Subject: [PATCH 1/5] Extract serialization module from dubbo-common --- all/pom.xml | 48 + bom/pom.xml | 30 + .../router/file/availablerule.javascript | 11 - .../cluster/router/file/methodrule.javascript | 10 - .../router/file/notAvailablerule.javascript | 11 - .../serialize/support/dubbo/Builder.java | 1412 ----------------- .../support/dubbo/DubboSerialization.java | 46 - .../support/dubbo/GenericDataFlags.java | 65 - .../support/dubbo/GenericDataInput.java | 566 ------- .../support/dubbo/GenericDataOutput.java | 493 ------ .../support/dubbo/GenericObjectInput.java | 277 ---- .../support/dubbo/GenericObjectOutput.java | 94 -- .../support/json/JsonObjectInput.java | 156 -- .../support/json/JsonObjectOutput.java | 105 -- .../support/json/JsonSerialization.java | 50 - ...ibaba.dubbo.common.serialize.Serialization | 8 - .../serialize/SerializationCompareTest.java | 215 --- .../common/serialize/dubbo/BuilderTest.java | 516 ------ .../serialize/dubbo/DataInputOutputTest.java | 121 -- .../serialization/DubboSerializationTest.java | 56 - .../serialization/JsonSerializationTest.java | 270 ---- .../dubbo/common/utils/ConfigUtilsTest.java | 22 +- dubbo-config/dubbo-config-api/pom.xml | 10 +- dubbo-demo/dubbo-demo-consumer/pom.xml | 6 +- dubbo-demo/dubbo-demo-provider/pom.xml | 6 +- dubbo-remoting/dubbo-remoting-api/pom.xml | 11 + dubbo-remoting/dubbo-remoting-netty/pom.xml | 6 + dubbo-rpc/dubbo-rpc-api/pom.xml | 5 + .../dubbo-serialization-api/pom.xml | 61 + .../dubbo/common/serialize/Cleanable.java | 0 .../dubbo/common/serialize/DataInput.java | 0 .../dubbo/common/serialize/DataOutput.java | 0 .../dubbo/common/serialize/ObjectInput.java | 0 .../dubbo/common/serialize/ObjectOutput.java | 0 .../dubbo/common/serialize/Serialization.java | 136 +- .../support/SerializableClassRegistry.java | 0 .../support/SerializationOptimizer.java | 0 .../dubbo-serialization-fastjson/pom.xml | 44 + .../fastjson}/FastJsonObjectInput.java | 270 ++-- .../fastjson}/FastJsonObjectOutput.java | 198 +-- .../fastjson}/FastJsonSerialization.java | 96 +- ...ibaba.dubbo.common.serialize.Serialization | 1 + .../dubbo/common/model/AnimalEnum.java | 21 + .../dubbo/common/model/BizException.java | 16 +- .../BizExceptionNoDefaultConstructor.java | 12 +- .../alibaba/dubbo/common/model/Person.java | 95 ++ .../common/model/SerializablePerson.java | 97 ++ .../dubbo/common/model/media/Image.java | 120 ++ .../dubbo/common/model/media/Media.java | 205 +++ .../common/model/media/MediaContent.java | 78 + .../dubbo/common/model/person/BigPerson.java | 151 ++ .../common/model/person/FullAddress.java | 202 +++ .../dubbo/common/model/person/PersonInfo.java | 206 +++ .../common/model/person/PersonStatus.java | 22 + .../dubbo/common/model/person/Phone.java | 139 ++ .../AbstractSerializationPersionFailTest.java | 0 .../AbstractSerializationPersionOkTest.java | 0 .../AbstractSerializationTest.java | 0 .../FastJsonSerializationTest.java | 2 +- ...mmon.extensionloader.activate.ActivateExt1 | 4 + ...on.extensionloader.adaptive.HasAdaptiveExt | 2 + ...n.extensionloader.compatible.CompatibleExt | 2 + ...ubbo.common.extensionloader.ext1.SimpleExt | 4 + ...aba.dubbo.common.extensionloader.ext2.Ext2 | 3 + ...mon.extensionloader.ext3.UseProtocolKeyExt | 3 + ....common.extensionloader.ext4.NoUrlParamExt | 2 + ...n.extensionloader.ext5.NoAdaptiveMethodExt | 2 + ...bo.common.extensionloader.ext6_inject.Ext6 | 2 + ...ommon.extensionloader.ext6_wrap.WrappedExt | 4 + ...o.common.extensionloader.ext7.InitErrorExt | 2 + ...bo.common.extensionloader.ext8_add.AddExt1 | 1 + ....alibaba.dubbo.common.status.StatusChecker | 1 + ...mmon.extensionloader.activate.ActivateExt1 | 1 + .../src/test/resources/StreamUtilsTest.txt | 1 + .../dubbo/common/serialize/dubbo/SimpleDO.fc | 2 + .../src/test/resources/json.flex | 68 + .../src/test/resources/log4j.xml | 32 + .../src/test/resources/properties.load | 3 + .../dubbo-serialization-fst/pom.xml | 44 + .../dubbo/serialize}/fst/FstFactory.java | 2 +- .../dubbo/serialize}/fst/FstObjectInput.java | 52 +- .../dubbo/serialize}/fst/FstObjectOutput.java | 2 +- .../serialize}/fst/FstSerialization.java | 2 +- ...ibaba.dubbo.common.serialize.Serialization | 1 + .../dubbo/common/model/AnimalEnum.java | 21 + .../dubbo/common/model/BizException.java | 26 +- .../BizExceptionNoDefaultConstructor.java | 26 + .../alibaba/dubbo/common/model/Person.java | 95 ++ .../common/model/SerializablePerson.java | 97 ++ .../dubbo/common/model/media/Image.java | 120 ++ .../dubbo/common/model/media/Media.java | 205 +++ .../common/model/media/MediaContent.java | 78 + .../dubbo/common/model/person/BigPerson.java | 151 ++ .../common/model/person/FullAddress.java | 202 +++ .../dubbo/common/model/person/PersonInfo.java | 206 +++ .../common/model/person/PersonStatus.java | 22 + .../dubbo/common/model/person/Phone.java | 139 ++ .../serialization/FstSerializationTest.java | 1124 +++++++++++++ .../src/test/resources/log4j.xml | 32 + .../dubbo-serialization-hessian2/pom.xml | 45 + .../hessian2}/Hessian2ObjectInput.java | 2 +- .../hessian2}/Hessian2ObjectOutput.java | 2 +- .../hessian2}/Hessian2Serialization.java | 94 +- .../hessian2}/Hessian2SerializerFactory.java | 66 +- ...ibaba.dubbo.common.serialize.Serialization | 1 + .../dubbo/common/model/AnimalEnum.java | 21 + .../dubbo/common/model/BizException.java | 29 + .../BizExceptionNoDefaultConstructor.java | 26 + .../alibaba/dubbo/common/model/Person.java | 95 ++ .../common/model/SerializablePerson.java | 97 ++ .../dubbo/common/model/media/Image.java | 120 ++ .../dubbo/common/model/media/Media.java | 205 +++ .../common/model/media/MediaContent.java | 78 + .../dubbo/common/model/person/BigPerson.java | 151 ++ .../common/model/person/FullAddress.java | 202 +++ .../dubbo/common/model/person/PersonInfo.java | 206 +++ .../common/model/person/PersonStatus.java | 22 + .../dubbo/common/model/person/Phone.java | 139 ++ .../AbstractSerializationPersionFailTest.java | 137 ++ .../AbstractSerializationPersionOkTest.java | 93 ++ .../AbstractSerializationTest.java | 1210 ++++++++++++++ .../Hessian2SerializationTest.java | 2 +- .../dubbo/common/serialize/dubbo/SimpleDO.fc | 2 + .../src/test/resources/log4j.xml | 32 + .../dubbo-serialization-jdk/pom.xml | 45 + .../java/CompactedJavaSerialization.java | 90 +- .../java/CompactedObjectInputStream.java | 2 +- .../java/CompactedObjectOutputStream.java | 2 +- .../serialize}/java/JavaObjectInput.java | 4 +- .../serialize}/java/JavaObjectOutput.java | 5 +- .../serialize}/java/JavaSerialization.java | 90 +- .../nativejava/NativeJavaObjectInput.java | 2 +- .../nativejava/NativeJavaObjectOutput.java | 2 +- .../nativejava/NativeJavaSerialization.java | 2 +- ...ibaba.dubbo.common.serialize.Serialization | 3 + .../dubbo/common/model/AnimalEnum.java | 21 + .../dubbo/common/model/BizException.java | 29 + .../BizExceptionNoDefaultConstructor.java | 26 + .../alibaba/dubbo/common/model/Person.java | 95 ++ .../common/model/SerializablePerson.java | 97 ++ .../dubbo/common/model/media/Image.java | 120 ++ .../dubbo/common/model/media/Media.java | 205 +++ .../common/model/media/MediaContent.java | 78 + .../dubbo/common/model/person/BigPerson.java | 151 ++ .../common/model/person/FullAddress.java | 202 +++ .../dubbo/common/model/person/PersonInfo.java | 206 +++ .../common/model/person/PersonStatus.java | 22 + .../dubbo/common/model/person/Phone.java | 139 ++ .../AbstractSerializationPersionFailTest.java | 137 ++ .../AbstractSerializationPersionOkTest.java | 93 ++ .../AbstractSerializationTest.java | 1210 ++++++++++++++ .../CompactedJavaSerializationTest.java | 48 +- .../serialization/JavaSerializationTest.java | 48 +- .../dubbo/common/serialize/dubbo/SimpleDO.fc | 2 + .../src/test/resources/log4j.xml | 32 + .../dubbo-serialization-kryo/pom.xml | 48 + .../dubbo/serialize}/kryo/CompatibleKryo.java | 4 +- .../serialize}/kryo/KryoObjectInput.java | 4 +- .../serialize}/kryo/KryoObjectOutput.java | 4 +- .../serialize}/kryo/KryoSerialization.java | 2 +- .../kryo/utils/AbstractKryoFactory.java | 4 +- .../serialize}/kryo/utils/KryoUtils.java | 2 +- .../kryo/utils/PooledKryoFactory.java | 2 +- .../kryo/utils/PrototypeKryoFactory.java | 2 +- .../kryo/utils/ReflectionUtils.java | 2 +- .../kryo/utils/ThreadLocalKryoFactory.java | 2 +- ...ibaba.dubbo.common.serialize.Serialization | 1 + .../dubbo/common/model/AnimalEnum.java | 21 + .../dubbo/common/model/BizException.java | 29 + .../BizExceptionNoDefaultConstructor.java | 26 + .../alibaba/dubbo/common/model/Person.java | 95 ++ .../common/model/SerializablePerson.java | 97 ++ .../dubbo/common/model/media/Image.java | 120 ++ .../dubbo/common/model/media/Media.java | 205 +++ .../common/model/media/MediaContent.java | 78 + .../dubbo/common/model/person/BigPerson.java | 151 ++ .../common/model/person/FullAddress.java | 202 +++ .../dubbo/common/model/person/PersonInfo.java | 206 +++ .../common/model/person/PersonStatus.java | 22 + .../dubbo/common/model/person/Phone.java | 139 ++ .../AbstractSerializationPersionFailTest.java | 137 ++ .../AbstractSerializationPersionOkTest.java | 93 ++ .../AbstractSerializationTest.java | 1210 ++++++++++++++ .../serialization/KyroSerializationTest.java | 2 +- .../serialization/ReflectionUtilsTest.java | 2 +- .../dubbo/common/serialize/dubbo/SimpleDO.fc | 2 + .../src/test/resources/log4j.xml | 32 + dubbo-serialization/pom.xml | 40 + pom.xml | 1 + 189 files changed, 13700 insertions(+), 5156 deletions(-) delete mode 100644 dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript delete mode 100644 dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/methodrule.javascript delete mode 100644 dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/Builder.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/DubboSerialization.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataFlags.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataInput.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataOutput.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectInput.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectOutput.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectInput.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectOutput.java delete mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonSerialization.java delete mode 100644 dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization delete mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/SerializationCompareTest.java delete mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/BuilderTest.java delete mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/DataInputOutputTest.java delete mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/DubboSerializationTest.java delete mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JsonSerializationTest.java create mode 100644 dubbo-serialization/dubbo-serialization-api/pom.xml rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/Cleanable.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/DataInput.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/DataOutput.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/ObjectInput.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/ObjectOutput.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/Serialization.java (96%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializableClassRegistry.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-api}/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializationOptimizer.java (100%) create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/pom.xml rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json => dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson}/FastJsonObjectInput.java (95%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json => dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson}/FastJsonObjectOutput.java (94%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json => dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson}/FastJsonSerialization.java (93%) create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java rename dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.java => dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizException.java (78%) rename dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java => dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java (75%) create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/Person.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Image.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Media.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java rename {dubbo-common => dubbo-serialization/dubbo-serialization-fastjson}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-fastjson}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-fastjson}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java (100%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-fastjson}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FastJsonSerializationTest.java (99%) create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.adaptive.HasAdaptiveExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.compatible.CompatibleExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext2.Ext2 create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext4.NoUrlParamExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext5.NoAdaptiveMethodExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_inject.Ext6 create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_wrap.WrappedExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext7.InitErrorExt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext8_add.AddExt1 create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.status.StatusChecker create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/services/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/StreamUtilsTest.txt create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/json.flex create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/log4j.xml create mode 100644 dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/properties.load create mode 100644 dubbo-serialization/dubbo-serialization-fst/pom.xml rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize}/fst/FstFactory.java (96%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize}/fst/FstObjectInput.java (69%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize}/fst/FstObjectOutput.java (97%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize}/fst/FstSerialization.java (96%) create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java rename dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/ClassDescriptorMapper.java => dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizException.java (66%) create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/Person.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Image.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Media.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java create mode 100644 dubbo-serialization/dubbo-serialization-fst/src/test/resources/log4j.xml create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/pom.xml rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian => dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2}/Hessian2ObjectInput.java (97%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian => dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2}/Hessian2ObjectOutput.java (97%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian => dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2}/Hessian2Serialization.java (93%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian => dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2}/Hessian2SerializerFactory.java (92%) create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizException.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/Person.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Image.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Media.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java rename {dubbo-common => dubbo-serialization/dubbo-serialization-hessian2}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/Hessian2SerializationTest.java (98%) create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc create mode 100644 dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/log4j.xml create mode 100644 dubbo-serialization/dubbo-serialization-jdk/pom.xml rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/java/CompactedJavaSerialization.java (93%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/java/CompactedObjectInputStream.java (97%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/java/CompactedObjectOutputStream.java (96%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/java/JavaObjectInput.java (94%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/java/JavaObjectOutput.java (93%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/java/JavaSerialization.java (93%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/nativejava/NativeJavaObjectInput.java (97%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/nativejava/NativeJavaObjectOutput.java (97%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize}/nativejava/NativeJavaSerialization.java (96%) create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizException.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/Person.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Image.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Media.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java rename {dubbo-common => dubbo-serialization/dubbo-serialization-jdk}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/CompactedJavaSerializationTest.java (97%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-jdk}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JavaSerializationTest.java (97%) create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc create mode 100644 dubbo-serialization/dubbo-serialization-jdk/src/test/resources/log4j.xml create mode 100644 dubbo-serialization/dubbo-serialization-kryo/pom.xml rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/CompatibleKryo.java (92%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/KryoObjectInput.java (96%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/KryoObjectOutput.java (95%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/KryoSerialization.java (96%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/utils/AbstractKryoFactory.java (97%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/utils/KryoUtils.java (95%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/utils/PooledKryoFactory.java (95%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/utils/PrototypeKryoFactory.java (94%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/utils/ReflectionUtils.java (94%) rename {dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support => dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize}/kryo/utils/ThreadLocalKryoFactory.java (95%) create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizException.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/Person.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Image.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Media.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java rename {dubbo-common => dubbo-serialization/dubbo-serialization-kryo}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/KyroSerializationTest.java (93%) rename {dubbo-common => dubbo-serialization/dubbo-serialization-kryo}/src/test/java/com/alibaba/dubbo/common/serialize/serialization/ReflectionUtilsTest.java (94%) create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc create mode 100644 dubbo-serialization/dubbo-serialization-kryo/src/test/resources/log4j.xml create mode 100644 dubbo-serialization/pom.xml diff --git a/all/pom.xml b/all/pom.xml index d27f797a5f1..d8ee49631f5 100644 --- a/all/pom.xml +++ b/all/pom.xml @@ -277,6 +277,48 @@ limitations under the License. compile true + + com.alibaba + dubbo-serialization-api + ${project.version} + compile + true + + + com.alibaba + dubbo-serialization-fastjson + ${project.version} + compile + true + + + com.alibaba + dubbo-serialization-fst + ${project.version} + compile + true + + + com.alibaba + dubbo-serialization-hessian2 + ${project.version} + compile + true + + + com.alibaba + dubbo-serialization-jdk + ${project.version} + compile + true + + + com.alibaba + dubbo-serialization-kryo + ${project.version} + compile + true + com.alibaba hessian-lite @@ -388,6 +430,12 @@ limitations under the License. com.alibaba:dubbo-container-log4j com.alibaba:dubbo-container-logback com.alibaba:dubbo-qos + com.alibaba:dubbo-serialization-api + com.alibaba:dubbo-serialization-fastjson + com.alibaba:dubbo-serialization-hessian2 + com.alibaba:dubbo-serialization-fst + com.alibaba:dubbo-serialization-kryo + com.alibaba:dubbo-serialization-jdk diff --git a/bom/pom.xml b/bom/pom.xml index 6cb48b5b7a4..b019f14227a 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -253,6 +253,36 @@ dubbo-qos ${project.version} + + com.alibaba + dubbo-serialization-api + ${project.version} + + + com.alibaba + dubbo-serialization-fastjson + ${project.version} + + + com.alibaba + dubbo-serialization-fst + ${project.version} + + + com.alibaba + dubbo-serialization-hessian2 + ${project.version} + + + com.alibaba + dubbo-serialization-jdk + ${project.version} + + + com.alibaba + dubbo-serialization-kryo + ${project.version} + com.alibaba hessian-lite diff --git a/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript b/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript deleted file mode 100644 index c76de32ee40..00000000000 --- a/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript +++ /dev/null @@ -1,11 +0,0 @@ -function route(invokers,invocation,context){ - var result = new java.util.ArrayList(invokers.size()); - - for (i=0;i1 && invocation.getMethodName() .equals("method1")) { - result.add(invokers.get(0)) ; - } else { - result.add(invokers.get(1)) ; - } - return result; -}; -route(invokers,invocation,context); \ No newline at end of file diff --git a/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript b/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript deleted file mode 100644 index 1fcdcaeb0d6..00000000000 --- a/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript +++ /dev/null @@ -1,11 +0,0 @@ -function route(invokers,invocation,context){ - var result = new java.util.ArrayList(invokers.size()); - - for (i=0;i type. - */ - -@SuppressWarnings({"unchecked", "rawtypes"}) -public abstract class Builder implements GenericDataFlags { - static final Builder SerializableBuilder = new Builder() { - @Override - public Class getType() { - return Serializable.class; - } - - @Override - public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_STREAM); - UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(); - CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos); - oos.writeObject(obj); - oos.flush(); - bos.close(); - byte[] b = bos.toByteArray(); - out.writeUInt(b.length); - out.write0(b, 0, b.length); - } - } - - @Override - public Serializable parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_STREAM) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_STREAM, get " + b + "."); - - UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(in.read0(in.readUInt())); - CompactedObjectInputStream ois = new CompactedObjectInputStream(bis); - try { - return (Serializable) ois.readObject(); - } catch (ClassNotFoundException e) { - throw new IOException(StringUtils.toString(e)); - } - } - }; - private static final AtomicLong BUILDER_CLASS_COUNTER = new AtomicLong(0); - - private static final String BUILDER_CLASS_NAME = Builder.class.getName(); - - private static final Map, Builder> BuilderMap = new ConcurrentHashMap, Builder>(); - private static final Map, Builder> nonSerializableBuilderMap = new ConcurrentHashMap, Builder>(); - - private static final String FIELD_CONFIG_SUFFIX = ".fc"; - - private static final int MAX_FIELD_CONFIG_FILE_SIZE = 16 * 1024; - - private static final Comparator FNC = new Comparator() { - public int compare(String n1, String n2) { - return compareFieldName(n1, n2); - } - }; - - private static final Comparator FC = new Comparator() { - public int compare(Field f1, Field f2) { - return compareFieldName(f1.getName(), f2.getName()); - } - }; - - private static final Comparator CC = new Comparator() { - public int compare(Constructor o1, Constructor o2) { - return o1.getParameterTypes().length - o2.getParameterTypes().length; - } - }; - - // class-descriptor mapper - private static final List mDescList = new ArrayList(); - - private static final Map mDescMap = new ConcurrentHashMap(); - - public static ClassDescriptorMapper DEFAULT_CLASS_DESCRIPTOR_MAPPER = new ClassDescriptorMapper() { - public String getDescriptor(int index) { - if (index < 0 || index >= mDescList.size()) - return null; - return mDescList.get(index); - } - - public int getDescriptorIndex(String desc) { - Integer ret = mDescMap.get(desc); - return ret == null ? -1 : ret.intValue(); - } - }; - // Must be protected. by qian.lei - protected static Logger logger = LoggerFactory.getLogger(Builder.class); - static final Builder GenericArrayBuilder = new AbstractObjectBuilder() { - @Override - public Class getType() { - return Object[].class; - } - - @Override - protected Object[] newInstance(GenericObjectInput in) throws IOException { - return new Object[in.readUInt()]; - } - - @Override - protected void readObject(Object[] ret, GenericObjectInput in) throws IOException { - for (int i = 0; i < ret.length; i++) - ret[i] = in.readObject(); - } - - @Override - protected void writeObject(Object[] obj, GenericObjectOutput out) throws IOException { - out.writeUInt(obj.length); - for (Object item : obj) - out.writeObject(item); - } - }; - static final Builder GenericBuilder = new Builder() { - @Override - public Class getType() { - return Object.class; - } - - @Override - public void writeTo(Object obj, GenericObjectOutput out) throws IOException { - out.writeObject(obj); - } - - @Override - public Object parseFrom(GenericObjectInput in) throws IOException { - return in.readObject(); - } - }; - - static { - addDesc(boolean[].class); - addDesc(byte[].class); - addDesc(char[].class); - addDesc(short[].class); - addDesc(int[].class); - addDesc(long[].class); - addDesc(float[].class); - addDesc(double[].class); - - addDesc(Boolean.class); - addDesc(Byte.class); - addDesc(Character.class); - addDesc(Short.class); - addDesc(Integer.class); - addDesc(Long.class); - addDesc(Float.class); - addDesc(Double.class); - - addDesc(String.class); - addDesc(String[].class); - - addDesc(ArrayList.class); - addDesc(HashMap.class); - addDesc(HashSet.class); - addDesc(Date.class); - addDesc(java.sql.Date.class); - addDesc(java.sql.Time.class); - addDesc(java.sql.Timestamp.class); - addDesc(java.util.LinkedList.class); - addDesc(java.util.LinkedHashMap.class); - addDesc(java.util.LinkedHashSet.class); - - register(byte[].class, new Builder() { - @Override - public Class getType() { - return byte[].class; - } - - @Override - public void writeTo(byte[] obj, GenericObjectOutput out) throws IOException { - out.writeBytes(obj); - } - - @Override - public byte[] parseFrom(GenericObjectInput in) throws IOException { - return in.readBytes(); - } - }); - register(Boolean.class, new Builder() { - @Override - public Class getType() { - return Boolean.class; - } - - @Override - public void writeTo(Boolean obj, GenericObjectOutput out) throws IOException { - if (obj == null) - out.write0(VARINT_N1); - else if (obj.booleanValue()) - out.write0(VARINT_1); - else - out.write0(VARINT_0); - } - - @Override - public Boolean parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - switch (b) { - case VARINT_N1: - return null; - case VARINT_0: - return Boolean.FALSE; - case VARINT_1: - return Boolean.TRUE; - default: - throw new IOException("Input format error, expect VARINT_N1|VARINT_0|VARINT_1, get " + b + "."); - } - } - }); - register(Byte.class, new Builder() { - @Override - public Class getType() { - return Byte.class; - } - - @Override - public void writeTo(Byte obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeByte(obj.byteValue()); - } - } - - @Override - public Byte parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return Byte.valueOf(in.readByte()); - } - }); - register(Character.class, new Builder() { - @Override - public Class getType() { - return Character.class; - } - - @Override - public void writeTo(Character obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeShort((short) obj.charValue()); - } - } - - @Override - public Character parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return Character.valueOf((char) in.readShort()); - } - }); - register(Short.class, new Builder() { - @Override - public Class getType() { - return Short.class; - } - - @Override - public void writeTo(Short obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeShort(obj.shortValue()); - } - } - - @Override - public Short parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return Short.valueOf(in.readShort()); - } - }); - register(Integer.class, new Builder() { - @Override - public Class getType() { - return Integer.class; - } - - @Override - public void writeTo(Integer obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeInt(obj.intValue()); - } - } - - @Override - public Integer parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return Integer.valueOf(in.readInt()); - } - }); - register(Long.class, new Builder() { - @Override - public Class getType() { - return Long.class; - } - - @Override - public void writeTo(Long obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeLong(obj.longValue()); - } - } - - @Override - public Long parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return Long.valueOf(in.readLong()); - } - }); - register(Float.class, new Builder() { - @Override - public Class getType() { - return Float.class; - } - - @Override - public void writeTo(Float obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeFloat(obj.floatValue()); - } - } - - @Override - public Float parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return new Float(in.readFloat()); - } - }); - register(Double.class, new Builder() { - @Override - public Class getType() { - return Double.class; - } - - @Override - public void writeTo(Double obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeDouble(obj.doubleValue()); - } - } - - @Override - public Double parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return new Double(in.readDouble()); - } - }); - register(String.class, new Builder() { - @Override - public Class getType() { - return String.class; - } - - @Override - public String parseFrom(GenericObjectInput in) throws IOException { - return in.readUTF(); - } - - @Override - public void writeTo(String obj, GenericObjectOutput out) throws IOException { - out.writeUTF(obj); - } - }); - register(StringBuilder.class, new Builder() { - @Override - public Class getType() { - return StringBuilder.class; - } - - @Override - public StringBuilder parseFrom(GenericObjectInput in) throws IOException { - return new StringBuilder(in.readUTF()); - } - - @Override - public void writeTo(StringBuilder obj, GenericObjectOutput out) throws IOException { - out.writeUTF(obj.toString()); - } - }); - register(StringBuffer.class, new Builder() { - @Override - public Class getType() { - return StringBuffer.class; - } - - @Override - public StringBuffer parseFrom(GenericObjectInput in) throws IOException { - return new StringBuffer(in.readUTF()); - } - - @Override - public void writeTo(StringBuffer obj, GenericObjectOutput out) throws IOException { - out.writeUTF(obj.toString()); - } - }); - - // java.util - register(ArrayList.class, new Builder() { - @Override - public Class getType() { - return ArrayList.class; - } - - @Override - public void writeTo(ArrayList obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUES); - out.writeUInt(obj.size()); - for (Object item : obj) - out.writeObject(item); - } - } - - @Override - public ArrayList parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUES) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUES, get " + b + "."); - - int len = in.readUInt(); - ArrayList ret = new ArrayList(len); - for (int i = 0; i < len; i++) - ret.add(in.readObject()); - return ret; - } - }); - register(HashMap.class, new Builder() { - @Override - public Class getType() { - return HashMap.class; - } - - @Override - public void writeTo(HashMap obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_MAP); - out.writeUInt(obj.size()); - for (Map.Entry entry : (Set) obj.entrySet()) { - out.writeObject(entry.getKey()); - out.writeObject(entry.getValue()); - } - } - } - - @Override - public HashMap parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_MAP) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_MAP, get " + b + "."); - - int len = in.readUInt(); - HashMap ret = new HashMap(len); - for (int i = 0; i < len; i++) - ret.put(in.readObject(), in.readObject()); - return ret; - } - }); - register(HashSet.class, new Builder() { - @Override - public Class getType() { - return HashSet.class; - } - - @Override - public void writeTo(HashSet obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUES); - out.writeUInt(obj.size()); - for (Object item : obj) - out.writeObject(item); - } - } - - @Override - public HashSet parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUES) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUES, get " + b + "."); - - int len = in.readUInt(); - HashSet ret = new HashSet(len); - for (int i = 0; i < len; i++) - ret.add(in.readObject()); - return ret; - } - }); - - register(Date.class, new Builder() { - @Override - public Class getType() { - return Date.class; - } - - @Override - public void writeTo(Date obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeLong(obj.getTime()); - } - } - - @Override - public Date parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return new Date(in.readLong()); - } - }); - - // java.sql - register(java.sql.Date.class, new Builder() { - @Override - public Class getType() { - return java.sql.Date.class; - } - - @Override - public void writeTo(java.sql.Date obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeLong(obj.getTime()); - } - } - - @Override - public java.sql.Date parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return new java.sql.Date(in.readLong()); - } - }); - register(java.sql.Timestamp.class, new Builder() { - @Override - public Class getType() { - return java.sql.Timestamp.class; - } - - @Override - public void writeTo(java.sql.Timestamp obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeLong(obj.getTime()); - } - } - - @Override - public java.sql.Timestamp parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return new java.sql.Timestamp(in.readLong()); - } - }); - register(java.sql.Time.class, new Builder() { - @Override - public Class getType() { - return java.sql.Time.class; - } - - @Override - public void writeTo(java.sql.Time obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - out.write0(OBJECT_VALUE); - out.writeLong(obj.getTime()); - } - } - - @Override - public java.sql.Time parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - if (b == OBJECT_NULL) - return null; - if (b != OBJECT_VALUE) - throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_VALUE, get " + b + "."); - - return new java.sql.Time(in.readLong()); - } - }); - } - - protected Builder() { - } - - public static Builder register(Class c, boolean isAllowNonSerializable) { - if (c == Object.class || c.isInterface()) - return (Builder) GenericBuilder; - if (c == Object[].class) - return (Builder) GenericArrayBuilder; - - Builder b = (Builder) BuilderMap.get(c); - if (null != b) return b; - - boolean isSerializable = Serializable.class.isAssignableFrom(c); - if (!isAllowNonSerializable && !isSerializable) { - throw new IllegalStateException("Serialized class " + c.getName() + - " must implement java.io.Serializable (dubbo codec setting: isAllowNonSerializable = false)"); - } - - b = (Builder) nonSerializableBuilderMap.get(c); - if (null != b) return b; - - b = newBuilder(c); - if (isSerializable) - BuilderMap.put(c, b); - else - nonSerializableBuilderMap.put(c, b); - - return b; - } - - public static Builder register(Class c) { - return register(c, false); - } - - public static void register(Class c, Builder b) { - if (Serializable.class.isAssignableFrom(c)) - BuilderMap.put(c, b); - else - nonSerializableBuilderMap.put(c, b); - } - - private static Builder newBuilder(Class c) { - if (c.isPrimitive()) - throw new RuntimeException("Can not create builder for primitive type: " + c); - - if (logger.isInfoEnabled()) - logger.info("create Builder for class: " + c); - - Builder builder; - if (c.isArray()) - builder = newArrayBuilder(c); - else - builder = newObjectBuilder(c); - return (Builder) builder; - } - - private static Builder newArrayBuilder(Class c) { - Class cc = c.getComponentType(); - if (cc.isInterface()) - return GenericArrayBuilder; - - ClassLoader cl = ClassHelper.getCallerClassLoader(Builder.class); - - String cn = ReflectUtils.getName(c), ccn = ReflectUtils.getName(cc); // get class name as int[][], double[]. - String bcn = BUILDER_CLASS_NAME + "$bc" + BUILDER_CLASS_COUNTER.getAndIncrement(); - - int ix = cn.indexOf(']'); - String s1 = cn.substring(0, ix), s2 = cn.substring(ix); // if name='int[][]' then s1='int[', s2='][]' - - StringBuilder cwt = new StringBuilder("public void writeTo(Object obj, ").append(GenericObjectOutput.class.getName()).append(" out) throws java.io.IOException{"); // writeTo code. - StringBuilder cpf = new StringBuilder("public Object parseFrom(").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{"); // parseFrom code. - - cwt.append("if( $1 == null ){ $2.write0(OBJECT_NULL); return; }"); - cwt.append(cn).append(" v = (").append(cn).append(")$1; int len = v.length; $2.write0(OBJECT_VALUES); $2.writeUInt(len); for(int i=0;i builder = null; - if (cc.isPrimitive()) { - if (cc == boolean.class) { - cwt.append("$2.writeBool(v[i]);"); - cpf.append("ret[i] = $1.readBool();"); - } else if (cc == byte.class) { - cwt.append("$2.writeByte(v[i]);"); - cpf.append("ret[i] = $1.readByte();"); - } else if (cc == char.class) { - cwt.append("$2.writeShort((short)v[i]);"); - cpf.append("ret[i] = (char)$1.readShort();"); - } else if (cc == short.class) { - cwt.append("$2.writeShort(v[i]);"); - cpf.append("ret[i] = $1.readShort();"); - } else if (cc == int.class) { - cwt.append("$2.writeInt(v[i]);"); - cpf.append("ret[i] = $1.readInt();"); - } else if (cc == long.class) { - cwt.append("$2.writeLong(v[i]);"); - cpf.append("ret[i] = $1.readLong();"); - } else if (cc == float.class) { - cwt.append("$2.writeFloat(v[i]);"); - cpf.append("ret[i] = $1.readFloat();"); - } else if (cc == double.class) { - cwt.append("$2.writeDouble(v[i]);"); - cpf.append("ret[i] = $1.readDouble();"); - } - } else { - builder = register(cc); - - cwt.append("builder.writeTo(v[i], $2);"); - cpf.append("ret[i] = (").append(ccn).append(")builder.parseFrom($1);"); - } - cwt.append(" } }"); - cpf.append(" } return ret; }"); - - ClassGenerator cg = ClassGenerator.newInstance(cl); - cg.setClassName(bcn); - cg.setSuperClass(Builder.class); - cg.addDefaultConstructor(); - if (builder != null) - cg.addField("public static " + BUILDER_CLASS_NAME + " builder;"); - cg.addMethod("public Class getType(){ return " + cn + ".class; }"); - cg.addMethod(cwt.toString()); - cg.addMethod(cpf.toString()); - try { - Class wc = cg.toClass(); - // set static field. - if (builder != null) - wc.getField("builder").set(null, builder); - return (Builder) wc.newInstance(); - } catch (RuntimeException e) { - throw e; - } catch (Throwable e) { - throw new RuntimeException(e.getMessage()); - } finally { - cg.release(); - } - } - - private static Builder newObjectBuilder(final Class c) { - if (c.isEnum()) - return newEnumBuilder(c); - - if (c.isAnonymousClass()) - throw new RuntimeException("Can not instantiation anonymous class: " + c); - - if (c.getEnclosingClass() != null && !Modifier.isStatic(c.getModifiers())) - throw new RuntimeException("Can not instantiation inner and non-static class: " + c); - - if (Throwable.class.isAssignableFrom(c)) - return SerializableBuilder; - - ClassLoader cl = ClassHelper.getCallerClassLoader(Builder.class); - - // is same package. - boolean isp; - String cn = c.getName(), bcn; - if (c.getClassLoader() == null) // is system class. if( cn.startsWith("java.") || cn.startsWith("javax.") || cn.startsWith("sun.") ) - { - isp = false; - bcn = BUILDER_CLASS_NAME + "$bc" + BUILDER_CLASS_COUNTER.getAndIncrement(); - } else { - isp = true; - bcn = cn + "$bc" + BUILDER_CLASS_COUNTER.getAndIncrement(); - } - - // is Collection, is Map, is Serializable. - boolean isc = Collection.class.isAssignableFrom(c); - boolean ism = !isc && Map.class.isAssignableFrom(c); - boolean iss = !(isc || ism) && Serializable.class.isAssignableFrom(c); - - // deal with fields. - String[] fns = null; // fix-order fields names - InputStream is = c.getResourceAsStream(c.getSimpleName() + FIELD_CONFIG_SUFFIX); // load field-config file. - if (is != null) { - try { - int len = is.available(); - if (len > 0) { - if (len > MAX_FIELD_CONFIG_FILE_SIZE) - throw new RuntimeException("Load [" + c.getName() + "] field-config file error: File-size too larger"); - - String[] lines = IOUtils.readLines(is); - if (lines != null && lines.length > 0) { - List list = new ArrayList(); - for (int i = 0; i < lines.length; i++) { - fns = lines[i].split(","); - Arrays.sort(fns, FNC); - for (int j = 0; j < fns.length; j++) - list.add(fns[j]); - } - fns = list.toArray(new String[0]); - } - } - } catch (IOException e) { - throw new RuntimeException("Load [" + c.getName() + "] field-config file error: " + e.getMessage()); - } finally { - try { - is.close(); - } catch (IOException e) { - } - } - } - - Field f, fs[]; - if (fns != null) { - fs = new Field[fns.length]; - for (int i = 0; i < fns.length; i++) { - String fn = fns[i]; - try { - f = c.getDeclaredField(fn); - int mod = f.getModifiers(); - if (Modifier.isStatic(mod) || (serializeIgnoreFinalModifier(c) && Modifier.isFinal(mod))) - throw new RuntimeException("Field [" + c.getName() + "." + fn + "] is static/final field."); - if (Modifier.isTransient(mod)) { - if (iss) - return SerializableBuilder; - throw new RuntimeException("Field [" + c.getName() + "." + fn + "] is transient field."); - } - f.setAccessible(true); - fs[i] = f; - } catch (SecurityException e) { - throw new RuntimeException(e.getMessage()); - } catch (NoSuchFieldException e) { - throw new RuntimeException("Field [" + c.getName() + "." + fn + "] not found."); - } - } - } else { - Class t = c; - List fl = new ArrayList(); - do { - fs = t.getDeclaredFields(); - for (Field tf : fs) { - int mod = tf.getModifiers(); - if (Modifier.isStatic(mod) - || (serializeIgnoreFinalModifier(c) && Modifier.isFinal(mod)) - || tf.getName().equals("this$0") // skip static or inner-class's 'this$0' field. - || !Modifier.isPublic(tf.getType().getModifiers())) //skip private inner-class field - continue; - if (Modifier.isTransient(mod)) { - if (iss) - return SerializableBuilder; - continue; - } - tf.setAccessible(true); - fl.add(tf); - } - t = t.getSuperclass(); - } - while (t != Object.class); - - fs = fl.toArray(new Field[0]); - if (fs.length > 1) - Arrays.sort(fs, FC); - } - - // deal with constructors. - Constructor[] cs = c.getDeclaredConstructors(); - if (cs.length == 0) { - Class t = c; - do { - t = t.getSuperclass(); - if (t == null) - throw new RuntimeException("Can not found Constructor?"); - cs = t.getDeclaredConstructors(); - } - while (cs.length == 0); - } - if (cs.length > 1) - Arrays.sort(cs, CC); - - // writeObject code. - StringBuilder cwf = new StringBuilder("protected void writeObject(Object obj, ").append(GenericObjectOutput.class.getName()).append(" out) throws java.io.IOException{"); - cwf.append(cn).append(" v = (").append(cn).append(")$1; "); - cwf.append("$2.writeInt(fields.length);"); - - // readObject code. - StringBuilder crf = new StringBuilder("protected void readObject(Object ret, ").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{"); - crf.append("int fc = $2.readInt();"); - crf.append("if( fc != ").append(fs.length).append(" ) throw new IllegalStateException(\"Deserialize Class [").append(cn).append("], field count not matched. Expect ").append(fs.length).append(" but get \" + fc +\".\");"); - crf.append(cn).append(" ret = (").append(cn).append(")$1;"); - - // newInstance code. - StringBuilder cni = new StringBuilder("protected Object newInstance(").append(GenericObjectInput.class.getName()).append(" in){ return "); - Constructor con = cs[0]; - int mod = con.getModifiers(); - boolean dn = Modifier.isPublic(mod) || (isp && !Modifier.isPrivate(mod)); - if (dn) { - cni.append("new ").append(cn).append("("); - } else { - con.setAccessible(true); - cni.append("constructor.newInstance(new Object[]{"); - } - Class[] pts = con.getParameterTypes(); - for (int i = 0; i < pts.length; i++) { - if (i > 0) - cni.append(','); - cni.append(defaultArg(pts[i])); - } - if (!dn) - cni.append("}"); // close object array. - cni.append("); }"); - - // get bean-style property metadata. - Map pms = propertyMetadatas(c); - List> builders = new ArrayList>(fs.length); - String fn, ftn; // field name, field type name. - Class ft; // field type. - boolean da; // direct access. - PropertyMetadata pm; - for (int i = 0; i < fs.length; i++) { - f = fs[i]; - fn = f.getName(); - ft = f.getType(); - ftn = ReflectUtils.getName(ft); - da = isp && (f.getDeclaringClass() == c) && (Modifier.isPrivate(f.getModifiers()) == false); - if (da) { - pm = null; - } else { - pm = pms.get(fn); - if (pm != null && (pm.type != ft || pm.setter == null || pm.getter == null)) - pm = null; - } - - crf.append("if( fc == ").append(i).append(" ) return;"); - if (ft.isPrimitive()) { - if (ft == boolean.class) { - if (da) { - cwf.append("$2.writeBool(v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = $2.readBool();"); - } else if (pm != null) { - cwf.append("$2.writeBool(v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("($2.readBool());"); - } else { - cwf.append("$2.writeBool(((Boolean)fields[").append(i).append("].get($1)).booleanValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)$2.readBool());"); - } - } else if (ft == byte.class) { - if (da) { - cwf.append("$2.writeByte(v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = $2.readByte();"); - } else if (pm != null) { - cwf.append("$2.writeByte(v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("($2.readByte());"); - } else { - cwf.append("$2.writeByte(((Byte)fields[").append(i).append("].get($1)).byteValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)$2.readByte());"); - } - } else if (ft == char.class) { - if (da) { - cwf.append("$2.writeShort((short)v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = (char)$2.readShort();"); - } else if (pm != null) { - cwf.append("$2.writeShort((short)v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("((char)$2.readShort());"); - } else { - cwf.append("$2.writeShort((short)((Character)fields[").append(i).append("].get($1)).charValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)((char)$2.readShort()));"); - } - } else if (ft == short.class) { - if (da) { - cwf.append("$2.writeShort(v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = $2.readShort();"); - } else if (pm != null) { - cwf.append("$2.writeShort(v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("($2.readShort());"); - } else { - cwf.append("$2.writeShort(((Short)fields[").append(i).append("].get($1)).shortValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)$2.readShort());"); - } - } else if (ft == int.class) { - if (da) { - cwf.append("$2.writeInt(v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = $2.readInt();"); - } else if (pm != null) { - cwf.append("$2.writeInt(v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("($2.readInt());"); - } else { - cwf.append("$2.writeInt(((Integer)fields[").append(i).append("].get($1)).intValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)$2.readInt());"); - } - } else if (ft == long.class) { - if (da) { - cwf.append("$2.writeLong(v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = $2.readLong();"); - } else if (pm != null) { - cwf.append("$2.writeLong(v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("($2.readLong());"); - } else { - cwf.append("$2.writeLong(((Long)fields[").append(i).append("].get($1)).longValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)$2.readLong());"); - } - } else if (ft == float.class) { - if (da) { - cwf.append("$2.writeFloat(v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = $2.readFloat();"); - } else if (pm != null) { - cwf.append("$2.writeFloat(v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("($2.readFloat());"); - } else { - cwf.append("$2.writeFloat(((Float)fields[").append(i).append("].get($1)).floatValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)$2.readFloat());"); - } - } else if (ft == double.class) { - if (da) { - cwf.append("$2.writeDouble(v.").append(fn).append(");"); - crf.append("ret.").append(fn).append(" = $2.readDouble();"); - } else if (pm != null) { - cwf.append("$2.writeDouble(v.").append(pm.getter).append("());"); - crf.append("ret.").append(pm.setter).append("($2.readDouble());"); - } else { - cwf.append("$2.writeDouble(((Double)fields[").append(i).append("].get($1)).doubleValue());"); - crf.append("fields[").append(i).append("].set(ret, ($w)$2.readDouble());"); - } - } - } else if (ft == c) { - if (da) { - cwf.append("this.writeTo(v.").append(fn).append(", $2);"); - crf.append("ret.").append(fn).append(" = (").append(ftn).append(")this.parseFrom($2);"); - } else if (pm != null) { - cwf.append("this.writeTo(v.").append(pm.getter).append("(), $2);"); - crf.append("ret.").append(pm.setter).append("((").append(ftn).append(")this.parseFrom($2));"); - } else { - cwf.append("this.writeTo((").append(ftn).append(")fields[").append(i).append("].get($1), $2);"); - crf.append("fields[").append(i).append("].set(ret, this.parseFrom($2));"); - } - } else { - int bc = builders.size(); - builders.add(register(ft)); - - if (da) { - cwf.append("builders[").append(bc).append("].writeTo(v.").append(fn).append(", $2);"); - crf.append("ret.").append(fn).append(" = (").append(ftn).append(")builders[").append(bc).append("].parseFrom($2);"); - } else if (pm != null) { - cwf.append("builders[").append(bc).append("].writeTo(v.").append(pm.getter).append("(), $2);"); - crf.append("ret.").append(pm.setter).append("((").append(ftn).append(")builders[").append(bc).append("].parseFrom($2));"); - } else { - cwf.append("builders[").append(bc).append("].writeTo((").append(ftn).append(")fields[").append(i).append("].get($1), $2);"); - crf.append("fields[").append(i).append("].set(ret, builders[").append(bc).append("].parseFrom($2));"); - } - } - } - - // skip any fields. - crf.append("for(int i=").append(fs.length).append(";i wc = cg.toClass(); - // set static field - wc.getField("fields").set(null, fs); - wc.getField("builders").set(null, builders.toArray(new Builder[0])); - if (!dn) - wc.getField("constructor").set(null, con); - return (Builder) wc.newInstance(); - } catch (RuntimeException e) { - throw e; - } catch (Throwable e) { - throw new RuntimeException(e.getMessage(), e); - } finally { - cg.release(); - } - } - - private static Builder newEnumBuilder(Class c) { - ClassLoader cl = ClassHelper.getCallerClassLoader(Builder.class); - - String cn = c.getName(); - String bcn = BUILDER_CLASS_NAME + "$bc" + BUILDER_CLASS_COUNTER.getAndIncrement(); - - StringBuilder cwt = new StringBuilder("public void writeTo(Object obj, ").append(GenericObjectOutput.class.getName()).append(" out) throws java.io.IOException{"); // writeTo code. - cwt.append(cn).append(" v = (").append(cn).append(")$1;"); - cwt.append("if( $1 == null ){ $2.writeUTF(null); }else{ $2.writeUTF(v.name()); } }"); - - StringBuilder cpf = new StringBuilder("public Object parseFrom(").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{"); // parseFrom code. - cpf.append("String name = $1.readUTF(); if( name == null ) return null; return (").append(cn).append(")Enum.valueOf(").append(cn).append(".class, name); }"); - - ClassGenerator cg = ClassGenerator.newInstance(cl); - cg.setClassName(bcn); - cg.setSuperClass(Builder.class); - cg.addDefaultConstructor(); - cg.addMethod("public Class getType(){ return " + cn + ".class; }"); - cg.addMethod(cwt.toString()); - cg.addMethod(cpf.toString()); - try { - Class wc = cg.toClass(); - return (Builder) wc.newInstance(); - } catch (RuntimeException e) { - throw e; - } catch (Throwable e) { - throw new RuntimeException(e.getMessage(), e); - } finally { - cg.release(); - } - } - - private static Map propertyMetadatas(Class c) { - Map mm = new HashMap(); // method map. - Map ret = new HashMap(); // property metadata map. - - // All public method. - for (Method m : c.getMethods()) { - if (m.getDeclaringClass() == Object.class) // Ignore Object's method. - continue; - mm.put(ReflectUtils.getDesc(m), m); - } - - Matcher matcher; - for (Map.Entry entry : mm.entrySet()) { - String desc = entry.getKey(); - Method method = entry.getValue(); - if ((matcher = ReflectUtils.GETTER_METHOD_DESC_PATTERN.matcher(desc)).matches() || - (matcher = ReflectUtils.IS_HAS_CAN_METHOD_DESC_PATTERN.matcher(desc)).matches()) { - String pn = propertyName(matcher.group(1)); - Class pt = method.getReturnType(); - PropertyMetadata pm = ret.get(pn); - if (pm == null) { - pm = new PropertyMetadata(); - pm.type = pt; - ret.put(pn, pm); - } else { - if (pm.type != pt) - continue; - } - pm.getter = method.getName(); - } else if ((matcher = ReflectUtils.SETTER_METHOD_DESC_PATTERN.matcher(desc)).matches()) { - String pn = propertyName(matcher.group(1)); - Class pt = method.getParameterTypes()[0]; - PropertyMetadata pm = ret.get(pn); - if (pm == null) { - pm = new PropertyMetadata(); - pm.type = pt; - ret.put(pn, pm); - } else { - if (pm.type != pt) - continue; - } - pm.setter = method.getName(); - } - } - return ret; - } - - private static String propertyName(String s) { - return s.length() == 1 || Character.isLowerCase(s.charAt(1)) ? Character.toLowerCase(s.charAt(0)) + s.substring(1) : s; - } - - private static boolean serializeIgnoreFinalModifier(Class cl) { -// if (cl.isAssignableFrom(BigInteger.class)) return false; -// for performance -// if (cl.getName().startsWith("java")) return true; -// if (cl.getName().startsWith("javax")) return true; - - return false; - } - - @SuppressWarnings("unused") - private static boolean isPrimitiveOrPrimitiveArray1(Class cl) { - if (cl.isPrimitive()) { - return true; - } else { - Class clazz = cl.getClass().getComponentType(); - if (clazz != null && clazz.isPrimitive()) { - return true; - } - } - return false; - } - - private static String defaultArg(Class cl) { - if (boolean.class == cl) return "false"; - if (int.class == cl) return "0"; - if (long.class == cl) return "0l"; - if (double.class == cl) return "(double)0"; - if (float.class == cl) return "(float)0"; - if (short.class == cl) return "(short)0"; - if (char.class == cl) return "(char)0"; - if (byte.class == cl) return "(byte)0"; - if (byte[].class == cl) return "new byte[]{0}"; - if (!cl.isPrimitive()) return "null"; - throw new UnsupportedOperationException(); - } - - private static int compareFieldName(String n1, String n2) { - int l = Math.min(n1.length(), n2.length()); - for (int i = 0; i < l; i++) { - int t = n1.charAt(i) - n2.charAt(i); - if (t != 0) - return t; - } - return n1.length() - n2.length(); - } - - private static void addDesc(Class c) { - String desc = ReflectUtils.getDesc(c); - int index = mDescList.size(); - mDescList.add(desc); - mDescMap.put(desc, index); - } - - abstract public Class getType(); - - public void writeTo(T obj, OutputStream os) throws IOException { - GenericObjectOutput out = new GenericObjectOutput(os); - writeTo(obj, out); - out.flushBuffer(); - } - - public T parseFrom(byte[] b) throws IOException { - return parseFrom(new UnsafeByteArrayInputStream(b)); - } - - public T parseFrom(InputStream is) throws IOException { - return parseFrom(new GenericObjectInput(is)); - } - - abstract public void writeTo(T obj, GenericObjectOutput out) throws IOException; - - abstract public T parseFrom(GenericObjectInput in) throws IOException; - - static class PropertyMetadata { - Class type; - String setter, getter; - } - - public static abstract class AbstractObjectBuilder extends Builder { - abstract public Class getType(); - - public void writeTo(T obj, GenericObjectOutput out) throws IOException { - if (obj == null) { - out.write0(OBJECT_NULL); - } else { - int ref = out.getRef(obj); - if (ref < 0) { - out.addRef(obj); - out.write0(OBJECT); - writeObject(obj, out); - } else { - out.write0(OBJECT_REF); - out.writeUInt(ref); - } - } - } - - public T parseFrom(GenericObjectInput in) throws IOException { - byte b = in.read0(); - switch (b) { - case OBJECT: { - T ret = newInstance(in); - in.addRef(ret); - readObject(ret, in); - return ret; - } - case OBJECT_REF: - return (T) in.getRef(in.readUInt()); - case OBJECT_NULL: - return null; - default: - throw new IOException("Input format error, expect OBJECT|OBJECT_REF|OBJECT_NULL, get " + b); - } - } - - abstract protected void writeObject(T obj, GenericObjectOutput out) throws IOException; - - abstract protected T newInstance(GenericObjectInput in) throws IOException; - - abstract protected void readObject(T ret, GenericObjectInput in) throws IOException; - } -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/DubboSerialization.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/DubboSerialization.java deleted file mode 100644 index 3a475285b28..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/DubboSerialization.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.dubbo; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.Serialization; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class DubboSerialization implements Serialization { - - public byte getContentTypeId() { - return 1; - } - - public String getContentType() { - return "x-application/dubbo"; - } - - public ObjectOutput serialize(URL url, OutputStream out) throws IOException { - return new GenericObjectOutput(out); - } - - public ObjectInput deserialize(URL url, InputStream is) throws IOException { - return new GenericObjectInput(is); - } - -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataFlags.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataFlags.java deleted file mode 100644 index 63806b040e9..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataFlags.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.dubbo; - -/** - * Constants. - */ -public interface GenericDataFlags { - // prefix three bits - byte VARINT = 0, OBJECT = (byte) 0x80; - - // varint tag - byte VARINT8 = VARINT, VARINT16 = VARINT | 1, VARINT24 = VARINT | 2, VARINT32 = VARINT | 3; - - byte VARINT40 = VARINT | 4, VARINT48 = VARINT | 5, VARINT56 = VARINT | 6, VARINT64 = VARINT | 7; - - // varint contants - byte VARINT_NF = VARINT | 10, VARINT_NE = VARINT | 11, VARINT_ND = VARINT | 12; - - byte VARINT_NC = VARINT | 13, VARINT_NB = VARINT | 14, VARINT_NA = VARINT | 15, VARINT_N9 = VARINT | 16; - - byte VARINT_N8 = VARINT | 17, VARINT_N7 = VARINT | 18, VARINT_N6 = VARINT | 19, VARINT_N5 = VARINT | 20; - - byte VARINT_N4 = VARINT | 21, VARINT_N3 = VARINT | 22, VARINT_N2 = VARINT | 23, VARINT_N1 = VARINT | 24; - - byte VARINT_0 = VARINT | 25, VARINT_1 = VARINT | 26, VARINT_2 = VARINT | 27, VARINT_3 = VARINT | 28; - - byte VARINT_4 = VARINT | 29, VARINT_5 = VARINT | 30, VARINT_6 = VARINT | 31, VARINT_7 = VARINT | 32; - - byte VARINT_8 = VARINT | 33, VARINT_9 = VARINT | 34, VARINT_A = VARINT | 35, VARINT_B = VARINT | 36; - - byte VARINT_C = VARINT | 37, VARINT_D = VARINT | 38, VARINT_E = VARINT | 39, VARINT_F = VARINT | 40; - - byte VARINT_10 = VARINT | 41, VARINT_11 = VARINT | 42, VARINT_12 = VARINT | 43, VARINT_13 = VARINT | 44; - - byte VARINT_14 = VARINT | 45, VARINT_15 = VARINT | 46, VARINT_16 = VARINT | 47, VARINT_17 = VARINT | 48; - - byte VARINT_18 = VARINT | 49, VARINT_19 = VARINT | 50, VARINT_1A = VARINT | 51, VARINT_1B = VARINT | 52; - - byte VARINT_1C = VARINT | 53, VARINT_1D = VARINT | 54, VARINT_1E = VARINT | 55, VARINT_1F = VARINT | 56; - - // object tag - byte OBJECT_REF = OBJECT | 1, OBJECT_STREAM = OBJECT | 2, OBJECT_BYTES = OBJECT | 3; - - byte OBJECT_VALUE = OBJECT | 4, OBJECT_VALUES = OBJECT | 5, OBJECT_MAP = OBJECT | 6; - - byte OBJECT_DESC = OBJECT | 10, OBJECT_DESC_ID = OBJECT | 11; - - // object constants - byte OBJECT_NULL = OBJECT | 20, OBJECT_DUMMY = OBJECT | 21; -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataInput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataInput.java deleted file mode 100644 index c899727a7ab..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataInput.java +++ /dev/null @@ -1,566 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.dubbo; - -import com.alibaba.dubbo.common.serialize.DataInput; - -import java.io.EOFException; -import java.io.IOException; -import java.io.InputStream; -import java.io.UTFDataFormatException; - -/** - * Default DataInput impl. - * Not thread-safe. - */ - -public class GenericDataInput implements DataInput, GenericDataFlags { - private static final String EMPTY_STRING = ""; - - private static final byte[] EMPTY_BYTES = {}; - - private final InputStream mInput; - - private final byte[] mBuffer; - - private int mRead = 0; - - private int mPosition = 0; - - public GenericDataInput(InputStream is) { - this(is, 1024); - } - - public GenericDataInput(InputStream is, int buffSize) { - mInput = is; - mBuffer = new byte[buffSize]; - } - - public boolean readBool() throws IOException { - byte b = read0(); - - switch (b) { - case VARINT_0: - return false; - case VARINT_1: - return true; - default: - throw new IOException("Tag error, expect BYTE_TRUE|BYTE_FALSE, but get " + b); - } - } - - public byte readByte() throws IOException { - byte b = read0(); - - switch (b) { - case VARINT8: - return read0(); - case VARINT_0: - return 0; - case VARINT_1: - return 1; - case VARINT_2: - return 2; - case VARINT_3: - return 3; - case VARINT_4: - return 4; - case VARINT_5: - return 5; - case VARINT_6: - return 6; - case VARINT_7: - return 7; - case VARINT_8: - return 8; - case VARINT_9: - return 9; - case VARINT_A: - return 10; - case VARINT_B: - return 11; - case VARINT_C: - return 12; - case VARINT_D: - return 13; - case VARINT_E: - return 14; - case VARINT_F: - return 15; - case VARINT_10: - return 16; - case VARINT_11: - return 17; - case VARINT_12: - return 18; - case VARINT_13: - return 19; - case VARINT_14: - return 20; - case VARINT_15: - return 21; - case VARINT_16: - return 22; - case VARINT_17: - return 23; - case VARINT_18: - return 24; - case VARINT_19: - return 25; - case VARINT_1A: - return 26; - case VARINT_1B: - return 27; - case VARINT_1C: - return 28; - case VARINT_1D: - return 29; - case VARINT_1E: - return 30; - case VARINT_1F: - return 31; - default: - throw new IOException("Tag error, expect VARINT, but get " + b); - } - } - - public short readShort() throws IOException { - return (short) readVarint32(); - } - - public int readInt() throws IOException { - return readVarint32(); - } - - public long readLong() throws IOException { - return readVarint64(); - } - - public float readFloat() throws IOException { - return Float.intBitsToFloat(readVarint32()); - } - - public double readDouble() throws IOException { - return Double.longBitsToDouble(readVarint64()); - } - - public String readUTF() throws IOException { - byte b = read0(); - - switch (b) { - case OBJECT_BYTES: - int len = readUInt(); - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < len; i++) { - byte b1 = read0(); - if ((b1 & 0x80) == 0) { - sb.append((char) b1); - } else if ((b1 & 0xE0) == 0xC0) { - byte b2 = read0(); - sb.append((char) (((b1 & 0x1F) << 6) | (b2 & 0x3F))); - } else if ((b1 & 0xF0) == 0xE0) { - byte b2 = read0(), b3 = read0(); - sb.append((char) (((b1 & 0x0F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F))); - } else - throw new UTFDataFormatException("Bad utf-8 encoding at " + b1); - } - return sb.toString(); - case OBJECT_NULL: - return null; - case OBJECT_DUMMY: - return EMPTY_STRING; - default: - throw new IOException("Tag error, expect BYTES|BYTES_NULL|BYTES_EMPTY, but get " + b); - } - } - - public byte[] readBytes() throws IOException { - byte b = read0(); - - switch (b) { - case OBJECT_BYTES: - return read0(readUInt()); - case OBJECT_NULL: - return null; - case OBJECT_DUMMY: - return EMPTY_BYTES; - default: - throw new IOException("Tag error, expect BYTES|BYTES_NULL|BYTES_EMPTY, but get " + b); - } - } - - public int readUInt() throws IOException { - byte tmp = read0(); - if (tmp < 0) - return tmp & 0x7f; - - int ret = tmp & 0x7f; - if ((tmp = read0()) < 0) { - ret |= (tmp & 0x7f) << 7; - } else { - ret |= tmp << 7; - if ((tmp = read0()) < 0) { - ret |= (tmp & 0x7f) << 14; - } else { - ret |= tmp << 14; - if ((tmp = read0()) < 0) { - ret |= (tmp & 0x7f) << 21; - } else { - ret |= tmp << 21; - ret |= (read0() & 0x7f) << 28; - } - } - } - return ret; - } - - protected byte read0() throws IOException { - if (mPosition == mRead) - fillBuffer(); - - return mBuffer[mPosition++]; - } - - protected byte[] read0(int len) throws IOException { - int rem = mRead - mPosition; - byte[] ret = new byte[len]; - if (len <= rem) { - System.arraycopy(mBuffer, mPosition, ret, 0, len); - mPosition += len; - } else { - System.arraycopy(mBuffer, mPosition, ret, 0, rem); - mPosition = mRead; - - len -= rem; - int read, pos = rem; - - while (len > 0) { - read = mInput.read(ret, pos, len); - if (read == -1) - throw new EOFException(); - pos += read; - len -= read; - } - } - return ret; - } - - private int readVarint32() throws IOException { - byte b = read0(); - - switch (b) { - case VARINT8: - return read0(); - case VARINT16: { - byte b1 = read0(), b2 = read0(); - return (short) ((b1 & 0xff) | ((b2 & 0xff) << 8)); - } - case VARINT24: { - byte b1 = read0(), b2 = read0(), b3 = read0(); - int ret = (b1 & 0xff) | ((b2 & 0xff) << 8) | ((b3 & 0xff) << 16); - if (b3 < 0) - return ret | 0xff000000; - return ret; - } - case VARINT32: { - byte b1 = read0(), b2 = read0(), b3 = read0(), b4 = read0(); - return ((b1 & 0xff) | - ((b2 & 0xff) << 8) | - ((b3 & 0xff) << 16) | - ((b4 & 0xff) << 24)); - } - case VARINT_NF: - return -15; - case VARINT_NE: - return -14; - case VARINT_ND: - return -13; - case VARINT_NC: - return -12; - case VARINT_NB: - return -11; - case VARINT_NA: - return -10; - case VARINT_N9: - return -9; - case VARINT_N8: - return -8; - case VARINT_N7: - return -7; - case VARINT_N6: - return -6; - case VARINT_N5: - return -5; - case VARINT_N4: - return -4; - case VARINT_N3: - return -3; - case VARINT_N2: - return -2; - case VARINT_N1: - return -1; - case VARINT_0: - return 0; - case VARINT_1: - return 1; - case VARINT_2: - return 2; - case VARINT_3: - return 3; - case VARINT_4: - return 4; - case VARINT_5: - return 5; - case VARINT_6: - return 6; - case VARINT_7: - return 7; - case VARINT_8: - return 8; - case VARINT_9: - return 9; - case VARINT_A: - return 10; - case VARINT_B: - return 11; - case VARINT_C: - return 12; - case VARINT_D: - return 13; - case VARINT_E: - return 14; - case VARINT_F: - return 15; - case VARINT_10: - return 16; - case VARINT_11: - return 17; - case VARINT_12: - return 18; - case VARINT_13: - return 19; - case VARINT_14: - return 20; - case VARINT_15: - return 21; - case VARINT_16: - return 22; - case VARINT_17: - return 23; - case VARINT_18: - return 24; - case VARINT_19: - return 25; - case VARINT_1A: - return 26; - case VARINT_1B: - return 27; - case VARINT_1C: - return 28; - case VARINT_1D: - return 29; - case VARINT_1E: - return 30; - case VARINT_1F: - return 31; - default: - throw new IOException("Tag error, expect VARINT, but get " + b); - } - } - - private long readVarint64() throws IOException { - byte b = read0(); - - switch (b) { - case VARINT8: - return read0(); - case VARINT16: { - byte b1 = read0(), b2 = read0(); - return (short) ((b1 & 0xff) | ((b2 & 0xff) << 8)); - } - case VARINT24: { - byte b1 = read0(), b2 = read0(), b3 = read0(); - int ret = (b1 & 0xff) | ((b2 & 0xff) << 8) | ((b3 & 0xff) << 16); - if (b3 < 0) - return ret | 0xff000000; - return ret; - } - case VARINT32: { - byte b1 = read0(), b2 = read0(), b3 = read0(), b4 = read0(); - return ((b1 & 0xff) | - ((b2 & 0xff) << 8) | - ((b3 & 0xff) << 16) | - ((b4 & 0xff) << 24)); - } - case VARINT40: { - byte b1 = read0(), b2 = read0(), b3 = read0(), b4 = read0(), b5 = read0(); - long ret = ((long) b1 & 0xff) | - (((long) b2 & 0xff) << 8) | - (((long) b3 & 0xff) << 16) | - (((long) b4 & 0xff) << 24) | - (((long) b5 & 0xff) << 32); - if (b5 < 0) - return ret | 0xffffff0000000000l; - return ret; - } - case VARINT48: { - byte b1 = read0(), b2 = read0(), b3 = read0(), b4 = read0(), b5 = read0(), b6 = read0(); - long ret = ((long) b1 & 0xff) | - (((long) b2 & 0xff) << 8) | - (((long) b3 & 0xff) << 16) | - (((long) b4 & 0xff) << 24) | - (((long) b5 & 0xff) << 32) | - (((long) b6 & 0xff) << 40); - if (b6 < 0) - return ret | 0xffff000000000000l; - return ret; - } - case VARINT56: { - byte b1 = read0(), b2 = read0(), b3 = read0(), b4 = read0(), b5 = read0(), b6 = read0(), b7 = read0(); - long ret = ((long) b1 & 0xff) | - (((long) b2 & 0xff) << 8) | - (((long) b3 & 0xff) << 16) | - (((long) b4 & 0xff) << 24) | - (((long) b5 & 0xff) << 32) | - (((long) b6 & 0xff) << 40) | - (((long) b7 & 0xff) << 48); - if (b7 < 0) - return ret | 0xff00000000000000l; - return ret; - } - case VARINT64: { - byte b1 = read0(), b2 = read0(), b3 = read0(), b4 = read0(); - byte b5 = read0(), b6 = read0(), b7 = read0(), b8 = read0(); - return (((long) b1 & 0xff) | - (((long) b2 & 0xff) << 8) | - (((long) b3 & 0xff) << 16) | - (((long) b4 & 0xff) << 24) | - (((long) b5 & 0xff) << 32) | - (((long) b6 & 0xff) << 40) | - (((long) b7 & 0xff) << 48) | - (((long) b8 & 0xff) << 56)); - } - case VARINT_NF: - return -15; - case VARINT_NE: - return -14; - case VARINT_ND: - return -13; - case VARINT_NC: - return -12; - case VARINT_NB: - return -11; - case VARINT_NA: - return -10; - case VARINT_N9: - return -9; - case VARINT_N8: - return -8; - case VARINT_N7: - return -7; - case VARINT_N6: - return -6; - case VARINT_N5: - return -5; - case VARINT_N4: - return -4; - case VARINT_N3: - return -3; - case VARINT_N2: - return -2; - case VARINT_N1: - return -1; - case VARINT_0: - return 0; - case VARINT_1: - return 1; - case VARINT_2: - return 2; - case VARINT_3: - return 3; - case VARINT_4: - return 4; - case VARINT_5: - return 5; - case VARINT_6: - return 6; - case VARINT_7: - return 7; - case VARINT_8: - return 8; - case VARINT_9: - return 9; - case VARINT_A: - return 10; - case VARINT_B: - return 11; - case VARINT_C: - return 12; - case VARINT_D: - return 13; - case VARINT_E: - return 14; - case VARINT_F: - return 15; - case VARINT_10: - return 16; - case VARINT_11: - return 17; - case VARINT_12: - return 18; - case VARINT_13: - return 19; - case VARINT_14: - return 20; - case VARINT_15: - return 21; - case VARINT_16: - return 22; - case VARINT_17: - return 23; - case VARINT_18: - return 24; - case VARINT_19: - return 25; - case VARINT_1A: - return 26; - case VARINT_1B: - return 27; - case VARINT_1C: - return 28; - case VARINT_1D: - return 29; - case VARINT_1E: - return 30; - case VARINT_1F: - return 31; - default: - throw new IOException("Tag error, expect VARINT, but get " + b); - } - } - - private void fillBuffer() throws IOException { - mPosition = 0; - mRead = mInput.read(mBuffer); - - if (mRead == -1) { - mRead = 0; - throw new EOFException(); - } - } -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataOutput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataOutput.java deleted file mode 100644 index 3119e9ccf71..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericDataOutput.java +++ /dev/null @@ -1,493 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.dubbo; - -import com.alibaba.dubbo.common.serialize.DataOutput; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * Default data output impl. - * Not thread-safe. - */ - -public class GenericDataOutput implements DataOutput, GenericDataFlags { - private static final int CHAR_BUF_SIZE = 256; - - private final byte[] mBuffer, mTemp = new byte[9]; - - private final char[] mCharBuf = new char[CHAR_BUF_SIZE]; - - private final OutputStream mOutput; - - private final int mLimit; - - private int mPosition = 0; - - public GenericDataOutput(OutputStream out) { - this(out, 1024); - } - - public GenericDataOutput(OutputStream out, int buffSize) { - mOutput = out; - mLimit = buffSize; - mBuffer = new byte[buffSize]; - } - - public void writeBool(boolean v) throws IOException { - write0(v ? VARINT_1 : VARINT_0); - } - - public void writeByte(byte v) throws IOException { - switch (v) { - case 0: - write0(VARINT_0); - break; - case 1: - write0(VARINT_1); - break; - case 2: - write0(VARINT_2); - break; - case 3: - write0(VARINT_3); - break; - case 4: - write0(VARINT_4); - break; - case 5: - write0(VARINT_5); - break; - case 6: - write0(VARINT_6); - break; - case 7: - write0(VARINT_7); - break; - case 8: - write0(VARINT_8); - break; - case 9: - write0(VARINT_9); - break; - case 10: - write0(VARINT_A); - break; - case 11: - write0(VARINT_B); - break; - case 12: - write0(VARINT_C); - break; - case 13: - write0(VARINT_D); - break; - case 14: - write0(VARINT_E); - break; - case 15: - write0(VARINT_F); - break; - case 16: - write0(VARINT_10); - break; - case 17: - write0(VARINT_11); - break; - case 18: - write0(VARINT_12); - break; - case 19: - write0(VARINT_13); - break; - case 20: - write0(VARINT_14); - break; - case 21: - write0(VARINT_15); - break; - case 22: - write0(VARINT_16); - break; - case 23: - write0(VARINT_17); - break; - case 24: - write0(VARINT_18); - break; - case 25: - write0(VARINT_19); - break; - case 26: - write0(VARINT_1A); - break; - case 27: - write0(VARINT_1B); - break; - case 28: - write0(VARINT_1C); - break; - case 29: - write0(VARINT_1D); - break; - case 30: - write0(VARINT_1E); - break; - case 31: - write0(VARINT_1F); - break; - default: - write0(VARINT8); - write0(v); - } - } - - public void writeShort(short v) throws IOException { - writeVarint32(v); - } - - public void writeInt(int v) throws IOException { - writeVarint32(v); - } - - public void writeLong(long v) throws IOException { - writeVarint64(v); - } - - public void writeFloat(float v) throws IOException { - writeVarint32(Float.floatToRawIntBits(v)); - } - - public void writeDouble(double v) throws IOException { - writeVarint64(Double.doubleToRawLongBits(v)); - } - - public void writeUTF(String v) throws IOException { - if (v == null) { - write0(OBJECT_NULL); - } else { - int len = v.length(); - if (len == 0) { - write0(OBJECT_DUMMY); - } else { - write0(OBJECT_BYTES); - writeUInt(len); - - int off = 0, limit = mLimit - 3, size; - char[] buf = mCharBuf; - do { - size = Math.min(len - off, CHAR_BUF_SIZE); - v.getChars(off, off + size, buf, 0); - - for (int i = 0; i < size; i++) { - char c = buf[i]; - if (mPosition > limit) { - if (c < 0x80) { - write0((byte) c); - } else if (c < 0x800) { - write0((byte) (0xC0 | ((c >> 6) & 0x1F))); - write0((byte) (0x80 | (c & 0x3F))); - } else { - write0((byte) (0xE0 | ((c >> 12) & 0x0F))); - write0((byte) (0x80 | ((c >> 6) & 0x3F))); - write0((byte) (0x80 | (c & 0x3F))); - } - } else { - if (c < 0x80) { - mBuffer[mPosition++] = (byte) c; - } else if (c < 0x800) { - mBuffer[mPosition++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); - mBuffer[mPosition++] = (byte) (0x80 | (c & 0x3F)); - } else { - mBuffer[mPosition++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); - mBuffer[mPosition++] = (byte) (0x80 | ((c >> 6) & 0x3F)); - mBuffer[mPosition++] = (byte) (0x80 | (c & 0x3F)); - } - } - } - off += size; - } - while (off < len); - } - } - } - - public void writeBytes(byte[] b) throws IOException { - if (b == null) - write0(OBJECT_NULL); - else - writeBytes(b, 0, b.length); - } - - public void writeBytes(byte[] b, int off, int len) throws IOException { - if (len == 0) { - write0(OBJECT_DUMMY); - } else { - write0(OBJECT_BYTES); - writeUInt(len); - write0(b, off, len); - } - } - - public void flushBuffer() throws IOException { - if (mPosition > 0) { - mOutput.write(mBuffer, 0, mPosition); - mPosition = 0; - } - } - - public void writeUInt(int v) throws IOException { - byte tmp; - while (true) { - tmp = (byte) (v & 0x7f); - if ((v >>>= 7) == 0) { - write0((byte) (tmp | 0x80)); - return; - } else { - write0(tmp); - } - } - } - - protected void write0(byte b) throws IOException { - if (mPosition == mLimit) - flushBuffer(); - - mBuffer[mPosition++] = b; - } - - protected void write0(byte[] b, int off, int len) throws IOException { - int rem = mLimit - mPosition; - if (rem > len) { - System.arraycopy(b, off, mBuffer, mPosition, len); - mPosition += len; - } else { - System.arraycopy(b, off, mBuffer, mPosition, rem); - mPosition = mLimit; - flushBuffer(); - - off += rem; - len -= rem; - - if (mLimit > len) { - System.arraycopy(b, off, mBuffer, 0, len); - mPosition = len; - } else { - mOutput.write(b, off, len); - } - } - } - - private void writeVarint32(int v) throws IOException { - switch (v) { - case -15: - write0(VARINT_NF); - break; - case -14: - write0(VARINT_NE); - break; - case -13: - write0(VARINT_ND); - break; - case -12: - write0(VARINT_NC); - break; - case -11: - write0(VARINT_NB); - break; - case -10: - write0(VARINT_NA); - break; - case -9: - write0(VARINT_N9); - break; - case -8: - write0(VARINT_N8); - break; - case -7: - write0(VARINT_N7); - break; - case -6: - write0(VARINT_N6); - break; - case -5: - write0(VARINT_N5); - break; - case -4: - write0(VARINT_N4); - break; - case -3: - write0(VARINT_N3); - break; - case -2: - write0(VARINT_N2); - break; - case -1: - write0(VARINT_N1); - break; - case 0: - write0(VARINT_0); - break; - case 1: - write0(VARINT_1); - break; - case 2: - write0(VARINT_2); - break; - case 3: - write0(VARINT_3); - break; - case 4: - write0(VARINT_4); - break; - case 5: - write0(VARINT_5); - break; - case 6: - write0(VARINT_6); - break; - case 7: - write0(VARINT_7); - break; - case 8: - write0(VARINT_8); - break; - case 9: - write0(VARINT_9); - break; - case 10: - write0(VARINT_A); - break; - case 11: - write0(VARINT_B); - break; - case 12: - write0(VARINT_C); - break; - case 13: - write0(VARINT_D); - break; - case 14: - write0(VARINT_E); - break; - case 15: - write0(VARINT_F); - break; - case 16: - write0(VARINT_10); - break; - case 17: - write0(VARINT_11); - break; - case 18: - write0(VARINT_12); - break; - case 19: - write0(VARINT_13); - break; - case 20: - write0(VARINT_14); - break; - case 21: - write0(VARINT_15); - break; - case 22: - write0(VARINT_16); - break; - case 23: - write0(VARINT_17); - break; - case 24: - write0(VARINT_18); - break; - case 25: - write0(VARINT_19); - break; - case 26: - write0(VARINT_1A); - break; - case 27: - write0(VARINT_1B); - break; - case 28: - write0(VARINT_1C); - break; - case 29: - write0(VARINT_1D); - break; - case 30: - write0(VARINT_1E); - break; - case 31: - write0(VARINT_1F); - break; - default: - int t = v, ix = 0; - byte[] b = mTemp; - - while (true) { - b[++ix] = (byte) (v & 0xff); - if ((v >>>= 8) == 0) - break; - } - - if (t > 0) { - // [ 0a e2 => 0a e2 00 ] [ 92 => 92 00 ] - if (b[ix] < 0) - b[++ix] = 0; - } else { - // [ 01 ff ff ff => 01 ff ] [ e0 ff ff ff => e0 ] - while (b[ix] == (byte) 0xff && b[ix - 1] < 0) - ix--; - } - - b[0] = (byte) (VARINT + ix - 1); - write0(b, 0, ix + 1); - } - } - - private void writeVarint64(long v) throws IOException { - int i = (int) v; - if (v == i) { - writeVarint32(i); - } else { - long t = v; - int ix = 0; - byte[] b = mTemp; - - while (true) { - b[++ix] = (byte) (v & 0xff); - if ((v >>>= 8) == 0) - break; - } - - if (t > 0) { - // [ 0a e2 => 0a e2 00 ] [ 92 => 92 00 ] - if (b[ix] < 0) - b[++ix] = 0; - } else { - // [ 01 ff ff ff => 01 ff ] [ e0 ff ff ff => e0 ] - while (b[ix] == (byte) 0xff && b[ix - 1] < 0) - ix--; - } - - b[0] = (byte) (VARINT + ix - 1); - write0(b, 0, ix + 1); - } - } -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectInput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectInput.java deleted file mode 100644 index 3f56218c741..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectInput.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.dubbo; - -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.utils.ReflectUtils; -import com.alibaba.dubbo.common.utils.StringUtils; - -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - -/** - * Generic Object Input. - */ -public class GenericObjectInput extends GenericDataInput implements ObjectInput { - private static Object SKIPPED_OBJECT = new Object(); - - private ClassDescriptorMapper mMapper; - - private List mRefs = new ArrayList(); - - public GenericObjectInput(InputStream is) { - this(is, Builder.DEFAULT_CLASS_DESCRIPTOR_MAPPER); - } - - public GenericObjectInput(InputStream is, ClassDescriptorMapper mapper) { - super(is); - mMapper = mapper; - } - - public GenericObjectInput(InputStream is, int buffSize) { - this(is, buffSize, Builder.DEFAULT_CLASS_DESCRIPTOR_MAPPER); - } - - public GenericObjectInput(InputStream is, int buffSize, ClassDescriptorMapper mapper) { - super(is, buffSize); - mMapper = mapper; - } - - public Object readObject() throws IOException { - String desc; - byte b = read0(); - - switch (b) { - case OBJECT_NULL: - return null; - case OBJECT_DUMMY: - return new Object(); - case OBJECT_DESC: { - desc = readUTF(); - break; - } - case OBJECT_DESC_ID: { - int index = readUInt(); - desc = mMapper.getDescriptor(index); - if (desc == null) - throw new IOException("Can not find desc id: " + index); - break; - } - default: - throw new IOException("Flag error, expect OBJECT_NULL|OBJECT_DUMMY|OBJECT_DESC|OBJECT_DESC_ID, get " + b); - } - try { - Class c = ReflectUtils.desc2class(desc); - return Builder.register(c).parseFrom(this); - } catch (ClassNotFoundException e) { - throw new IOException("Read object failed, class not found. " + StringUtils.toString(e)); - } - } - - @SuppressWarnings("unchecked") - public T readObject(Class cls) throws IOException, ClassNotFoundException { - return (T) readObject(); - } - - @SuppressWarnings("unchecked") - public T readObject(Class cls, Type type) throws IOException, ClassNotFoundException { - return (T) readObject(); - } - - public void addRef(Object obj) { - mRefs.add(obj); - } - - public Object getRef(int index) throws IOException { - if (index < 0 || index >= mRefs.size()) - return null; - - Object ret = mRefs.get(index); - if (ret == SKIPPED_OBJECT) - throw new IOException("Ref skipped-object."); - return ret; - } - - public void skipAny() throws IOException { - byte b = read0(); - switch (b) { - case VARINT_NF: - case VARINT_NE: - case VARINT_ND: - case VARINT_NC: - case VARINT_NB: - case VARINT_NA: - case VARINT_N9: - case VARINT_N8: - case VARINT_N7: - case VARINT_N6: - case VARINT_N5: - case VARINT_N4: - case VARINT_N3: - case VARINT_N2: - case VARINT_N1: - case VARINT_0: - case VARINT_1: - case VARINT_2: - case VARINT_3: - case VARINT_4: - case VARINT_5: - case VARINT_6: - case VARINT_7: - case VARINT_8: - case VARINT_9: - case VARINT_A: - case VARINT_B: - case VARINT_C: - case VARINT_D: - case VARINT_E: - case VARINT_F: - case VARINT_10: - case VARINT_11: - case VARINT_12: - case VARINT_13: - case VARINT_14: - case VARINT_15: - case VARINT_16: - case VARINT_17: - case VARINT_18: - case VARINT_19: - case VARINT_1A: - case VARINT_1B: - case VARINT_1C: - case VARINT_1D: - case VARINT_1E: - case VARINT_1F: - case OBJECT_NULL: - case OBJECT_DUMMY: - break; - case VARINT8: { - read0(); - break; - } - case VARINT16: { - read0(); - read0(); - break; - } - case VARINT24: { - read0(); - read0(); - read0(); - break; - } - case VARINT32: { - read0(); - read0(); - read0(); - read0(); - break; - } - case VARINT40: { - read0(); - read0(); - read0(); - read0(); - read0(); - break; - } - case VARINT48: { - read0(); - read0(); - read0(); - read0(); - read0(); - read0(); - break; - } - case VARINT56: { - read0(); - read0(); - read0(); - read0(); - read0(); - read0(); - read0(); - break; - } - case VARINT64: { - read0(); - read0(); - read0(); - read0(); - read0(); - read0(); - read0(); - read0(); - break; - } - case OBJECT: { - addRef(SKIPPED_OBJECT); - int len = readUInt(); - for (int i = 0; i < len; i++) - skipAny(); - break; - } - case OBJECT_REF: { - readUInt(); - break; - } - case OBJECT_STREAM: - case OBJECT_BYTES: { - read0(readUInt()); - break; - } - case OBJECT_VALUE: { - skipAny(); - break; - } - case OBJECT_VALUES: { - int len = readUInt(); - for (int i = 0; i < len; i++) - skipAny(); - break; - } - case OBJECT_MAP: { - int len = readUInt(); - for (int i = 0; i < len; i++) { - skipAny(); // skip key - skipAny(); // skip value - } - break; - } - case OBJECT_DESC: { - readUTF(); - int len = readUInt(); - for (int i = 0; i < len; i++) - skipAny(); - break; - } - case OBJECT_DESC_ID: { - readUInt(); - int len = readUInt(); - for (int i = 0; i < len; i++) - skipAny(); - break; - } - default: - throw new IOException("Flag error, get " + b); - } - } -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectOutput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectOutput.java deleted file mode 100644 index 71e9eb9a92b..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/GenericObjectOutput.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.dubbo; - -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.utils.ReflectUtils; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Generic Object Output. - */ -public class GenericObjectOutput extends GenericDataOutput implements ObjectOutput { - private final boolean isAllowNonSerializable; - private ClassDescriptorMapper mMapper; - private Map mRefs = new ConcurrentHashMap(); - - public GenericObjectOutput(OutputStream out) { - this(out, Builder.DEFAULT_CLASS_DESCRIPTOR_MAPPER); - } - - public GenericObjectOutput(OutputStream out, ClassDescriptorMapper mapper) { - super(out); - mMapper = mapper; - isAllowNonSerializable = false; - } - - public GenericObjectOutput(OutputStream out, int buffSize) { - this(out, buffSize, Builder.DEFAULT_CLASS_DESCRIPTOR_MAPPER, false); - } - - public GenericObjectOutput(OutputStream out, int buffSize, ClassDescriptorMapper mapper) { - this(out, buffSize, mapper, false); - } - - public GenericObjectOutput(OutputStream out, int buffSize, ClassDescriptorMapper mapper, boolean isAllowNonSerializable) { - super(out, buffSize); - mMapper = mapper; - this.isAllowNonSerializable = isAllowNonSerializable; - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - public void writeObject(Object obj) throws IOException { - if (obj == null) { - write0(OBJECT_NULL); - return; - } - - Class c = obj.getClass(); - if (c == Object.class) { - write0(OBJECT_DUMMY); - } else { - String desc = ReflectUtils.getDesc(c); - int index = mMapper.getDescriptorIndex(desc); - if (index < 0) { - write0(OBJECT_DESC); - writeUTF(desc); - } else { - write0(OBJECT_DESC_ID); - writeUInt(index); - } - Builder b = Builder.register(c, isAllowNonSerializable); - b.writeTo(obj, this); - } - } - - public void addRef(Object obj) { - mRefs.put(obj, mRefs.size()); - } - - public int getRef(Object obj) { - Integer ref = mRefs.get(obj); - if (ref == null) - return -1; - return ref.intValue(); - } -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectInput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectInput.java deleted file mode 100644 index 3a4a718cb7b..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectInput.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.json; - -import com.alibaba.dubbo.common.json.JSON; -import com.alibaba.dubbo.common.json.ParseException; -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.utils.PojoUtils; - -import java.io.BufferedReader; -import java.io.EOFException; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.lang.reflect.Type; -import java.util.Map; - -/** - * JsonObjectInput - */ -@Deprecated -public class JsonObjectInput implements ObjectInput { - - private final BufferedReader reader; - - public JsonObjectInput(InputStream in) { - this(new InputStreamReader(in)); - } - - public JsonObjectInput(Reader reader) { - this.reader = new BufferedReader(reader); - } - - public boolean readBool() throws IOException { - try { - return readObject(boolean.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public byte readByte() throws IOException { - try { - return readObject(byte.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public short readShort() throws IOException { - try { - return readObject(short.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public int readInt() throws IOException { - try { - return readObject(int.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public long readLong() throws IOException { - try { - return readObject(long.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public float readFloat() throws IOException { - try { - return readObject(float.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public double readDouble() throws IOException { - try { - return readObject(double.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public String readUTF() throws IOException { - try { - return readObject(String.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public byte[] readBytes() throws IOException { - return readLine().getBytes(); - } - - public Object readObject() throws IOException, ClassNotFoundException { - try { - String json = readLine(); - if (json.startsWith("{")) { - return JSON.parse(json, Map.class); - } else { - json = "{\"value\":" + json + "}"; - - @SuppressWarnings("unchecked") - Map map = JSON.parse(json, Map.class); - return map.get("value"); - } - } catch (ParseException e) { - throw new IOException(e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - public T readObject(Class cls) throws IOException, ClassNotFoundException { - Object value = readObject(); - return (T) PojoUtils.realize(value, cls); - /*try { - return JSON.parse(readLine(), cls); - } catch (ParseException e) { - throw new IOException(e.getMessage()); - }*/ - } - - @SuppressWarnings("unchecked") - public T readObject(Class cls, Type type) throws IOException, ClassNotFoundException { - Object value = readObject(); - return (T) PojoUtils.realize(value, cls, type); - } - - private String readLine() throws IOException, EOFException { - String line = reader.readLine(); - if (line == null || line.trim().length() == 0) throw new EOFException(); - return line; - } -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectOutput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectOutput.java deleted file mode 100644 index 491f173d5a6..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonObjectOutput.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.json; - -import com.alibaba.dubbo.common.json.JSON; -import com.alibaba.dubbo.common.serialize.ObjectOutput; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.Writer; - -/** - * JsonObjectOutput - */ -@Deprecated -public class JsonObjectOutput implements ObjectOutput { - - private final PrintWriter writer; - - private final boolean writeClass; - - public JsonObjectOutput(OutputStream out) { - this(new OutputStreamWriter(out), false); - } - - public JsonObjectOutput(Writer writer) { - this(writer, false); - } - - public JsonObjectOutput(OutputStream out, boolean writeClass) { - this(new OutputStreamWriter(out), writeClass); - } - - public JsonObjectOutput(Writer writer, boolean writeClass) { - this.writer = new PrintWriter(writer); - this.writeClass = writeClass; - } - - public void writeBool(boolean v) throws IOException { - writeObject(v); - } - - public void writeByte(byte v) throws IOException { - writeObject(v); - } - - public void writeShort(short v) throws IOException { - writeObject(v); - } - - public void writeInt(int v) throws IOException { - writeObject(v); - } - - public void writeLong(long v) throws IOException { - writeObject(v); - } - - public void writeFloat(float v) throws IOException { - writeObject(v); - } - - public void writeDouble(double v) throws IOException { - writeObject(v); - } - - public void writeUTF(String v) throws IOException { - writeObject(v); - } - - public void writeBytes(byte[] b) throws IOException { - writer.println(new String(b)); - } - - public void writeBytes(byte[] b, int off, int len) throws IOException { - writer.println(new String(b, off, len)); - } - - public void writeObject(Object obj) throws IOException { - JSON.json(obj, writer, writeClass); - writer.println(); - writer.flush(); - } - - public void flushBuffer() throws IOException { - writer.flush(); - } - -} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonSerialization.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonSerialization.java deleted file mode 100644 index 488786961b1..00000000000 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/JsonSerialization.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.json; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.Serialization; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * JsonSerialization - */ -@Deprecated -public class JsonSerialization implements Serialization { - - public byte getContentTypeId() { - return 5; - } - - public String getContentType() { - return "text/json"; - } - - public ObjectOutput serialize(URL url, OutputStream output) throws IOException { - return new JsonObjectOutput(output, url.getParameter("with.class", true)); - } - - public ObjectInput deserialize(URL url, InputStream input) throws IOException { - return new JsonObjectInput(input); - } - -} \ No newline at end of file diff --git a/dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization b/dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization deleted file mode 100644 index 92cc56d4775..00000000000 --- a/dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization +++ /dev/null @@ -1,8 +0,0 @@ -dubbo=com.alibaba.dubbo.common.serialize.support.dubbo.DubboSerialization -hessian2=com.alibaba.dubbo.common.serialize.support.hessian.Hessian2Serialization -java=com.alibaba.dubbo.common.serialize.support.java.JavaSerialization -compactedjava=com.alibaba.dubbo.common.serialize.support.java.CompactedJavaSerialization -fastjson=com.alibaba.dubbo.common.serialize.support.json.FastJsonSerialization -nativejava=com.alibaba.dubbo.common.serialize.support.nativejava.NativeJavaSerialization -kryo=com.alibaba.dubbo.common.serialize.support.kryo.KryoSerialization -fst=com.alibaba.dubbo.common.serialize.support.fst.FstSerialization \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/SerializationCompareTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/SerializationCompareTest.java deleted file mode 100644 index 056fa352117..00000000000 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/SerializationCompareTest.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize; - -import com.alibaba.com.caucho.hessian.io.Hessian2Input; -import com.alibaba.com.caucho.hessian.io.Hessian2Output; -import com.alibaba.dubbo.common.io.Bytes; -import com.alibaba.dubbo.common.serialize.support.dubbo.Builder; -import com.alibaba.dubbo.common.serialize.support.java.CompactedObjectInputStream; -import com.alibaba.dubbo.common.serialize.support.java.CompactedObjectOutputStream; - -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.HashMap; - -import static org.junit.Assert.assertEquals; - -public class SerializationCompareTest { - @Test - public void test_CompareSerializeLength() throws Exception { - long[] data = new long[]{-1l, 2l, 3l, 4l, 5l}; - ByteArrayOutputStream os; - - os = new ByteArrayOutputStream(); - ObjectOutputStream jos = new ObjectOutputStream(os); - jos.writeObject(data); - System.out.println("java:" + Bytes.bytes2hex(os.toByteArray()) + ":" + os.size()); - - os = new ByteArrayOutputStream(); - CompactedObjectOutputStream oos = new CompactedObjectOutputStream(os); - oos.writeObject(data); - System.out.println("compacted java:" + Bytes.bytes2hex(os.toByteArray()) + ":" + os.size()); - - os = new ByteArrayOutputStream(); - Hessian2Output h2o = new Hessian2Output(os); - h2o.writeObject(data); - h2o.flushBuffer(); - System.out.println("hessian:" + Bytes.bytes2hex(os.toByteArray()) + ":" + os.size()); - - os = new ByteArrayOutputStream(); - Builder lb = Builder.register(long[].class); - lb.writeTo(data, os); - System.out.println("DataOutput:" + Bytes.bytes2hex(os.toByteArray()) + ":" + os.size()); - } - - @Test - public void testBuilderPerm() throws Exception { - Builder bb = Builder.register(Bean.class); - Bean bean = new Bean(); - int len = 0; - long now = System.currentTimeMillis(); - for (int i = 0; i < 500; i++) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - bb.writeTo(bean, os); - os.close(); - if (i == 0) - len = os.toByteArray().length; - - ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); - Bean b = bb.parseFrom(is); - assertEquals(b.getClass(), Bean.class); - } - System.out.println("Builder write and parse 500 times in " + (System.currentTimeMillis() - now) + "ms, size " + len); - } - - @Test - public void testH2oPerm() throws Exception { - Bean bean = new Bean(); - int len = 0; - long now = System.currentTimeMillis(); - for (int i = 0; i < 500; i++) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - Hessian2Output out = new Hessian2Output(os); - out.writeObject(bean); - out.flushBuffer(); - os.close(); - if (i == 0) - len = os.toByteArray().length; - ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); - Hessian2Input in = new Hessian2Input(is); - assertEquals(in.readObject().getClass(), Bean.class); - } - System.out.println("Hessian2 write and parse 500 times in " + (System.currentTimeMillis() - now) + "ms, size " + len); - } - - @Test - public void testJavaOutputPerm() throws Exception { - Bean bean = new Bean(); - int len = 0; - long now = System.currentTimeMillis(); - for (int i = 0; i < 500; i++) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(os); - out.writeObject(bean); - os.close(); - if (i == 0) - len = os.toByteArray().length; - ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); - ObjectInputStream in = new ObjectInputStream(is); - assertEquals(in.readObject().getClass(), Bean.class); - } - System.out.println("java write and parse 500 times in " + (System.currentTimeMillis() - now) + "ms, size " + len); - } - - @Test - public void testCompactedJavaOutputPerm() throws Exception { - Bean bean = new Bean(); - int len = 0; - long now = System.currentTimeMillis(); - for (int i = 0; i < 500; i++) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - CompactedObjectOutputStream out = new CompactedObjectOutputStream(os); - out.writeObject(bean); - os.close(); - if (i == 0) - len = os.toByteArray().length; - ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); - CompactedObjectInputStream in = new CompactedObjectInputStream(is); - assertEquals(in.readObject().getClass(), Bean.class); - } - System.out.println("compacted java write and parse 500 times in " + (System.currentTimeMillis() - now) + "ms, size " + len); - } - - public static enum EnumTest {READ, WRITE, CREATE, UNREGISTER} - - ; - - static class MyList extends ArrayList { - private static final long serialVersionUID = 1L; - - private int code = 12345; - private String id = "feedback"; - } - - static class MyMap extends HashMap { - private static final long serialVersionUID = 1L; - - private int code = 12345; - private String id = "feedback"; - } - - public static class Bean implements Serializable { - private static final long serialVersionUID = 7737610585231102146L; - - public EnumTest ve = EnumTest.CREATE; - - public int vi = 0; - public long vl = 100l; - - boolean b = true; - boolean[] bs = {false, true}; - - String s1 = "1234567890"; - String s2 = "1234567890一二三四五六七八九零"; - - int i = 123123, ni = -12344, is[] = {1, 2, 3, 4, -1, -2, -3, -4}; - short s = 12, ns = -76; - double d = 12.345, nd = -12.345; - long l = 1281447759383l, nl = -13445l; - private ArrayList mylist = new ArrayList(); - private HashMap mymap = new HashMap(); - - { - mylist.add(1); - mylist.add("qianlei"); - mylist.add("qianlei"); - mylist.add("qianlei"); - mylist.add("qianlei"); - } - - { - mymap.put(1, 2); - mymap.put(2, "1234"); - mymap.put("2345", 12938.122); - mymap.put("2345", -1); - mymap.put("2345", -1.20); - } - - public ArrayList getMylist() { - return mylist; - } - - public void setMylist(ArrayList list) { - mylist = list; - } - - public HashMap getMymap() { - return mymap; - } - - public void setMymap(HashMap map) { - mymap = map; - } - } -} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/BuilderTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/BuilderTest.java deleted file mode 100644 index 3b3e613791a..00000000000 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/BuilderTest.java +++ /dev/null @@ -1,516 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.dubbo; - -import com.alibaba.dubbo.common.io.Bytes; -import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; -import com.alibaba.dubbo.common.serialize.support.dubbo.Builder; - -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; - -import java.io.Serializable; -import java.lang.reflect.Modifier; -import java.sql.Time; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; - -public class BuilderTest { - public static void main(String[] args) { - System.out.println(Modifier.isPublic(String.class.getModifiers())); - } - - @Test - public void testPrimaryTypeBuilder() throws Exception { - System.out.println((new byte[2]).hashCode()); - Builder builder = Builder.register(String.class); - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - String v = "123"; - builder.writeTo(v, os); - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - v = builder.parseFrom(b); - builder.writeTo(v, os); - b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - } - - @Test - public void testEnumBuilder() throws Exception { - Builder builder = Builder.register(Type.class); - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - Type v = Type.High; - builder.writeTo(v, os); - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - v = builder.parseFrom(b); - } - - @Test - public void testThrowableBuilder() throws Exception { - Builder builder = Builder.register(Throwable.class); - Throwable th = new Throwable(); - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - builder.writeTo(th, os); - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - - th = builder.parseFrom(b); - } - - @Test - public void testArrayClassBuilder() throws Exception { - UnsafeByteArrayOutputStream os; - - byte[] b; - - Builder osb = Builder.register(Object[].class); - os = new UnsafeByteArrayOutputStream(); - osb.writeTo(new Object[]{new String[0]}, os); - b = os.toByteArray(); - - Builder lsb = Builder.register(long[].class); - os = new UnsafeByteArrayOutputStream(); - lsb.writeTo(new long[]{1, 121232, -3, 4, -5, 61321432413l}, os); - lsb.writeTo(new long[]{1, 121232, -3, 4, -5, 61321432413l}, os); - lsb.writeTo(new long[]{1, 2, 3, 12131314, 123132313135l, -6}, os); - b = os.toByteArray(); - long[] ls = lsb.parseFrom(b); - assertEquals(ls.length, 6); - - Builder bsb = Builder.register(byte[].class); - os = new UnsafeByteArrayOutputStream(); - bsb.writeTo("i am a string.".getBytes(), os); - b = os.toByteArray(); - - Builder iisb = Builder.register(int[][].class); - os = new UnsafeByteArrayOutputStream(); - iisb.writeTo(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10}, {122, 123, 444}}, os); - b = os.toByteArray(); - int[][] iis = iisb.parseFrom(b); - assertEquals(iis.length, 4); - - Builder iiisb = Builder.register(int[][][].class); - os = new UnsafeByteArrayOutputStream(); - iiisb.writeTo(new int[][][]{ - {{1, 2, 3, 4}}, - {{5, 6, 7, 8}}, - {{122, 123, 444}} - }, os); - b = os.toByteArray(); - int[][][] iii = iiisb.parseFrom(b); - assertEquals(iii.length, 3); - } - - @Test - public void testObjectBuilder() throws Exception { - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - Builder BeanBuilder = Builder.register(Bean.class); - - Bean bean = new Bean(); - bean.name = "ql"; - bean.type = Type.High; - bean.types = new Type[]{Type.High, Type.High}; - BeanBuilder.writeTo(bean, os); - - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - - bean = BeanBuilder.parseFrom(b); - assertNull(bean.time); - assertEquals(bean.i, 123123); - assertEquals(bean.ni, -12344); - assertEquals(bean.d, 12.345); - assertEquals(bean.nd, -12.345); - assertEquals(bean.l, 1281447759383l); - assertEquals(bean.nl, -13445l); - assertEquals(bean.vl, 100l); - assertEquals(bean.type, Type.High); - assertEquals(bean.types.length, 2); - assertEquals(bean.types[0], Type.High); - assertEquals(bean.types[1], Type.High); - assertEquals(bean.list.size(), 3); - assertEquals(bean.list.get(0), 1); - assertEquals(bean.list.get(1), 2); - assertEquals(bean.list.get(2), 1308147); - } - - @Test - public void testInterfaceBuilder() throws Exception { - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - Builder builder = Builder.register(TestDO.class); - TestDO d = new TestDOImpl(); - builder.writeTo(d, os); - - byte[] b = os.toByteArray(); - - d = builder.parseFrom(b); - assertTrue(TestDO.class.isAssignableFrom(d.getClass())); - assertEquals("name", d.getName()); - assertEquals(28, d.getArg()); - assertEquals(Type.High, d.getType()); - } - - @Test - public void testGenericBuilder() throws Exception { - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - Builder ob = Builder.register(Object.class); - - Object o = new Object(); - ob.writeTo(o, os); - byte[] b = os.toByteArray(); - - os = new UnsafeByteArrayOutputStream(); - Bean bean = new Bean(); - bean.name = "ql"; - bean.type = Type.High; - bean.types = new Type[]{Type.High, Type.High}; - ob.writeTo(bean, os); - - b = os.toByteArray(); - bean = (Bean) ob.parseFrom(b); - assertEquals(bean.i, 123123); - assertEquals(bean.ni, -12344); - assertEquals(bean.d, 12.345); - assertEquals(bean.nd, -12.345); - assertEquals(bean.l, 1281447759383l); - assertEquals(bean.nl, -13445l); - assertEquals(bean.vl, 100l); - assertEquals(bean.type, Type.High); - assertEquals(bean.types.length, 2); - assertEquals(bean.types[0], Type.High); - assertEquals(bean.types[1], Type.High); - assertEquals(bean.list.size(), 3); - assertEquals(bean.list.get(0), 1); - assertEquals(bean.list.get(1), 2); - assertEquals(bean.list.get(2), 1308147); - } - - @Test - public void testObjectArrayBuilder() throws Exception { - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - Builder builder = Builder.register(Object[].class); - - Object[] obj = new Object[5]; - obj[0] = "1234"; - obj[1] = new Double(109.23); - obj[2] = "3455"; - obj[3] = null; - obj[4] = Boolean.TRUE; - - builder.writeTo(obj, os); - byte[] b = os.toByteArray(); - System.out.println("Object array:" + b.length + ":" + Bytes.bytes2hex(b)); - - Assert.assertArrayEquals(obj, builder.parseFrom(b)); - } - - // FIXME MyList's attribute 'size' is inherited from ArrayList, and it's set when list is decoded. Attribute 'size' - // will get incorrect if add more elements into the list - @Ignore - @Test - @SuppressWarnings({"rawtypes", "unchecked"}) - public void testBuilder_MyList() throws Exception { - Builder b1 = Builder.register(MyList.class); - MyList list = new MyList(); - list.add(new boolean[]{true, false}); - list.add(new int[]{1, 2, 3, 4, 5}); - list.add("String"); - list.add(4); - list.code = 4321; - - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - b1.writeTo(list, os); - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - MyList result = b1.parseFrom(b); - - assertEquals(4, result.size()); - assertEquals(result.code, 4321); - assertEquals(result.id, "feedback"); - } - - @Test - @SuppressWarnings({"rawtypes", "unchecked"}) - public void testBuilder_MyMap() throws Exception { - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - Builder b2 = Builder.register(MyMap.class); - MyMap map = new MyMap(); - map.put("name", "qianlei"); - map.put("displayName", "钱磊"); - map.code = 4321; - b2.writeTo(map, os); - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - - map = b2.parseFrom(b); - - assertEquals(map.size(), 2); - assertEquals(map.code, 4321); - assertEquals(map.id, "feedback"); - } - - @Test - @SuppressWarnings("unchecked") - public void testSerializableBean() throws Exception { - System.out.println("testSerializableBean"); - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - - SerializableBean sb = new SerializableBean(); - Builder sbb = Builder.register(SerializableBean.class); - sbb.writeTo(sb, os); - - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - assertEquals(sbb.parseFrom(os.toByteArray()), sb); - } - - @Test - @SuppressWarnings("unchecked") - public void testOthers() throws Exception { - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - - StringBuffer buf = new StringBuffer(); - for (int i = 0; i < 1024 * 32 + 32; i++) - buf.append('A'); - Builder sb = Builder.register(String.class); - sb.writeTo(buf.toString(), os); - assertEquals(sb.parseFrom(os.toByteArray()), buf.toString()); - - os = new UnsafeByteArrayOutputStream(); - Builder builder = Builder.register(HashMap.class); - Map services = new HashMap(); - HashMap map = new HashMap(); - services.put("test.service", "http://127.0.0.1:9010/test.service"); - map.put("name", "qianlei"); - map.put("password", "123455"); - map.put("services", services); - - builder.writeTo(map, os); - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - map = builder.parseFrom(b); - assertTrue(map.size() > 0); - assertEquals("http://127.0.0.1:9010/test.service", ((Map) map.get("services")).get("test.service")); - - services = new ConcurrentHashMap(); - services.put("test.service", "http://127.0.0.1:9010/test.service"); - map.put("services", services); - - os = new UnsafeByteArrayOutputStream(); - builder.writeTo(map, os); - b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - map = builder.parseFrom(b); - assertTrue(map.size() > 0); - assertEquals("http://127.0.0.1:9010/test.service", ((Map) map.get("services")).get("test.service")); - - Node node1 = new Node(); - Node node0 = new Node(); - node0.value = "0"; - node0.next = node1; - node1.value = "1"; - node1.prev = node0; - // write. - Builder nodebuilder = Builder.register(Node.class); - os = new UnsafeByteArrayOutputStream(); - nodebuilder.writeTo(node0, os); - b = os.toByteArray(); - System.out.println("Node:" + b.length + ":" + Bytes.bytes2hex(b)); - // parse - node0 = nodebuilder.parseFrom(b); - assertEquals(node0, node0.prev); - assertEquals(node0, node0.next.prev); - assertEquals(node0.value, "0"); - } - - @Test - public void testWithFC() throws Exception { - Builder builder = Builder.register(SimpleDO.class); - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - - SimpleDO sd = new SimpleDO(); - sd.a = 1; - sd.b = 2; - sd.c = 3; - sd.str1 = "12345"; - sd.str2 = "54321"; - builder.writeTo(sd, os); - byte[] b = os.toByteArray(); - System.out.println(b.length + ":" + Bytes.bytes2hex(b)); - - sd = builder.parseFrom(b); - assertEquals(sd.a, 1); - assertEquals(sd.b, 2); - assertEquals(sd.c, 3); - assertEquals(sd.str1, "124"); - System.out.println(sd.str2); - } - - public enum Type { - Lower, Normal, High; - } - - static interface TestDO { - String getName(); - - void setName(String name); - - Type getType(); - - void setType(Type t); - - int getArg(); - - void setArg(int arg); - } - - static class TestDOImpl implements TestDO, Serializable { - private static final long serialVersionUID = 1L; - - public String getName() { - return "name"; - } - - public void setName(String name) { - } - - public Type getType() { - return Type.High; - } - - public void setType(Type t) { - } - - public int getArg() { - return 28; - } - - public void setArg(int arg) { - } - } - - static class Bean implements Serializable { - private static final long serialVersionUID = 1L; - public int vi = 0; - public long vl = 100l; - - boolean b = true; - boolean[] bs = {false, true}; - - String s1 = "1234567890"; - String s2 = "1234567890一二三四五六七八九零"; - - private int i = 123123, ni = -12344, is[] = {1, 2, 3, 4, -1, -2, -3, -4}; - private short s = 12, ns = -76; - private double d = 12.345, nd = -12.345; - private long l = 1281447759383l, nl = -13445l; - - private Boolean B = Boolean.FALSE; - private Integer I = -1234; - private Double D = new Double(1.23); - private String name = "qianlei"; - private Type type = Type.Lower, type1 = Type.Normal; - private Type[] types = {Type.Lower, Type.Lower}; - - private Time time = null; - private ArrayList list = new ArrayList(); - - { - list.add(1); - list.add(2); - list.add(1308147); - } - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - } - - static class MyList extends ArrayList { - private int code = 12345; - private String id = "feedback"; - } - - static class MyMap extends HashMap { - private int code = 12345; - private String id = "feedback"; - } - - static class Node implements Serializable { - private static final long serialVersionUID = 1L; - Node prev = this; - Node next = this; - String value = "value"; - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - Node other = (Node) obj; - if (value == null) { - if (other.value != null) return false; - } else if (!value.equals(other.value)) return false; - return true; - } - } - - static class SerializableBean implements Serializable { - private static final long serialVersionUID = -8949681707161463700L; - - public int a = 0; - public long b = 100l; - boolean c = true; - String s1 = "1234567890"; - String s2 = "1234567890一二三四五六七八九零"; - - public int hashCode() { - return s1.hashCode() ^ s2.hashCode(); - } - - public boolean equals(Object obj) { - if (obj == null) return false; - if (obj == this) return true; - if (obj instanceof SerializableBean) { - SerializableBean sb = (SerializableBean) obj; - return this.a == sb.a && this.b == sb.b && this.c == sb.c && this.s1.equals(sb.s1) && this.s2.equals(sb.s2); - } - - return false; - } - } -} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/DataInputOutputTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/DataInputOutputTest.java deleted file mode 100644 index 7df481bac21..00000000000 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/DataInputOutputTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.dubbo; - -import com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream; -import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; -import com.alibaba.dubbo.common.serialize.DataInput; -import com.alibaba.dubbo.common.serialize.DataOutput; -import com.alibaba.dubbo.common.serialize.support.dubbo.GenericDataInput; -import com.alibaba.dubbo.common.serialize.support.dubbo.GenericDataOutput; - -import junit.framework.TestCase; - -import java.io.IOException; - -public class DataInputOutputTest extends TestCase { - private static final String SMALL_STRING = DataInputOutputTest.class.getName(), BIG_STREAM = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - - private static final byte[] SMALL_BYTES = SMALL_STRING.getBytes(), BIG_BYTES = BIG_STREAM.getBytes(); - - private static void assertSameArray(byte[] b1, byte[] b2) { - assertEquals(b1.length, b2.length); - for (int i = 0; i < b1.length; i++) - assertEquals(b1[i], b2[i]); - } - - public void testMain() throws Exception { - // write. - UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); - DataOutput cos = new GenericDataOutput(os); - writeTest(cos); - - // read. - byte[] b = os.toByteArray(); - DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b)); - readTest(cis); - } - - private void writeTest(DataOutput out) throws IOException { - out.writeShort((short) 'a'); - out.writeShort((short) -1); - out.writeShort((short) 1234); - out.writeInt(0x22); - out.writeInt(-0x22); - out.writeInt(0x2222); - out.writeInt(-0x2222); - out.writeInt(0x222222); - out.writeInt(-0x222222); - out.writeInt(0x22222222); - out.writeInt(-0x22222222); - out.writeLong(0x22); - out.writeLong(-0x22); - out.writeLong(0x2222); - out.writeLong(-0x2222); - out.writeLong(0x222222); - out.writeLong(-0x222222); - out.writeLong(0x22222222); - out.writeLong(-0x22222222); - out.writeLong(0x2222222222l); - out.writeLong(-0x2222222222l); - out.writeLong(0x222222222222l); - out.writeLong(-0x222222222222l); - out.writeLong(0x22222222222222l); - out.writeLong(-0x22222222222222l); - out.writeLong(0x2222222222222222l); - out.writeLong(-0x2222222222222222l); - out.writeDouble(1212.454); - out.writeBytes(SMALL_BYTES); - out.writeUTF(SMALL_STRING); - out.writeBytes(BIG_BYTES); - out.flushBuffer(); - } - - private void readTest(DataInput in) throws IOException { - assertEquals(in.readShort(), 'a'); - assertEquals(in.readShort(), -1); - assertEquals(in.readShort(), 1234); - assertEquals(in.readInt(), 0x22); - assertEquals(in.readInt(), -0x22); - assertEquals(in.readInt(), 0x2222); - assertEquals(in.readInt(), -0x2222); - assertEquals(in.readInt(), 0x222222); - assertEquals(in.readInt(), -0x222222); - assertEquals(in.readInt(), 0x22222222); - assertEquals(in.readInt(), -0x22222222); - assertEquals(in.readLong(), 0x22); - assertEquals(in.readLong(), -0x22); - assertEquals(in.readLong(), 0x2222); - assertEquals(in.readLong(), -0x2222); - assertEquals(in.readLong(), 0x222222); - assertEquals(in.readLong(), -0x222222); - assertEquals(in.readLong(), 0x22222222); - assertEquals(in.readLong(), -0x22222222); - assertEquals(in.readLong(), 0x2222222222l); - assertEquals(in.readLong(), -0x2222222222l); - assertEquals(in.readLong(), 0x222222222222l); - assertEquals(in.readLong(), -0x222222222222l); - assertEquals(in.readLong(), 0x22222222222222l); - assertEquals(in.readLong(), -0x22222222222222l); - assertEquals(in.readLong(), 0x2222222222222222l); - assertEquals(in.readLong(), -0x2222222222222222l); - assertEquals(in.readDouble(), 1212.454); - assertSameArray(in.readBytes(), SMALL_BYTES); - assertEquals(in.readUTF(), SMALL_STRING); - assertSameArray(in.readBytes(), BIG_BYTES); - } -} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/DubboSerializationTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/DubboSerializationTest.java deleted file mode 100644 index 06415a7f985..00000000000 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/DubboSerializationTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.serialization; - -import com.alibaba.dubbo.common.serialize.support.dubbo.DubboSerialization; - -import org.junit.Ignore; -import org.junit.Test; - -public class DubboSerializationTest extends AbstractSerializationPersionFailTest { - { - serialization = new DubboSerialization(); - } - - /** - * @desc:DubboSerialization error: java.lang.IllegalAccessError: tried to access class java.util.Arrays$ArrayList from class com.alibaba.dubbo.common.serialize.support.dubbo.Builder$bc6 - * @reason:in writeObject method, the first line is :java.util.Arrays$ArrayList v = (java.util.Arrays$ArrayList)$1; java.util.Arrays$ArrayList is a inter static class ,can not access. - * @tradeoff: how to resolve:we need change writeObject method, replace the first line with java.util.ArrayList v = new java.util.ArrayList((List)$1) , and in the same time, modify the defaultArg method ,return special construct args for ArrayList ... too ugly to support. - */ - @Ignore - @Test - public void test_StringList_asListReturn() throws Exception { - super.test_StringList_asListReturn(); - } - - // FIXME - @Ignore("StackOverflowError") - @Test(timeout = 3000) - public void test_LoopReference() throws Exception { - } - - @Ignore("Expecting to find object/array on stack") - @Test - public void test_BigInteger() throws Exception { - } - - @Ignore("Expecting to find object/array on stack") - @Test - public void test_BigInteger_withType() throws Exception { - } - -} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JsonSerializationTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JsonSerializationTest.java deleted file mode 100644 index 944dd9ac65e..00000000000 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JsonSerializationTest.java +++ /dev/null @@ -1,270 +0,0 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.serialization; - -import com.alibaba.dubbo.common.serialize.support.json.JsonSerialization; - -import org.junit.Ignore; -import org.junit.Test; - -/** - * FIXME Temporarily ignore Json Serialization failure. - */ -public class JsonSerializationTest extends AbstractSerializationPersionOkTest { - { - serialization = new JsonSerialization(); - } - - // FIXME - @Ignore - @Test - public void test_BytesRange() throws Exception { - } - - @Ignore("bool[] type missing to List") - @Test - public void test_boolArray() throws Exception { - } - - @Ignore("char[] type missing to List") - @Test - public void test_charArray() throws Exception { - } - - @Ignore("short[] type missing to List") - @Test - public void test_shortArray() throws Exception { - } - - @Ignore("int[] type missing to List") - @Test - public void test_intArray() throws Exception { - } - - @Ignore("long[] type missing to List") - @Test - public void test_longArray() throws Exception { - } - - @Ignore - @Test - public void test_floatArray() throws Exception { - } - - @Ignore - @Test - public void test_doubleArray() throws Exception { - } - - @Ignore("String[] type missing to List") - @Test - public void test_StringArray() throws Exception { - } - - @Ignore("Integer[] type missing to List") - @Test - public void test_IntegerArray() throws Exception { - } - - @Ignore - @Test - public void test_EnumArray() throws Exception { - } - - @Ignore - @Test - public void test_Date() throws Exception { - } - - @Ignore("lost millisecond precision") - @Test - public void test_Date_withType() throws Exception { - } - - @Ignore - @Test - public void test_Time() throws Exception { - } - - @Ignore - @Test - public void test_Time_withType() throws Exception { - } - - @Ignore("Byte type missing to Long") - @Test - public void test_ByteWrap() throws Exception { - } - - @Ignore("BigInteger type missing to Long") - @Test - public void test_BigInteger() throws Exception { - } - - @Ignore - @Test - public void test_BigDecimal() throws Exception { - } - - @Ignore - @Test - public void test_BigDecimal_withType() throws Exception { - } - - @Ignore("BizException type missing") - @Test - public void test_BizException() throws Exception { - } - - @Ignore("BizExceptionNoDefaultConstructor type missing") - @Test - public void test_BizExceptionNoDefaultConstructor() throws Exception { - } - - @Ignore("NoDefaultConstructor") - @Test - public void test_BizExceptionNoDefaultConstructor_WithType() throws Exception { - } - - @Ignore("Enum type missing") - @Test - public void test_enum() throws Exception { - } - - @Ignore("Set type missing to Map") - @Test - public void test_StringSet() throws Exception { - } - - @Ignore("LinkedHashMap type missing to Map") - @Test - public void test_LinkedHashMap() throws Exception { - } - - @Ignore("SPerson type missing") - @Test - public void test_SPerson() throws Exception { - } - - @Ignore("SPerson type missing") - @Test - public void test_SPersonList() throws Exception { - } - - @Ignore("SPerson type missing") - @Test - public void test_SPersonSet() throws Exception { - } - - @Ignore("SPerson type missing") - @Test - public void test_IntSPersonMap() throws Exception { - } - - @Ignore("SPerson type missing") - @Test - public void test_StringSPersonMap() throws Exception { - } - - @Ignore("SPerson type missing") - @Test - public void test_StringSPersonListMap() throws Exception { - } - - @Ignore("SPerson type missing") - @Test - public void test_SPersonListList() throws Exception { - } - - @Ignore("type missing") - @Test - public void test_BigPerson() throws Exception { - } - - @Ignore("type missing") - @Test - public void test_BigPerson_WithType() throws Exception { - } - - @Ignore("type missing") - @Test - public void test_MediaContent() throws Exception { - } - - @Ignore("type missing") - @Test - public void test_MediaContent_WithType() throws Exception { - } - - @Ignore("type missing") - @Test - public void test_MultiObject() throws Exception { - } - - @Ignore("type missing") - @Test - public void test_MultiObject_WithType() throws Exception { - } - - @Ignore - @Test - public void test_LoopReference() throws Exception { - } - - // FIXME DUBBO-63 - @Ignore - @Test - public void test_URL_mutable_withType() throws Exception { - } - - // Person - - @Ignore("person type missing") - @Test - public void test_Person() throws Exception { - } - - @Ignore("person type missing") - @Test - public void test_PersonList() throws Exception { - } - - @Ignore("person type missing") - @Test - public void test_PersonSet() throws Exception { - } - - @Ignore("person type missing") - @Test - public void test_IntPersonMap() throws Exception { - } - - @Ignore("person type missing") - @Test - public void test_StringPersonMap() throws Exception { - } - - @Ignore("person type missing") - @Test - public void test_StringPersonListMap() throws Exception { - } - - @Ignore("person type missing") - @Test - public void test_PersonListList() throws Exception { - } -} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java index 3969b011689..8d32e7ed675 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ConfigUtilsTest.java @@ -16,7 +16,7 @@ */ package com.alibaba.dubbo.common.utils; -import com.alibaba.dubbo.common.serialize.Serialization; +import com.alibaba.dubbo.common.threadpool.ThreadPool; import org.junit.Assert; import org.junit.Ignore; @@ -41,27 +41,27 @@ public static List toArray(T... args) { @Test public void testMergeValues() { - List merged = ConfigUtils.mergeValues(Serialization.class, "aaa,bbb,default.cunstom", - toArray("dubbo", "default.hessian2", "fastjson")); - Assert.assertEquals(toArray("dubbo", "fastjson", "aaa", "bbb", "default.cunstom"), merged); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "aaa,bbb,default.cunstom", + toArray("fixed", "default.limited", "cached")); + Assert.assertEquals(toArray("fixed", "cached", "aaa", "bbb", "default.cunstom"), merged); } @Test public void testMergeValues_addDefault() { - List merged = ConfigUtils.mergeValues(Serialization.class, "aaa,bbb,default,zzz", - toArray("dubbo", "default.hessian2", "fastjson")); - Assert.assertEquals(toArray("aaa", "bbb", "dubbo", "fastjson", "zzz"), merged); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "aaa,bbb,default,zzz", + toArray("fixed", "default.limited", "cached")); + Assert.assertEquals(toArray("aaa", "bbb", "fixed", "cached", "zzz"), merged); } @Test public void testMergeValuesDeleteDefault() { - List merged = ConfigUtils.mergeValues(Serialization.class, "-default", toArray("dubbo", "default.hessian2", "json")); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "-default", toArray("fixed", "default.limited", "cached")); Assert.assertEquals(toArray(), merged); } @Test public void testMergeValuesDeleteDefault_2() { - List merged = ConfigUtils.mergeValues(Serialization.class, "-default,aaa", toArray("dubbo", "default.hessian2", "json")); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "-default,aaa", toArray("fixed", "default.limited", "cached")); Assert.assertEquals(toArray("aaa"), merged); } @@ -70,8 +70,8 @@ public void testMergeValuesDeleteDefault_2() { */ @Test public void testMergeValuesDelete() { - List merged = ConfigUtils.mergeValues(Serialization.class, "-dubbo,aaa", toArray("dubbo", "default.hessian2", "fastjson")); - Assert.assertEquals(toArray("fastjson", "aaa"), merged); + List merged = ConfigUtils.mergeValues(ThreadPool.class, "-fixed,aaa", toArray("fixed", "default.limited", "cached")); + Assert.assertEquals(toArray("cached", "aaa"), merged); } @Test diff --git a/dubbo-config/dubbo-config-api/pom.xml b/dubbo-config/dubbo-config-api/pom.xml index d4688e754d4..4cc70132536 100644 --- a/dubbo-config/dubbo-config-api/pom.xml +++ b/dubbo-config/dubbo-config-api/pom.xml @@ -45,6 +45,11 @@ limitations under the License. dubbo-remoting-api ${project.parent.version} + + com.alibaba + dubbo-rpc-injvm + ${project.parent.version} + com.alibaba dubbo-filter-validation @@ -79,11 +84,6 @@ limitations under the License. ${project.parent.version} test - - com.alibaba - dubbo-rpc-injvm - ${project.parent.version} - com.alibaba dubbo-remoting-netty diff --git a/dubbo-demo/dubbo-demo-consumer/pom.xml b/dubbo-demo/dubbo-demo-consumer/pom.xml index 832f7f372d6..bc0a77ade9a 100644 --- a/dubbo-demo/dubbo-demo-consumer/pom.xml +++ b/dubbo-demo/dubbo-demo-consumer/pom.xml @@ -49,15 +49,15 @@ limitations under the License. com.alibaba - dubbo-cluster + dubbo-rpc-default com.alibaba - dubbo-rpc-default + dubbo-remoting-netty com.alibaba - dubbo-remoting-netty + dubbo-serialization-hessian2 \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-provider/pom.xml b/dubbo-demo/dubbo-demo-provider/pom.xml index 05fc311f4dd..6072d7e9c10 100644 --- a/dubbo-demo/dubbo-demo-provider/pom.xml +++ b/dubbo-demo/dubbo-demo-provider/pom.xml @@ -50,15 +50,15 @@ limitations under the License. com.alibaba - dubbo-cluster + dubbo-rpc-default com.alibaba - dubbo-rpc-default + dubbo-remoting-netty com.alibaba - dubbo-remoting-netty + dubbo-serialization-hessian2 \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-api/pom.xml b/dubbo-remoting/dubbo-remoting-api/pom.xml index bd8ff80f257..01b18f32782 100644 --- a/dubbo-remoting/dubbo-remoting-api/pom.xml +++ b/dubbo-remoting/dubbo-remoting-api/pom.xml @@ -35,5 +35,16 @@ dubbo-common ${project.parent.version} + + com.alibaba + dubbo-serialization-api + ${project.parent.version} + + + com.alibaba + dubbo-serialization-hessian2 + ${project.parent.version} + test + \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-netty/pom.xml b/dubbo-remoting/dubbo-remoting-netty/pom.xml index dcb6ca51264..221ad36cd6d 100644 --- a/dubbo-remoting/dubbo-remoting-netty/pom.xml +++ b/dubbo-remoting/dubbo-remoting-netty/pom.xml @@ -38,5 +38,11 @@ org.jboss.netty netty + + com.alibaba + dubbo-serialization-hessian2 + ${project.parent.version} + test + \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-api/pom.xml b/dubbo-rpc/dubbo-rpc-api/pom.xml index 415a0099532..d9b06dda914 100644 --- a/dubbo-rpc/dubbo-rpc-api/pom.xml +++ b/dubbo-rpc/dubbo-rpc-api/pom.xml @@ -35,5 +35,10 @@ limitations under the License. dubbo-common ${project.parent.version} + + com.alibaba + dubbo-serialization-api + ${project.parent.version} + \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-api/pom.xml b/dubbo-serialization/dubbo-serialization-api/pom.xml new file mode 100644 index 00000000000..53c5bdadc20 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-api/pom.xml @@ -0,0 +1,61 @@ + + + + 4.0.0 + + com.alibaba + dubbo-serialization + 2.6.1 + + dubbo-serialization-api + jar + ${project.artifactId} + The common module of dubbo project + + true + + + + com.alibaba + dubbo-common + ${project.parent.version} + + + com.alibaba + hessian-lite + 3.2.1-fixed-2 + + + com.alibaba + fastjson + + + com.esotericsoftware + kryo + + + de.javakaffee + kryo-serializers + + + de.ruedigermoeller + fst + + + \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/Cleanable.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/Cleanable.java similarity index 100% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/Cleanable.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/Cleanable.java diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/DataInput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/DataInput.java similarity index 100% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/DataInput.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/DataInput.java diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/DataOutput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/DataOutput.java similarity index 100% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/DataOutput.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/DataOutput.java diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/ObjectInput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/ObjectInput.java similarity index 100% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/ObjectInput.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/ObjectInput.java diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/ObjectOutput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/ObjectOutput.java similarity index 100% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/ObjectOutput.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/ObjectOutput.java diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/Serialization.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/Serialization.java similarity index 96% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/Serialization.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/Serialization.java index 43da3d6a6f7..ad0da60ddfe 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/Serialization.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/Serialization.java @@ -1,69 +1,69 @@ -/* - * 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 com.alibaba.dubbo.common.serialize; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.extension.Adaptive; -import com.alibaba.dubbo.common.extension.SPI; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * Serialization. (SPI, Singleton, ThreadSafe) - */ -@SPI("hessian2") -public interface Serialization { - - /** - * get content type id - * - * @return content type id - */ - byte getContentTypeId(); - - /** - * get content type - * - * @return content type - */ - String getContentType(); - - /** - * create serializer - * - * @param url - * @param output - * @return serializer - * @throws IOException - */ - @Adaptive - ObjectOutput serialize(URL url, OutputStream output) throws IOException; - - /** - * create deserializer - * - * @param url - * @param input - * @return deserializer - * @throws IOException - */ - @Adaptive - ObjectInput deserialize(URL url, InputStream input) throws IOException; - +/* + * 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 com.alibaba.dubbo.common.serialize; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.extension.Adaptive; +import com.alibaba.dubbo.common.extension.SPI; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Serialization. (SPI, Singleton, ThreadSafe) + */ +@SPI("hessian2") +public interface Serialization { + + /** + * get content type id + * + * @return content type id + */ + byte getContentTypeId(); + + /** + * get content type + * + * @return content type + */ + String getContentType(); + + /** + * create serializer + * + * @param url + * @param output + * @return serializer + * @throws IOException + */ + @Adaptive + ObjectOutput serialize(URL url, OutputStream output) throws IOException; + + /** + * create deserializer + * + * @param url + * @param input + * @return deserializer + * @throws IOException + */ + @Adaptive + ObjectInput deserialize(URL url, InputStream input) throws IOException; + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializableClassRegistry.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializableClassRegistry.java similarity index 100% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializableClassRegistry.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializableClassRegistry.java diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializationOptimizer.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializationOptimizer.java similarity index 100% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializationOptimizer.java rename to dubbo-serialization/dubbo-serialization-api/src/main/java/com/alibaba/dubbo/common/serialize/support/SerializationOptimizer.java diff --git a/dubbo-serialization/dubbo-serialization-fastjson/pom.xml b/dubbo-serialization/dubbo-serialization-fastjson/pom.xml new file mode 100644 index 00000000000..bbe776b1f62 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/pom.xml @@ -0,0 +1,44 @@ + + + + 4.0.0 + + com.alibaba + dubbo-serialization + 2.6.1 + + dubbo-serialization-fastjson + jar + ${project.artifactId} + The common module of dubbo project + + true + + + + com.alibaba + dubbo-serialization-api + ${project.parent.version} + + + com.alibaba + fastjson + + + \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonObjectInput.java similarity index 95% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonObjectInput.java index 9861698c823..73c19af2b47 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonObjectInput.java @@ -1,136 +1,136 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.json; - -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.utils.PojoUtils; -import com.alibaba.fastjson.JSON; - -import java.io.BufferedReader; -import java.io.EOFException; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.lang.reflect.Type; - -/** - * JsonObjectInput - */ -public class FastJsonObjectInput implements ObjectInput { - - private final BufferedReader reader; - - public FastJsonObjectInput(InputStream in) { - this(new InputStreamReader(in)); - } - - public FastJsonObjectInput(Reader reader) { - this.reader = new BufferedReader(reader); - } - - public boolean readBool() throws IOException { - try { - return readObject(boolean.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public byte readByte() throws IOException { - try { - return readObject(byte.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public short readShort() throws IOException { - try { - return readObject(short.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public int readInt() throws IOException { - try { - return readObject(int.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public long readLong() throws IOException { - try { - return readObject(long.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public float readFloat() throws IOException { - try { - return readObject(float.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public double readDouble() throws IOException { - try { - return readObject(double.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public String readUTF() throws IOException { - try { - return readObject(String.class); - } catch (ClassNotFoundException e) { - throw new IOException(e.getMessage()); - } - } - - public byte[] readBytes() throws IOException { - return readLine().getBytes(); - } - - public Object readObject() throws IOException, ClassNotFoundException { - String json = readLine(); - return JSON.parse(json); - } - - public T readObject(Class cls) throws IOException, ClassNotFoundException { - String json = readLine(); - return JSON.parseObject(json, cls); - } - - @SuppressWarnings("unchecked") - public T readObject(Class cls, Type type) throws IOException, ClassNotFoundException { - Object value = readObject(cls); - return (T) PojoUtils.realize(value, cls, type); - } - - private String readLine() throws IOException, EOFException { - String line = reader.readLine(); - if (line == null || line.trim().length() == 0) throw new EOFException(); - return line; - } - +/* + * 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 com.alibaba.dubbo.serialize.fastjson; + +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.utils.PojoUtils; +import com.alibaba.fastjson.JSON; + +import java.io.BufferedReader; +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.lang.reflect.Type; + +/** + * JsonObjectInput + */ +public class FastJsonObjectInput implements ObjectInput { + + private final BufferedReader reader; + + public FastJsonObjectInput(InputStream in) { + this(new InputStreamReader(in)); + } + + public FastJsonObjectInput(Reader reader) { + this.reader = new BufferedReader(reader); + } + + public boolean readBool() throws IOException { + try { + return readObject(boolean.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public byte readByte() throws IOException { + try { + return readObject(byte.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public short readShort() throws IOException { + try { + return readObject(short.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public int readInt() throws IOException { + try { + return readObject(int.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public long readLong() throws IOException { + try { + return readObject(long.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public float readFloat() throws IOException { + try { + return readObject(float.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public double readDouble() throws IOException { + try { + return readObject(double.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public String readUTF() throws IOException { + try { + return readObject(String.class); + } catch (ClassNotFoundException e) { + throw new IOException(e.getMessage()); + } + } + + public byte[] readBytes() throws IOException { + return readLine().getBytes(); + } + + public Object readObject() throws IOException, ClassNotFoundException { + String json = readLine(); + return JSON.parse(json); + } + + public T readObject(Class cls) throws IOException, ClassNotFoundException { + String json = readLine(); + return JSON.parseObject(json, cls); + } + + @SuppressWarnings("unchecked") + public T readObject(Class cls, Type type) throws IOException, ClassNotFoundException { + Object value = readObject(cls); + return (T) PojoUtils.realize(value, cls, type); + } + + private String readLine() throws IOException, EOFException { + String line = reader.readLine(); + if (line == null || line.trim().length() == 0) throw new EOFException(); + return line; + } + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectOutput.java b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonObjectOutput.java similarity index 94% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectOutput.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonObjectOutput.java index 4048e8a10a8..196122df686 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonObjectOutput.java @@ -1,100 +1,100 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.json; - -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.fastjson.serializer.JSONSerializer; -import com.alibaba.fastjson.serializer.SerializeWriter; -import com.alibaba.fastjson.serializer.SerializerFeature; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.Writer; - -/** - * JsonObjectOutput - */ -public class FastJsonObjectOutput implements ObjectOutput { - - private final PrintWriter writer; - - public FastJsonObjectOutput(OutputStream out) { - this(new OutputStreamWriter(out)); - } - - public FastJsonObjectOutput(Writer writer) { - this.writer = new PrintWriter(writer); - } - - public void writeBool(boolean v) throws IOException { - writeObject(v); - } - - public void writeByte(byte v) throws IOException { - writeObject(v); - } - - public void writeShort(short v) throws IOException { - writeObject(v); - } - - public void writeInt(int v) throws IOException { - writeObject(v); - } - - public void writeLong(long v) throws IOException { - writeObject(v); - } - - public void writeFloat(float v) throws IOException { - writeObject(v); - } - - public void writeDouble(double v) throws IOException { - writeObject(v); - } - - public void writeUTF(String v) throws IOException { - writeObject(v); - } - - public void writeBytes(byte[] b) throws IOException { - writer.println(new String(b)); - } - - public void writeBytes(byte[] b, int off, int len) throws IOException { - writer.println(new String(b, off, len)); - } - - public void writeObject(Object obj) throws IOException { - SerializeWriter out = new SerializeWriter(); - JSONSerializer serializer = new JSONSerializer(out); - serializer.config(SerializerFeature.WriteEnumUsingToString, true); - serializer.write(obj); - out.writeTo(writer); - out.close(); // for reuse SerializeWriter buf - writer.println(); - writer.flush(); - } - - public void flushBuffer() throws IOException { - writer.flush(); - } - +/* + * 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 com.alibaba.dubbo.serialize.fastjson; + +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.fastjson.serializer.JSONSerializer; +import com.alibaba.fastjson.serializer.SerializeWriter; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.Writer; + +/** + * JsonObjectOutput + */ +public class FastJsonObjectOutput implements ObjectOutput { + + private final PrintWriter writer; + + public FastJsonObjectOutput(OutputStream out) { + this(new OutputStreamWriter(out)); + } + + public FastJsonObjectOutput(Writer writer) { + this.writer = new PrintWriter(writer); + } + + public void writeBool(boolean v) throws IOException { + writeObject(v); + } + + public void writeByte(byte v) throws IOException { + writeObject(v); + } + + public void writeShort(short v) throws IOException { + writeObject(v); + } + + public void writeInt(int v) throws IOException { + writeObject(v); + } + + public void writeLong(long v) throws IOException { + writeObject(v); + } + + public void writeFloat(float v) throws IOException { + writeObject(v); + } + + public void writeDouble(double v) throws IOException { + writeObject(v); + } + + public void writeUTF(String v) throws IOException { + writeObject(v); + } + + public void writeBytes(byte[] b) throws IOException { + writer.println(new String(b)); + } + + public void writeBytes(byte[] b, int off, int len) throws IOException { + writer.println(new String(b, off, len)); + } + + public void writeObject(Object obj) throws IOException { + SerializeWriter out = new SerializeWriter(); + JSONSerializer serializer = new JSONSerializer(out); + serializer.config(SerializerFeature.WriteEnumUsingToString, true); + serializer.write(obj); + out.writeTo(writer); + out.close(); // for reuse SerializeWriter buf + writer.println(); + writer.flush(); + } + + public void flushBuffer() throws IOException { + writer.flush(); + } + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonSerialization.java b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonSerialization.java similarity index 93% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonSerialization.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonSerialization.java index 7100ccf0946..f3c466523a5 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonSerialization.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/dubbo/serialize/fastjson/FastJsonSerialization.java @@ -1,49 +1,49 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.json; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.Serialization; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * FastJsonSerialization - */ -public class FastJsonSerialization implements Serialization { - - public byte getContentTypeId() { - return 6; - } - - public String getContentType() { - return "text/json"; - } - - public ObjectOutput serialize(URL url, OutputStream output) throws IOException { - return new FastJsonObjectOutput(output); - } - - public ObjectInput deserialize(URL url, InputStream input) throws IOException { - return new FastJsonObjectInput(input); - } - +/* + * 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 com.alibaba.dubbo.serialize.fastjson; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * FastJsonSerialization + */ +public class FastJsonSerialization implements Serialization { + + public byte getContentTypeId() { + return 6; + } + + public String getContentType() { + return "text/json"; + } + + public ObjectOutput serialize(URL url, OutputStream output) throws IOException { + return new FastJsonObjectOutput(output); + } + + public ObjectInput deserialize(URL url, InputStream input) throws IOException { + return new FastJsonObjectInput(input); + } + } \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization b/dubbo-serialization/dubbo-serialization-fastjson/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization new file mode 100644 index 00000000000..cc7bde2a6c2 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization @@ -0,0 +1 @@ +fastjson=com.alibaba.dubbo.serialize.fastjson.FastJsonSerialization \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java new file mode 100644 index 00000000000..f920a0c6575 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java @@ -0,0 +1,21 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public enum AnimalEnum { + dog, cat, rat, cow, bull, horse; +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizException.java similarity index 78% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizException.java index 09a522ab048..b6eb1f4bbce 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizException.java @@ -14,16 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.dubbo; +package com.alibaba.dubbo.common.model; -import java.io.Serializable; - -public class SimpleDO implements Serializable { +public class BizException extends RuntimeException { private static final long serialVersionUID = 1L; - public int a, b, c; - public int str3; - float d = 1.2f, e = 12.56f; - String str1 = "124", str2; + public BizException(String message) { + super(message); + } + + public BizException() { + } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java similarity index 75% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java index fb87e9cf57c..29db6e00fc9 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java @@ -14,13 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.serialization; +package com.alibaba.dubbo.common.model; -import com.alibaba.dubbo.common.serialize.support.kryo.KryoSerialization; +public class BizExceptionNoDefaultConstructor extends RuntimeException { -public class FstSerializationTest extends AbstractSerializationTest { + private static final long serialVersionUID = 1L; - { - serialization = new KryoSerialization(); + public BizExceptionNoDefaultConstructor(String message) { + super(message); } -} +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/Person.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/Person.java new file mode 100644 index 00000000000..6383706cf79 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/Person.java @@ -0,0 +1,95 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.util.Arrays; + +public class Person { + 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java new file mode 100644 index 00000000000..e7dd2486786 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java @@ -0,0 +1,97 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.io.Serializable; +import java.util.Arrays; + +public class SerializablePerson implements Serializable { + private static final long serialVersionUID = 1L; + 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; + SerializablePerson other = (SerializablePerson) 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Image.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Image.java new file mode 100644 index 00000000000..0d6a74a3d4a --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Image.java @@ -0,0 +1,120 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + + +public class Image implements java.io.Serializable { + private static final long serialVersionUID = 1L; + public String uri; + public String title; // Can be null + public int width; + public int height; + public Size size; + + public Image() { + } + + public Image(String uri, String title, int width, int height, Size size) { + this.height = height; + this.title = title; + this.uri = uri; + this.width = width; + this.size = size; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Image image = (Image) o; + + if (height != image.height) return false; + if (width != image.width) return false; + if (size != image.size) return false; + if (title != null ? !title.equals(image.title) : image.title != null) return false; + if (uri != null ? !uri.equals(image.uri) : image.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (size != null ? size.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Image "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", size=").append(size); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + public enum Size { + SMALL, LARGE + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Media.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Media.java new file mode 100644 index 00000000000..8a78f6d313b --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/Media.java @@ -0,0 +1,205 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class Media implements java.io.Serializable { + public String uri; + public String title; // Can be unset. + public int width; + public int height; + public String format; + public long duration; + public long size; + public int bitrate; // Can be unset. + public boolean hasBitrate; + public List persons; + public Player player; + public String copyright; // Can be unset. + + public Media() { + } + + public Media(String uri, String title, int width, int height, String format, long duration, long size, int bitrate, boolean hasBitrate, List persons, Player player, String copyright) { + this.uri = uri; + this.title = title; + this.width = width; + this.height = height; + this.format = format; + this.duration = duration; + this.size = size; + this.bitrate = bitrate; + this.hasBitrate = hasBitrate; + this.persons = persons; + this.player = player; + this.copyright = copyright; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Media media = (Media) o; + + if (bitrate != media.bitrate) return false; + if (duration != media.duration) return false; + if (hasBitrate != media.hasBitrate) return false; + if (height != media.height) return false; + if (size != media.size) return false; + if (width != media.width) return false; + if (copyright != null ? !copyright.equals(media.copyright) : media.copyright != null) return false; + if (format != null ? !format.equals(media.format) : media.format != null) return false; + if (persons != null ? !persons.equals(media.persons) : media.persons != null) return false; + if (player != media.player) return false; + if (title != null ? !title.equals(media.title) : media.title != null) return false; + if (uri != null ? !uri.equals(media.uri) : media.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (format != null ? format.hashCode() : 0); + result = 31 * result + (int) (duration ^ (duration >>> 32)); + result = 31 * result + (int) (size ^ (size >>> 32)); + result = 31 * result + bitrate; + result = 31 * result + (hasBitrate ? 1 : 0); + result = 31 * result + (persons != null ? persons.hashCode() : 0); + result = 31 * result + (player != null ? player.hashCode() : 0); + result = 31 * result + (copyright != null ? copyright.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Media "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", format=").append(format); + sb.append(", duration=").append(duration); + sb.append(", size=").append(size); + sb.append(", hasBitrate=").append(hasBitrate); + sb.append(", bitrate=").append(String.valueOf(bitrate)); + sb.append(", persons=").append(persons); + sb.append(", player=").append(player); + sb.append(", copyright=").append(copyright); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public int getBitrate() { + return bitrate; + } + + public void setBitrate(int bitrate) { + this.bitrate = bitrate; + this.hasBitrate = true; + } + + public List getPersons() { + return persons; + } + + public void setPersons(List persons) { + this.persons = persons; + } + + public Player getPlayer() { + return player; + } + + public void setPlayer(Player player) { + this.player = player; + } + + public String getCopyright() { + return copyright; + } + + public void setCopyright(String copyright) { + this.copyright = copyright; + } + + public enum Player { + JAVA, FLASH + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java new file mode 100644 index 00000000000..9664116a635 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java @@ -0,0 +1,78 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class MediaContent implements java.io.Serializable { + public Media media; + public List images; + + public MediaContent() { + } + + public MediaContent(Media media, List images) { + this.media = media; + this.images = images; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MediaContent that = (MediaContent) o; + + if (images != null ? !images.equals(that.images) : that.images != null) return false; + if (media != null ? !media.equals(that.media) : that.media != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = media != null ? media.hashCode() : 0; + result = 31 * result + (images != null ? images.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[MediaContent: "); + sb.append("media=").append(media); + sb.append(", images=").append(images); + sb.append("]"); + return sb.toString(); + } + + public Media getMedia() { + return media; + } + + public void setMedia(Media media) { + this.media = media; + } + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java new file mode 100644 index 00000000000..70bc793979e --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java @@ -0,0 +1,151 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class BigPerson implements Serializable { + private static final long serialVersionUID = 1L; + + String personId; + + String loginName; + + PersonStatus status; + + String email; + + String penName; + + PersonInfo infoProfile; + + public BigPerson() { + + } + + public BigPerson(String id) { + this.personId = id; + } + + public String getPersonId() { + return personId; + } + + public void setPersonId(String personId) { + this.personId = personId; + } + + public PersonInfo getInfoProfile() { + return infoProfile; + } + + public void setInfoProfile(PersonInfo infoProfile) { + this.infoProfile = infoProfile; + } + + public String getEmail() { + return this.email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getLoginName() { + return this.loginName; + } + + public void setLoginName(String loginName) { + this.loginName = loginName; + } + + public PersonStatus getStatus() { + return this.status; + } + + public void setStatus(PersonStatus status) { + this.status = status; + } + + public String getPenName() { + return penName; + } + + public void setPenName(String penName) { + this.penName = penName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + ((infoProfile == null) ? 0 : infoProfile.hashCode()); + result = prime * result + ((loginName == null) ? 0 : loginName.hashCode()); + result = prime * result + ((penName == null) ? 0 : penName.hashCode()); + result = prime * result + ((personId == null) ? 0 : personId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BigPerson other = (BigPerson) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + if (infoProfile == null) { + if (other.infoProfile != null) + return false; + } else if (!infoProfile.equals(other.infoProfile)) + return false; + if (loginName == null) { + if (other.loginName != null) + return false; + } else if (!loginName.equals(other.loginName)) + return false; + if (penName == null) { + if (other.penName != null) + return false; + } else if (!penName.equals(other.penName)) + return false; + if (personId == null) { + if (other.personId != null) + return false; + } else if (!personId.equals(other.personId)) + return false; + if (status != other.status) + return false; + return true; + } + + @Override + public String toString() { + return "BigPerson [personId=" + personId + ", loginName=" + loginName + ", status=" + + status + ", email=" + email + ", penName=" + penName + ", infoProfile=" + + infoProfile + "]"; + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java new file mode 100644 index 00000000000..dcc93cf8bd3 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java @@ -0,0 +1,202 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class FullAddress implements Serializable { + + private static final long serialVersionUID = 5163979984269419831L; + + private String countryId; + + private String countryName; + + private String provinceName; + + private String cityId; + + private String cityName; + + private String streetAddress; + + private String zipCode; + + public FullAddress() { + } + + public FullAddress(String countryId, String provinceName, String cityId, String streetAddress, + String zipCode) { + this.countryId = countryId; + this.countryName = countryId; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityId; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public FullAddress(String countryId, String countryName, String provinceName, String cityId, + String cityName, String streetAddress, String zipCode) { + this.countryId = countryId; + this.countryName = countryName; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityName; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public String getCountryId() { + return countryId; + } + + public void setCountryId(String countryId) { + this.countryId = countryId; + } + + public String getCountryName() { + return countryName; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + + public String getProvinceName() { + return provinceName; + } + + public void setProvinceName(String provinceName) { + this.provinceName = provinceName; + } + + public String getCityId() { + return cityId; + } + + public void setCityId(String cityId) { + this.cityId = cityId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getStreetAddress() { + return streetAddress; + } + + public void setStreetAddress(String streetAddress) { + this.streetAddress = streetAddress; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((cityId == null) ? 0 : cityId.hashCode()); + result = prime * result + ((cityName == null) ? 0 : cityName.hashCode()); + result = prime * result + ((countryId == null) ? 0 : countryId.hashCode()); + result = prime * result + ((countryName == null) ? 0 : countryName.hashCode()); + result = prime * result + ((provinceName == null) ? 0 : provinceName.hashCode()); + result = prime * result + ((streetAddress == null) ? 0 : streetAddress.hashCode()); + result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FullAddress other = (FullAddress) obj; + if (cityId == null) { + if (other.cityId != null) + return false; + } else if (!cityId.equals(other.cityId)) + return false; + if (cityName == null) { + if (other.cityName != null) + return false; + } else if (!cityName.equals(other.cityName)) + return false; + if (countryId == null) { + if (other.countryId != null) + return false; + } else if (!countryId.equals(other.countryId)) + return false; + if (countryName == null) { + if (other.countryName != null) + return false; + } else if (!countryName.equals(other.countryName)) + return false; + if (provinceName == null) { + if (other.provinceName != null) + return false; + } else if (!provinceName.equals(other.provinceName)) + return false; + if (streetAddress == null) { + if (other.streetAddress != null) + return false; + } else if (!streetAddress.equals(other.streetAddress)) + return false; + if (zipCode == null) { + if (other.zipCode != null) + return false; + } else if (!zipCode.equals(other.zipCode)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (countryName != null && countryName.length() > 0) { + sb.append(countryName); + } + if (provinceName != null && provinceName.length() > 0) { + sb.append(" "); + sb.append(provinceName); + } + if (cityName != null && cityName.length() > 0) { + sb.append(" "); + sb.append(cityName); + } + if (streetAddress != null && streetAddress.length() > 0) { + sb.append(" "); + sb.append(streetAddress); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java new file mode 100644 index 00000000000..f1ff041e939 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java @@ -0,0 +1,206 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; +import java.util.List; + +public class PersonInfo implements Serializable { + private static final long serialVersionUID = 7443011149612231882L; + + List phones; + + Phone fax; + + FullAddress fullAddress; + + String mobileNo; + + String name; + + boolean male; + + boolean female; + + String department; + + String jobTitle; + + String homepageUrl; + + public List getPhones() { + return phones; + } + + public void setPhones(List phones) { + this.phones = phones; + } + + public boolean isMale() { + return male; + } + + public void setMale(boolean male) { + this.male = male; + } + + public boolean isFemale() { + return female; + } + + public void setFemale(boolean female) { + this.female = female; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public Phone getFax() { + return fax; + } + + public void setFax(Phone fax) { + this.fax = fax; + } + + public FullAddress getFullAddress() { + return fullAddress; + } + + public void setFullAddress(FullAddress fullAddress) { + this.fullAddress = fullAddress; + } + + public String getHomepageUrl() { + return homepageUrl; + } + + public void setHomepageUrl(String homepageUrl) { + this.homepageUrl = homepageUrl; + } + + public String getJobTitle() { + return jobTitle; + } + + public void setJobTitle(String jobTitle) { + this.jobTitle = jobTitle; + } + + public String getMobileNo() { + return mobileNo; + } + + public void setMobileNo(String mobileNo) { + this.mobileNo = mobileNo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((department == null) ? 0 : department.hashCode()); + result = prime * result + ((fax == null) ? 0 : fax.hashCode()); + result = prime * result + (female ? 1231 : 1237); + result = prime * result + ((fullAddress == null) ? 0 : fullAddress.hashCode()); + result = prime * result + ((homepageUrl == null) ? 0 : homepageUrl.hashCode()); + result = prime * result + ((jobTitle == null) ? 0 : jobTitle.hashCode()); + result = prime * result + (male ? 1231 : 1237); + result = prime * result + ((mobileNo == null) ? 0 : mobileNo.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((phones == null) ? 0 : phones.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PersonInfo other = (PersonInfo) obj; + if (department == null) { + if (other.department != null) + return false; + } else if (!department.equals(other.department)) + return false; + if (fax == null) { + if (other.fax != null) + return false; + } else if (!fax.equals(other.fax)) + return false; + if (female != other.female) + return false; + if (fullAddress == null) { + if (other.fullAddress != null) + return false; + } else if (!fullAddress.equals(other.fullAddress)) + return false; + if (homepageUrl == null) { + if (other.homepageUrl != null) + return false; + } else if (!homepageUrl.equals(other.homepageUrl)) + return false; + if (jobTitle == null) { + if (other.jobTitle != null) + return false; + } else if (!jobTitle.equals(other.jobTitle)) + return false; + if (male != other.male) + return false; + if (mobileNo == null) { + if (other.mobileNo != null) + return false; + } else if (!mobileNo.equals(other.mobileNo)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (phones == null) { + if (other.phones != null) + return false; + } else if (!phones.equals(other.phones)) + return false; + return true; + } + + @Override + public String toString() { + return "PersonInfo [phones=" + phones + ", fax=" + fax + ", fullAddress=" + fullAddress + + ", mobileNo=" + mobileNo + ", name=" + name + ", male=" + male + ", female=" + + female + ", department=" + department + ", jobTitle=" + jobTitle + + ", homepageUrl=" + homepageUrl + "]"; + } + +} diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java new file mode 100644 index 00000000000..66b67c96340 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java @@ -0,0 +1,22 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +public enum PersonStatus { + ENABLED, + DISABLED +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java new file mode 100644 index 00000000000..f7da708364c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java @@ -0,0 +1,139 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class Phone implements Serializable { + + private static final long serialVersionUID = 4399060521859707703L; + + private String country; + + private String area; + + private String number; + + private String extensionNumber; + + public Phone() { + } + + public Phone(String country, String area, String number, String extensionNumber) { + this.country = country; + this.area = area; + this.number = number; + this.extensionNumber = extensionNumber; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getExtensionNumber() { + return extensionNumber; + } + + public void setExtensionNumber(String extensionNumber) { + this.extensionNumber = extensionNumber; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((area == null) ? 0 : area.hashCode()); + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((extensionNumber == null) ? 0 : extensionNumber.hashCode()); + result = prime * result + ((number == null) ? 0 : number.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Phone other = (Phone) obj; + if (area == null) { + if (other.area != null) + return false; + } else if (!area.equals(other.area)) + return false; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (extensionNumber == null) { + if (other.extensionNumber != null) + return false; + } else if (!extensionNumber.equals(other.extensionNumber)) + return false; + if (number == null) { + if (other.number != null) + return false; + } else if (!number.equals(other.number)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (country != null && country.length() > 0) { + sb.append(country); + sb.append("-"); + } + if (area != null && area.length() > 0) { + sb.append(area); + sb.append("-"); + } + if (number != null && number.length() > 0) { + sb.append(number); + } + if (extensionNumber != null && extensionNumber.length() > 0) { + sb.append("-"); + sb.append(extensionNumber); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java similarity index 100% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java similarity index 100% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java similarity index 100% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FastJsonSerializationTest.java b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FastJsonSerializationTest.java similarity index 99% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FastJsonSerializationTest.java rename to dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FastJsonSerializationTest.java index bd298ab1039..937d62546c3 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FastJsonSerializationTest.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FastJsonSerializationTest.java @@ -19,7 +19,7 @@ import com.alibaba.dubbo.common.model.media.MediaContent; import com.alibaba.dubbo.common.serialize.ObjectInput; import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.support.json.FastJsonSerialization; +import com.alibaba.dubbo.serialize.fastjson.FastJsonSerialization; import com.alibaba.fastjson.JSONException; import org.junit.Ignore; diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 new file mode 100644 index 00000000000..8ff89345cba --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 @@ -0,0 +1,4 @@ +group=com.alibaba.dubbo.common.extensionloader.activate.impl.GroupActivateExtImpl +value=com.alibaba.dubbo.common.extensionloader.activate.impl.ValueActivateExtImpl +order1=com.alibaba.dubbo.common.extensionloader.activate.impl.OrderActivateExtImpl1 +order2=com.alibaba.dubbo.common.extensionloader.activate.impl.OrderActivateExtImpl2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.adaptive.HasAdaptiveExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.adaptive.HasAdaptiveExt new file mode 100644 index 00000000000..c700dbd7c26 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.adaptive.HasAdaptiveExt @@ -0,0 +1,2 @@ +adaptive=com.alibaba.dubbo.common.extensionloader.adaptive.impl.HasAdaptiveExt_ManualAdaptive +impl1=com.alibaba.dubbo.common.extensionloader.adaptive.impl.HasAdaptiveExtImpl1 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.compatible.CompatibleExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.compatible.CompatibleExt new file mode 100644 index 00000000000..9f176b744c6 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.compatible.CompatibleExt @@ -0,0 +1,2 @@ +com.alibaba.dubbo.common.extensionloader.compatible.impl.CompatibleExtImpl1 +impl2=com.alibaba.dubbo.common.extensionloader.compatible.impl.CompatibleExtImpl2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt new file mode 100644 index 00000000000..f91b958af68 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt @@ -0,0 +1,4 @@ +# Comment 1 +impl1=com.alibaba.dubbo.common.extensionloader.ext1.impl.SimpleExtImpl1#Hello World +impl2=com.alibaba.dubbo.common.extensionloader.ext1.impl.SimpleExtImpl2 # Comment 2 + impl3=com.alibaba.dubbo.common.extensionloader.ext1.impl.SimpleExtImpl3 # with head space \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext2.Ext2 b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext2.Ext2 new file mode 100644 index 00000000000..37bb715ada0 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext2.Ext2 @@ -0,0 +1,3 @@ +impl1=com.alibaba.dubbo.common.extensionloader.ext2.impl.Ext2Impl1 +impl2=com.alibaba.dubbo.common.extensionloader.ext2.impl.Ext2Impl2 +impl3=com.alibaba.dubbo.common.extensionloader.ext2.impl.Ext2Impl3 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt new file mode 100644 index 00000000000..08dc3d72968 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext3.UseProtocolKeyExt @@ -0,0 +1,3 @@ +impl1=com.alibaba.dubbo.common.extensionloader.ext3.impl.UseProtocolKeyExtImpl1 +impl2=com.alibaba.dubbo.common.extensionloader.ext3.impl.UseProtocolKeyExtImpl2 +impl3=com.alibaba.dubbo.common.extensionloader.ext3.impl.UseProtocolKeyExtImpl3 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext4.NoUrlParamExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext4.NoUrlParamExt new file mode 100644 index 00000000000..97aa3838b87 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext4.NoUrlParamExt @@ -0,0 +1,2 @@ +impl1=com.alibaba.dubbo.common.extensionloader.ext4.impl.Ext4Impl1 +impl2=com.alibaba.dubbo.common.extensionloader.ext4.impl.Ext4Impl2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext5.NoAdaptiveMethodExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext5.NoAdaptiveMethodExt new file mode 100644 index 00000000000..a416f9d7537 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext5.NoAdaptiveMethodExt @@ -0,0 +1,2 @@ +impl1=com.alibaba.dubbo.common.extensionloader.ext5.impl.Ext5Impl1 +impl2=com.alibaba.dubbo.common.extensionloader.ext5.impl.Ext5Impl2 diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_inject.Ext6 b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_inject.Ext6 new file mode 100644 index 00000000000..833274fbc39 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_inject.Ext6 @@ -0,0 +1,2 @@ +impl1=com.alibaba.dubbo.common.extensionloader.ext6_inject.impl.Ext6Impl1 +impl2=com.alibaba.dubbo.common.extensionloader.ext6_inject.impl.Ext6Impl2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_wrap.WrappedExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_wrap.WrappedExt new file mode 100644 index 00000000000..e0897cd99f9 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext6_wrap.WrappedExt @@ -0,0 +1,4 @@ +impl1=com.alibaba.dubbo.common.extensionloader.ext6_wrap.impl.Ext5Impl1 +impl2=com.alibaba.dubbo.common.extensionloader.ext6_wrap.impl.Ext5Impl2 +wrapper1=com.alibaba.dubbo.common.extensionloader.ext6_wrap.impl.Ext5Wrapper1 +wrapper2=com.alibaba.dubbo.common.extensionloader.ext6_wrap.impl.Ext5Wrapper2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext7.InitErrorExt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext7.InitErrorExt new file mode 100644 index 00000000000..549094e3bce --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext7.InitErrorExt @@ -0,0 +1,2 @@ +error=com.alibaba.dubbo.common.extensionloader.ext7.impl.Ext7InitErrorImpl +ok=com.alibaba.dubbo.common.extensionloader.ext7.impl.Ext7Impl diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext8_add.AddExt1 b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext8_add.AddExt1 new file mode 100644 index 00000000000..02ee0c0f5ca --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.ext8_add.AddExt1 @@ -0,0 +1 @@ +impl1=com.alibaba.dubbo.common.extensionloader.ext8_add.impl.AddExt1Impl1 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.status.StatusChecker b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.status.StatusChecker new file mode 100644 index 00000000000..82d8158e90d --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.status.StatusChecker @@ -0,0 +1 @@ +aa=12 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/services/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/services/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 new file mode 100644 index 00000000000..2e16c111acf --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/META-INF/services/com.alibaba.dubbo.common.extensionloader.activate.ActivateExt1 @@ -0,0 +1 @@ +com.alibaba.dubbo.common.extensionloader.activate.impl.ActivateExt1Impl1 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/StreamUtilsTest.txt b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/StreamUtilsTest.txt new file mode 100644 index 00000000000..ad471007bd7 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/StreamUtilsTest.txt @@ -0,0 +1 @@ +0123456789 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc new file mode 100644 index 00000000000..c8c083c17eb --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc @@ -0,0 +1,2 @@ +a,d,e,b,c +str3,str2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/json.flex b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/json.flex new file mode 100644 index 00000000000..c6d4015aafa --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/json.flex @@ -0,0 +1,68 @@ +package com.alibaba.dubbo.common.json; +%% + +%{ +private StringBuffer sb; +%} + +%table +%unicode +%state STR1,STR2 + +%yylexthrow ParseException + +HEX = [a-fA-F0-9] +HEX4 = {HEX}{HEX}{HEX}{HEX} + +IDENT = [a-zA-Z_$] [a-zA-Z0-9_$]* +INT_LITERAL = [-]? [0-9]+ +FLOAT_LITERAL = {INT_LITERAL} ( ( \.[0-9]+ ) ? ( [eE][-+]? [0-9]+ )? ) + +ESC1 = [^\"\\] +ESC2 = [^\'\\] + +SKIP = [ \t\r\n] +OTHERS = . +%% + +{ + \" { yybegin(YYINITIAL); return new JSONToken(JSONToken.STRING, sb.toString()); } + {ESC1}+ { sb.append(yytext()); } + \\\" { sb.append('"'); } +} + +{ + \' { yybegin(YYINITIAL); return new JSONToken(JSONToken.STRING, sb.toString()); } + {ESC2}+ { sb.append(yytext()); } + \\\' { sb.append('\''); } +} + +{ + \\\\ { sb.append('\\'); } + \\\/ { sb.append('/'); } + \\b { sb.append('\b'); } + \\f { sb.append('\f'); } + \\n { sb.append('\n'); } + \\r { sb.append('\r'); } + \\t { sb.append('\t'); } + \\u{HEX4} { try{ sb.append((char)Integer.parseInt(yytext().substring(2),16)); }catch(Exception e){ throw new ParseException(e.getMessage()); } } +} + +{ + \" { sb = new StringBuffer(); yybegin(STR1); } + \' { sb = new StringBuffer(); yybegin(STR2); } + {INT_LITERAL} { Long val = Long.valueOf(yytext()); return new JSONToken(JSONToken.INT, val); } + {FLOAT_LITERAL} { Double val = Double.valueOf(yytext()); return new JSONToken(JSONToken.FLOAT, val); } + "true"|"TRUE" { return new JSONToken(JSONToken.BOOL, Boolean.TRUE); } + "false"|"FALSE" { return new JSONToken(JSONToken.BOOL, Boolean.FALSE); } + "null"|"NULL" { return new JSONToken(JSONToken.NULL, null); } + {IDENT} { return new JSONToken(JSONToken.IDENT, yytext()); } + "{" { return new JSONToken(JSONToken.LBRACE); } + "}" { return new JSONToken(JSONToken.RBRACE); } + "[" { return new JSONToken(JSONToken.LSQUARE); } + "]" { return new JSONToken(JSONToken.RSQUARE); } + "," { return new JSONToken(JSONToken.COMMA); } + ":" { return new JSONToken(JSONToken.COLON); } + {SKIP}+ {} + {OTHERS} { throw new ParseException("Unexpected char [" + yytext() +"]"); } +} diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/log4j.xml b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/log4j.xml new file mode 100644 index 00000000000..671f2af8839 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/log4j.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/properties.load b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/properties.load new file mode 100644 index 00000000000..43bf492f5d4 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/test/resources/properties.load @@ -0,0 +1,3 @@ +a=12 +b=34 +c=56 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/pom.xml b/dubbo-serialization/dubbo-serialization-fst/pom.xml new file mode 100644 index 00000000000..077dd176e02 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/pom.xml @@ -0,0 +1,44 @@ + + + + 4.0.0 + + com.alibaba + dubbo-serialization + 2.6.1 + + dubbo-serialization-fst + jar + ${project.artifactId} + The common module of dubbo project + + true + + + + com.alibaba + dubbo-serialization-api + ${project.parent.version} + + + de.ruedigermoeller + fst + + + \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstFactory.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstFactory.java similarity index 96% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstFactory.java rename to dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstFactory.java index 5c57f02148a..6b98745c939 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstFactory.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstFactory.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.fst; +package com.alibaba.dubbo.serialize.fst; import com.alibaba.dubbo.common.serialize.support.SerializableClassRegistry; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstObjectInput.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstObjectInput.java similarity index 69% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstObjectInput.java rename to dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstObjectInput.java index 975c9bda5e6..e825b512bcd 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstObjectInput.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.fst; +package com.alibaba.dubbo.serialize.fst; import com.alibaba.dubbo.common.serialize.ObjectInput; @@ -34,62 +34,70 @@ public FstObjectInput(InputStream inputStream) { } public boolean readBool() throws IOException { - return input.readBoolean(); + return input.readBoolean(); } public byte readByte() throws IOException { - return input.readByte(); + return input.readByte(); } public short readShort() throws IOException { - return input.readShort(); + return input.readShort(); } public int readInt() throws IOException { - return input.readInt(); + return input.readInt(); } public long readLong() throws IOException { - return input.readLong(); + return input.readLong(); } public float readFloat() throws IOException { - return input.readFloat(); + return input.readFloat(); } public double readDouble() throws IOException { - return input.readDouble(); + return input.readDouble(); } public byte[] readBytes() throws IOException { - int len = input.readInt(); - if (len < 0) { - return null; - } else if (len == 0) { - return new byte[]{}; - } else { - byte[] b = new byte[len]; - input.readFully(b); - return b; - } + int len = input.readInt(); + if (len < 0) { + return null; + } else if (len == 0) { + return new byte[]{}; + } else { + byte[] b = new byte[len]; + input.readFully(b); + return b; + } } public String readUTF() throws IOException { - return input.readUTF(); + return input.readUTF(); } public Object readObject() throws IOException, ClassNotFoundException { - return input.readObject(); + return input.readObject(); } @SuppressWarnings("unchecked") public T readObject(Class clazz) throws IOException, ClassNotFoundException { - return (T) readObject(); + try { + return (T) input.readObject(clazz); + } catch (Exception e) { + throw new IOException(e); + } } @SuppressWarnings("unchecked") public T readObject(Class clazz, Type type) throws IOException, ClassNotFoundException { - return (T) readObject(); + try { + return (T) input.readObject(clazz); + } catch (Exception e) { + throw new IOException(e); + } } } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstObjectOutput.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstObjectOutput.java similarity index 97% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstObjectOutput.java rename to dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstObjectOutput.java index e546ff1b151..df0ce7e0db7 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstObjectOutput.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.fst; +package com.alibaba.dubbo.serialize.fst; import com.alibaba.dubbo.common.serialize.ObjectOutput; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstSerialization.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstSerialization.java similarity index 96% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstSerialization.java rename to dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstSerialization.java index cd5e50e4177..308a8576c2f 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/fst/FstSerialization.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/com/alibaba/dubbo/serialize/fst/FstSerialization.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.fst; +package com.alibaba.dubbo.serialize.fst; import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.common.serialize.ObjectInput; diff --git a/dubbo-serialization/dubbo-serialization-fst/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization b/dubbo-serialization/dubbo-serialization-fst/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization new file mode 100644 index 00000000000..ea852d8f1c1 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization @@ -0,0 +1 @@ +fst=com.alibaba.dubbo.serialize.fst.FstSerialization \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java new file mode 100644 index 00000000000..f920a0c6575 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java @@ -0,0 +1,21 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public enum AnimalEnum { + dog, cat, rat, cow, bull, horse; +} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/ClassDescriptorMapper.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizException.java similarity index 66% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/ClassDescriptorMapper.java rename to dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizException.java index 34bedd3fb59..b6eb1f4bbce 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/dubbo/ClassDescriptorMapper.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizException.java @@ -14,22 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.dubbo; +package com.alibaba.dubbo.common.model; -public interface ClassDescriptorMapper { - /** - * get Class-Descriptor by index. - * - * @param index index. - * @return string. - */ - String getDescriptor(int index); +public class BizException extends RuntimeException { - /** - * get Class-Descriptor index - * - * @param desc Class-Descriptor - * @return index. - */ - int getDescriptorIndex(String desc); + private static final long serialVersionUID = 1L; + + public BizException(String message) { + super(message); + } + + public BizException() { + } } \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java new file mode 100644 index 00000000000..29db6e00fc9 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java @@ -0,0 +1,26 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public class BizExceptionNoDefaultConstructor extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public BizExceptionNoDefaultConstructor(String message) { + super(message); + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/Person.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/Person.java new file mode 100644 index 00000000000..6383706cf79 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/Person.java @@ -0,0 +1,95 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.util.Arrays; + +public class Person { + 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java new file mode 100644 index 00000000000..e7dd2486786 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java @@ -0,0 +1,97 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.io.Serializable; +import java.util.Arrays; + +public class SerializablePerson implements Serializable { + private static final long serialVersionUID = 1L; + 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; + SerializablePerson other = (SerializablePerson) 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Image.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Image.java new file mode 100644 index 00000000000..0d6a74a3d4a --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Image.java @@ -0,0 +1,120 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + + +public class Image implements java.io.Serializable { + private static final long serialVersionUID = 1L; + public String uri; + public String title; // Can be null + public int width; + public int height; + public Size size; + + public Image() { + } + + public Image(String uri, String title, int width, int height, Size size) { + this.height = height; + this.title = title; + this.uri = uri; + this.width = width; + this.size = size; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Image image = (Image) o; + + if (height != image.height) return false; + if (width != image.width) return false; + if (size != image.size) return false; + if (title != null ? !title.equals(image.title) : image.title != null) return false; + if (uri != null ? !uri.equals(image.uri) : image.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (size != null ? size.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Image "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", size=").append(size); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + public enum Size { + SMALL, LARGE + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Media.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Media.java new file mode 100644 index 00000000000..8a78f6d313b --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/Media.java @@ -0,0 +1,205 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class Media implements java.io.Serializable { + public String uri; + public String title; // Can be unset. + public int width; + public int height; + public String format; + public long duration; + public long size; + public int bitrate; // Can be unset. + public boolean hasBitrate; + public List persons; + public Player player; + public String copyright; // Can be unset. + + public Media() { + } + + public Media(String uri, String title, int width, int height, String format, long duration, long size, int bitrate, boolean hasBitrate, List persons, Player player, String copyright) { + this.uri = uri; + this.title = title; + this.width = width; + this.height = height; + this.format = format; + this.duration = duration; + this.size = size; + this.bitrate = bitrate; + this.hasBitrate = hasBitrate; + this.persons = persons; + this.player = player; + this.copyright = copyright; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Media media = (Media) o; + + if (bitrate != media.bitrate) return false; + if (duration != media.duration) return false; + if (hasBitrate != media.hasBitrate) return false; + if (height != media.height) return false; + if (size != media.size) return false; + if (width != media.width) return false; + if (copyright != null ? !copyright.equals(media.copyright) : media.copyright != null) return false; + if (format != null ? !format.equals(media.format) : media.format != null) return false; + if (persons != null ? !persons.equals(media.persons) : media.persons != null) return false; + if (player != media.player) return false; + if (title != null ? !title.equals(media.title) : media.title != null) return false; + if (uri != null ? !uri.equals(media.uri) : media.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (format != null ? format.hashCode() : 0); + result = 31 * result + (int) (duration ^ (duration >>> 32)); + result = 31 * result + (int) (size ^ (size >>> 32)); + result = 31 * result + bitrate; + result = 31 * result + (hasBitrate ? 1 : 0); + result = 31 * result + (persons != null ? persons.hashCode() : 0); + result = 31 * result + (player != null ? player.hashCode() : 0); + result = 31 * result + (copyright != null ? copyright.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Media "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", format=").append(format); + sb.append(", duration=").append(duration); + sb.append(", size=").append(size); + sb.append(", hasBitrate=").append(hasBitrate); + sb.append(", bitrate=").append(String.valueOf(bitrate)); + sb.append(", persons=").append(persons); + sb.append(", player=").append(player); + sb.append(", copyright=").append(copyright); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public int getBitrate() { + return bitrate; + } + + public void setBitrate(int bitrate) { + this.bitrate = bitrate; + this.hasBitrate = true; + } + + public List getPersons() { + return persons; + } + + public void setPersons(List persons) { + this.persons = persons; + } + + public Player getPlayer() { + return player; + } + + public void setPlayer(Player player) { + this.player = player; + } + + public String getCopyright() { + return copyright; + } + + public void setCopyright(String copyright) { + this.copyright = copyright; + } + + public enum Player { + JAVA, FLASH + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java new file mode 100644 index 00000000000..9664116a635 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java @@ -0,0 +1,78 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class MediaContent implements java.io.Serializable { + public Media media; + public List images; + + public MediaContent() { + } + + public MediaContent(Media media, List images) { + this.media = media; + this.images = images; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MediaContent that = (MediaContent) o; + + if (images != null ? !images.equals(that.images) : that.images != null) return false; + if (media != null ? !media.equals(that.media) : that.media != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = media != null ? media.hashCode() : 0; + result = 31 * result + (images != null ? images.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[MediaContent: "); + sb.append("media=").append(media); + sb.append(", images=").append(images); + sb.append("]"); + return sb.toString(); + } + + public Media getMedia() { + return media; + } + + public void setMedia(Media media) { + this.media = media; + } + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java new file mode 100644 index 00000000000..70bc793979e --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java @@ -0,0 +1,151 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class BigPerson implements Serializable { + private static final long serialVersionUID = 1L; + + String personId; + + String loginName; + + PersonStatus status; + + String email; + + String penName; + + PersonInfo infoProfile; + + public BigPerson() { + + } + + public BigPerson(String id) { + this.personId = id; + } + + public String getPersonId() { + return personId; + } + + public void setPersonId(String personId) { + this.personId = personId; + } + + public PersonInfo getInfoProfile() { + return infoProfile; + } + + public void setInfoProfile(PersonInfo infoProfile) { + this.infoProfile = infoProfile; + } + + public String getEmail() { + return this.email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getLoginName() { + return this.loginName; + } + + public void setLoginName(String loginName) { + this.loginName = loginName; + } + + public PersonStatus getStatus() { + return this.status; + } + + public void setStatus(PersonStatus status) { + this.status = status; + } + + public String getPenName() { + return penName; + } + + public void setPenName(String penName) { + this.penName = penName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + ((infoProfile == null) ? 0 : infoProfile.hashCode()); + result = prime * result + ((loginName == null) ? 0 : loginName.hashCode()); + result = prime * result + ((penName == null) ? 0 : penName.hashCode()); + result = prime * result + ((personId == null) ? 0 : personId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BigPerson other = (BigPerson) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + if (infoProfile == null) { + if (other.infoProfile != null) + return false; + } else if (!infoProfile.equals(other.infoProfile)) + return false; + if (loginName == null) { + if (other.loginName != null) + return false; + } else if (!loginName.equals(other.loginName)) + return false; + if (penName == null) { + if (other.penName != null) + return false; + } else if (!penName.equals(other.penName)) + return false; + if (personId == null) { + if (other.personId != null) + return false; + } else if (!personId.equals(other.personId)) + return false; + if (status != other.status) + return false; + return true; + } + + @Override + public String toString() { + return "BigPerson [personId=" + personId + ", loginName=" + loginName + ", status=" + + status + ", email=" + email + ", penName=" + penName + ", infoProfile=" + + infoProfile + "]"; + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java new file mode 100644 index 00000000000..dcc93cf8bd3 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java @@ -0,0 +1,202 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class FullAddress implements Serializable { + + private static final long serialVersionUID = 5163979984269419831L; + + private String countryId; + + private String countryName; + + private String provinceName; + + private String cityId; + + private String cityName; + + private String streetAddress; + + private String zipCode; + + public FullAddress() { + } + + public FullAddress(String countryId, String provinceName, String cityId, String streetAddress, + String zipCode) { + this.countryId = countryId; + this.countryName = countryId; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityId; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public FullAddress(String countryId, String countryName, String provinceName, String cityId, + String cityName, String streetAddress, String zipCode) { + this.countryId = countryId; + this.countryName = countryName; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityName; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public String getCountryId() { + return countryId; + } + + public void setCountryId(String countryId) { + this.countryId = countryId; + } + + public String getCountryName() { + return countryName; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + + public String getProvinceName() { + return provinceName; + } + + public void setProvinceName(String provinceName) { + this.provinceName = provinceName; + } + + public String getCityId() { + return cityId; + } + + public void setCityId(String cityId) { + this.cityId = cityId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getStreetAddress() { + return streetAddress; + } + + public void setStreetAddress(String streetAddress) { + this.streetAddress = streetAddress; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((cityId == null) ? 0 : cityId.hashCode()); + result = prime * result + ((cityName == null) ? 0 : cityName.hashCode()); + result = prime * result + ((countryId == null) ? 0 : countryId.hashCode()); + result = prime * result + ((countryName == null) ? 0 : countryName.hashCode()); + result = prime * result + ((provinceName == null) ? 0 : provinceName.hashCode()); + result = prime * result + ((streetAddress == null) ? 0 : streetAddress.hashCode()); + result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FullAddress other = (FullAddress) obj; + if (cityId == null) { + if (other.cityId != null) + return false; + } else if (!cityId.equals(other.cityId)) + return false; + if (cityName == null) { + if (other.cityName != null) + return false; + } else if (!cityName.equals(other.cityName)) + return false; + if (countryId == null) { + if (other.countryId != null) + return false; + } else if (!countryId.equals(other.countryId)) + return false; + if (countryName == null) { + if (other.countryName != null) + return false; + } else if (!countryName.equals(other.countryName)) + return false; + if (provinceName == null) { + if (other.provinceName != null) + return false; + } else if (!provinceName.equals(other.provinceName)) + return false; + if (streetAddress == null) { + if (other.streetAddress != null) + return false; + } else if (!streetAddress.equals(other.streetAddress)) + return false; + if (zipCode == null) { + if (other.zipCode != null) + return false; + } else if (!zipCode.equals(other.zipCode)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (countryName != null && countryName.length() > 0) { + sb.append(countryName); + } + if (provinceName != null && provinceName.length() > 0) { + sb.append(" "); + sb.append(provinceName); + } + if (cityName != null && cityName.length() > 0) { + sb.append(" "); + sb.append(cityName); + } + if (streetAddress != null && streetAddress.length() > 0) { + sb.append(" "); + sb.append(streetAddress); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java new file mode 100644 index 00000000000..f1ff041e939 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java @@ -0,0 +1,206 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; +import java.util.List; + +public class PersonInfo implements Serializable { + private static final long serialVersionUID = 7443011149612231882L; + + List phones; + + Phone fax; + + FullAddress fullAddress; + + String mobileNo; + + String name; + + boolean male; + + boolean female; + + String department; + + String jobTitle; + + String homepageUrl; + + public List getPhones() { + return phones; + } + + public void setPhones(List phones) { + this.phones = phones; + } + + public boolean isMale() { + return male; + } + + public void setMale(boolean male) { + this.male = male; + } + + public boolean isFemale() { + return female; + } + + public void setFemale(boolean female) { + this.female = female; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public Phone getFax() { + return fax; + } + + public void setFax(Phone fax) { + this.fax = fax; + } + + public FullAddress getFullAddress() { + return fullAddress; + } + + public void setFullAddress(FullAddress fullAddress) { + this.fullAddress = fullAddress; + } + + public String getHomepageUrl() { + return homepageUrl; + } + + public void setHomepageUrl(String homepageUrl) { + this.homepageUrl = homepageUrl; + } + + public String getJobTitle() { + return jobTitle; + } + + public void setJobTitle(String jobTitle) { + this.jobTitle = jobTitle; + } + + public String getMobileNo() { + return mobileNo; + } + + public void setMobileNo(String mobileNo) { + this.mobileNo = mobileNo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((department == null) ? 0 : department.hashCode()); + result = prime * result + ((fax == null) ? 0 : fax.hashCode()); + result = prime * result + (female ? 1231 : 1237); + result = prime * result + ((fullAddress == null) ? 0 : fullAddress.hashCode()); + result = prime * result + ((homepageUrl == null) ? 0 : homepageUrl.hashCode()); + result = prime * result + ((jobTitle == null) ? 0 : jobTitle.hashCode()); + result = prime * result + (male ? 1231 : 1237); + result = prime * result + ((mobileNo == null) ? 0 : mobileNo.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((phones == null) ? 0 : phones.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PersonInfo other = (PersonInfo) obj; + if (department == null) { + if (other.department != null) + return false; + } else if (!department.equals(other.department)) + return false; + if (fax == null) { + if (other.fax != null) + return false; + } else if (!fax.equals(other.fax)) + return false; + if (female != other.female) + return false; + if (fullAddress == null) { + if (other.fullAddress != null) + return false; + } else if (!fullAddress.equals(other.fullAddress)) + return false; + if (homepageUrl == null) { + if (other.homepageUrl != null) + return false; + } else if (!homepageUrl.equals(other.homepageUrl)) + return false; + if (jobTitle == null) { + if (other.jobTitle != null) + return false; + } else if (!jobTitle.equals(other.jobTitle)) + return false; + if (male != other.male) + return false; + if (mobileNo == null) { + if (other.mobileNo != null) + return false; + } else if (!mobileNo.equals(other.mobileNo)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (phones == null) { + if (other.phones != null) + return false; + } else if (!phones.equals(other.phones)) + return false; + return true; + } + + @Override + public String toString() { + return "PersonInfo [phones=" + phones + ", fax=" + fax + ", fullAddress=" + fullAddress + + ", mobileNo=" + mobileNo + ", name=" + name + ", male=" + male + ", female=" + + female + ", department=" + department + ", jobTitle=" + jobTitle + + ", homepageUrl=" + homepageUrl + "]"; + } + +} diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java new file mode 100644 index 00000000000..66b67c96340 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java @@ -0,0 +1,22 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +public enum PersonStatus { + ENABLED, + DISABLED +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java new file mode 100644 index 00000000000..f7da708364c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java @@ -0,0 +1,139 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class Phone implements Serializable { + + private static final long serialVersionUID = 4399060521859707703L; + + private String country; + + private String area; + + private String number; + + private String extensionNumber; + + public Phone() { + } + + public Phone(String country, String area, String number, String extensionNumber) { + this.country = country; + this.area = area; + this.number = number; + this.extensionNumber = extensionNumber; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getExtensionNumber() { + return extensionNumber; + } + + public void setExtensionNumber(String extensionNumber) { + this.extensionNumber = extensionNumber; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((area == null) ? 0 : area.hashCode()); + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((extensionNumber == null) ? 0 : extensionNumber.hashCode()); + result = prime * result + ((number == null) ? 0 : number.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Phone other = (Phone) obj; + if (area == null) { + if (other.area != null) + return false; + } else if (!area.equals(other.area)) + return false; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (extensionNumber == null) { + if (other.extensionNumber != null) + return false; + } else if (!extensionNumber.equals(other.extensionNumber)) + return false; + if (number == null) { + if (other.number != null) + return false; + } else if (!number.equals(other.number)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (country != null && country.length() > 0) { + sb.append(country); + sb.append("-"); + } + if (area != null && area.length() > 0) { + sb.append(area); + sb.append("-"); + } + if (number != null && number.length() > 0) { + sb.append(number); + } + if (extensionNumber != null && extensionNumber.length() > 0) { + sb.append("-"); + sb.append(extensionNumber); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java new file mode 100644 index 00000000000..015c8c1703e --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/java/com/alibaba/dubbo/common/serialize/serialization/FstSerializationTest.java @@ -0,0 +1,1124 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.model.AnimalEnum; +import com.alibaba.dubbo.common.model.BizException; +import com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor; +import com.alibaba.dubbo.common.model.SerializablePerson; +import com.alibaba.dubbo.common.model.media.Image; +import com.alibaba.dubbo.common.model.media.Media; +import com.alibaba.dubbo.common.model.media.MediaContent; +import com.alibaba.dubbo.common.model.person.BigPerson; +import com.alibaba.dubbo.common.model.person.FullAddress; +import com.alibaba.dubbo.common.model.person.PersonInfo; +import com.alibaba.dubbo.common.model.person.PersonStatus; +import com.alibaba.dubbo.common.model.person.Phone; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; +import com.alibaba.dubbo.serialize.fst.FstSerialization; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Time; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class FstSerializationTest { + + static Random random = new Random(); + Serialization serialization = new FstSerialization(); + URL url = new URL("protocl", "1.1.1.1", 1234); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + // ================ Primitive Type ================ + BigPerson bigPerson; + MediaContent mediaContent; + + { + bigPerson = new BigPerson(); + bigPerson.setPersonId("superman111"); + bigPerson.setLoginName("superman"); + bigPerson.setStatus(PersonStatus.ENABLED); + bigPerson.setEmail("sm@1.com"); + bigPerson.setPenName("pname"); + + ArrayList phones = new ArrayList(); + Phone phone1 = new Phone("86", "0571", "87654321", "001"); + Phone phone2 = new Phone("86", "0571", "87654322", "002"); + phones.add(phone1); + phones.add(phone2); + + PersonInfo pi = new PersonInfo(); + pi.setPhones(phones); + Phone fax = new Phone("86", "0571", "87654321", null); + pi.setFax(fax); + FullAddress addr = new FullAddress("CN", "zj", "3480", "wensanlu", "315000"); + pi.setFullAddress(addr); + pi.setMobileNo("13584652131"); + pi.setMale(true); + pi.setDepartment("b2b"); + pi.setHomepageUrl("www.capcom.com"); + pi.setJobTitle("qa"); + pi.setName("superman"); + + bigPerson.setInfoProfile(pi); + } + + { + Media media = new Media(); + media.setUri("uri://中华人民共和国"); + media.setTitle("title"); + media.setWidth(1239); + media.setHeight(1938); + media.setFormat("format-xxxx"); + media.setDuration(93419235); + media.setSize(3477897); + media.setBitrate(94523); + List persons = new ArrayList(); + persons.add("jerry"); + persons.add("tom"); + persons.add("lucy"); + media.setPersons(persons); + media.setCopyright("1999-2011"); + media.setPlayer(Media.Player.FLASH); + + List images = new ArrayList(); + for (int i = 0; i < 10; ++i) { + Image image = new Image(); + image.setUri("url" + i); + if (i % 2 == 0) image.setTitle("title" + i); + image.setWidth(34 + i); + image.setHeight(2323 + i); + image.setSize((i % 2 == 0) ? Image.Size.SMALL : Image.Size.LARGE); + + images.add(image); + } + + mediaContent = new MediaContent(media, images); + } + + @Test + public void test_Bool() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeBool(false); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertFalse(deserialize.readBool()); + } + + @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()); + } + } + + @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()); + } + + @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()); + } + } + + @Test + public void test_Short() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeShort((short) 123); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals((short) 123, deserialize.readShort()); + +// try { +// deserialize.readShort(); +// fail(); +// } catch (IOException expected) { +// } + } + + @Test + public void test_Integer() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeInt(1); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + int i = deserialize.readInt(); + assertEquals(1, i); + + try { + assertEquals(0, deserialize.readInt()); + } catch (IOException expected) { + } + } + + @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()); + } + + @Test + public void test_Float() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeFloat(1.28F); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(1.28F == deserialize.readFloat()); + } + + // ================== 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); + + assertTrue(1.28 == deserialize.readDouble()); + + } + + @Test + public void test_UtfString() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeUTF("123中华人民共和国"); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals("123中华人民共和国", deserialize.readUTF()); + } + + @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()); + + } + + @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()); + + } + + // ================ Array Type ================ + + void assertObjectArray(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject())); + } + + void assertObjectArrayWithType(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject(clazz))); + } + + @SuppressWarnings("unchecked") + 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()); + + } + + void assertObjectWithType(T data, Class clazz) 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(clazz)); + + } + + @Test + public void test_boolArray() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject())); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_boolArray_withType() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject(boolean[].class))); + + try { + deserialize.readObject(boolean[].class); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_charArray_withType() 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(char[].class)); + + try { + deserialize.readObject(char[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray_withType() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject()); + } + + @Test + public void test_intArray() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_intArray_withType() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject(int[].class)); + + try { + deserialize.readObject(int[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray_withType() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject(long[].class)); + + try { + deserialize.readObject(long[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(), 0.0001F); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray_withType() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(float[].class), 0.0001F); + + try { + deserialize.readObject(float[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(), 0.0001); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray_withType() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(double[].class), 0.0001); + + try { + deserialize.readObject(double[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_StringArray() throws Exception { + assertObjectArray(new String[]{"1", "b"}, String[].class); + } + + @Test + public void test_StringArray_withType() throws Exception { + assertObjectArrayWithType(new String[]{"1", "b"}, String[].class); + } + + // ================ Simple Type ================ + + @Test + public void test_IntegerArray() throws Exception { + assertObjectArray(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_IntegerArray_withType() throws Exception { + assertObjectArrayWithType(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_EnumArray() throws Exception { + assertObjectArray(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_EnumArray_withType() throws Exception { + assertObjectArrayWithType(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_SPerson() throws Exception { + assertObject(new SerializablePerson()); + } + + @Test + public void test_SPerson_withType() throws Exception { + assertObjectWithType(new SerializablePerson(), SerializablePerson.class); + } + + @Test + public void test_BizException() 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(); + assertEquals("Hello", ((BizException) read).getMessage()); + } + + @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()); + } + + @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()); + } + + @Test + public void test_BizExceptionNoDefaultConstructor_WithType() 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(BizExceptionNoDefaultConstructor.class); + assertEquals("Hello", ((BizExceptionNoDefaultConstructor) read).getMessage()); + } + + @Test + public void test_enum() throws Exception { + assertObject(AnimalEnum.dog); + } + + @Test + public void test_enum_withType() throws Exception { + assertObjectWithType(AnimalEnum.dog, AnimalEnum.class); + } + + @Test + public void test_Date() throws Exception { + assertObject(new Date()); + } + + @Test + public void test_Date_withType() throws Exception { + assertObjectWithType(new Date(), Date.class); + } + + @Test + public void test_Time() throws Exception { + assertObject(new Time(System.currentTimeMillis())); + } + + @Test + public void test_Time_withType() throws Exception { + assertObjectWithType(new Time(System.currentTimeMillis()), Time.class); + } + + @Test + public void test_ByteWrap() throws Exception { + assertObject(new Byte((byte) 12)); + } + + @Test + public void test_ByteWrap_withType() throws Exception { + assertObjectWithType(new Byte((byte) 12), Byte.class); + } + + @Test + public void test_LongWrap() throws Exception { + assertObject(new Long(12)); + } + + @Test + public void test_LongWrap_withType() throws Exception { + assertObjectWithType(new Long(12), Long.class); + } + + @Test + public void test_BigInteger() throws Exception { + assertObject(new BigInteger("23423434234234234")); + } + + @Test + public void test_BigInteger_withType() throws Exception { + assertObjectWithType(new BigInteger("23423434234234234"), BigInteger.class); + } + + @Test + public void test_BigDecimal() throws Exception { + assertObject(new BigDecimal("23423434234234234.341274832341234235")); + } + + @Test + public void test_BigDecimal_withType() throws Exception { + assertObjectWithType(new BigDecimal("23423434234234234.341274832341234235"), BigDecimal.class); + } + + @Test + public void test_StringList_asListReturn() throws Exception { + List args = Arrays.asList(new String[]{"1", "b"}); + + assertObject(args); + } + + @Test + public void test_StringArrayList() throws Exception { + List args = new ArrayList(Arrays.asList(new String[]{"1", "b"})); + + assertObject(args); + } + + @Test + public void test_StringSet() throws Exception { + Set args = new HashSet(); + args.add("1"); + + assertObject(args); + } + + @Test + public void test_LinkedHashMap() throws Exception { + LinkedHashMap data = new LinkedHashMap(); + 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) read).entrySet().iterator().next().getKey(); + assertEquals("1", key1); + + assertEquals(data, read); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + // ================ Complex Collection Type ================ + + @Test + public void test_SPersonList() throws Exception { + List args = new ArrayList(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_SPersonSet() throws Exception { + Set args = new HashSet(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + // ================ complex POJO ============= + + @Test + public void test_IntSPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.put("1", sublist); + + assertObject(args); + } + + @Test + public void test_SPersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.add(sublist); + + assertObject(args); + } + + @Test + public void test_BigPerson() throws Exception { + assertObject(bigPerson); + } + + @Test + public void test_BigPerson_WithType() throws Exception { + assertObjectWithType(bigPerson, BigPerson.class); + } + + @Test + public void test_MediaContent() throws Exception { + assertObject(mediaContent); + } + + @Test + public void test_MediaContent_WithType() throws Exception { + assertObjectWithType(mediaContent, MediaContent.class); + } + + @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) { + } + } + + @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) { + } + } + + + // abnormal case + + @Test + public void test_MediaContent_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(); + assertEquals(null, read); + } catch (IOException expected) { + System.out.println(expected); + } + } + + @Test + public void test_MediaContent_WithType_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(MediaContent.class); + assertEquals(null, read); + } catch (IOException expected) { + System.out.println(expected); + } + } + + + @Test(timeout = 3000) + public void test_LoopReference() throws Exception { + Map map = new HashMap(); + 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 output = (Map) deserialize.readObject(); + + assertEquals("v1", output.get("k1")); + assertSame(output, output.get("self")); + } + + // ================ 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) { + } + } +} diff --git a/dubbo-serialization/dubbo-serialization-fst/src/test/resources/log4j.xml b/dubbo-serialization/dubbo-serialization-fst/src/test/resources/log4j.xml new file mode 100644 index 00000000000..671f2af8839 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-fst/src/test/resources/log4j.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/pom.xml b/dubbo-serialization/dubbo-serialization-hessian2/pom.xml new file mode 100644 index 00000000000..332a7b80e46 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/pom.xml @@ -0,0 +1,45 @@ + + + + 4.0.0 + + com.alibaba + dubbo-serialization + 2.6.1 + + dubbo-serialization-hessian2 + jar + ${project.artifactId} + The common module of dubbo project + + true + + + + com.alibaba + dubbo-serialization-api + ${project.parent.version} + + + com.alibaba + hessian-lite + 3.2.1-fixed-2 + + + \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2ObjectInput.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2ObjectInput.java similarity index 97% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2ObjectInput.java rename to dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2ObjectInput.java index 6de66982c27..aa64f8ba9b0 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2ObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2ObjectInput.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.hessian; +package com.alibaba.dubbo.serialize.hessian2; import com.alibaba.com.caucho.hessian.io.Hessian2Input; import com.alibaba.dubbo.common.serialize.ObjectInput; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2ObjectOutput.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2ObjectOutput.java similarity index 97% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2ObjectOutput.java rename to dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2ObjectOutput.java index 6a2fa67bf76..fd9997bad20 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2ObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2ObjectOutput.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.hessian; +package com.alibaba.dubbo.serialize.hessian2; import com.alibaba.com.caucho.hessian.io.Hessian2Output; import com.alibaba.dubbo.common.serialize.ObjectOutput; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2Serialization.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2Serialization.java similarity index 93% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2Serialization.java rename to dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2Serialization.java index eb78df05020..155eaa0806d 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2Serialization.java @@ -1,48 +1,48 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.hessian; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.Serialization; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class Hessian2Serialization implements Serialization { - - public static final byte ID = 2; - - public byte getContentTypeId() { - return ID; - } - - public String getContentType() { - return "x-application/hessian2"; - } - - public ObjectOutput serialize(URL url, OutputStream out) throws IOException { - return new Hessian2ObjectOutput(out); - } - - public ObjectInput deserialize(URL url, InputStream is) throws IOException { - return new Hessian2ObjectInput(is); - } - +/* + * 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 com.alibaba.dubbo.serialize.hessian2; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class Hessian2Serialization implements Serialization { + + public static final byte ID = 2; + + public byte getContentTypeId() { + return ID; + } + + public String getContentType() { + return "x-application/hessian2"; + } + + public ObjectOutput serialize(URL url, OutputStream out) throws IOException { + return new Hessian2ObjectOutput(out); + } + + public ObjectInput deserialize(URL url, InputStream is) throws IOException { + return new Hessian2ObjectInput(is); + } + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2SerializerFactory.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2SerializerFactory.java similarity index 92% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2SerializerFactory.java rename to dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2SerializerFactory.java index 2d5b7aae4d5..af73a072b29 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/hessian/Hessian2SerializerFactory.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/com/alibaba/dubbo/serialize/hessian2/Hessian2SerializerFactory.java @@ -1,33 +1,33 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.hessian; - -import com.alibaba.com.caucho.hessian.io.SerializerFactory; - -public class Hessian2SerializerFactory extends SerializerFactory { - - public static final SerializerFactory SERIALIZER_FACTORY = new Hessian2SerializerFactory(); - - private Hessian2SerializerFactory() { - } - - @Override - public ClassLoader getClassLoader() { - return Thread.currentThread().getContextClassLoader(); - } - -} +/* + * 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 com.alibaba.dubbo.serialize.hessian2; + +import com.alibaba.com.caucho.hessian.io.SerializerFactory; + +public class Hessian2SerializerFactory extends SerializerFactory { + + public static final SerializerFactory SERIALIZER_FACTORY = new Hessian2SerializerFactory(); + + private Hessian2SerializerFactory() { + } + + @Override + public ClassLoader getClassLoader() { + return Thread.currentThread().getContextClassLoader(); + } + +} diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization b/dubbo-serialization/dubbo-serialization-hessian2/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization new file mode 100644 index 00000000000..d9b20c8c029 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization @@ -0,0 +1 @@ +hessian2=com.alibaba.dubbo.serialize.hessian2.Hessian2Serialization \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java new file mode 100644 index 00000000000..f920a0c6575 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java @@ -0,0 +1,21 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public enum AnimalEnum { + dog, cat, rat, cow, bull, horse; +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizException.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizException.java new file mode 100644 index 00000000000..b6eb1f4bbce --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizException.java @@ -0,0 +1,29 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public class BizException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public BizException(String message) { + super(message); + } + + public BizException() { + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java new file mode 100644 index 00000000000..29db6e00fc9 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java @@ -0,0 +1,26 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public class BizExceptionNoDefaultConstructor extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public BizExceptionNoDefaultConstructor(String message) { + super(message); + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/Person.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/Person.java new file mode 100644 index 00000000000..6383706cf79 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/Person.java @@ -0,0 +1,95 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.util.Arrays; + +public class Person { + 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java new file mode 100644 index 00000000000..e7dd2486786 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java @@ -0,0 +1,97 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.io.Serializable; +import java.util.Arrays; + +public class SerializablePerson implements Serializable { + private static final long serialVersionUID = 1L; + 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; + SerializablePerson other = (SerializablePerson) 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Image.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Image.java new file mode 100644 index 00000000000..0d6a74a3d4a --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Image.java @@ -0,0 +1,120 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + + +public class Image implements java.io.Serializable { + private static final long serialVersionUID = 1L; + public String uri; + public String title; // Can be null + public int width; + public int height; + public Size size; + + public Image() { + } + + public Image(String uri, String title, int width, int height, Size size) { + this.height = height; + this.title = title; + this.uri = uri; + this.width = width; + this.size = size; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Image image = (Image) o; + + if (height != image.height) return false; + if (width != image.width) return false; + if (size != image.size) return false; + if (title != null ? !title.equals(image.title) : image.title != null) return false; + if (uri != null ? !uri.equals(image.uri) : image.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (size != null ? size.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Image "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", size=").append(size); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + public enum Size { + SMALL, LARGE + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Media.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Media.java new file mode 100644 index 00000000000..8a78f6d313b --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/Media.java @@ -0,0 +1,205 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class Media implements java.io.Serializable { + public String uri; + public String title; // Can be unset. + public int width; + public int height; + public String format; + public long duration; + public long size; + public int bitrate; // Can be unset. + public boolean hasBitrate; + public List persons; + public Player player; + public String copyright; // Can be unset. + + public Media() { + } + + public Media(String uri, String title, int width, int height, String format, long duration, long size, int bitrate, boolean hasBitrate, List persons, Player player, String copyright) { + this.uri = uri; + this.title = title; + this.width = width; + this.height = height; + this.format = format; + this.duration = duration; + this.size = size; + this.bitrate = bitrate; + this.hasBitrate = hasBitrate; + this.persons = persons; + this.player = player; + this.copyright = copyright; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Media media = (Media) o; + + if (bitrate != media.bitrate) return false; + if (duration != media.duration) return false; + if (hasBitrate != media.hasBitrate) return false; + if (height != media.height) return false; + if (size != media.size) return false; + if (width != media.width) return false; + if (copyright != null ? !copyright.equals(media.copyright) : media.copyright != null) return false; + if (format != null ? !format.equals(media.format) : media.format != null) return false; + if (persons != null ? !persons.equals(media.persons) : media.persons != null) return false; + if (player != media.player) return false; + if (title != null ? !title.equals(media.title) : media.title != null) return false; + if (uri != null ? !uri.equals(media.uri) : media.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (format != null ? format.hashCode() : 0); + result = 31 * result + (int) (duration ^ (duration >>> 32)); + result = 31 * result + (int) (size ^ (size >>> 32)); + result = 31 * result + bitrate; + result = 31 * result + (hasBitrate ? 1 : 0); + result = 31 * result + (persons != null ? persons.hashCode() : 0); + result = 31 * result + (player != null ? player.hashCode() : 0); + result = 31 * result + (copyright != null ? copyright.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Media "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", format=").append(format); + sb.append(", duration=").append(duration); + sb.append(", size=").append(size); + sb.append(", hasBitrate=").append(hasBitrate); + sb.append(", bitrate=").append(String.valueOf(bitrate)); + sb.append(", persons=").append(persons); + sb.append(", player=").append(player); + sb.append(", copyright=").append(copyright); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public int getBitrate() { + return bitrate; + } + + public void setBitrate(int bitrate) { + this.bitrate = bitrate; + this.hasBitrate = true; + } + + public List getPersons() { + return persons; + } + + public void setPersons(List persons) { + this.persons = persons; + } + + public Player getPlayer() { + return player; + } + + public void setPlayer(Player player) { + this.player = player; + } + + public String getCopyright() { + return copyright; + } + + public void setCopyright(String copyright) { + this.copyright = copyright; + } + + public enum Player { + JAVA, FLASH + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java new file mode 100644 index 00000000000..9664116a635 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java @@ -0,0 +1,78 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class MediaContent implements java.io.Serializable { + public Media media; + public List images; + + public MediaContent() { + } + + public MediaContent(Media media, List images) { + this.media = media; + this.images = images; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MediaContent that = (MediaContent) o; + + if (images != null ? !images.equals(that.images) : that.images != null) return false; + if (media != null ? !media.equals(that.media) : that.media != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = media != null ? media.hashCode() : 0; + result = 31 * result + (images != null ? images.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[MediaContent: "); + sb.append("media=").append(media); + sb.append(", images=").append(images); + sb.append("]"); + return sb.toString(); + } + + public Media getMedia() { + return media; + } + + public void setMedia(Media media) { + this.media = media; + } + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java new file mode 100644 index 00000000000..70bc793979e --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java @@ -0,0 +1,151 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class BigPerson implements Serializable { + private static final long serialVersionUID = 1L; + + String personId; + + String loginName; + + PersonStatus status; + + String email; + + String penName; + + PersonInfo infoProfile; + + public BigPerson() { + + } + + public BigPerson(String id) { + this.personId = id; + } + + public String getPersonId() { + return personId; + } + + public void setPersonId(String personId) { + this.personId = personId; + } + + public PersonInfo getInfoProfile() { + return infoProfile; + } + + public void setInfoProfile(PersonInfo infoProfile) { + this.infoProfile = infoProfile; + } + + public String getEmail() { + return this.email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getLoginName() { + return this.loginName; + } + + public void setLoginName(String loginName) { + this.loginName = loginName; + } + + public PersonStatus getStatus() { + return this.status; + } + + public void setStatus(PersonStatus status) { + this.status = status; + } + + public String getPenName() { + return penName; + } + + public void setPenName(String penName) { + this.penName = penName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + ((infoProfile == null) ? 0 : infoProfile.hashCode()); + result = prime * result + ((loginName == null) ? 0 : loginName.hashCode()); + result = prime * result + ((penName == null) ? 0 : penName.hashCode()); + result = prime * result + ((personId == null) ? 0 : personId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BigPerson other = (BigPerson) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + if (infoProfile == null) { + if (other.infoProfile != null) + return false; + } else if (!infoProfile.equals(other.infoProfile)) + return false; + if (loginName == null) { + if (other.loginName != null) + return false; + } else if (!loginName.equals(other.loginName)) + return false; + if (penName == null) { + if (other.penName != null) + return false; + } else if (!penName.equals(other.penName)) + return false; + if (personId == null) { + if (other.personId != null) + return false; + } else if (!personId.equals(other.personId)) + return false; + if (status != other.status) + return false; + return true; + } + + @Override + public String toString() { + return "BigPerson [personId=" + personId + ", loginName=" + loginName + ", status=" + + status + ", email=" + email + ", penName=" + penName + ", infoProfile=" + + infoProfile + "]"; + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java new file mode 100644 index 00000000000..dcc93cf8bd3 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java @@ -0,0 +1,202 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class FullAddress implements Serializable { + + private static final long serialVersionUID = 5163979984269419831L; + + private String countryId; + + private String countryName; + + private String provinceName; + + private String cityId; + + private String cityName; + + private String streetAddress; + + private String zipCode; + + public FullAddress() { + } + + public FullAddress(String countryId, String provinceName, String cityId, String streetAddress, + String zipCode) { + this.countryId = countryId; + this.countryName = countryId; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityId; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public FullAddress(String countryId, String countryName, String provinceName, String cityId, + String cityName, String streetAddress, String zipCode) { + this.countryId = countryId; + this.countryName = countryName; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityName; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public String getCountryId() { + return countryId; + } + + public void setCountryId(String countryId) { + this.countryId = countryId; + } + + public String getCountryName() { + return countryName; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + + public String getProvinceName() { + return provinceName; + } + + public void setProvinceName(String provinceName) { + this.provinceName = provinceName; + } + + public String getCityId() { + return cityId; + } + + public void setCityId(String cityId) { + this.cityId = cityId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getStreetAddress() { + return streetAddress; + } + + public void setStreetAddress(String streetAddress) { + this.streetAddress = streetAddress; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((cityId == null) ? 0 : cityId.hashCode()); + result = prime * result + ((cityName == null) ? 0 : cityName.hashCode()); + result = prime * result + ((countryId == null) ? 0 : countryId.hashCode()); + result = prime * result + ((countryName == null) ? 0 : countryName.hashCode()); + result = prime * result + ((provinceName == null) ? 0 : provinceName.hashCode()); + result = prime * result + ((streetAddress == null) ? 0 : streetAddress.hashCode()); + result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FullAddress other = (FullAddress) obj; + if (cityId == null) { + if (other.cityId != null) + return false; + } else if (!cityId.equals(other.cityId)) + return false; + if (cityName == null) { + if (other.cityName != null) + return false; + } else if (!cityName.equals(other.cityName)) + return false; + if (countryId == null) { + if (other.countryId != null) + return false; + } else if (!countryId.equals(other.countryId)) + return false; + if (countryName == null) { + if (other.countryName != null) + return false; + } else if (!countryName.equals(other.countryName)) + return false; + if (provinceName == null) { + if (other.provinceName != null) + return false; + } else if (!provinceName.equals(other.provinceName)) + return false; + if (streetAddress == null) { + if (other.streetAddress != null) + return false; + } else if (!streetAddress.equals(other.streetAddress)) + return false; + if (zipCode == null) { + if (other.zipCode != null) + return false; + } else if (!zipCode.equals(other.zipCode)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (countryName != null && countryName.length() > 0) { + sb.append(countryName); + } + if (provinceName != null && provinceName.length() > 0) { + sb.append(" "); + sb.append(provinceName); + } + if (cityName != null && cityName.length() > 0) { + sb.append(" "); + sb.append(cityName); + } + if (streetAddress != null && streetAddress.length() > 0) { + sb.append(" "); + sb.append(streetAddress); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java new file mode 100644 index 00000000000..f1ff041e939 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java @@ -0,0 +1,206 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; +import java.util.List; + +public class PersonInfo implements Serializable { + private static final long serialVersionUID = 7443011149612231882L; + + List phones; + + Phone fax; + + FullAddress fullAddress; + + String mobileNo; + + String name; + + boolean male; + + boolean female; + + String department; + + String jobTitle; + + String homepageUrl; + + public List getPhones() { + return phones; + } + + public void setPhones(List phones) { + this.phones = phones; + } + + public boolean isMale() { + return male; + } + + public void setMale(boolean male) { + this.male = male; + } + + public boolean isFemale() { + return female; + } + + public void setFemale(boolean female) { + this.female = female; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public Phone getFax() { + return fax; + } + + public void setFax(Phone fax) { + this.fax = fax; + } + + public FullAddress getFullAddress() { + return fullAddress; + } + + public void setFullAddress(FullAddress fullAddress) { + this.fullAddress = fullAddress; + } + + public String getHomepageUrl() { + return homepageUrl; + } + + public void setHomepageUrl(String homepageUrl) { + this.homepageUrl = homepageUrl; + } + + public String getJobTitle() { + return jobTitle; + } + + public void setJobTitle(String jobTitle) { + this.jobTitle = jobTitle; + } + + public String getMobileNo() { + return mobileNo; + } + + public void setMobileNo(String mobileNo) { + this.mobileNo = mobileNo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((department == null) ? 0 : department.hashCode()); + result = prime * result + ((fax == null) ? 0 : fax.hashCode()); + result = prime * result + (female ? 1231 : 1237); + result = prime * result + ((fullAddress == null) ? 0 : fullAddress.hashCode()); + result = prime * result + ((homepageUrl == null) ? 0 : homepageUrl.hashCode()); + result = prime * result + ((jobTitle == null) ? 0 : jobTitle.hashCode()); + result = prime * result + (male ? 1231 : 1237); + result = prime * result + ((mobileNo == null) ? 0 : mobileNo.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((phones == null) ? 0 : phones.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PersonInfo other = (PersonInfo) obj; + if (department == null) { + if (other.department != null) + return false; + } else if (!department.equals(other.department)) + return false; + if (fax == null) { + if (other.fax != null) + return false; + } else if (!fax.equals(other.fax)) + return false; + if (female != other.female) + return false; + if (fullAddress == null) { + if (other.fullAddress != null) + return false; + } else if (!fullAddress.equals(other.fullAddress)) + return false; + if (homepageUrl == null) { + if (other.homepageUrl != null) + return false; + } else if (!homepageUrl.equals(other.homepageUrl)) + return false; + if (jobTitle == null) { + if (other.jobTitle != null) + return false; + } else if (!jobTitle.equals(other.jobTitle)) + return false; + if (male != other.male) + return false; + if (mobileNo == null) { + if (other.mobileNo != null) + return false; + } else if (!mobileNo.equals(other.mobileNo)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (phones == null) { + if (other.phones != null) + return false; + } else if (!phones.equals(other.phones)) + return false; + return true; + } + + @Override + public String toString() { + return "PersonInfo [phones=" + phones + ", fax=" + fax + ", fullAddress=" + fullAddress + + ", mobileNo=" + mobileNo + ", name=" + name + ", male=" + male + ", female=" + + female + ", department=" + department + ", jobTitle=" + jobTitle + + ", homepageUrl=" + homepageUrl + "]"; + } + +} diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java new file mode 100644 index 00000000000..66b67c96340 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java @@ -0,0 +1,22 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +public enum PersonStatus { + ENABLED, + DISABLED +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java new file mode 100644 index 00000000000..f7da708364c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java @@ -0,0 +1,139 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class Phone implements Serializable { + + private static final long serialVersionUID = 4399060521859707703L; + + private String country; + + private String area; + + private String number; + + private String extensionNumber; + + public Phone() { + } + + public Phone(String country, String area, String number, String extensionNumber) { + this.country = country; + this.area = area; + this.number = number; + this.extensionNumber = extensionNumber; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getExtensionNumber() { + return extensionNumber; + } + + public void setExtensionNumber(String extensionNumber) { + this.extensionNumber = extensionNumber; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((area == null) ? 0 : area.hashCode()); + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((extensionNumber == null) ? 0 : extensionNumber.hashCode()); + result = prime * result + ((number == null) ? 0 : number.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Phone other = (Phone) obj; + if (area == null) { + if (other.area != null) + return false; + } else if (!area.equals(other.area)) + return false; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (extensionNumber == null) { + if (other.extensionNumber != null) + return false; + } else if (!extensionNumber.equals(other.extensionNumber)) + return false; + if (number == null) { + if (other.number != null) + return false; + } else if (!number.equals(other.number)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (country != null && country.length() > 0) { + sb.append(country); + sb.append("-"); + } + if (area != null && area.length() > 0) { + sb.append(area); + sb.append("-"); + } + if (number != null && number.length() > 0) { + sb.append(number); + } + if (extensionNumber != null && extensionNumber.length() > 0) { + sb.append("-"); + sb.append(extensionNumber); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java new file mode 100644 index 00000000000..68060336da6 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java @@ -0,0 +1,137 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.model.Person; +import com.alibaba.dubbo.common.serialize.ObjectOutput; + +import org.junit.Test; + +import java.io.NotSerializableException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; + +public abstract class AbstractSerializationPersionFailTest extends AbstractSerializationTest { + @Test + public void test_Person() throws Exception { + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(new Person()); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonList() throws Exception { + List args = new ArrayList(); + args.add(new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonSet() throws Exception { + Set args = new HashSet(); + args.add(new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_IntPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_StringPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_StringPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new Person()); + args.put("1", sublist); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new Person()); + args.add(sublist); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java new file mode 100644 index 00000000000..32ff6e4856c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java @@ -0,0 +1,93 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.model.Person; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public abstract class AbstractSerializationPersionOkTest extends AbstractSerializationTest { + @Test + public void test_Person() throws Exception { + assertObject(new Person()); + } + + @Test + public void test_Person_withType() throws Exception { + assertObjectWithType(new Person(), Person.class); + } + + @Test + public void test_PersonList() throws Exception { + List args = new ArrayList(); + args.add(new Person()); + + assertObject(args); + } + + @Test + public void test_PersonSet() throws Exception { + Set args = new HashSet(); + args.add(new Person()); + + assertObject(args); + } + + @Test + public void test_IntPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new Person()); + + assertObject(args); + } + + @Test + public void test_StringPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new Person()); + + assertObject(args); + } + + @Test + public void test_StringPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new Person()); + args.put("1", sublist); + + assertObject(args); + } + + @Test + public void test_PersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new Person()); + args.add(sublist); + + assertObject(args); + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java new file mode 100644 index 00000000000..3219603ed69 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java @@ -0,0 +1,1210 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.model.AnimalEnum; +import com.alibaba.dubbo.common.model.BizException; +import com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor; +import com.alibaba.dubbo.common.model.SerializablePerson; +import com.alibaba.dubbo.common.model.media.Image; +import com.alibaba.dubbo.common.model.media.Image.Size; +import com.alibaba.dubbo.common.model.media.Media; +import com.alibaba.dubbo.common.model.media.Media.Player; +import com.alibaba.dubbo.common.model.media.MediaContent; +import com.alibaba.dubbo.common.model.person.BigPerson; +import com.alibaba.dubbo.common.model.person.FullAddress; +import com.alibaba.dubbo.common.model.person.PersonInfo; +import com.alibaba.dubbo.common.model.person.PersonStatus; +import com.alibaba.dubbo.common.model.person.Phone; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Time; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public abstract class AbstractSerializationTest { + static Random random = new Random(); + Serialization serialization; + URL url = new URL("protocl", "1.1.1.1", 1234); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + // ================ Primitive Type ================ + BigPerson bigPerson; + MediaContent mediaContent; + + { + bigPerson = new BigPerson(); + bigPerson.setPersonId("superman111"); + bigPerson.setLoginName("superman"); + bigPerson.setStatus(PersonStatus.ENABLED); + bigPerson.setEmail("sm@1.com"); + bigPerson.setPenName("pname"); + + ArrayList phones = new ArrayList(); + Phone phone1 = new Phone("86", "0571", "87654321", "001"); + Phone phone2 = new Phone("86", "0571", "87654322", "002"); + phones.add(phone1); + phones.add(phone2); + + PersonInfo pi = new PersonInfo(); + pi.setPhones(phones); + Phone fax = new Phone("86", "0571", "87654321", null); + pi.setFax(fax); + FullAddress addr = new FullAddress("CN", "zj", "3480", "wensanlu", "315000"); + pi.setFullAddress(addr); + pi.setMobileNo("13584652131"); + pi.setMale(true); + pi.setDepartment("b2b"); + pi.setHomepageUrl("www.capcom.com"); + pi.setJobTitle("qa"); + pi.setName("superman"); + + bigPerson.setInfoProfile(pi); + } + + { + Media media = new Media(); + media.setUri("uri://中华人民共和国"); + media.setTitle("title"); + media.setWidth(1239); + media.setHeight(1938); + media.setFormat("format-xxxx"); + media.setDuration(93419235); + media.setSize(3477897); + media.setBitrate(94523); + List persons = new ArrayList(); + persons.add("jerry"); + persons.add("tom"); + persons.add("lucy"); + media.setPersons(persons); + media.setCopyright("1999-2011"); + media.setPlayer(Player.FLASH); + + List images = new ArrayList(); + for (int i = 0; i < 10; ++i) { + Image image = new Image(); + image.setUri("url" + i); + if (i % 2 == 0) image.setTitle("title" + i); + image.setWidth(34 + i); + image.setHeight(2323 + i); + image.setSize((i % 2 == 0) ? Size.SMALL : Size.LARGE); + + images.add(image); + } + + mediaContent = new MediaContent(media, images); + } + + @Test + public void test_Bool() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeBool(false); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertFalse(deserialize.readBool()); + + try { + deserialize.readBool(); + fail(); + } catch (IOException expected) { + } + } + + @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 (IOException expected) { + } + } + + @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 (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_Short() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeShort((short) 123); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals((short) 123, deserialize.readShort()); + + try { + deserialize.readShort(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_Integer() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeInt(1); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + int i = deserialize.readInt(); + assertEquals(1, i); + + try { + deserialize.readInt(); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_Float() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeFloat(1.28F); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(1.28F == deserialize.readFloat()); + + try { + deserialize.readFloat(); + fail(); + } catch (IOException expected) { + } + } + + // ================== 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); + + assertTrue(1.28 == deserialize.readDouble()); + + try { + deserialize.readDouble(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_UtfString() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeUTF("123中华人民共和国"); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals("123中华人民共和国", deserialize.readUTF()); + + try { + deserialize.readUTF(); + fail(); + } catch (IOException expected) { + } + } + + @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()); + + try { + deserialize.readBytes(); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + // ================ Array Type ================ + + void assertObjectArray(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject())); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + void assertObjectArrayWithType(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject(clazz))); + + try { + deserialize.readObject(clazz); + fail(); + } catch (IOException expected) { + } + } + + @SuppressWarnings("unchecked") + 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()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + void assertObjectWithType(T data, Class clazz) 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(clazz)); + + try { + deserialize.readObject(clazz); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_boolArray() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject())); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_boolArray_withType() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject(boolean[].class))); + + try { + deserialize.readObject(boolean[].class); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_charArray_withType() 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(char[].class)); + + try { + deserialize.readObject(char[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray_withType() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject(short[].class)); + + try { + deserialize.readObject(short[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_intArray() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_intArray_withType() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject(int[].class)); + + try { + deserialize.readObject(int[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray_withType() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject(long[].class)); + + try { + deserialize.readObject(long[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(), 0.0001F); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray_withType() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(float[].class), 0.0001F); + + try { + deserialize.readObject(float[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(), 0.0001); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray_withType() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(double[].class), 0.0001); + + try { + deserialize.readObject(double[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_StringArray() throws Exception { + assertObjectArray(new String[]{"1", "b"}, String[].class); + } + + @Test + public void test_StringArray_withType() throws Exception { + assertObjectArrayWithType(new String[]{"1", "b"}, String[].class); + } + + // ================ Simple Type ================ + + @Test + public void test_IntegerArray() throws Exception { + assertObjectArray(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_IntegerArray_withType() throws Exception { + assertObjectArrayWithType(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_EnumArray() throws Exception { + assertObjectArray(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_EnumArray_withType() throws Exception { + assertObjectArrayWithType(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_SPerson() throws Exception { + assertObject(new SerializablePerson()); + } + + @Test + public void test_SPerson_withType() throws Exception { + assertObjectWithType(new SerializablePerson(), SerializablePerson.class); + } + + @Test + public void test_BizException() 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(); + assertEquals("Hello", ((BizException) read).getMessage()); + } + + @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()); + } + + @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()); + } + + @Test + public void test_BizExceptionNoDefaultConstructor_WithType() 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(BizExceptionNoDefaultConstructor.class); + assertEquals("Hello", ((BizExceptionNoDefaultConstructor) read).getMessage()); + } + + @Test + public void test_enum() throws Exception { + assertObject(AnimalEnum.dog); + } + + @Test + public void test_enum_withType() throws Exception { + assertObjectWithType(AnimalEnum.dog, AnimalEnum.class); + } + + @Test + public void test_Date() throws Exception { + assertObject(new Date()); + } + + @Test + public void test_Date_withType() throws Exception { + assertObjectWithType(new Date(), Date.class); + } + + @Test + public void test_Time() throws Exception { + assertObject(new Time(System.currentTimeMillis())); + } + + @Test + public void test_Time_withType() throws Exception { + assertObjectWithType(new Time(System.currentTimeMillis()), Time.class); + } + + @Test + public void test_ByteWrap() throws Exception { + assertObject(new Byte((byte) 12)); + } + + @Test + public void test_ByteWrap_withType() throws Exception { + assertObjectWithType(new Byte((byte) 12), Byte.class); + } + + @Test + public void test_LongWrap() throws Exception { + assertObject(new Long(12)); + } + + @Test + public void test_LongWrap_withType() throws Exception { + assertObjectWithType(new Long(12), Long.class); + } + + @Test + public void test_BigInteger() throws Exception { + assertObject(new BigInteger("23423434234234234")); + } + + @Test + public void test_BigInteger_withType() throws Exception { + assertObjectWithType(new BigInteger("23423434234234234"), BigInteger.class); + } + + @Test + public void test_BigDecimal() throws Exception { + assertObject(new BigDecimal("23423434234234234.341274832341234235")); + } + + @Test + public void test_BigDecimal_withType() throws Exception { + assertObjectWithType(new BigDecimal("23423434234234234.341274832341234235"), BigDecimal.class); + } + + @Test + public void test_StringList_asListReturn() throws Exception { + List args = Arrays.asList(new String[]{"1", "b"}); + + assertObject(args); + } + + @Test + public void test_StringArrayList() throws Exception { + List args = new ArrayList(Arrays.asList(new String[]{"1", "b"})); + + assertObject(args); + } + + @Test + public void test_StringSet() throws Exception { + Set args = new HashSet(); + args.add("1"); + + assertObject(args); + } + + @Test + public void test_LinkedHashMap() throws Exception { + LinkedHashMap data = new LinkedHashMap(); + 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) read).entrySet().iterator().next().getKey(); + assertEquals("1", key1); + + assertEquals(data, read); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + // ================ Complex Collection Type ================ + + @Test + public void test_SPersonList() throws Exception { + List args = new ArrayList(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_SPersonSet() throws Exception { + Set args = new HashSet(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + // ================ complex POJO ============= + + @Test + public void test_IntSPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.put("1", sublist); + + assertObject(args); + } + + @Test + public void test_SPersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.add(sublist); + + assertObject(args); + } + + @Test + public void test_BigPerson() throws Exception { + assertObject(bigPerson); + } + + @Test + public void test_BigPerson_WithType() throws Exception { + assertObjectWithType(bigPerson, BigPerson.class); + } + + @Test + public void test_MediaContent() throws Exception { + assertObject(mediaContent); + } + + @Test + public void test_MediaContent_WithType() throws Exception { + assertObjectWithType(mediaContent, MediaContent.class); + } + + @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) { + } + } + + @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) { + } + } + + + // abnormal case + + @Test + public void test_MediaContent_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(); + fail(); + } catch (IOException expected) { + System.out.println(expected); + } + } + + @Test + public void test_MediaContent_WithType_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(MediaContent.class); + fail(); + } catch (IOException expected) { + System.out.println(expected); + } + } + + + @Test(timeout = 3000) + public void test_LoopReference() throws Exception { + Map map = new HashMap(); + 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 output = (Map) deserialize.readObject(); + + assertEquals("v1", output.get("k1")); + assertSame(output, output.get("self")); + } + + // ================ 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) { + } + } +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/Hessian2SerializationTest.java b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/Hessian2SerializationTest.java similarity index 98% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/Hessian2SerializationTest.java rename to dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/Hessian2SerializationTest.java index 78c78b7ba05..4e57f3fb2a2 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/Hessian2SerializationTest.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/java/com/alibaba/dubbo/common/serialize/serialization/Hessian2SerializationTest.java @@ -18,7 +18,7 @@ import com.alibaba.dubbo.common.serialize.ObjectInput; import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.support.hessian.Hessian2Serialization; +import com.alibaba.dubbo.serialize.hessian2.Hessian2Serialization; import org.junit.Ignore; import org.junit.Test; diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc b/dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc new file mode 100644 index 00000000000..c8c083c17eb --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc @@ -0,0 +1,2 @@ +a,d,e,b,c +str3,str2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/log4j.xml b/dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/log4j.xml new file mode 100644 index 00000000000..671f2af8839 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/test/resources/log4j.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/pom.xml b/dubbo-serialization/dubbo-serialization-jdk/pom.xml new file mode 100644 index 00000000000..91c5e6481a8 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/pom.xml @@ -0,0 +1,45 @@ + + + + 4.0.0 + + com.alibaba + dubbo-serialization + 2.6.1 + + dubbo-serialization-jdk + jar + ${project.artifactId} + The common module of dubbo project + + true + + + + com.alibaba + dubbo-serialization-api + ${project.parent.version} + + + com.alibaba + hessian-lite + 3.2.1-fixed-2 + + + \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedJavaSerialization.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedJavaSerialization.java similarity index 93% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedJavaSerialization.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedJavaSerialization.java index 04da07a871e..fbbc5f71d99 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedJavaSerialization.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedJavaSerialization.java @@ -1,46 +1,46 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.java; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.Serialization; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class CompactedJavaSerialization implements Serialization { - - public byte getContentTypeId() { - return 4; - } - - public String getContentType() { - return "x-application/compactedjava"; - } - - public ObjectOutput serialize(URL url, OutputStream out) throws IOException { - return new JavaObjectOutput(out, true); - } - - public ObjectInput deserialize(URL url, InputStream is) throws IOException { - return new JavaObjectInput(is, true); - } - +/* + * 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 com.alibaba.dubbo.serialize.java; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class CompactedJavaSerialization implements Serialization { + + public byte getContentTypeId() { + return 4; + } + + public String getContentType() { + return "x-application/compactedjava"; + } + + public ObjectOutput serialize(URL url, OutputStream out) throws IOException { + return new JavaObjectOutput(out, true); + } + + public ObjectInput deserialize(URL url, InputStream is) throws IOException { + return new JavaObjectInput(is, true); + } + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedObjectInputStream.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedObjectInputStream.java similarity index 97% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedObjectInputStream.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedObjectInputStream.java index 0a971936c1c..8e02907f261 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedObjectInputStream.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedObjectInputStream.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.java; +package com.alibaba.dubbo.serialize.java; import com.alibaba.dubbo.common.utils.ClassHelper; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedObjectOutputStream.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedObjectOutputStream.java similarity index 96% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedObjectOutputStream.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedObjectOutputStream.java index 122d5a2c977..9a49f5a2ccd 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/CompactedObjectOutputStream.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/CompactedObjectOutputStream.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.java; +package com.alibaba.dubbo.serialize.java; import java.io.IOException; import java.io.ObjectOutputStream; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaObjectInput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaObjectInput.java similarity index 94% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaObjectInput.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaObjectInput.java index 8cfcf4b657c..09c8e0cf629 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaObjectInput.java @@ -14,9 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.java; +package com.alibaba.dubbo.serialize.java; -import com.alibaba.dubbo.common.serialize.support.nativejava.NativeJavaObjectInput; +import com.alibaba.dubbo.serialize.nativejava.NativeJavaObjectInput; import java.io.IOException; import java.io.InputStream; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaObjectOutput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaObjectOutput.java similarity index 93% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaObjectOutput.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaObjectOutput.java index daadbe5dd62..07f6d530bad 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaObjectOutput.java @@ -14,9 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.java; +package com.alibaba.dubbo.serialize.java; -import com.alibaba.dubbo.common.serialize.support.nativejava.NativeJavaObjectOutput; + +import com.alibaba.dubbo.serialize.nativejava.NativeJavaObjectOutput; import java.io.IOException; import java.io.ObjectOutputStream; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaSerialization.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaSerialization.java similarity index 93% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaSerialization.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaSerialization.java index c4206b52156..4ce7ad3e2df 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/java/JavaSerialization.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/java/JavaSerialization.java @@ -1,46 +1,46 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.support.java; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.Serialization; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class JavaSerialization implements Serialization { - - public byte getContentTypeId() { - return 3; - } - - public String getContentType() { - return "x-application/java"; - } - - public ObjectOutput serialize(URL url, OutputStream out) throws IOException { - return new JavaObjectOutput(out); - } - - public ObjectInput deserialize(URL url, InputStream is) throws IOException { - return new JavaObjectInput(is); - } - +/* + * 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 com.alibaba.dubbo.serialize.java; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class JavaSerialization implements Serialization { + + public byte getContentTypeId() { + return 3; + } + + public String getContentType() { + return "x-application/java"; + } + + public ObjectOutput serialize(URL url, OutputStream out) throws IOException { + return new JavaObjectOutput(out); + } + + public ObjectInput deserialize(URL url, InputStream is) throws IOException { + return new JavaObjectInput(is); + } + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaObjectInput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaObjectInput.java similarity index 97% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaObjectInput.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaObjectInput.java index 28354576ce8..ae19288a548 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaObjectInput.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.nativejava; +package com.alibaba.dubbo.serialize.nativejava; import com.alibaba.dubbo.common.serialize.ObjectInput; import com.alibaba.dubbo.common.utils.Assert; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaObjectOutput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaObjectOutput.java similarity index 97% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaObjectOutput.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaObjectOutput.java index d686d1df2cb..a0b8fef6cd2 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaObjectOutput.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.nativejava; +package com.alibaba.dubbo.serialize.nativejava; import com.alibaba.dubbo.common.serialize.ObjectOutput; import com.alibaba.dubbo.common.utils.Assert; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaSerialization.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaSerialization.java similarity index 96% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaSerialization.java rename to dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaSerialization.java index ec337cb4a7c..81d28421d1f 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/nativejava/NativeJavaSerialization.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/com/alibaba/dubbo/serialize/nativejava/NativeJavaSerialization.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.nativejava; +package com.alibaba.dubbo.serialize.nativejava; import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.common.serialize.ObjectInput; diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization b/dubbo-serialization/dubbo-serialization-jdk/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization new file mode 100644 index 00000000000..f429b0f775c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization @@ -0,0 +1,3 @@ +java=com.alibaba.dubbo.serialize.java.JavaSerialization +compactedjava=com.alibaba.dubbo.serialize.java.CompactedJavaSerialization +nativejava=com.alibaba.dubbo.serialize.nativejava.NativeJavaSerialization \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java new file mode 100644 index 00000000000..f920a0c6575 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java @@ -0,0 +1,21 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public enum AnimalEnum { + dog, cat, rat, cow, bull, horse; +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizException.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizException.java new file mode 100644 index 00000000000..b6eb1f4bbce --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizException.java @@ -0,0 +1,29 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public class BizException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public BizException(String message) { + super(message); + } + + public BizException() { + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java new file mode 100644 index 00000000000..29db6e00fc9 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java @@ -0,0 +1,26 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public class BizExceptionNoDefaultConstructor extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public BizExceptionNoDefaultConstructor(String message) { + super(message); + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/Person.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/Person.java new file mode 100644 index 00000000000..6383706cf79 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/Person.java @@ -0,0 +1,95 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.util.Arrays; + +public class Person { + 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java new file mode 100644 index 00000000000..e7dd2486786 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java @@ -0,0 +1,97 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.io.Serializable; +import java.util.Arrays; + +public class SerializablePerson implements Serializable { + private static final long serialVersionUID = 1L; + 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; + SerializablePerson other = (SerializablePerson) 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Image.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Image.java new file mode 100644 index 00000000000..0d6a74a3d4a --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Image.java @@ -0,0 +1,120 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + + +public class Image implements java.io.Serializable { + private static final long serialVersionUID = 1L; + public String uri; + public String title; // Can be null + public int width; + public int height; + public Size size; + + public Image() { + } + + public Image(String uri, String title, int width, int height, Size size) { + this.height = height; + this.title = title; + this.uri = uri; + this.width = width; + this.size = size; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Image image = (Image) o; + + if (height != image.height) return false; + if (width != image.width) return false; + if (size != image.size) return false; + if (title != null ? !title.equals(image.title) : image.title != null) return false; + if (uri != null ? !uri.equals(image.uri) : image.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (size != null ? size.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Image "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", size=").append(size); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + public enum Size { + SMALL, LARGE + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Media.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Media.java new file mode 100644 index 00000000000..8a78f6d313b --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/Media.java @@ -0,0 +1,205 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class Media implements java.io.Serializable { + public String uri; + public String title; // Can be unset. + public int width; + public int height; + public String format; + public long duration; + public long size; + public int bitrate; // Can be unset. + public boolean hasBitrate; + public List persons; + public Player player; + public String copyright; // Can be unset. + + public Media() { + } + + public Media(String uri, String title, int width, int height, String format, long duration, long size, int bitrate, boolean hasBitrate, List persons, Player player, String copyright) { + this.uri = uri; + this.title = title; + this.width = width; + this.height = height; + this.format = format; + this.duration = duration; + this.size = size; + this.bitrate = bitrate; + this.hasBitrate = hasBitrate; + this.persons = persons; + this.player = player; + this.copyright = copyright; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Media media = (Media) o; + + if (bitrate != media.bitrate) return false; + if (duration != media.duration) return false; + if (hasBitrate != media.hasBitrate) return false; + if (height != media.height) return false; + if (size != media.size) return false; + if (width != media.width) return false; + if (copyright != null ? !copyright.equals(media.copyright) : media.copyright != null) return false; + if (format != null ? !format.equals(media.format) : media.format != null) return false; + if (persons != null ? !persons.equals(media.persons) : media.persons != null) return false; + if (player != media.player) return false; + if (title != null ? !title.equals(media.title) : media.title != null) return false; + if (uri != null ? !uri.equals(media.uri) : media.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (format != null ? format.hashCode() : 0); + result = 31 * result + (int) (duration ^ (duration >>> 32)); + result = 31 * result + (int) (size ^ (size >>> 32)); + result = 31 * result + bitrate; + result = 31 * result + (hasBitrate ? 1 : 0); + result = 31 * result + (persons != null ? persons.hashCode() : 0); + result = 31 * result + (player != null ? player.hashCode() : 0); + result = 31 * result + (copyright != null ? copyright.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Media "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", format=").append(format); + sb.append(", duration=").append(duration); + sb.append(", size=").append(size); + sb.append(", hasBitrate=").append(hasBitrate); + sb.append(", bitrate=").append(String.valueOf(bitrate)); + sb.append(", persons=").append(persons); + sb.append(", player=").append(player); + sb.append(", copyright=").append(copyright); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public int getBitrate() { + return bitrate; + } + + public void setBitrate(int bitrate) { + this.bitrate = bitrate; + this.hasBitrate = true; + } + + public List getPersons() { + return persons; + } + + public void setPersons(List persons) { + this.persons = persons; + } + + public Player getPlayer() { + return player; + } + + public void setPlayer(Player player) { + this.player = player; + } + + public String getCopyright() { + return copyright; + } + + public void setCopyright(String copyright) { + this.copyright = copyright; + } + + public enum Player { + JAVA, FLASH + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java new file mode 100644 index 00000000000..9664116a635 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java @@ -0,0 +1,78 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class MediaContent implements java.io.Serializable { + public Media media; + public List images; + + public MediaContent() { + } + + public MediaContent(Media media, List images) { + this.media = media; + this.images = images; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MediaContent that = (MediaContent) o; + + if (images != null ? !images.equals(that.images) : that.images != null) return false; + if (media != null ? !media.equals(that.media) : that.media != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = media != null ? media.hashCode() : 0; + result = 31 * result + (images != null ? images.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[MediaContent: "); + sb.append("media=").append(media); + sb.append(", images=").append(images); + sb.append("]"); + return sb.toString(); + } + + public Media getMedia() { + return media; + } + + public void setMedia(Media media) { + this.media = media; + } + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java new file mode 100644 index 00000000000..70bc793979e --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java @@ -0,0 +1,151 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class BigPerson implements Serializable { + private static final long serialVersionUID = 1L; + + String personId; + + String loginName; + + PersonStatus status; + + String email; + + String penName; + + PersonInfo infoProfile; + + public BigPerson() { + + } + + public BigPerson(String id) { + this.personId = id; + } + + public String getPersonId() { + return personId; + } + + public void setPersonId(String personId) { + this.personId = personId; + } + + public PersonInfo getInfoProfile() { + return infoProfile; + } + + public void setInfoProfile(PersonInfo infoProfile) { + this.infoProfile = infoProfile; + } + + public String getEmail() { + return this.email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getLoginName() { + return this.loginName; + } + + public void setLoginName(String loginName) { + this.loginName = loginName; + } + + public PersonStatus getStatus() { + return this.status; + } + + public void setStatus(PersonStatus status) { + this.status = status; + } + + public String getPenName() { + return penName; + } + + public void setPenName(String penName) { + this.penName = penName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + ((infoProfile == null) ? 0 : infoProfile.hashCode()); + result = prime * result + ((loginName == null) ? 0 : loginName.hashCode()); + result = prime * result + ((penName == null) ? 0 : penName.hashCode()); + result = prime * result + ((personId == null) ? 0 : personId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BigPerson other = (BigPerson) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + if (infoProfile == null) { + if (other.infoProfile != null) + return false; + } else if (!infoProfile.equals(other.infoProfile)) + return false; + if (loginName == null) { + if (other.loginName != null) + return false; + } else if (!loginName.equals(other.loginName)) + return false; + if (penName == null) { + if (other.penName != null) + return false; + } else if (!penName.equals(other.penName)) + return false; + if (personId == null) { + if (other.personId != null) + return false; + } else if (!personId.equals(other.personId)) + return false; + if (status != other.status) + return false; + return true; + } + + @Override + public String toString() { + return "BigPerson [personId=" + personId + ", loginName=" + loginName + ", status=" + + status + ", email=" + email + ", penName=" + penName + ", infoProfile=" + + infoProfile + "]"; + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java new file mode 100644 index 00000000000..dcc93cf8bd3 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java @@ -0,0 +1,202 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class FullAddress implements Serializable { + + private static final long serialVersionUID = 5163979984269419831L; + + private String countryId; + + private String countryName; + + private String provinceName; + + private String cityId; + + private String cityName; + + private String streetAddress; + + private String zipCode; + + public FullAddress() { + } + + public FullAddress(String countryId, String provinceName, String cityId, String streetAddress, + String zipCode) { + this.countryId = countryId; + this.countryName = countryId; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityId; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public FullAddress(String countryId, String countryName, String provinceName, String cityId, + String cityName, String streetAddress, String zipCode) { + this.countryId = countryId; + this.countryName = countryName; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityName; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public String getCountryId() { + return countryId; + } + + public void setCountryId(String countryId) { + this.countryId = countryId; + } + + public String getCountryName() { + return countryName; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + + public String getProvinceName() { + return provinceName; + } + + public void setProvinceName(String provinceName) { + this.provinceName = provinceName; + } + + public String getCityId() { + return cityId; + } + + public void setCityId(String cityId) { + this.cityId = cityId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getStreetAddress() { + return streetAddress; + } + + public void setStreetAddress(String streetAddress) { + this.streetAddress = streetAddress; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((cityId == null) ? 0 : cityId.hashCode()); + result = prime * result + ((cityName == null) ? 0 : cityName.hashCode()); + result = prime * result + ((countryId == null) ? 0 : countryId.hashCode()); + result = prime * result + ((countryName == null) ? 0 : countryName.hashCode()); + result = prime * result + ((provinceName == null) ? 0 : provinceName.hashCode()); + result = prime * result + ((streetAddress == null) ? 0 : streetAddress.hashCode()); + result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FullAddress other = (FullAddress) obj; + if (cityId == null) { + if (other.cityId != null) + return false; + } else if (!cityId.equals(other.cityId)) + return false; + if (cityName == null) { + if (other.cityName != null) + return false; + } else if (!cityName.equals(other.cityName)) + return false; + if (countryId == null) { + if (other.countryId != null) + return false; + } else if (!countryId.equals(other.countryId)) + return false; + if (countryName == null) { + if (other.countryName != null) + return false; + } else if (!countryName.equals(other.countryName)) + return false; + if (provinceName == null) { + if (other.provinceName != null) + return false; + } else if (!provinceName.equals(other.provinceName)) + return false; + if (streetAddress == null) { + if (other.streetAddress != null) + return false; + } else if (!streetAddress.equals(other.streetAddress)) + return false; + if (zipCode == null) { + if (other.zipCode != null) + return false; + } else if (!zipCode.equals(other.zipCode)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (countryName != null && countryName.length() > 0) { + sb.append(countryName); + } + if (provinceName != null && provinceName.length() > 0) { + sb.append(" "); + sb.append(provinceName); + } + if (cityName != null && cityName.length() > 0) { + sb.append(" "); + sb.append(cityName); + } + if (streetAddress != null && streetAddress.length() > 0) { + sb.append(" "); + sb.append(streetAddress); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java new file mode 100644 index 00000000000..f1ff041e939 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java @@ -0,0 +1,206 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; +import java.util.List; + +public class PersonInfo implements Serializable { + private static final long serialVersionUID = 7443011149612231882L; + + List phones; + + Phone fax; + + FullAddress fullAddress; + + String mobileNo; + + String name; + + boolean male; + + boolean female; + + String department; + + String jobTitle; + + String homepageUrl; + + public List getPhones() { + return phones; + } + + public void setPhones(List phones) { + this.phones = phones; + } + + public boolean isMale() { + return male; + } + + public void setMale(boolean male) { + this.male = male; + } + + public boolean isFemale() { + return female; + } + + public void setFemale(boolean female) { + this.female = female; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public Phone getFax() { + return fax; + } + + public void setFax(Phone fax) { + this.fax = fax; + } + + public FullAddress getFullAddress() { + return fullAddress; + } + + public void setFullAddress(FullAddress fullAddress) { + this.fullAddress = fullAddress; + } + + public String getHomepageUrl() { + return homepageUrl; + } + + public void setHomepageUrl(String homepageUrl) { + this.homepageUrl = homepageUrl; + } + + public String getJobTitle() { + return jobTitle; + } + + public void setJobTitle(String jobTitle) { + this.jobTitle = jobTitle; + } + + public String getMobileNo() { + return mobileNo; + } + + public void setMobileNo(String mobileNo) { + this.mobileNo = mobileNo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((department == null) ? 0 : department.hashCode()); + result = prime * result + ((fax == null) ? 0 : fax.hashCode()); + result = prime * result + (female ? 1231 : 1237); + result = prime * result + ((fullAddress == null) ? 0 : fullAddress.hashCode()); + result = prime * result + ((homepageUrl == null) ? 0 : homepageUrl.hashCode()); + result = prime * result + ((jobTitle == null) ? 0 : jobTitle.hashCode()); + result = prime * result + (male ? 1231 : 1237); + result = prime * result + ((mobileNo == null) ? 0 : mobileNo.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((phones == null) ? 0 : phones.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PersonInfo other = (PersonInfo) obj; + if (department == null) { + if (other.department != null) + return false; + } else if (!department.equals(other.department)) + return false; + if (fax == null) { + if (other.fax != null) + return false; + } else if (!fax.equals(other.fax)) + return false; + if (female != other.female) + return false; + if (fullAddress == null) { + if (other.fullAddress != null) + return false; + } else if (!fullAddress.equals(other.fullAddress)) + return false; + if (homepageUrl == null) { + if (other.homepageUrl != null) + return false; + } else if (!homepageUrl.equals(other.homepageUrl)) + return false; + if (jobTitle == null) { + if (other.jobTitle != null) + return false; + } else if (!jobTitle.equals(other.jobTitle)) + return false; + if (male != other.male) + return false; + if (mobileNo == null) { + if (other.mobileNo != null) + return false; + } else if (!mobileNo.equals(other.mobileNo)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (phones == null) { + if (other.phones != null) + return false; + } else if (!phones.equals(other.phones)) + return false; + return true; + } + + @Override + public String toString() { + return "PersonInfo [phones=" + phones + ", fax=" + fax + ", fullAddress=" + fullAddress + + ", mobileNo=" + mobileNo + ", name=" + name + ", male=" + male + ", female=" + + female + ", department=" + department + ", jobTitle=" + jobTitle + + ", homepageUrl=" + homepageUrl + "]"; + } + +} diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java new file mode 100644 index 00000000000..66b67c96340 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java @@ -0,0 +1,22 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +public enum PersonStatus { + ENABLED, + DISABLED +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java new file mode 100644 index 00000000000..f7da708364c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java @@ -0,0 +1,139 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class Phone implements Serializable { + + private static final long serialVersionUID = 4399060521859707703L; + + private String country; + + private String area; + + private String number; + + private String extensionNumber; + + public Phone() { + } + + public Phone(String country, String area, String number, String extensionNumber) { + this.country = country; + this.area = area; + this.number = number; + this.extensionNumber = extensionNumber; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getExtensionNumber() { + return extensionNumber; + } + + public void setExtensionNumber(String extensionNumber) { + this.extensionNumber = extensionNumber; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((area == null) ? 0 : area.hashCode()); + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((extensionNumber == null) ? 0 : extensionNumber.hashCode()); + result = prime * result + ((number == null) ? 0 : number.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Phone other = (Phone) obj; + if (area == null) { + if (other.area != null) + return false; + } else if (!area.equals(other.area)) + return false; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (extensionNumber == null) { + if (other.extensionNumber != null) + return false; + } else if (!extensionNumber.equals(other.extensionNumber)) + return false; + if (number == null) { + if (other.number != null) + return false; + } else if (!number.equals(other.number)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (country != null && country.length() > 0) { + sb.append(country); + sb.append("-"); + } + if (area != null && area.length() > 0) { + sb.append(area); + sb.append("-"); + } + if (number != null && number.length() > 0) { + sb.append(number); + } + if (extensionNumber != null && extensionNumber.length() > 0) { + sb.append("-"); + sb.append(extensionNumber); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java new file mode 100644 index 00000000000..68060336da6 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java @@ -0,0 +1,137 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.model.Person; +import com.alibaba.dubbo.common.serialize.ObjectOutput; + +import org.junit.Test; + +import java.io.NotSerializableException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; + +public abstract class AbstractSerializationPersionFailTest extends AbstractSerializationTest { + @Test + public void test_Person() throws Exception { + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(new Person()); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonList() throws Exception { + List args = new ArrayList(); + args.add(new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonSet() throws Exception { + Set args = new HashSet(); + args.add(new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_IntPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_StringPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_StringPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new Person()); + args.put("1", sublist); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new Person()); + args.add(sublist); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java new file mode 100644 index 00000000000..32ff6e4856c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java @@ -0,0 +1,93 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.model.Person; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public abstract class AbstractSerializationPersionOkTest extends AbstractSerializationTest { + @Test + public void test_Person() throws Exception { + assertObject(new Person()); + } + + @Test + public void test_Person_withType() throws Exception { + assertObjectWithType(new Person(), Person.class); + } + + @Test + public void test_PersonList() throws Exception { + List args = new ArrayList(); + args.add(new Person()); + + assertObject(args); + } + + @Test + public void test_PersonSet() throws Exception { + Set args = new HashSet(); + args.add(new Person()); + + assertObject(args); + } + + @Test + public void test_IntPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new Person()); + + assertObject(args); + } + + @Test + public void test_StringPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new Person()); + + assertObject(args); + } + + @Test + public void test_StringPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new Person()); + args.put("1", sublist); + + assertObject(args); + } + + @Test + public void test_PersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new Person()); + args.add(sublist); + + assertObject(args); + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java new file mode 100644 index 00000000000..3219603ed69 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java @@ -0,0 +1,1210 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.model.AnimalEnum; +import com.alibaba.dubbo.common.model.BizException; +import com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor; +import com.alibaba.dubbo.common.model.SerializablePerson; +import com.alibaba.dubbo.common.model.media.Image; +import com.alibaba.dubbo.common.model.media.Image.Size; +import com.alibaba.dubbo.common.model.media.Media; +import com.alibaba.dubbo.common.model.media.Media.Player; +import com.alibaba.dubbo.common.model.media.MediaContent; +import com.alibaba.dubbo.common.model.person.BigPerson; +import com.alibaba.dubbo.common.model.person.FullAddress; +import com.alibaba.dubbo.common.model.person.PersonInfo; +import com.alibaba.dubbo.common.model.person.PersonStatus; +import com.alibaba.dubbo.common.model.person.Phone; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Time; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public abstract class AbstractSerializationTest { + static Random random = new Random(); + Serialization serialization; + URL url = new URL("protocl", "1.1.1.1", 1234); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + // ================ Primitive Type ================ + BigPerson bigPerson; + MediaContent mediaContent; + + { + bigPerson = new BigPerson(); + bigPerson.setPersonId("superman111"); + bigPerson.setLoginName("superman"); + bigPerson.setStatus(PersonStatus.ENABLED); + bigPerson.setEmail("sm@1.com"); + bigPerson.setPenName("pname"); + + ArrayList phones = new ArrayList(); + Phone phone1 = new Phone("86", "0571", "87654321", "001"); + Phone phone2 = new Phone("86", "0571", "87654322", "002"); + phones.add(phone1); + phones.add(phone2); + + PersonInfo pi = new PersonInfo(); + pi.setPhones(phones); + Phone fax = new Phone("86", "0571", "87654321", null); + pi.setFax(fax); + FullAddress addr = new FullAddress("CN", "zj", "3480", "wensanlu", "315000"); + pi.setFullAddress(addr); + pi.setMobileNo("13584652131"); + pi.setMale(true); + pi.setDepartment("b2b"); + pi.setHomepageUrl("www.capcom.com"); + pi.setJobTitle("qa"); + pi.setName("superman"); + + bigPerson.setInfoProfile(pi); + } + + { + Media media = new Media(); + media.setUri("uri://中华人民共和国"); + media.setTitle("title"); + media.setWidth(1239); + media.setHeight(1938); + media.setFormat("format-xxxx"); + media.setDuration(93419235); + media.setSize(3477897); + media.setBitrate(94523); + List persons = new ArrayList(); + persons.add("jerry"); + persons.add("tom"); + persons.add("lucy"); + media.setPersons(persons); + media.setCopyright("1999-2011"); + media.setPlayer(Player.FLASH); + + List images = new ArrayList(); + for (int i = 0; i < 10; ++i) { + Image image = new Image(); + image.setUri("url" + i); + if (i % 2 == 0) image.setTitle("title" + i); + image.setWidth(34 + i); + image.setHeight(2323 + i); + image.setSize((i % 2 == 0) ? Size.SMALL : Size.LARGE); + + images.add(image); + } + + mediaContent = new MediaContent(media, images); + } + + @Test + public void test_Bool() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeBool(false); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertFalse(deserialize.readBool()); + + try { + deserialize.readBool(); + fail(); + } catch (IOException expected) { + } + } + + @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 (IOException expected) { + } + } + + @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 (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_Short() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeShort((short) 123); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals((short) 123, deserialize.readShort()); + + try { + deserialize.readShort(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_Integer() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeInt(1); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + int i = deserialize.readInt(); + assertEquals(1, i); + + try { + deserialize.readInt(); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_Float() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeFloat(1.28F); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(1.28F == deserialize.readFloat()); + + try { + deserialize.readFloat(); + fail(); + } catch (IOException expected) { + } + } + + // ================== 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); + + assertTrue(1.28 == deserialize.readDouble()); + + try { + deserialize.readDouble(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_UtfString() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeUTF("123中华人民共和国"); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals("123中华人民共和国", deserialize.readUTF()); + + try { + deserialize.readUTF(); + fail(); + } catch (IOException expected) { + } + } + + @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()); + + try { + deserialize.readBytes(); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + // ================ Array Type ================ + + void assertObjectArray(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject())); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + void assertObjectArrayWithType(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject(clazz))); + + try { + deserialize.readObject(clazz); + fail(); + } catch (IOException expected) { + } + } + + @SuppressWarnings("unchecked") + 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()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + void assertObjectWithType(T data, Class clazz) 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(clazz)); + + try { + deserialize.readObject(clazz); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_boolArray() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject())); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_boolArray_withType() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject(boolean[].class))); + + try { + deserialize.readObject(boolean[].class); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_charArray_withType() 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(char[].class)); + + try { + deserialize.readObject(char[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray_withType() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject(short[].class)); + + try { + deserialize.readObject(short[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_intArray() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_intArray_withType() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject(int[].class)); + + try { + deserialize.readObject(int[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray_withType() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject(long[].class)); + + try { + deserialize.readObject(long[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(), 0.0001F); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray_withType() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(float[].class), 0.0001F); + + try { + deserialize.readObject(float[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(), 0.0001); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray_withType() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(double[].class), 0.0001); + + try { + deserialize.readObject(double[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_StringArray() throws Exception { + assertObjectArray(new String[]{"1", "b"}, String[].class); + } + + @Test + public void test_StringArray_withType() throws Exception { + assertObjectArrayWithType(new String[]{"1", "b"}, String[].class); + } + + // ================ Simple Type ================ + + @Test + public void test_IntegerArray() throws Exception { + assertObjectArray(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_IntegerArray_withType() throws Exception { + assertObjectArrayWithType(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_EnumArray() throws Exception { + assertObjectArray(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_EnumArray_withType() throws Exception { + assertObjectArrayWithType(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_SPerson() throws Exception { + assertObject(new SerializablePerson()); + } + + @Test + public void test_SPerson_withType() throws Exception { + assertObjectWithType(new SerializablePerson(), SerializablePerson.class); + } + + @Test + public void test_BizException() 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(); + assertEquals("Hello", ((BizException) read).getMessage()); + } + + @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()); + } + + @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()); + } + + @Test + public void test_BizExceptionNoDefaultConstructor_WithType() 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(BizExceptionNoDefaultConstructor.class); + assertEquals("Hello", ((BizExceptionNoDefaultConstructor) read).getMessage()); + } + + @Test + public void test_enum() throws Exception { + assertObject(AnimalEnum.dog); + } + + @Test + public void test_enum_withType() throws Exception { + assertObjectWithType(AnimalEnum.dog, AnimalEnum.class); + } + + @Test + public void test_Date() throws Exception { + assertObject(new Date()); + } + + @Test + public void test_Date_withType() throws Exception { + assertObjectWithType(new Date(), Date.class); + } + + @Test + public void test_Time() throws Exception { + assertObject(new Time(System.currentTimeMillis())); + } + + @Test + public void test_Time_withType() throws Exception { + assertObjectWithType(new Time(System.currentTimeMillis()), Time.class); + } + + @Test + public void test_ByteWrap() throws Exception { + assertObject(new Byte((byte) 12)); + } + + @Test + public void test_ByteWrap_withType() throws Exception { + assertObjectWithType(new Byte((byte) 12), Byte.class); + } + + @Test + public void test_LongWrap() throws Exception { + assertObject(new Long(12)); + } + + @Test + public void test_LongWrap_withType() throws Exception { + assertObjectWithType(new Long(12), Long.class); + } + + @Test + public void test_BigInteger() throws Exception { + assertObject(new BigInteger("23423434234234234")); + } + + @Test + public void test_BigInteger_withType() throws Exception { + assertObjectWithType(new BigInteger("23423434234234234"), BigInteger.class); + } + + @Test + public void test_BigDecimal() throws Exception { + assertObject(new BigDecimal("23423434234234234.341274832341234235")); + } + + @Test + public void test_BigDecimal_withType() throws Exception { + assertObjectWithType(new BigDecimal("23423434234234234.341274832341234235"), BigDecimal.class); + } + + @Test + public void test_StringList_asListReturn() throws Exception { + List args = Arrays.asList(new String[]{"1", "b"}); + + assertObject(args); + } + + @Test + public void test_StringArrayList() throws Exception { + List args = new ArrayList(Arrays.asList(new String[]{"1", "b"})); + + assertObject(args); + } + + @Test + public void test_StringSet() throws Exception { + Set args = new HashSet(); + args.add("1"); + + assertObject(args); + } + + @Test + public void test_LinkedHashMap() throws Exception { + LinkedHashMap data = new LinkedHashMap(); + 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) read).entrySet().iterator().next().getKey(); + assertEquals("1", key1); + + assertEquals(data, read); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + // ================ Complex Collection Type ================ + + @Test + public void test_SPersonList() throws Exception { + List args = new ArrayList(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_SPersonSet() throws Exception { + Set args = new HashSet(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + // ================ complex POJO ============= + + @Test + public void test_IntSPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.put("1", sublist); + + assertObject(args); + } + + @Test + public void test_SPersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.add(sublist); + + assertObject(args); + } + + @Test + public void test_BigPerson() throws Exception { + assertObject(bigPerson); + } + + @Test + public void test_BigPerson_WithType() throws Exception { + assertObjectWithType(bigPerson, BigPerson.class); + } + + @Test + public void test_MediaContent() throws Exception { + assertObject(mediaContent); + } + + @Test + public void test_MediaContent_WithType() throws Exception { + assertObjectWithType(mediaContent, MediaContent.class); + } + + @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) { + } + } + + @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) { + } + } + + + // abnormal case + + @Test + public void test_MediaContent_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(); + fail(); + } catch (IOException expected) { + System.out.println(expected); + } + } + + @Test + public void test_MediaContent_WithType_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(MediaContent.class); + fail(); + } catch (IOException expected) { + System.out.println(expected); + } + } + + + @Test(timeout = 3000) + public void test_LoopReference() throws Exception { + Map map = new HashMap(); + 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 output = (Map) deserialize.readObject(); + + assertEquals("v1", output.get("k1")); + assertSame(output, output.get("self")); + } + + // ================ 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) { + } + } +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/CompactedJavaSerializationTest.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/CompactedJavaSerializationTest.java similarity index 97% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/CompactedJavaSerializationTest.java rename to dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/CompactedJavaSerializationTest.java index e1127f5952a..8700dec1633 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/CompactedJavaSerializationTest.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/CompactedJavaSerializationTest.java @@ -1,25 +1,25 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.serialization; - -import com.alibaba.dubbo.common.serialize.support.java.CompactedJavaSerialization; - -public class CompactedJavaSerializationTest extends AbstractSerializationPersionFailTest { - { - serialization = new CompactedJavaSerialization(); - } +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.serialize.support.java.CompactedJavaSerialization; + +public class CompactedJavaSerializationTest extends AbstractSerializationPersionFailTest { + { + serialization = new CompactedJavaSerialization(); + } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JavaSerializationTest.java b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JavaSerializationTest.java similarity index 97% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JavaSerializationTest.java rename to dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JavaSerializationTest.java index 96b7fefccbb..e125993feb6 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JavaSerializationTest.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/java/com/alibaba/dubbo/common/serialize/serialization/JavaSerializationTest.java @@ -1,25 +1,25 @@ -/* - * 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 com.alibaba.dubbo.common.serialize.serialization; - -import com.alibaba.dubbo.common.serialize.support.java.JavaSerialization; - -public class JavaSerializationTest extends AbstractSerializationPersionFailTest { - { - serialization = new JavaSerialization(); - } +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.serialize.support.java.JavaSerialization; + +public class JavaSerializationTest extends AbstractSerializationPersionFailTest { + { + serialization = new JavaSerialization(); + } } \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc b/dubbo-serialization/dubbo-serialization-jdk/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc new file mode 100644 index 00000000000..c8c083c17eb --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc @@ -0,0 +1,2 @@ +a,d,e,b,c +str3,str2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/test/resources/log4j.xml b/dubbo-serialization/dubbo-serialization-jdk/src/test/resources/log4j.xml new file mode 100644 index 00000000000..671f2af8839 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-jdk/src/test/resources/log4j.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/pom.xml b/dubbo-serialization/dubbo-serialization-kryo/pom.xml new file mode 100644 index 00000000000..898d95361d1 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + + com.alibaba + dubbo-serialization + 2.6.1 + + dubbo-serialization-kryo + jar + ${project.artifactId} + The common module of dubbo project + + true + + + + com.alibaba + dubbo-serialization-api + ${project.parent.version} + + + com.esotericsoftware + kryo + + + de.javakaffee + kryo-serializers + + + \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/CompatibleKryo.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/CompatibleKryo.java similarity index 92% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/CompatibleKryo.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/CompatibleKryo.java index f434ce34f27..8483a47aa11 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/CompatibleKryo.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/CompatibleKryo.java @@ -14,11 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo; +package com.alibaba.dubbo.serialize.kryo; import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; -import com.alibaba.dubbo.common.serialize.support.kryo.utils.ReflectionUtils; +import com.alibaba.dubbo.serialize.kryo.utils.ReflectionUtils; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Serializer; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoObjectInput.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoObjectInput.java similarity index 96% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoObjectInput.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoObjectInput.java index b2d373a1b74..d0a1b36f83c 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoObjectInput.java @@ -14,11 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo; +package com.alibaba.dubbo.serialize.kryo; import com.alibaba.dubbo.common.serialize.Cleanable; import com.alibaba.dubbo.common.serialize.ObjectInput; -import com.alibaba.dubbo.common.serialize.support.kryo.utils.KryoUtils; +import com.alibaba.dubbo.serialize.kryo.utils.KryoUtils; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.KryoException; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoObjectOutput.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoObjectOutput.java similarity index 95% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoObjectOutput.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoObjectOutput.java index 652fc3028e0..7034079451e 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoObjectOutput.java @@ -14,11 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo; +package com.alibaba.dubbo.serialize.kryo; import com.alibaba.dubbo.common.serialize.Cleanable; import com.alibaba.dubbo.common.serialize.ObjectOutput; -import com.alibaba.dubbo.common.serialize.support.kryo.utils.KryoUtils; +import com.alibaba.dubbo.serialize.kryo.utils.KryoUtils; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Output; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoSerialization.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoSerialization.java similarity index 96% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoSerialization.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoSerialization.java index f43e7ca7f83..377dc683a4c 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/KryoSerialization.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/KryoSerialization.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo; +package com.alibaba.dubbo.serialize.kryo; import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.common.serialize.ObjectInput; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/AbstractKryoFactory.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/AbstractKryoFactory.java similarity index 97% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/AbstractKryoFactory.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/AbstractKryoFactory.java index b664a9209d3..9c7d3799da2 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/AbstractKryoFactory.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/AbstractKryoFactory.java @@ -14,10 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo.utils; +package com.alibaba.dubbo.serialize.kryo.utils; import com.alibaba.dubbo.common.serialize.support.SerializableClassRegistry; -import com.alibaba.dubbo.common.serialize.support.kryo.CompatibleKryo; +import com.alibaba.dubbo.serialize.kryo.CompatibleKryo; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.pool.KryoFactory; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/KryoUtils.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/KryoUtils.java similarity index 95% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/KryoUtils.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/KryoUtils.java index 748bd3449ab..741d43a9568 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/KryoUtils.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/KryoUtils.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo.utils; +package com.alibaba.dubbo.serialize.kryo.utils; import com.esotericsoftware.kryo.Kryo; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/PooledKryoFactory.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/PooledKryoFactory.java similarity index 95% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/PooledKryoFactory.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/PooledKryoFactory.java index 8f4380ce3bf..6ae384ec6d1 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/PooledKryoFactory.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/PooledKryoFactory.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo.utils; +package com.alibaba.dubbo.serialize.kryo.utils; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.pool.KryoPool; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/PrototypeKryoFactory.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/PrototypeKryoFactory.java similarity index 94% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/PrototypeKryoFactory.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/PrototypeKryoFactory.java index 12a0313536e..b0cf38365d0 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/PrototypeKryoFactory.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/PrototypeKryoFactory.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo.utils; +package com.alibaba.dubbo.serialize.kryo.utils; import com.esotericsoftware.kryo.Kryo; diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/ReflectionUtils.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/ReflectionUtils.java similarity index 94% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/ReflectionUtils.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/ReflectionUtils.java index 9a6a77eda8c..4000e611094 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/ReflectionUtils.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/ReflectionUtils.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo.utils; +package com.alibaba.dubbo.serialize.kryo.utils; public abstract class ReflectionUtils { diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/ThreadLocalKryoFactory.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/ThreadLocalKryoFactory.java similarity index 95% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/ThreadLocalKryoFactory.java rename to dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/ThreadLocalKryoFactory.java index f6668786daf..1a66676266a 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/kryo/utils/ThreadLocalKryoFactory.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/com/alibaba/dubbo/serialize/kryo/utils/ThreadLocalKryoFactory.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.dubbo.common.serialize.support.kryo.utils; +package com.alibaba.dubbo.serialize.kryo.utils; import com.esotericsoftware.kryo.Kryo; diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization b/dubbo-serialization/dubbo-serialization-kryo/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization new file mode 100644 index 00000000000..c4a0f413d9b --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization @@ -0,0 +1 @@ +kryo=com.alibaba.dubbo.serialize.kryo.KryoSerialization \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java new file mode 100644 index 00000000000..f920a0c6575 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/AnimalEnum.java @@ -0,0 +1,21 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public enum AnimalEnum { + dog, cat, rat, cow, bull, horse; +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizException.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizException.java new file mode 100644 index 00000000000..b6eb1f4bbce --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizException.java @@ -0,0 +1,29 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public class BizException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public BizException(String message) { + super(message); + } + + public BizException() { + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java new file mode 100644 index 00000000000..29db6e00fc9 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/BizExceptionNoDefaultConstructor.java @@ -0,0 +1,26 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +public class BizExceptionNoDefaultConstructor extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public BizExceptionNoDefaultConstructor(String message) { + super(message); + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/Person.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/Person.java new file mode 100644 index 00000000000..6383706cf79 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/Person.java @@ -0,0 +1,95 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.util.Arrays; + +public class Person { + 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java new file mode 100644 index 00000000000..e7dd2486786 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/SerializablePerson.java @@ -0,0 +1,97 @@ +/* + * 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 com.alibaba.dubbo.common.model; + +import java.io.Serializable; +import java.util.Arrays; + +public class SerializablePerson implements Serializable { + private static final long serialVersionUID = 1L; + 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; + SerializablePerson other = (SerializablePerson) 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; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Image.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Image.java new file mode 100644 index 00000000000..0d6a74a3d4a --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Image.java @@ -0,0 +1,120 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + + +public class Image implements java.io.Serializable { + private static final long serialVersionUID = 1L; + public String uri; + public String title; // Can be null + public int width; + public int height; + public Size size; + + public Image() { + } + + public Image(String uri, String title, int width, int height, Size size) { + this.height = height; + this.title = title; + this.uri = uri; + this.width = width; + this.size = size; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Image image = (Image) o; + + if (height != image.height) return false; + if (width != image.width) return false; + if (size != image.size) return false; + if (title != null ? !title.equals(image.title) : image.title != null) return false; + if (uri != null ? !uri.equals(image.uri) : image.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (size != null ? size.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Image "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", size=").append(size); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + public enum Size { + SMALL, LARGE + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Media.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Media.java new file mode 100644 index 00000000000..8a78f6d313b --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/Media.java @@ -0,0 +1,205 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class Media implements java.io.Serializable { + public String uri; + public String title; // Can be unset. + public int width; + public int height; + public String format; + public long duration; + public long size; + public int bitrate; // Can be unset. + public boolean hasBitrate; + public List persons; + public Player player; + public String copyright; // Can be unset. + + public Media() { + } + + public Media(String uri, String title, int width, int height, String format, long duration, long size, int bitrate, boolean hasBitrate, List persons, Player player, String copyright) { + this.uri = uri; + this.title = title; + this.width = width; + this.height = height; + this.format = format; + this.duration = duration; + this.size = size; + this.bitrate = bitrate; + this.hasBitrate = hasBitrate; + this.persons = persons; + this.player = player; + this.copyright = copyright; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Media media = (Media) o; + + if (bitrate != media.bitrate) return false; + if (duration != media.duration) return false; + if (hasBitrate != media.hasBitrate) return false; + if (height != media.height) return false; + if (size != media.size) return false; + if (width != media.width) return false; + if (copyright != null ? !copyright.equals(media.copyright) : media.copyright != null) return false; + if (format != null ? !format.equals(media.format) : media.format != null) return false; + if (persons != null ? !persons.equals(media.persons) : media.persons != null) return false; + if (player != media.player) return false; + if (title != null ? !title.equals(media.title) : media.title != null) return false; + if (uri != null ? !uri.equals(media.uri) : media.uri != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + width; + result = 31 * result + height; + result = 31 * result + (format != null ? format.hashCode() : 0); + result = 31 * result + (int) (duration ^ (duration >>> 32)); + result = 31 * result + (int) (size ^ (size >>> 32)); + result = 31 * result + bitrate; + result = 31 * result + (hasBitrate ? 1 : 0); + result = 31 * result + (persons != null ? persons.hashCode() : 0); + result = 31 * result + (player != null ? player.hashCode() : 0); + result = 31 * result + (copyright != null ? copyright.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[Media "); + sb.append("uri=").append(uri); + sb.append(", title=").append(title); + sb.append(", width=").append(width); + sb.append(", height=").append(height); + sb.append(", format=").append(format); + sb.append(", duration=").append(duration); + sb.append(", size=").append(size); + sb.append(", hasBitrate=").append(hasBitrate); + sb.append(", bitrate=").append(String.valueOf(bitrate)); + sb.append(", persons=").append(persons); + sb.append(", player=").append(player); + sb.append(", copyright=").append(copyright); + sb.append("]"); + return sb.toString(); + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public int getBitrate() { + return bitrate; + } + + public void setBitrate(int bitrate) { + this.bitrate = bitrate; + this.hasBitrate = true; + } + + public List getPersons() { + return persons; + } + + public void setPersons(List persons) { + this.persons = persons; + } + + public Player getPlayer() { + return player; + } + + public void setPlayer(Player player) { + this.player = player; + } + + public String getCopyright() { + return copyright; + } + + public void setCopyright(String copyright) { + this.copyright = copyright; + } + + public enum Player { + JAVA, FLASH + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java new file mode 100644 index 00000000000..9664116a635 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/media/MediaContent.java @@ -0,0 +1,78 @@ +/* + * 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 com.alibaba.dubbo.common.model.media; + +import java.util.List; + +@SuppressWarnings("serial") +public class MediaContent implements java.io.Serializable { + public Media media; + public List images; + + public MediaContent() { + } + + public MediaContent(Media media, List images) { + this.media = media; + this.images = images; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MediaContent that = (MediaContent) o; + + if (images != null ? !images.equals(that.images) : that.images != null) return false; + if (media != null ? !media.equals(that.media) : that.media != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = media != null ? media.hashCode() : 0; + result = 31 * result + (images != null ? images.hashCode() : 0); + return result; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[MediaContent: "); + sb.append("media=").append(media); + sb.append(", images=").append(images); + sb.append("]"); + return sb.toString(); + } + + public Media getMedia() { + return media; + } + + public void setMedia(Media media) { + this.media = media; + } + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java new file mode 100644 index 00000000000..70bc793979e --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/BigPerson.java @@ -0,0 +1,151 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class BigPerson implements Serializable { + private static final long serialVersionUID = 1L; + + String personId; + + String loginName; + + PersonStatus status; + + String email; + + String penName; + + PersonInfo infoProfile; + + public BigPerson() { + + } + + public BigPerson(String id) { + this.personId = id; + } + + public String getPersonId() { + return personId; + } + + public void setPersonId(String personId) { + this.personId = personId; + } + + public PersonInfo getInfoProfile() { + return infoProfile; + } + + public void setInfoProfile(PersonInfo infoProfile) { + this.infoProfile = infoProfile; + } + + public String getEmail() { + return this.email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getLoginName() { + return this.loginName; + } + + public void setLoginName(String loginName) { + this.loginName = loginName; + } + + public PersonStatus getStatus() { + return this.status; + } + + public void setStatus(PersonStatus status) { + this.status = status; + } + + public String getPenName() { + return penName; + } + + public void setPenName(String penName) { + this.penName = penName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + ((infoProfile == null) ? 0 : infoProfile.hashCode()); + result = prime * result + ((loginName == null) ? 0 : loginName.hashCode()); + result = prime * result + ((penName == null) ? 0 : penName.hashCode()); + result = prime * result + ((personId == null) ? 0 : personId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BigPerson other = (BigPerson) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + if (infoProfile == null) { + if (other.infoProfile != null) + return false; + } else if (!infoProfile.equals(other.infoProfile)) + return false; + if (loginName == null) { + if (other.loginName != null) + return false; + } else if (!loginName.equals(other.loginName)) + return false; + if (penName == null) { + if (other.penName != null) + return false; + } else if (!penName.equals(other.penName)) + return false; + if (personId == null) { + if (other.personId != null) + return false; + } else if (!personId.equals(other.personId)) + return false; + if (status != other.status) + return false; + return true; + } + + @Override + public String toString() { + return "BigPerson [personId=" + personId + ", loginName=" + loginName + ", status=" + + status + ", email=" + email + ", penName=" + penName + ", infoProfile=" + + infoProfile + "]"; + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java new file mode 100644 index 00000000000..dcc93cf8bd3 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/FullAddress.java @@ -0,0 +1,202 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class FullAddress implements Serializable { + + private static final long serialVersionUID = 5163979984269419831L; + + private String countryId; + + private String countryName; + + private String provinceName; + + private String cityId; + + private String cityName; + + private String streetAddress; + + private String zipCode; + + public FullAddress() { + } + + public FullAddress(String countryId, String provinceName, String cityId, String streetAddress, + String zipCode) { + this.countryId = countryId; + this.countryName = countryId; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityId; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public FullAddress(String countryId, String countryName, String provinceName, String cityId, + String cityName, String streetAddress, String zipCode) { + this.countryId = countryId; + this.countryName = countryName; + this.provinceName = provinceName; + this.cityId = cityId; + this.cityName = cityName; + this.streetAddress = streetAddress; + this.zipCode = zipCode; + } + + public String getCountryId() { + return countryId; + } + + public void setCountryId(String countryId) { + this.countryId = countryId; + } + + public String getCountryName() { + return countryName; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + + public String getProvinceName() { + return provinceName; + } + + public void setProvinceName(String provinceName) { + this.provinceName = provinceName; + } + + public String getCityId() { + return cityId; + } + + public void setCityId(String cityId) { + this.cityId = cityId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getStreetAddress() { + return streetAddress; + } + + public void setStreetAddress(String streetAddress) { + this.streetAddress = streetAddress; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((cityId == null) ? 0 : cityId.hashCode()); + result = prime * result + ((cityName == null) ? 0 : cityName.hashCode()); + result = prime * result + ((countryId == null) ? 0 : countryId.hashCode()); + result = prime * result + ((countryName == null) ? 0 : countryName.hashCode()); + result = prime * result + ((provinceName == null) ? 0 : provinceName.hashCode()); + result = prime * result + ((streetAddress == null) ? 0 : streetAddress.hashCode()); + result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FullAddress other = (FullAddress) obj; + if (cityId == null) { + if (other.cityId != null) + return false; + } else if (!cityId.equals(other.cityId)) + return false; + if (cityName == null) { + if (other.cityName != null) + return false; + } else if (!cityName.equals(other.cityName)) + return false; + if (countryId == null) { + if (other.countryId != null) + return false; + } else if (!countryId.equals(other.countryId)) + return false; + if (countryName == null) { + if (other.countryName != null) + return false; + } else if (!countryName.equals(other.countryName)) + return false; + if (provinceName == null) { + if (other.provinceName != null) + return false; + } else if (!provinceName.equals(other.provinceName)) + return false; + if (streetAddress == null) { + if (other.streetAddress != null) + return false; + } else if (!streetAddress.equals(other.streetAddress)) + return false; + if (zipCode == null) { + if (other.zipCode != null) + return false; + } else if (!zipCode.equals(other.zipCode)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (countryName != null && countryName.length() > 0) { + sb.append(countryName); + } + if (provinceName != null && provinceName.length() > 0) { + sb.append(" "); + sb.append(provinceName); + } + if (cityName != null && cityName.length() > 0) { + sb.append(" "); + sb.append(cityName); + } + if (streetAddress != null && streetAddress.length() > 0) { + sb.append(" "); + sb.append(streetAddress); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java new file mode 100644 index 00000000000..f1ff041e939 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonInfo.java @@ -0,0 +1,206 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; +import java.util.List; + +public class PersonInfo implements Serializable { + private static final long serialVersionUID = 7443011149612231882L; + + List phones; + + Phone fax; + + FullAddress fullAddress; + + String mobileNo; + + String name; + + boolean male; + + boolean female; + + String department; + + String jobTitle; + + String homepageUrl; + + public List getPhones() { + return phones; + } + + public void setPhones(List phones) { + this.phones = phones; + } + + public boolean isMale() { + return male; + } + + public void setMale(boolean male) { + this.male = male; + } + + public boolean isFemale() { + return female; + } + + public void setFemale(boolean female) { + this.female = female; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public Phone getFax() { + return fax; + } + + public void setFax(Phone fax) { + this.fax = fax; + } + + public FullAddress getFullAddress() { + return fullAddress; + } + + public void setFullAddress(FullAddress fullAddress) { + this.fullAddress = fullAddress; + } + + public String getHomepageUrl() { + return homepageUrl; + } + + public void setHomepageUrl(String homepageUrl) { + this.homepageUrl = homepageUrl; + } + + public String getJobTitle() { + return jobTitle; + } + + public void setJobTitle(String jobTitle) { + this.jobTitle = jobTitle; + } + + public String getMobileNo() { + return mobileNo; + } + + public void setMobileNo(String mobileNo) { + this.mobileNo = mobileNo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((department == null) ? 0 : department.hashCode()); + result = prime * result + ((fax == null) ? 0 : fax.hashCode()); + result = prime * result + (female ? 1231 : 1237); + result = prime * result + ((fullAddress == null) ? 0 : fullAddress.hashCode()); + result = prime * result + ((homepageUrl == null) ? 0 : homepageUrl.hashCode()); + result = prime * result + ((jobTitle == null) ? 0 : jobTitle.hashCode()); + result = prime * result + (male ? 1231 : 1237); + result = prime * result + ((mobileNo == null) ? 0 : mobileNo.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((phones == null) ? 0 : phones.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PersonInfo other = (PersonInfo) obj; + if (department == null) { + if (other.department != null) + return false; + } else if (!department.equals(other.department)) + return false; + if (fax == null) { + if (other.fax != null) + return false; + } else if (!fax.equals(other.fax)) + return false; + if (female != other.female) + return false; + if (fullAddress == null) { + if (other.fullAddress != null) + return false; + } else if (!fullAddress.equals(other.fullAddress)) + return false; + if (homepageUrl == null) { + if (other.homepageUrl != null) + return false; + } else if (!homepageUrl.equals(other.homepageUrl)) + return false; + if (jobTitle == null) { + if (other.jobTitle != null) + return false; + } else if (!jobTitle.equals(other.jobTitle)) + return false; + if (male != other.male) + return false; + if (mobileNo == null) { + if (other.mobileNo != null) + return false; + } else if (!mobileNo.equals(other.mobileNo)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (phones == null) { + if (other.phones != null) + return false; + } else if (!phones.equals(other.phones)) + return false; + return true; + } + + @Override + public String toString() { + return "PersonInfo [phones=" + phones + ", fax=" + fax + ", fullAddress=" + fullAddress + + ", mobileNo=" + mobileNo + ", name=" + name + ", male=" + male + ", female=" + + female + ", department=" + department + ", jobTitle=" + jobTitle + + ", homepageUrl=" + homepageUrl + "]"; + } + +} diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java new file mode 100644 index 00000000000..66b67c96340 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/PersonStatus.java @@ -0,0 +1,22 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +public enum PersonStatus { + ENABLED, + DISABLED +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java new file mode 100644 index 00000000000..f7da708364c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/model/person/Phone.java @@ -0,0 +1,139 @@ +/* + * 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 com.alibaba.dubbo.common.model.person; + +import java.io.Serializable; + +public class Phone implements Serializable { + + private static final long serialVersionUID = 4399060521859707703L; + + private String country; + + private String area; + + private String number; + + private String extensionNumber; + + public Phone() { + } + + public Phone(String country, String area, String number, String extensionNumber) { + this.country = country; + this.area = area; + this.number = number; + this.extensionNumber = extensionNumber; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getExtensionNumber() { + return extensionNumber; + } + + public void setExtensionNumber(String extensionNumber) { + this.extensionNumber = extensionNumber; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((area == null) ? 0 : area.hashCode()); + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((extensionNumber == null) ? 0 : extensionNumber.hashCode()); + result = prime * result + ((number == null) ? 0 : number.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Phone other = (Phone) obj; + if (area == null) { + if (other.area != null) + return false; + } else if (!area.equals(other.area)) + return false; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (extensionNumber == null) { + if (other.extensionNumber != null) + return false; + } else if (!extensionNumber.equals(other.extensionNumber)) + return false; + if (number == null) { + if (other.number != null) + return false; + } else if (!number.equals(other.number)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (country != null && country.length() > 0) { + sb.append(country); + sb.append("-"); + } + if (area != null && area.length() > 0) { + sb.append(area); + sb.append("-"); + } + if (number != null && number.length() > 0) { + sb.append(number); + } + if (extensionNumber != null && extensionNumber.length() > 0) { + sb.append("-"); + sb.append(extensionNumber); + } + return sb.toString(); + } + +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java new file mode 100644 index 00000000000..68060336da6 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionFailTest.java @@ -0,0 +1,137 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.model.Person; +import com.alibaba.dubbo.common.serialize.ObjectOutput; + +import org.junit.Test; + +import java.io.NotSerializableException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; + +public abstract class AbstractSerializationPersionFailTest extends AbstractSerializationTest { + @Test + public void test_Person() throws Exception { + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(new Person()); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonList() throws Exception { + List args = new ArrayList(); + args.add(new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonSet() throws Exception { + Set args = new HashSet(); + args.add(new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_IntPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_StringPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new Person()); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_StringPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new Person()); + args.put("1", sublist); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } + + @Test + public void test_PersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new Person()); + args.add(sublist); + try { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(args); + fail(); + } catch (NotSerializableException expected) { + } catch (IllegalStateException expected) { + assertThat(expected.getMessage(), containsString("Serialized class com.alibaba.dubbo.common.model.Person must implement java.io.Serializable")); + } + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java new file mode 100644 index 00000000000..32ff6e4856c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationPersionOkTest.java @@ -0,0 +1,93 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.model.Person; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public abstract class AbstractSerializationPersionOkTest extends AbstractSerializationTest { + @Test + public void test_Person() throws Exception { + assertObject(new Person()); + } + + @Test + public void test_Person_withType() throws Exception { + assertObjectWithType(new Person(), Person.class); + } + + @Test + public void test_PersonList() throws Exception { + List args = new ArrayList(); + args.add(new Person()); + + assertObject(args); + } + + @Test + public void test_PersonSet() throws Exception { + Set args = new HashSet(); + args.add(new Person()); + + assertObject(args); + } + + @Test + public void test_IntPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new Person()); + + assertObject(args); + } + + @Test + public void test_StringPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new Person()); + + assertObject(args); + } + + @Test + public void test_StringPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new Person()); + args.put("1", sublist); + + assertObject(args); + } + + @Test + public void test_PersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new Person()); + args.add(sublist); + + assertObject(args); + } +} \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java new file mode 100644 index 00000000000..3219603ed69 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/AbstractSerializationTest.java @@ -0,0 +1,1210 @@ +/* + * 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 com.alibaba.dubbo.common.serialize.serialization; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.model.AnimalEnum; +import com.alibaba.dubbo.common.model.BizException; +import com.alibaba.dubbo.common.model.BizExceptionNoDefaultConstructor; +import com.alibaba.dubbo.common.model.SerializablePerson; +import com.alibaba.dubbo.common.model.media.Image; +import com.alibaba.dubbo.common.model.media.Image.Size; +import com.alibaba.dubbo.common.model.media.Media; +import com.alibaba.dubbo.common.model.media.Media.Player; +import com.alibaba.dubbo.common.model.media.MediaContent; +import com.alibaba.dubbo.common.model.person.BigPerson; +import com.alibaba.dubbo.common.model.person.FullAddress; +import com.alibaba.dubbo.common.model.person.PersonInfo; +import com.alibaba.dubbo.common.model.person.PersonStatus; +import com.alibaba.dubbo.common.model.person.Phone; +import com.alibaba.dubbo.common.serialize.ObjectInput; +import com.alibaba.dubbo.common.serialize.ObjectOutput; +import com.alibaba.dubbo.common.serialize.Serialization; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Time; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public abstract class AbstractSerializationTest { + static Random random = new Random(); + Serialization serialization; + URL url = new URL("protocl", "1.1.1.1", 1234); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + // ================ Primitive Type ================ + BigPerson bigPerson; + MediaContent mediaContent; + + { + bigPerson = new BigPerson(); + bigPerson.setPersonId("superman111"); + bigPerson.setLoginName("superman"); + bigPerson.setStatus(PersonStatus.ENABLED); + bigPerson.setEmail("sm@1.com"); + bigPerson.setPenName("pname"); + + ArrayList phones = new ArrayList(); + Phone phone1 = new Phone("86", "0571", "87654321", "001"); + Phone phone2 = new Phone("86", "0571", "87654322", "002"); + phones.add(phone1); + phones.add(phone2); + + PersonInfo pi = new PersonInfo(); + pi.setPhones(phones); + Phone fax = new Phone("86", "0571", "87654321", null); + pi.setFax(fax); + FullAddress addr = new FullAddress("CN", "zj", "3480", "wensanlu", "315000"); + pi.setFullAddress(addr); + pi.setMobileNo("13584652131"); + pi.setMale(true); + pi.setDepartment("b2b"); + pi.setHomepageUrl("www.capcom.com"); + pi.setJobTitle("qa"); + pi.setName("superman"); + + bigPerson.setInfoProfile(pi); + } + + { + Media media = new Media(); + media.setUri("uri://中华人民共和国"); + media.setTitle("title"); + media.setWidth(1239); + media.setHeight(1938); + media.setFormat("format-xxxx"); + media.setDuration(93419235); + media.setSize(3477897); + media.setBitrate(94523); + List persons = new ArrayList(); + persons.add("jerry"); + persons.add("tom"); + persons.add("lucy"); + media.setPersons(persons); + media.setCopyright("1999-2011"); + media.setPlayer(Player.FLASH); + + List images = new ArrayList(); + for (int i = 0; i < 10; ++i) { + Image image = new Image(); + image.setUri("url" + i); + if (i % 2 == 0) image.setTitle("title" + i); + image.setWidth(34 + i); + image.setHeight(2323 + i); + image.setSize((i % 2 == 0) ? Size.SMALL : Size.LARGE); + + images.add(image); + } + + mediaContent = new MediaContent(media, images); + } + + @Test + public void test_Bool() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeBool(false); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertFalse(deserialize.readBool()); + + try { + deserialize.readBool(); + fail(); + } catch (IOException expected) { + } + } + + @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 (IOException expected) { + } + } + + @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 (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_Short() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeShort((short) 123); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals((short) 123, deserialize.readShort()); + + try { + deserialize.readShort(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_Integer() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeInt(1); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + int i = deserialize.readInt(); + assertEquals(1, i); + + try { + deserialize.readInt(); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_Float() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeFloat(1.28F); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(1.28F == deserialize.readFloat()); + + try { + deserialize.readFloat(); + fail(); + } catch (IOException expected) { + } + } + + // ================== 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); + + assertTrue(1.28 == deserialize.readDouble()); + + try { + deserialize.readDouble(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_UtfString() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeUTF("123中华人民共和国"); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertEquals("123中华人民共和国", deserialize.readUTF()); + + try { + deserialize.readUTF(); + fail(); + } catch (IOException expected) { + } + } + + @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()); + + try { + deserialize.readBytes(); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + // ================ Array Type ================ + + void assertObjectArray(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject())); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + void assertObjectArrayWithType(T[] data, Class clazz) 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); + + assertArrayEquals(data, clazz.cast(deserialize.readObject(clazz))); + + try { + deserialize.readObject(clazz); + fail(); + } catch (IOException expected) { + } + } + + @SuppressWarnings("unchecked") + 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()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + void assertObjectWithType(T data, Class clazz) 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(clazz)); + + try { + deserialize.readObject(clazz); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_boolArray() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject())); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_boolArray_withType() throws Exception { + boolean[] data = new boolean[]{true, false, true}; + + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(data); + objectOutput.flushBuffer(); + + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + byteArrayOutputStream.toByteArray()); + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + + assertTrue(Arrays.equals(data, (boolean[]) deserialize.readObject(boolean[].class))); + + try { + deserialize.readObject(boolean[].class); + fail(); + } catch (IOException expected) { + } + } + + @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) { + } + } + + @Test + public void test_charArray_withType() 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(char[].class)); + + try { + deserialize.readObject(char[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_shortArray_withType() throws Exception { + short[] data = new short[]{37, 39, 12}; + + 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, (short[]) deserialize.readObject(short[].class)); + + try { + deserialize.readObject(short[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_intArray() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_intArray_withType() throws Exception { + int[] data = new int[]{234, 0, -1}; + + 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, (int[]) deserialize.readObject(int[].class)); + + try { + deserialize.readObject(int[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject()); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_longArray_withType() throws Exception { + long[] data = new long[]{234, 0, -1}; + + 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, (long[]) deserialize.readObject(long[].class)); + + try { + deserialize.readObject(long[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(), 0.0001F); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_floatArray_withType() throws Exception { + float[] data = new float[]{37F, -3.14F, 123456.7F}; + + 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, (float[]) deserialize.readObject(float[].class), 0.0001F); + + try { + deserialize.readObject(float[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(), 0.0001); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_doubleArray_withType() throws Exception { + double[] data = new double[]{37D, -3.14D, 123456.7D}; + + 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, (double[]) deserialize.readObject(double[].class), 0.0001); + + try { + deserialize.readObject(double[].class); + fail(); + } catch (IOException expected) { + } + } + + @Test + public void test_StringArray() throws Exception { + assertObjectArray(new String[]{"1", "b"}, String[].class); + } + + @Test + public void test_StringArray_withType() throws Exception { + assertObjectArrayWithType(new String[]{"1", "b"}, String[].class); + } + + // ================ Simple Type ================ + + @Test + public void test_IntegerArray() throws Exception { + assertObjectArray(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_IntegerArray_withType() throws Exception { + assertObjectArrayWithType(new Integer[]{234, 0, -1}, Integer[].class); + } + + @Test + public void test_EnumArray() throws Exception { + assertObjectArray(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_EnumArray_withType() throws Exception { + assertObjectArrayWithType(new AnimalEnum[]{AnimalEnum.bull, AnimalEnum.cat, AnimalEnum.dog, AnimalEnum.horse}, AnimalEnum[].class); + } + + @Test + public void test_SPerson() throws Exception { + assertObject(new SerializablePerson()); + } + + @Test + public void test_SPerson_withType() throws Exception { + assertObjectWithType(new SerializablePerson(), SerializablePerson.class); + } + + @Test + public void test_BizException() 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(); + assertEquals("Hello", ((BizException) read).getMessage()); + } + + @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()); + } + + @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()); + } + + @Test + public void test_BizExceptionNoDefaultConstructor_WithType() 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(BizExceptionNoDefaultConstructor.class); + assertEquals("Hello", ((BizExceptionNoDefaultConstructor) read).getMessage()); + } + + @Test + public void test_enum() throws Exception { + assertObject(AnimalEnum.dog); + } + + @Test + public void test_enum_withType() throws Exception { + assertObjectWithType(AnimalEnum.dog, AnimalEnum.class); + } + + @Test + public void test_Date() throws Exception { + assertObject(new Date()); + } + + @Test + public void test_Date_withType() throws Exception { + assertObjectWithType(new Date(), Date.class); + } + + @Test + public void test_Time() throws Exception { + assertObject(new Time(System.currentTimeMillis())); + } + + @Test + public void test_Time_withType() throws Exception { + assertObjectWithType(new Time(System.currentTimeMillis()), Time.class); + } + + @Test + public void test_ByteWrap() throws Exception { + assertObject(new Byte((byte) 12)); + } + + @Test + public void test_ByteWrap_withType() throws Exception { + assertObjectWithType(new Byte((byte) 12), Byte.class); + } + + @Test + public void test_LongWrap() throws Exception { + assertObject(new Long(12)); + } + + @Test + public void test_LongWrap_withType() throws Exception { + assertObjectWithType(new Long(12), Long.class); + } + + @Test + public void test_BigInteger() throws Exception { + assertObject(new BigInteger("23423434234234234")); + } + + @Test + public void test_BigInteger_withType() throws Exception { + assertObjectWithType(new BigInteger("23423434234234234"), BigInteger.class); + } + + @Test + public void test_BigDecimal() throws Exception { + assertObject(new BigDecimal("23423434234234234.341274832341234235")); + } + + @Test + public void test_BigDecimal_withType() throws Exception { + assertObjectWithType(new BigDecimal("23423434234234234.341274832341234235"), BigDecimal.class); + } + + @Test + public void test_StringList_asListReturn() throws Exception { + List args = Arrays.asList(new String[]{"1", "b"}); + + assertObject(args); + } + + @Test + public void test_StringArrayList() throws Exception { + List args = new ArrayList(Arrays.asList(new String[]{"1", "b"})); + + assertObject(args); + } + + @Test + public void test_StringSet() throws Exception { + Set args = new HashSet(); + args.add("1"); + + assertObject(args); + } + + @Test + public void test_LinkedHashMap() throws Exception { + LinkedHashMap data = new LinkedHashMap(); + 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) read).entrySet().iterator().next().getKey(); + assertEquals("1", key1); + + assertEquals(data, read); + + try { + deserialize.readObject(); + fail(); + } catch (IOException expected) { + } + } + + // ================ Complex Collection Type ================ + + @Test + public void test_SPersonList() throws Exception { + List args = new ArrayList(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_SPersonSet() throws Exception { + Set args = new HashSet(); + args.add(new SerializablePerson()); + + assertObject(args); + } + + // ================ complex POJO ============= + + @Test + public void test_IntSPersonMap() throws Exception { + Map args = new HashMap(); + args.put(1, new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonMap() throws Exception { + Map args = new HashMap(); + args.put("1", new SerializablePerson()); + + assertObject(args); + } + + @Test + public void test_StringSPersonListMap() throws Exception { + Map> args = new HashMap>(); + + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.put("1", sublist); + + assertObject(args); + } + + @Test + public void test_SPersonListList() throws Exception { + List> args = new ArrayList>(); + List sublist = new ArrayList(); + sublist.add(new SerializablePerson()); + args.add(sublist); + + assertObject(args); + } + + @Test + public void test_BigPerson() throws Exception { + assertObject(bigPerson); + } + + @Test + public void test_BigPerson_WithType() throws Exception { + assertObjectWithType(bigPerson, BigPerson.class); + } + + @Test + public void test_MediaContent() throws Exception { + assertObject(mediaContent); + } + + @Test + public void test_MediaContent_WithType() throws Exception { + assertObjectWithType(mediaContent, MediaContent.class); + } + + @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) { + } + } + + @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) { + } + } + + + // abnormal case + + @Test + public void test_MediaContent_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(); + fail(); + } catch (IOException expected) { + System.out.println(expected); + } + } + + @Test + public void test_MediaContent_WithType_badStream() throws Exception { + ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream); + objectOutput.writeObject(mediaContent); + objectOutput.flushBuffer(); + + byte[] byteArray = byteArrayOutputStream.toByteArray(); + for (int i = 0; i < byteArray.length; i++) { + if (i % 3 == 0) { + byteArray[i] = (byte) ~byteArray[i]; + } + } + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + + try { + ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream); + @SuppressWarnings("unused") // local variable, convenient for debug + Object read = deserialize.readObject(MediaContent.class); + fail(); + } catch (IOException expected) { + System.out.println(expected); + } + } + + + @Test(timeout = 3000) + public void test_LoopReference() throws Exception { + Map map = new HashMap(); + 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 output = (Map) deserialize.readObject(); + + assertEquals("v1", output.get("k1")); + assertSame(output, output.get("self")); + } + + // ================ 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) { + } + } +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/KyroSerializationTest.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/KyroSerializationTest.java similarity index 93% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/KyroSerializationTest.java rename to dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/KyroSerializationTest.java index 552af2843eb..f60e3966348 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/KyroSerializationTest.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/KyroSerializationTest.java @@ -16,7 +16,7 @@ */ package com.alibaba.dubbo.common.serialize.serialization; -import com.alibaba.dubbo.common.serialize.support.kryo.KryoSerialization; +import com.alibaba.dubbo.serialize.kryo.KryoSerialization; public class KyroSerializationTest extends AbstractSerializationTest { diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/ReflectionUtilsTest.java b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/ReflectionUtilsTest.java similarity index 94% rename from dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/ReflectionUtilsTest.java rename to dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/ReflectionUtilsTest.java index 84d2b886adf..064081abdcf 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/serialize/serialization/ReflectionUtilsTest.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/java/com/alibaba/dubbo/common/serialize/serialization/ReflectionUtilsTest.java @@ -16,7 +16,7 @@ */ package com.alibaba.dubbo.common.serialize.serialization; -import com.alibaba.dubbo.common.serialize.support.kryo.utils.ReflectionUtils; +import com.alibaba.dubbo.serialize.kryo.utils.ReflectionUtils; import org.junit.Test; diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc b/dubbo-serialization/dubbo-serialization-kryo/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc new file mode 100644 index 00000000000..c8c083c17eb --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/resources/com/alibaba/dubbo/common/serialize/dubbo/SimpleDO.fc @@ -0,0 +1,2 @@ +a,d,e,b,c +str3,str2 \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/test/resources/log4j.xml b/dubbo-serialization/dubbo-serialization-kryo/src/test/resources/log4j.xml new file mode 100644 index 00000000000..671f2af8839 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-kryo/src/test/resources/log4j.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dubbo-serialization/pom.xml b/dubbo-serialization/pom.xml new file mode 100644 index 00000000000..54b35b8c9e9 --- /dev/null +++ b/dubbo-serialization/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + com.alibaba + dubbo-parent + 2.6.1 + + dubbo-serialization + pom + ${project.artifactId} + The rpc module of dubbo project + + true + + + dubbo-serialization-api + dubbo-serialization-hessian2 + dubbo-serialization-fastjson + dubbo-serialization-kryo + dubbo-serialization-fst + dubbo-serialization-jdk + + diff --git a/pom.xml b/pom.xml index 672f13537ed..f6627c0f08f 100644 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,7 @@ limitations under the License. dubbo-config dubbo-demo dubbo-plugin + dubbo-serialization dependencies-bom bom all From 25a9f15f7b9098b75c2ef6fa77e6b3459bf1e592 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 13 Feb 2018 10:20:44 +0800 Subject: [PATCH 2/5] Fixes #889, support implicit delivery of attachments from provider to consumer --- .../com/alibaba/dubbo/common/Version.java | 31 ++++++++++++++++- .../exchange/codec/ExchangeCodec.java | 16 +++++++-- .../support/header/HeaderExchangeChannel.java | 5 +-- .../support/header/HeartBeatTask.java | 3 +- .../remoting/codec/ExchangeCodecTest.java | 11 +++--- .../codec/DeprecatedExchangeCodec.java | 3 +- .../com/alibaba/dubbo/rpc/RpcContext.java | 24 +++++++++++++ .../java/com/alibaba/dubbo/rpc/RpcResult.java | 12 +++++-- .../rpc/filter/ConsumerContextFilter.java | 5 ++- .../dubbo/rpc/filter/ContextFilter.java | 7 +++- .../dubbo/DecodeableRpcInvocation.java | 5 ++- .../protocol/dubbo/DecodeableRpcResult.java | 30 ++++++++++++++++ .../dubbo/rpc/protocol/dubbo/DubboCodec.java | 34 +++++++++++++++---- 13 files changed, 161 insertions(+), 25 deletions(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java index 97e3a2173dc..f2c22b40761 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java @@ -23,7 +23,9 @@ import java.net.URL; import java.security.CodeSource; import java.util.Enumeration; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; /** @@ -31,9 +33,11 @@ */ public final class Version { - private static final String DEFAULT_DUBBO_VERSION = "2.0.0"; + private static final String DEFAULT_DUBBO_VERSION = "2.0.2"; + private static final int LOWEST_VERSION_FOR_RESPONSE_ATTATCHMENT = 202; // 2.0.2 private static final Logger logger = LoggerFactory.getLogger(Version.class); private static final String VERSION = getVersion(Version.class, DEFAULT_DUBBO_VERSION); + private static final Map VERSION2INT = new HashMap(); static { // check if there's duplicated jar @@ -47,6 +51,31 @@ public static String getVersion() { return VERSION; } + public static boolean isSupportResponseAttatchment(String version) { + if (version == null || version.length() == 0) { + return false; + } + return getIntVersion(version) >= LOWEST_VERSION_FOR_RESPONSE_ATTATCHMENT; + } + + public static int getIntVersion(String version) { + Integer v = VERSION2INT.get(version); + if (v == null) { + v = parseInt(version); + VERSION2INT.put(version, v); + } + return v; + } + + private static int parseInt(String version) { + int v = 0; + String[] vArr = version.split("\\."); + int len = vArr.length; + for (int i = 1; i <= len; i++) { + v += Integer.parseInt(vArr[len - i]) * Math.pow(10, i - 1); + } + return v; + } private static boolean hasResource(String path) { try { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java index c35da1eede8..ecc26995077 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java @@ -16,6 +16,7 @@ */ package com.alibaba.dubbo.remoting.exchange.codec; +import com.alibaba.dubbo.common.Version; import com.alibaba.dubbo.common.io.Bytes; import com.alibaba.dubbo.common.io.StreamUtils; import com.alibaba.dubbo.common.logger.Logger; @@ -170,7 +171,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro } else { // decode request. Request req = new Request(id); - req.setVersion("2.0.0"); + req.setVersion(Version.getVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { req.setEvent(Request.HEARTBEAT_EVENT); @@ -228,7 +229,7 @@ protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request req) if (req.isEvent()) { encodeEventData(channel, out, req.getData()); } else { - encodeRequestData(channel, out, req.getData()); + encodeRequestData(channel, out, req.getData(), req.getVersion()); } out.flushBuffer(); if (out instanceof Cleanable) { @@ -271,7 +272,7 @@ protected void encodeResponse(Channel channel, ChannelBuffer buffer, Response re if (res.isHeartbeat()) { encodeHeartbeatData(channel, out, res.getResult()); } else { - encodeResponseData(channel, out, res.getResult()); + encodeResponseData(channel, out, res.getResult(), res.getVersion()); } } else out.writeUTF(res.getErrorMessage()); out.flushBuffer(); @@ -439,4 +440,13 @@ protected void encodeResponseData(Channel channel, ObjectOutput out, Object data encodeResponseData(out, data); } + protected void encodeRequestData(Channel channel, ObjectOutput out, Object data, String version) throws IOException { + encodeRequestData(out, data); + } + + protected void encodeResponseData(Channel channel, ObjectOutput out, Object data, String version) throws IOException { + encodeResponseData(out, data); + } + + } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java index 9654cef0c4e..edfc9abfc6e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java @@ -18,6 +18,7 @@ import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.Version; import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import com.alibaba.dubbo.remoting.Channel; @@ -86,7 +87,7 @@ public void send(Object message, boolean sent) throws RemotingException { channel.send(message, sent); } else { Request request = new Request(); - request.setVersion("2.0.0"); + request.setVersion(Version.getVersion()); request.setTwoWay(false); request.setData(message); channel.send(request, sent); @@ -103,7 +104,7 @@ public ResponseFuture request(Object request, int timeout) throws RemotingExcept } // create request. Request req = new Request(); - req.setVersion("2.0.0"); + req.setVersion(Version.getVersion()); req.setTwoWay(true); req.setData(request); DefaultFuture future = new DefaultFuture(channel, req, timeout); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java index 2d417b2e74d..9f32fe93910 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java @@ -17,6 +17,7 @@ package com.alibaba.dubbo.remoting.exchange.support.header; +import com.alibaba.dubbo.common.Version; import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import com.alibaba.dubbo.remoting.Channel; @@ -56,7 +57,7 @@ public void run() { if ((lastRead != null && now - lastRead > heartbeat) || (lastWrite != null && now - lastWrite > heartbeat)) { Request req = new Request(); - req.setVersion("2.0.0"); + req.setVersion(Version.getVersion()); req.setTwoWay(true); req.setEvent(Request.HEARTBEAT_EVENT); channel.send(req); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java index 0f36da42f1b..a2d11fc6b62 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java @@ -18,6 +18,7 @@ import com.alibaba.dubbo.common.Constants; +import com.alibaba.dubbo.common.Version; import com.alibaba.dubbo.common.extension.ExtensionLoader; import com.alibaba.dubbo.common.io.Bytes; import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; @@ -216,7 +217,7 @@ public void test_Decode_Return_Request_Event_Object() throws IOException { Assert.assertEquals(person, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(true, obj.isEvent()); - Assert.assertEquals("2.0.0", obj.getVersion()); + Assert.assertEquals(Version.getVersion(), obj.getVersion()); System.out.println(obj); } @@ -231,7 +232,7 @@ public void test_Decode_Return_Request_Event_String() throws IOException { Assert.assertEquals(event, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(true, obj.isEvent()); - Assert.assertEquals("2.0.0", obj.getVersion()); + Assert.assertEquals(Version.getVersion(), obj.getVersion()); System.out.println(obj); } @@ -244,7 +245,7 @@ public void test_Decode_Return_Request_Heartbeat_Object() throws IOException { Assert.assertEquals(null, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(true, obj.isHeartbeat()); - Assert.assertEquals("2.0.0", obj.getVersion()); + Assert.assertEquals(Version.getVersion(), obj.getVersion()); System.out.println(obj); } @@ -259,7 +260,7 @@ public void test_Decode_Return_Request_Object() throws IOException { Assert.assertEquals(person, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(false, obj.isHeartbeat()); - Assert.assertEquals("2.0.0", obj.getVersion()); + Assert.assertEquals(Version.getVersion(), obj.getVersion()); System.out.println(obj); } @@ -388,7 +389,7 @@ public void test_Encode_Error_Response() throws IOException { public void testMessageLengthGreaterThanMessageActualLength() throws Exception { Channel channel = getCliendSideChannel(url); Request request = new Request(1L); - request.setVersion("2.0.0"); + request.setVersion(Version.getVersion()); Date date = new Date(); request.setData(date); ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java index d70edb1c693..32234e35e94 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java @@ -16,6 +16,7 @@ */ package com.alibaba.dubbo.remoting.transport.codec; +import com.alibaba.dubbo.common.Version; import com.alibaba.dubbo.common.io.Bytes; import com.alibaba.dubbo.common.io.StreamUtils; import com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream; @@ -164,7 +165,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro } else { // decode request. Request req = new Request(id); - req.setVersion("2.0.0"); + req.setVersion(Version.getVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { req.setEvent(Request.HEARTBEAT_EVENT); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java index b2993b44bdd..dac1f76cce9 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java @@ -51,6 +51,12 @@ protected RpcContext initialValue() { return new RpcContext(); } }; + private static final ThreadLocal SERVER_LOCAL = new ThreadLocal() { + @Override + protected RpcContext initialValue() { + return new RpcContext(); + } + }; private final Map attachments = new HashMap(); private final Map values = new HashMap(); private Future future; @@ -83,6 +89,24 @@ protected RpcContext initialValue() { protected RpcContext() { } + /** + * get server side context. + * + * @return server context + */ + public static RpcContext getServerContext() { + return SERVER_LOCAL.get(); + } + + /** + * remove server side context. + * + * @see com.alibaba.dubbo.rpc.filter.ContextFilter + */ + public static void removeServerContext() { + SERVER_LOCAL.remove(); + } + /** * get context. * diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcResult.java index 423b7c06448..55313659eab 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcResult.java @@ -96,9 +96,17 @@ public Map getAttachments() { } public void setAttachments(Map map) { - if (map != null && map.size() > 0) { - attachments.putAll(map); + this.attachments = map == null ? new HashMap() : map; + } + + public void addAttachments(Map map) { + if (map == null) { + return; + } + if (this.attachments == null) { + this.attachments = new HashMap(); } + this.attachments.putAll(map); } public String getAttachment(String key) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ConsumerContextFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ConsumerContextFilter.java index 1c86a9b1f78..abfe2176d3c 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ConsumerContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ConsumerContextFilter.java @@ -26,6 +26,7 @@ import com.alibaba.dubbo.rpc.RpcContext; import com.alibaba.dubbo.rpc.RpcException; import com.alibaba.dubbo.rpc.RpcInvocation; +import com.alibaba.dubbo.rpc.RpcResult; /** * ConsumerContextInvokerFilter @@ -44,7 +45,9 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept ((RpcInvocation) invocation).setInvoker(invoker); } try { - return invoker.invoke(invocation); + RpcResult result = (RpcResult) invoker.invoke(invocation); + RpcContext.getServerContext().setAttachments(result.getAttachments()); + return result; } finally { RpcContext.getContext().clearAttachments(); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ContextFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ContextFilter.java index ad53baf2879..376f6208784 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ContextFilter.java @@ -25,6 +25,7 @@ import com.alibaba.dubbo.rpc.RpcContext; import com.alibaba.dubbo.rpc.RpcException; import com.alibaba.dubbo.rpc.RpcInvocation; +import com.alibaba.dubbo.rpc.RpcResult; import java.util.HashMap; import java.util.Map; @@ -68,9 +69,13 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept ((RpcInvocation) invocation).setInvoker(invoker); } try { - return invoker.invoke(invocation); + RpcResult result = (RpcResult) invoker.invoke(invocation); + // pass attachments to result + result.addAttachments(RpcContext.getServerContext().getAttachments()); + return result; } finally { RpcContext.removeContext(); + RpcContext.getServerContext().clearAttachments(); } } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java index 23e3d796828..7152ae81be7 100644 --- a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java @@ -87,7 +87,10 @@ public Object decode(Channel channel, InputStream input) throws IOException { ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType) .deserialize(channel.getUrl(), input); - setAttachment(Constants.DUBBO_VERSION_KEY, in.readUTF()); + String dubboVersion = in.readUTF(); + request.setVersion(dubboVersion); + setAttachment(Constants.DUBBO_VERSION_KEY, dubboVersion); + setAttachment(Constants.PATH_KEY, in.readUTF()); setAttachment(Constants.VERSION_KEY, in.readUTF()); diff --git a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java index 75ef0ac55f8..4a257a70a6d 100644 --- a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java @@ -35,6 +35,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Type; +import java.util.Map; public class DecodeableRpcResult extends RpcResult implements Codec, Decodeable { @@ -95,6 +96,35 @@ public Object decode(Channel channel, InputStream input) throws IOException { throw new IOException(StringUtils.toString("Read response data failed.", e)); } break; + case DubboCodec.RESPONSE_NULL_VALUE_WITH_ATTACHMENTS: + try { + setAttachments((Map) in.readObject(Map.class)); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } + break; + case DubboCodec.RESPONSE_VALUE_WITH_ATTACHMENTS: + try { + Type[] returnType = RpcUtils.getReturnTypes(invocation); + setValue(returnType == null || returnType.length == 0 ? in.readObject() : + (returnType.length == 1 ? in.readObject((Class) returnType[0]) + : in.readObject((Class) returnType[0], returnType[1]))); + setAttachments((Map) in.readObject(Map.class)); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } + break; + case DubboCodec.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS: + try { + Object obj = in.readObject(); + if (obj instanceof Throwable == false) + throw new IOException("Response data error, expect Throwable, but get " + obj); + setException((Throwable) obj); + setAttachments((Map) in.readObject(Map.class)); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } + break; default: throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag); } diff --git a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java index 012051cbc43..bbc1bb749e5 100644 --- a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java +++ b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java @@ -53,6 +53,9 @@ public class DubboCodec extends ExchangeCodec implements Codec2 { public static final byte RESPONSE_WITH_EXCEPTION = 0; public static final byte RESPONSE_VALUE = 1; public static final byte RESPONSE_NULL_VALUE = 2; + public static final byte RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS = 3; + public static final byte RESPONSE_VALUE_WITH_ATTACHMENTS = 4; + public static final byte RESPONSE_NULL_VALUE_WITH_ATTACHMENTS = 5; public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; public static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; private static final Logger log = LoggerFactory.getLogger(DubboCodec.class); @@ -108,7 +111,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro } else { // decode request. Request req = new Request(id); - req.setVersion("2.0.0"); + req.setVersion(Version.getVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { req.setEvent(Request.HEARTBEAT_EVENT); @@ -161,9 +164,19 @@ private byte[] readMessageData(InputStream is) throws IOException { @Override protected void encodeRequestData(Channel channel, ObjectOutput out, Object data) throws IOException { + encodeRequestData(channel, out, data, DUBBO_VERSION); + } + + @Override + protected void encodeResponseData(Channel channel, ObjectOutput out, Object data) throws IOException { + encodeResponseData(channel, out, data, DUBBO_VERSION); + } + + @Override + protected void encodeRequestData(Channel channel, ObjectOutput out, Object data, String version) throws IOException { RpcInvocation inv = (RpcInvocation) data; - out.writeUTF(inv.getAttachment(Constants.DUBBO_VERSION_KEY, DUBBO_VERSION)); + out.writeUTF(version); out.writeUTF(inv.getAttachment(Constants.PATH_KEY)); out.writeUTF(inv.getAttachment(Constants.VERSION_KEY)); @@ -178,21 +191,28 @@ protected void encodeRequestData(Channel channel, ObjectOutput out, Object data) } @Override - protected void encodeResponseData(Channel channel, ObjectOutput out, Object data) throws IOException { + protected void encodeResponseData(Channel channel, ObjectOutput out, Object data, String version) throws IOException { Result result = (Result) data; - + // currently, the version value in Response records the version of Request + boolean attach = Version.isSupportResponseAttatchment(version); Throwable th = result.getException(); if (th == null) { Object ret = result.getValue(); if (ret == null) { - out.writeByte(RESPONSE_NULL_VALUE); + out.writeByte(attach ? RESPONSE_NULL_VALUE_WITH_ATTACHMENTS : RESPONSE_NULL_VALUE); } else { - out.writeByte(RESPONSE_VALUE); + out.writeByte(attach ? RESPONSE_VALUE_WITH_ATTACHMENTS : RESPONSE_VALUE); out.writeObject(ret); } } else { - out.writeByte(RESPONSE_WITH_EXCEPTION); + out.writeByte(attach ? RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS : RESPONSE_WITH_EXCEPTION); out.writeObject(th); } + + if (attach) { + // returns current version of Response to consumer side. + result.getAttachments().put(Constants.DUBBO_VERSION_KEY, Version.getVersion()); + out.writeObject(result.getAttachments()); + } } } \ No newline at end of file From 7dbd92ba1875775564c69c83e3f38687872d8132 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Fri, 9 Mar 2018 18:12:38 +0800 Subject: [PATCH 3/5] Add .script route rules back for UT --- .../rpc/cluster/router/file/availablerule.javascript | 11 +++++++++++ .../rpc/cluster/router/file/methodrule.javascript | 10 ++++++++++ .../cluster/router/file/notAvailablerule.javascript | 11 +++++++++++ 3 files changed, 32 insertions(+) create mode 100644 dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript create mode 100644 dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/methodrule.javascript create mode 100644 dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript diff --git a/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript b/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript new file mode 100644 index 00000000000..c76de32ee40 --- /dev/null +++ b/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/availablerule.javascript @@ -0,0 +1,11 @@ +function route(invokers,invocation,context){ + var result = new java.util.ArrayList(invokers.size()); + + for (i=0;i1 && invocation.getMethodName() .equals("method1")) { + result.add(invokers.get(0)) ; + } else { + result.add(invokers.get(1)) ; + } + return result; +}; +route(invokers,invocation,context); \ No newline at end of file diff --git a/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript b/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript new file mode 100644 index 00000000000..1fcdcaeb0d6 --- /dev/null +++ b/dubbo-cluster/src/test/resources/com/alibaba/dubbo/rpc/cluster/router/file/notAvailablerule.javascript @@ -0,0 +1,11 @@ +function route(invokers,invocation,context){ + var result = new java.util.ArrayList(invokers.size()); + + for (i=0;i Date: Thu, 24 May 2018 14:31:11 +0800 Subject: [PATCH 4/5] Separate protocol version from implementation version. --- .../java/com/alibaba/dubbo/common/Version.java | 17 ++++++++++++++--- .../dubbo/config/AbstractInterfaceConfig.java | 4 ++-- .../alibaba/dubbo/config/ReferenceConfig.java | 2 +- .../com/alibaba/dubbo/config/ServiceConfig.java | 2 +- .../remoting/exchange/codec/ExchangeCodec.java | 2 +- .../support/header/HeaderExchangeChannel.java | 4 ++-- .../support/header/HeaderExchangeServer.java | 3 ++- .../exchange/support/header/HeartBeatTask.java | 2 +- .../dubbo/remoting/codec/ExchangeCodecTest.java | 14 +++++++------- .../codec/DeprecatedExchangeCodec.java | 2 +- .../java/com/alibaba/dubbo/rpc/RpcContext.java | 3 ++- .../dubbo/rpc/protocol/dubbo/DubboCodec.java | 6 +++--- .../dubbo/rpc/protocol/rmi/RmiProtocol.java | 2 +- 13 files changed, 38 insertions(+), 25 deletions(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java index f2c22b40761..3e812dedab6 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java @@ -32,11 +32,18 @@ * Version */ public final class Version { + private static final Logger logger = LoggerFactory.getLogger(Version.class); + + // Dubbo RPC protocol version + private static final String DEFAULT_DUBBO_PROTOCOL_VERSION = "2.0.2"; + // Dubbo implementation version, usually is jar version. + private static final String VERSION = getVersion(Version.class, ""); - private static final String DEFAULT_DUBBO_VERSION = "2.0.2"; + /** + * For protocol compatibility purpose. + * Because {@link #isSupportResponseAttatchment} is checked for every call, int compare expect to has higher performance than string. + */ private static final int LOWEST_VERSION_FOR_RESPONSE_ATTATCHMENT = 202; // 2.0.2 - private static final Logger logger = LoggerFactory.getLogger(Version.class); - private static final String VERSION = getVersion(Version.class, DEFAULT_DUBBO_VERSION); private static final Map VERSION2INT = new HashMap(); static { @@ -47,6 +54,10 @@ public final class Version { private Version() { } + public static String getProtocolVersion() { + return DEFAULT_DUBBO_PROTOCOL_VERSION; + } + public static String getVersion() { return VERSION; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java index a42d07c1093..168bb10d2ea 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java @@ -174,7 +174,7 @@ protected List loadRegistries(boolean provider) { appendParameters(map, application); appendParameters(map, config); map.put("path", RegistryService.class.getName()); - map.put("dubbo", Version.getVersion()); + map.put("dubbo", Version.getProtocolVersion()); map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())); if (ConfigUtils.getPid() > 0) { map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid())); @@ -220,7 +220,7 @@ protected URL loadMonitor(URL registryURL) { appendProperties(monitor); Map map = new HashMap(); map.put(Constants.INTERFACE_KEY, MonitorService.class.getName()); - map.put("dubbo", Version.getVersion()); + map.put("dubbo", Version.getProtocolVersion()); map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())); if (ConfigUtils.getPid() > 0) { map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid())); diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java index 7b5175243a1..c6c75b16c68 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java @@ -280,7 +280,7 @@ private void init() { Map map = new HashMap(); Map attributes = new HashMap(); map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE); - map.put(Constants.DUBBO_VERSION_KEY, Version.getVersion()); + map.put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion()); map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())); if (ConfigUtils.getPid() > 0) { map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid())); diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java index d04d51c6660..8637662d335 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java @@ -367,7 +367,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r Map map = new HashMap(); map.put(Constants.SIDE_KEY, Constants.PROVIDER_SIDE); - map.put(Constants.DUBBO_VERSION_KEY, Version.getVersion()); + map.put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion()); map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())); if (ConfigUtils.getPid() > 0) { map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid())); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java index 35cf6035853..c1b8e096226 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java @@ -174,7 +174,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro } else { // decode request. Request req = new Request(id); - req.setVersion(Version.getVersion()); + req.setVersion(Version.getProtocolVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { req.setEvent(Request.HEARTBEAT_EVENT); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java index ffb130485a9..3c0fb65c6b7 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java @@ -89,7 +89,7 @@ public void send(Object message, boolean sent) throws RemotingException { channel.send(message, sent); } else { Request request = new Request(); - request.setVersion(Version.getVersion()); + request.setVersion(Version.getProtocolVersion()); request.setTwoWay(false); request.setData(message); channel.send(request, sent); @@ -108,7 +108,7 @@ public ResponseFuture request(Object request, int timeout) throws RemotingExcept } // create request. Request req = new Request(); - req.setVersion(Version.getVersion()); + req.setVersion(Version.getProtocolVersion()); req.setTwoWay(true); req.setData(request); DefaultFuture future = new DefaultFuture(channel, req, timeout); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java index 2ab32f008d1..6e520e3acb8 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java @@ -29,6 +29,7 @@ import com.alibaba.dubbo.remoting.exchange.ExchangeChannel; import com.alibaba.dubbo.remoting.exchange.ExchangeServer; import com.alibaba.dubbo.remoting.exchange.Request; + import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Collection; @@ -133,7 +134,7 @@ private void sendChannelReadOnlyEvent() { Request request = new Request(); request.setEvent(Request.READONLY_EVENT); request.setTwoWay(false); - request.setVersion(Version.getVersion()); + request.setVersion(Version.getProtocolVersion()); Collection channels = getChannels(); for (Channel channel : channels) { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java index b35e27f0914..5dd00d6a204 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java @@ -58,7 +58,7 @@ public void run() { if ((lastRead != null && now - lastRead > heartbeat) || (lastWrite != null && now - lastWrite > heartbeat)) { Request req = new Request(); - req.setVersion(Version.getVersion()); + req.setVersion(Version.getProtocolVersion()); req.setTwoWay(true); req.setEvent(Request.HEARTBEAT_EVENT); channel.send(req); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java index a2d11fc6b62..e372e14f650 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java @@ -217,7 +217,7 @@ public void test_Decode_Return_Request_Event_Object() throws IOException { Assert.assertEquals(person, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(true, obj.isEvent()); - Assert.assertEquals(Version.getVersion(), obj.getVersion()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -232,7 +232,7 @@ public void test_Decode_Return_Request_Event_String() throws IOException { Assert.assertEquals(event, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(true, obj.isEvent()); - Assert.assertEquals(Version.getVersion(), obj.getVersion()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -245,7 +245,7 @@ public void test_Decode_Return_Request_Heartbeat_Object() throws IOException { Assert.assertEquals(null, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(true, obj.isHeartbeat()); - Assert.assertEquals(Version.getVersion(), obj.getVersion()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -260,7 +260,7 @@ public void test_Decode_Return_Request_Object() throws IOException { Assert.assertEquals(person, obj.getData()); Assert.assertEquals(true, obj.isTwoWay()); Assert.assertEquals(false, obj.isHeartbeat()); - Assert.assertEquals(Version.getVersion(), obj.getVersion()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -351,7 +351,7 @@ public void test_Encode_Response() throws IOException { Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat()); Assert.assertEquals(person, obj.getResult()); // encode response verson ?? -// Assert.assertEquals(response.getVersion(), obj.getVersion()); +// Assert.assertEquals(response.getProtocolVersion(), obj.getVersion()); } @@ -381,7 +381,7 @@ public void test_Encode_Error_Response() throws IOException { Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat()); Assert.assertEquals(badString, obj.getErrorMessage()); Assert.assertEquals(null, obj.getResult()); -// Assert.assertEquals(response.getVersion(), obj.getVersion()); +// Assert.assertEquals(response.getProtocolVersion(), obj.getVersion()); } // http://code.alibabatech.com/jira/browse/DUBBO-392 @@ -389,7 +389,7 @@ public void test_Encode_Error_Response() throws IOException { public void testMessageLengthGreaterThanMessageActualLength() throws Exception { Channel channel = getCliendSideChannel(url); Request request = new Request(1L); - request.setVersion(Version.getVersion()); + request.setVersion(Version.getProtocolVersion()); Date date = new Date(); request.setData(date); ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java index 32234e35e94..2b44ada3941 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java @@ -165,7 +165,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro } else { // decode request. Request req = new Request(id); - req.setVersion(Version.getVersion()); + req.setVersion(Version.getProtocolVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { req.setEvent(Request.HEARTBEAT_EVENT); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java index 6f9a89d9d8f..6bb4d867d88 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java @@ -18,6 +18,7 @@ import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.threadlocal.InternalThreadLocal; import com.alibaba.dubbo.common.utils.NetUtils; import java.net.InetSocketAddress; @@ -54,7 +55,7 @@ protected RpcContext initialValue() { return new RpcContext(); } }; - private static final ThreadLocal SERVER_LOCAL = new ThreadLocal() { + private static final InternalThreadLocal SERVER_LOCAL = new InternalThreadLocal() { @Override protected RpcContext initialValue() { return new RpcContext(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java index 2e0e1237996..80e42090510 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java @@ -49,7 +49,7 @@ public class DubboCodec extends ExchangeCodec implements Codec2 { public static final String NAME = "dubbo"; - public static final String DUBBO_VERSION = Version.getVersion(DubboCodec.class, Version.getVersion()); + public static final String DUBBO_VERSION = Version.getProtocolVersion(); public static final byte RESPONSE_WITH_EXCEPTION = 0; public static final byte RESPONSE_VALUE = 1; public static final byte RESPONSE_NULL_VALUE = 2; @@ -112,7 +112,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro } else { // decode request. Request req = new Request(id); - req.setVersion(Version.getVersion()); + req.setVersion(Version.getProtocolVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { req.setEvent(Request.HEARTBEAT_EVENT); @@ -212,7 +212,7 @@ protected void encodeResponseData(Channel channel, ObjectOutput out, Object data if (attach) { // returns current version of Response to consumer side. - result.getAttachments().put(Constants.DUBBO_VERSION_KEY, Version.getVersion()); + result.getAttachments().put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion()); out.writeObject(result.getAttachments()); } } diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/main/java/com/alibaba/dubbo/rpc/protocol/rmi/RmiProtocol.java b/dubbo-rpc/dubbo-rpc-rmi/src/main/java/com/alibaba/dubbo/rpc/protocol/rmi/RmiProtocol.java index 8b01c1d934e..55fe9326925 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/main/java/com/alibaba/dubbo/rpc/protocol/rmi/RmiProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/main/java/com/alibaba/dubbo/rpc/protocol/rmi/RmiProtocol.java @@ -78,7 +78,7 @@ public void run() { protected T doRefer(final Class serviceType, final URL url) throws RpcException { final RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean(); // RMI needs extra parameter since it uses customized remote invocation object - if (url.getParameter(Constants.DUBBO_VERSION_KEY, Version.getVersion()).equals(Version.getVersion())) { + if (url.getParameter(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion()).equals(Version.getProtocolVersion())) { // Check dubbo version on provider, this feature only support rmiProxyFactoryBean.setRemoteInvocationFactory(new RemoteInvocationFactory() { @Override From f6a0c11c9438577bf726a5e32b2c73bda28ab244 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 24 May 2018 15:43:55 +0800 Subject: [PATCH 5/5] Add unit test for Version --- .../com/alibaba/dubbo/common/Version.java | 2 +- .../dubbo/common/version/VersionTest.java | 38 +++++++++++++++++++ pom.xml | 4 ++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/version/VersionTest.java diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java index 3e812dedab6..b84aaa5bfba 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java @@ -35,7 +35,7 @@ public final class Version { private static final Logger logger = LoggerFactory.getLogger(Version.class); // Dubbo RPC protocol version - private static final String DEFAULT_DUBBO_PROTOCOL_VERSION = "2.0.2"; + public static final String DEFAULT_DUBBO_PROTOCOL_VERSION = "2.0.2"; // Dubbo implementation version, usually is jar version. private static final String VERSION = getVersion(Version.class, ""); diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/version/VersionTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/version/VersionTest.java new file mode 100644 index 00000000000..139216b0955 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/version/VersionTest.java @@ -0,0 +1,38 @@ +/* + * 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 com.alibaba.dubbo.common.version; + + +import com.alibaba.dubbo.common.Version; + +import org.junit.Assert; +import org.junit.Test; + +public class VersionTest { + + @Test + public void testGetProtocolVersion() { + Assert.assertEquals(Version.getProtocolVersion(), Version.DEFAULT_DUBBO_PROTOCOL_VERSION); + } + + @Test + public void testSupportResponseAttatchment() { + Assert.assertTrue(Version.isSupportResponseAttatchment("2.0.2")); + Assert.assertTrue(Version.isSupportResponseAttatchment("2.0.3")); + Assert.assertFalse(Version.isSupportResponseAttatchment("2.0.0")); + } +} diff --git a/pom.xml b/pom.xml index c8536aef145..4da9e7dbbdc 100644 --- a/pom.xml +++ b/pom.xml @@ -333,6 +333,10 @@ true true + + ${project.version} + ${project.version} +