From 748236fef0675b7fd97484018ca95fc887466a02 Mon Sep 17 00:00:00 2001 From: Muga Nishizawa Date: Thu, 9 Feb 2012 02:21:43 +0900 Subject: [PATCH] MessagePack for Java was moved to https://github.com/msgpack/msgpack-java. --- java/AUTHORS | 1 - java/CHANGES.txt | 103 - java/LICENSE.txt | 202 -- java/Makefile | 24 - java/README | 29 +- java/build.xml | 303 --- java/ivy.xml | 21 - java/msgpack.iml | 17 - java/pom.xml | 188 -- .../java/org/msgpack/AbstractTemplate.java | 26 - .../org/msgpack/BufferedUnpackerImpl.java | 457 ---- .../java/org/msgpack/MessageConvertable.java | 23 - .../java/org/msgpack/MessageConverter.java | 23 - .../main/java/org/msgpack/MessagePack.java | 160 -- .../java/org/msgpack/MessagePackObject.java | 160 -- .../msgpack/MessagePackTemplateProvider.java | 22 - .../java/org/msgpack/MessagePackable.java | 25 - .../main/java/org/msgpack/MessagePacker.java | 25 - .../org/msgpack/MessageTypeException.java | 35 - .../java/org/msgpack/MessageUnpackable.java | 25 - .../java/org/msgpack/MessageUnpacker.java | 25 - java/src/main/java/org/msgpack/Packer.java | 541 ---- java/src/main/java/org/msgpack/Template.java | 21 - java/src/main/java/org/msgpack/Templates.java | 119 - .../java/org/msgpack/UnpackException.java | 29 - .../main/java/org/msgpack/UnpackIterator.java | 53 - .../main/java/org/msgpack/UnpackResult.java | 42 - java/src/main/java/org/msgpack/Unpacker.java | 610 ----- .../main/java/org/msgpack/UnpackerImpl.java | 473 ---- .../java/org/msgpack/annotation/Ignore.java | 28 - .../java/org/msgpack/annotation/Index.java | 29 - .../msgpack/annotation/MessagePackBeans.java | 35 - .../annotation/MessagePackDelegate.java | 29 - .../annotation/MessagePackMessage.java | 30 - .../annotation/MessagePackOrdinalEnum.java | 28 - .../java/org/msgpack/annotation/Nullable.java | 28 - .../java/org/msgpack/annotation/Optional.java | 28 - .../java/org/msgpack/annotation/Required.java | 28 - .../msgpack/buffer/VectoredByteBuffer.java | 462 ---- .../java/org/msgpack/object/ArrayType.java | 81 - .../msgpack/object/BigIntegerTypeIMPL.java | 131 - .../java/org/msgpack/object/BooleanType.java | 71 - .../org/msgpack/object/DoubleTypeIMPL.java | 101 - .../java/org/msgpack/object/FloatType.java | 36 - .../org/msgpack/object/FloatTypeIMPL.java | 94 - .../java/org/msgpack/object/IntegerType.java | 49 - .../msgpack/object/LongIntegerTypeIMPL.java | 128 - .../main/java/org/msgpack/object/MapType.java | 84 - .../main/java/org/msgpack/object/NilType.java | 60 - .../main/java/org/msgpack/object/RawType.java | 90 - .../msgpack/object/ShortIntegerTypeIMPL.java | 125 - .../main/java/org/msgpack/package-info.java | 8 - .../org/msgpack/template/AnyTemplate.java | 54 - .../org/msgpack/template/BeansFieldEntry.java | 143 -- .../template/BeansFieldEntryReader.java | 188 -- .../msgpack/template/BigDecimalTemplate.java | 61 - .../msgpack/template/BigIntegerTemplate.java | 53 - .../template/BooleanArrayTemplate.java | 80 - .../org/msgpack/template/BooleanTemplate.java | 53 - .../template/BuiltInTemplateLoader.java | 49 - .../msgpack/template/ByteArrayTemplate.java | 52 - .../msgpack/template/ByteBufferTemplate.java | 56 - .../org/msgpack/template/ByteTemplate.java | 53 - .../msgpack/template/CollectionTemplate.java | 93 - .../org/msgpack/template/DateTemplate.java | 57 - .../org/msgpack/template/DefaultTemplate.java | 96 - .../msgpack/template/DoubleArrayTemplate.java | 80 - .../org/msgpack/template/DoubleTemplate.java | 53 - .../java/org/msgpack/template/FieldEntry.java | 143 -- .../msgpack/template/FieldEntryReader.java | 182 -- .../java/org/msgpack/template/FieldList.java | 96 - .../org/msgpack/template/FieldOption.java | 27 - .../msgpack/template/FloatArrayTemplate.java | 80 - .../org/msgpack/template/FloatTemplate.java | 53 - .../org/msgpack/template/GenericTemplate.java | 25 - .../msgpack/template/GenericTemplate1.java | 54 - .../msgpack/template/GenericTemplate2.java | 54 - .../org/msgpack/template/IFieldEntry.java | 42 - .../msgpack/template/IFieldEntryReader.java | 25 - .../msgpack/template/IntArrayTemplate.java | 80 - .../org/msgpack/template/IntegerTemplate.java | 53 - .../org/msgpack/template/ListTemplate.java | 91 - .../msgpack/template/LongArrayTemplate.java | 80 - .../org/msgpack/template/LongTemplate.java | 53 - .../org/msgpack/template/MapTemplate.java | 101 - .../msgpack/template/NullableTemplate.java | 58 - .../msgpack/template/OptionalTemplate.java | 62 - .../msgpack/template/ShortArrayTemplate.java | 80 - .../org/msgpack/template/ShortTemplate.java | 53 - .../org/msgpack/template/StringTemplate.java | 52 - .../template/TemplateBuildException.java | 35 - .../msgpack/template/TemplateRegistry.java | 208 -- .../AnnotationTemplateBuilderSelector.java | 54 - .../builder/ArrayTemplateBuilder.java | 194 -- .../builder/ArrayTemplateBuilderSelector.java | 49 - .../template/builder/BeansBuildContext.java | 285 --- .../builder/BeansTemplateBuilder.java | 328 --- .../builder/BeansTemplateBuilderSelector.java | 49 - .../template/builder/BuildContext.java | 293 --- .../template/builder/BuildContextBase.java | 285 --- .../template/builder/BuildContextFactory.java | 25 - .../template/builder/BuilderSelector.java | 33 - .../builder/BuilderSelectorRegistry.java | 212 -- .../builder/CustomTemplateBuilder.java | 77 - .../builder/EnumTemplateBuilderSelector.java | 42 - .../builder/JavassistTemplateBuilder.java | 207 -- .../builder/OrdinalEnumTemplateBuilder.java | 96 - .../OrdinalEnumTemplateBuilderSelector.java | 45 - .../builder/ReflectionTemplateBuilder.java | 423 ---- .../template/builder/TemplateBuilder.java | 31 - java/src/main/java/org/msgpack/type/Raw.java | 69 - .../java/org/msgpack/type/RawTemplate.java | 51 - .../org/msgpack/util/TemplatePrecompiler.java | 133 - java/src/main/resources/log4j.properties | 14 - java/src/test/java/org/msgpack/Image.java | 50 - .../java/org/msgpack/TestAnnotations.java | 150 -- .../src/test/java/org/msgpack/TestArrays.java | 359 --- java/src/test/java/org/msgpack/TestCases.java | 210 -- .../org/msgpack/TestDirectConversion.java | 279 --- java/src/test/java/org/msgpack/TestEnums.java | 113 - .../msgpack/TestMessagePackStaticMethods.java | 319 --- .../org/msgpack/TestMessageUnpackable.java | 32 - .../java/org/msgpack/TestObjectEquals.java | 96 - .../test/java/org/msgpack/TestPackUnpack.java | 275 -- .../src/test/java/org/msgpack/TestSample.java | 11 - java/src/test/java/org/msgpack/Util.java | 19 - .../buffer/VectoredByteBufferTest.java | 45 - .../template/BeansEntryReaderTest.java | 133 - .../org/msgpack/template/BeansEquals.java | 83 - .../org/msgpack/template/TestPackConvert.java | 829 ------ .../org/msgpack/template/TestPackUnpack.java | 858 ------- ...ionTemplateBuilderJavaBeansPackUnpack.java | 2200 ---------------- ...tReflectionTemplateBuilderPackConvert.java | 1409 ----------- ...stReflectionTemplateBuilderPackUnpack.java | 1414 ----------- ...estTemplateBuilderJavaBeansPackUnpack.java | 2226 ----------------- .../TestTemplateBuilderPackConvert.java | 1395 ----------- .../TestTemplateBuilderPackUnpack.java | 1391 ---------- .../TestTemplatePrecompilerPackConvert.java | 1360 ---------- .../TestTemplatePrecompilerPackUnpack.java | 1422 ----------- .../org/msgpack/util/test/AfterMethod.java | 15 - .../org/msgpack/util/test/BeforeMethod.java | 15 - .../test/BlockContextJUnit4ClassRunner.java | 234 -- java/src/test/resources/cases.json | 1 - java/src/test/resources/cases.mpac | Bin 213 -> 0 bytes java/src/test/resources/cases_compact.mpac | Bin 116 -> 0 bytes java/src/test/resources/log4j.properties | 14 - 146 files changed, 1 insertion(+), 28524 deletions(-) delete mode 100644 java/AUTHORS delete mode 100644 java/CHANGES.txt delete mode 100644 java/LICENSE.txt delete mode 100755 java/Makefile delete mode 100755 java/build.xml delete mode 100644 java/ivy.xml delete mode 100644 java/msgpack.iml delete mode 100755 java/pom.xml delete mode 100644 java/src/main/java/org/msgpack/AbstractTemplate.java delete mode 100644 java/src/main/java/org/msgpack/BufferedUnpackerImpl.java delete mode 100644 java/src/main/java/org/msgpack/MessageConvertable.java delete mode 100644 java/src/main/java/org/msgpack/MessageConverter.java delete mode 100644 java/src/main/java/org/msgpack/MessagePack.java delete mode 100644 java/src/main/java/org/msgpack/MessagePackObject.java delete mode 100644 java/src/main/java/org/msgpack/MessagePackTemplateProvider.java delete mode 100644 java/src/main/java/org/msgpack/MessagePackable.java delete mode 100644 java/src/main/java/org/msgpack/MessagePacker.java delete mode 100644 java/src/main/java/org/msgpack/MessageTypeException.java delete mode 100644 java/src/main/java/org/msgpack/MessageUnpackable.java delete mode 100644 java/src/main/java/org/msgpack/MessageUnpacker.java delete mode 100644 java/src/main/java/org/msgpack/Packer.java delete mode 100644 java/src/main/java/org/msgpack/Template.java delete mode 100644 java/src/main/java/org/msgpack/Templates.java delete mode 100644 java/src/main/java/org/msgpack/UnpackException.java delete mode 100644 java/src/main/java/org/msgpack/UnpackIterator.java delete mode 100644 java/src/main/java/org/msgpack/UnpackResult.java delete mode 100644 java/src/main/java/org/msgpack/Unpacker.java delete mode 100644 java/src/main/java/org/msgpack/UnpackerImpl.java delete mode 100644 java/src/main/java/org/msgpack/annotation/Ignore.java delete mode 100644 java/src/main/java/org/msgpack/annotation/Index.java delete mode 100644 java/src/main/java/org/msgpack/annotation/MessagePackBeans.java delete mode 100644 java/src/main/java/org/msgpack/annotation/MessagePackDelegate.java delete mode 100644 java/src/main/java/org/msgpack/annotation/MessagePackMessage.java delete mode 100644 java/src/main/java/org/msgpack/annotation/MessagePackOrdinalEnum.java delete mode 100644 java/src/main/java/org/msgpack/annotation/Nullable.java delete mode 100644 java/src/main/java/org/msgpack/annotation/Optional.java delete mode 100644 java/src/main/java/org/msgpack/annotation/Required.java delete mode 100644 java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java delete mode 100644 java/src/main/java/org/msgpack/object/ArrayType.java delete mode 100644 java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java delete mode 100644 java/src/main/java/org/msgpack/object/BooleanType.java delete mode 100644 java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java delete mode 100644 java/src/main/java/org/msgpack/object/FloatType.java delete mode 100644 java/src/main/java/org/msgpack/object/FloatTypeIMPL.java delete mode 100644 java/src/main/java/org/msgpack/object/IntegerType.java delete mode 100644 java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java delete mode 100644 java/src/main/java/org/msgpack/object/MapType.java delete mode 100644 java/src/main/java/org/msgpack/object/NilType.java delete mode 100644 java/src/main/java/org/msgpack/object/RawType.java delete mode 100644 java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java delete mode 100644 java/src/main/java/org/msgpack/package-info.java delete mode 100644 java/src/main/java/org/msgpack/template/AnyTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/BeansFieldEntry.java delete mode 100644 java/src/main/java/org/msgpack/template/BeansFieldEntryReader.java delete mode 100644 java/src/main/java/org/msgpack/template/BigDecimalTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/BigIntegerTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/BooleanArrayTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/BooleanTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/BuiltInTemplateLoader.java delete mode 100644 java/src/main/java/org/msgpack/template/ByteArrayTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/ByteBufferTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/ByteTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/CollectionTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/DateTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/DefaultTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/DoubleArrayTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/DoubleTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/FieldEntry.java delete mode 100644 java/src/main/java/org/msgpack/template/FieldEntryReader.java delete mode 100644 java/src/main/java/org/msgpack/template/FieldList.java delete mode 100644 java/src/main/java/org/msgpack/template/FieldOption.java delete mode 100644 java/src/main/java/org/msgpack/template/FloatArrayTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/FloatTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/GenericTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/GenericTemplate1.java delete mode 100644 java/src/main/java/org/msgpack/template/GenericTemplate2.java delete mode 100644 java/src/main/java/org/msgpack/template/IFieldEntry.java delete mode 100644 java/src/main/java/org/msgpack/template/IFieldEntryReader.java delete mode 100644 java/src/main/java/org/msgpack/template/IntArrayTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/IntegerTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/ListTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/LongArrayTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/LongTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/MapTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/NullableTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/OptionalTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/ShortArrayTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/ShortTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/StringTemplate.java delete mode 100644 java/src/main/java/org/msgpack/template/TemplateBuildException.java delete mode 100644 java/src/main/java/org/msgpack/template/TemplateRegistry.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/AnnotationTemplateBuilderSelector.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilder.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilderSelector.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BeansBuildContext.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilder.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilderSelector.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BuildContext.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BuildContextBase.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BuildContextFactory.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BuilderSelector.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/BuilderSelectorRegistry.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/CustomTemplateBuilder.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/EnumTemplateBuilderSelector.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/JavassistTemplateBuilder.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilder.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilderSelector.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/ReflectionTemplateBuilder.java delete mode 100644 java/src/main/java/org/msgpack/template/builder/TemplateBuilder.java delete mode 100644 java/src/main/java/org/msgpack/type/Raw.java delete mode 100644 java/src/main/java/org/msgpack/type/RawTemplate.java delete mode 100644 java/src/main/java/org/msgpack/util/TemplatePrecompiler.java delete mode 100644 java/src/main/resources/log4j.properties delete mode 100644 java/src/test/java/org/msgpack/Image.java delete mode 100644 java/src/test/java/org/msgpack/TestAnnotations.java delete mode 100644 java/src/test/java/org/msgpack/TestArrays.java delete mode 100644 java/src/test/java/org/msgpack/TestCases.java delete mode 100644 java/src/test/java/org/msgpack/TestDirectConversion.java delete mode 100644 java/src/test/java/org/msgpack/TestEnums.java delete mode 100644 java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java delete mode 100644 java/src/test/java/org/msgpack/TestMessageUnpackable.java delete mode 100644 java/src/test/java/org/msgpack/TestObjectEquals.java delete mode 100644 java/src/test/java/org/msgpack/TestPackUnpack.java delete mode 100644 java/src/test/java/org/msgpack/TestSample.java delete mode 100644 java/src/test/java/org/msgpack/Util.java delete mode 100644 java/src/test/java/org/msgpack/buffer/VectoredByteBufferTest.java delete mode 100644 java/src/test/java/org/msgpack/template/BeansEntryReaderTest.java delete mode 100644 java/src/test/java/org/msgpack/template/BeansEquals.java delete mode 100644 java/src/test/java/org/msgpack/template/TestPackConvert.java delete mode 100644 java/src/test/java/org/msgpack/template/TestPackUnpack.java delete mode 100644 java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderJavaBeansPackUnpack.java delete mode 100644 java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackConvert.java delete mode 100644 java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackUnpack.java delete mode 100644 java/src/test/java/org/msgpack/template/TestTemplateBuilderJavaBeansPackUnpack.java delete mode 100644 java/src/test/java/org/msgpack/template/TestTemplateBuilderPackConvert.java delete mode 100644 java/src/test/java/org/msgpack/template/TestTemplateBuilderPackUnpack.java delete mode 100644 java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackConvert.java delete mode 100644 java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackUnpack.java delete mode 100644 java/src/test/java/org/msgpack/util/test/AfterMethod.java delete mode 100644 java/src/test/java/org/msgpack/util/test/BeforeMethod.java delete mode 100644 java/src/test/java/org/msgpack/util/test/BlockContextJUnit4ClassRunner.java delete mode 100644 java/src/test/resources/cases.json delete mode 100644 java/src/test/resources/cases.mpac delete mode 100644 java/src/test/resources/cases_compact.mpac delete mode 100644 java/src/test/resources/log4j.properties diff --git a/java/AUTHORS b/java/AUTHORS deleted file mode 100644 index ababacb0..00000000 --- a/java/AUTHORS +++ /dev/null @@ -1 +0,0 @@ -FURUHASHI Sadayuki diff --git a/java/CHANGES.txt b/java/CHANGES.txt deleted file mode 100644 index e7cbcfd6..00000000 --- a/java/CHANGES.txt +++ /dev/null @@ -1,103 +0,0 @@ - -Release 0.5.2 - 2011/04/23 - NEW FEATURES - MSGPACK-6 Added TemplatePrecompiler program - - Added built-in templates of BigDecimal and Date classes. - - Added @MessagePackBeans annotation that enables you to serialize/deserialize - JavaBeans. - - BUG FIXES - MSGPACK-4 Fixes the deserialization routine of Long value - - IMPROVEMENTS - #35 Improves handling of ClassLoader on Javassist template builder. - - MSGPACK-7 Improves compatibility with Java 1.5. Now it runs on JRE 5. - It is tested on Jenkins CI: http://ci.msgpack.org/job/java-jre5/ - - -Release 0.5.1 - 2010/12/14 - BUG FIXES - Fixes cast error on GenericArrayType - - Throws MessagePackException instead of NullPointerException if target is null - on pack method. - - -Release 0.5.0 - 2010/12/09 - NEW FEATURES - Dynamic template builder is rewritten. New ReflectionTemplateBuilder - supports DalvikVM. - - Some optimization for dynamic code generator. - - @MessagePackMessage accepts default filed option. - Added new field annotations: @Ignore, @Requred and @Index. - - Supported pack/unpack/convertion of arrays including multidimensional arrays. - - Added native pack/unpack routine of ByteBuffer. It will be zero-copy optimized - under a specific condition. - - -Release 0.4.3 - 2010/11/10 - NEW FEATURES - Added FieldList class and MessagePack.register(Class, FieldList) method - to specify optional/nullable options on runtime without annotations. - - Changed annotation name: @MessagePackNullable -> @Nullable - Changed annotation name: @MessagePackOptional -> @Optional - - Supported pack/unpack/convertion of enums. - - Added MessagePack.unpack(buffer, T to) and MessagePackObject.convert(T to) - methods. They can unpack/convert buffer/object into existing object and - eliminate re-allocation overhead. - - -Release 0.4.2 - 2010/11/09 - NEW FEATURES - Added MessagePackNullable annotation and Tempalte.tNullable(Template) - method. - - Added T MessagePackObject.unpack(Class) method. - - -Release 0.4.1 - 2010/11/05 - BUG FIXES - Fixed dynamic code generation of unpack methods - - -Release 0.4.0 - 2010/10/25 - NEW FEATURES - Added MessagePackObject class and org.msgpack.object package that - represent unpacked (=dynamically typed) objects. - Unpacker.unpack method returns MessagePackObject instead of Object. - - Added Templates class and org.msgpack.template package that provide - type conversion feature. - - User-defined classes annotated with MessagePackMessage can be - pack/unpack/converted. - - User-defined classes registered with MessagePack.register(Class) can be - pack/unpack/converted. - - Added dynamic code generation feature for user-defined classes. - - Added MessagePackOptional annotation. - - Added MessagePack class that implements typical useful methods. - - -Release 0.3 - 2010/05/23 - NEW FEATURES - Added Unbuffered API + Direct Conversion API to the Unpacker. - - BUG FIXES - Zero-length Array and Map is deserialized as List and Map, instead of the - array of the Object. - - fixed the bug around Packer.packByte(). diff --git a/java/LICENSE.txt b/java/LICENSE.txt deleted file mode 100644 index d6456956..00000000 --- a/java/LICENSE.txt +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed 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. diff --git a/java/Makefile b/java/Makefile deleted file mode 100755 index 7885a13f..00000000 --- a/java/Makefile +++ /dev/null @@ -1,24 +0,0 @@ - -.PHONY: compile test eclipse clean package - -all: - compile - -package: - mvn package - -install: - mvn install - -compile: - mvn compile - -test: - mvn test - -# generate .project and .classpath file for Eclipse -eclipse: - mvn eclipse:eclipse - -clean: - mvn clean diff --git a/java/README b/java/README index 33e6376f..4cf4249e 100755 --- a/java/README +++ b/java/README @@ -1,28 +1 @@ - -To build the JAR file of Message Pack, you need to install Maven (http://maven.apache.org), then type the following command: - -$ mvn package - -To locally install the project, type -$ mvn install - -To generate project files (.project, .classpath) for Eclipse, do - -$ mvn eclipse:eclipse - -then import the folder from your Eclipse. - -Next, open the preference page in Eclipse and add the CLASSPATH variable: - -M2_REPO = $HOME/.m2/repository - -where $HOME is your home directory. In Windows XP, $HOME is: -C:/Documents and Settings/(user name)/.m2/repository - - -# How to release the project (compile, test, tagging, deploy) - -$ mvn release:prepare -$ mvn release:perform - - +MessagePack for Java was moved to https://github.com/msgpack/msgpack-java. diff --git a/java/build.xml b/java/build.xml deleted file mode 100755 index bae89232..00000000 --- a/java/build.xml +++ /dev/null @@ -1,303 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Tests Failed! - - - - - - - - - - - - - - - -
- - -
-
-
-
- - - - - - - - - - - - - - - - - - - Javadoc warnings! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/java/ivy.xml b/java/ivy.xml deleted file mode 100644 index 18ebe08e..00000000 --- a/java/ivy.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - MessagePack - - - - - - - - - - - - - - diff --git a/java/msgpack.iml b/java/msgpack.iml deleted file mode 100644 index 3a6d962d..00000000 --- a/java/msgpack.iml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/java/pom.xml b/java/pom.xml deleted file mode 100755 index c2922cdf..00000000 --- a/java/pom.xml +++ /dev/null @@ -1,188 +0,0 @@ - - - 4.0.0 - org.msgpack - msgpack - 0.5.2-devel - MessagePack for Java - - MessagePack for Java - http://msgpack.org/ - - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - scm:git:git://github.com/msgpack/msgpack.git - scm:git:git://github.com/msgpack/msgpack.git - - - - - junit - junit - 4.8.2 - test - - - org.slf4j - slf4j-api - 1.6.1 - - - org.slf4j - slf4j-log4j12 - 1.6.1 - - - javassist - javassist - 3.12.1.GA - compile - - - - - - - src/main/resources - - - - - src/test/resources - - - - - - maven-compiler-plugin - - 1.5 - 1.5 - - - - - maven-eclipse-plugin - 2.5.1 - - - - maven-release-plugin - - - deploy - scm:git://github.com/msgpack/msgpack.git - - - - - org.apache.maven.plugins - maven-source-plugin - - - attach-sources - - jar - - - - - - - - - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - ${project.name} ${project.version} API - true - en_US - UTF-8 - - - - - org.apache.maven.plugins - maven-jxr-plugin - - - - org.apache.maven.plugins - maven-surefire-report-plugin - - - - - - - msgpack.org - MessagePack Maven2 Repository - http://msgpack.org/maven2 - - - repository.jboss.org - https://repository.jboss.org/nexus/content/groups/public/ - - false - - - - - - - false - msgpack.org - Repository at msgpack.org - file://${project.build.directory}/website/maven2/ - - - true - msgpack.org - Repository at msgpack.org - file://${project.build.directory}/website/maven2/ - - - - - - - release - - - - true - org.apache.maven.plugins - maven-deploy-plugin - 2.4 - - true - - - - - - - diff --git a/java/src/main/java/org/msgpack/AbstractTemplate.java b/java/src/main/java/org/msgpack/AbstractTemplate.java deleted file mode 100644 index 429a4705..00000000 --- a/java/src/main/java/org/msgpack/AbstractTemplate.java +++ /dev/null @@ -1,26 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; - -public abstract class AbstractTemplate implements Template { - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return convert(pac.unpackObject(), to); - } -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java deleted file mode 100644 index 30637a1a..00000000 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ /dev/null @@ -1,457 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.math.BigInteger; - -abstract class BufferedUnpackerImpl extends UnpackerImpl { - int offset = 0; - int filled = 0; - byte[] buffer = null; - boolean bufferReferenced = false; // TODO zero-copy buffer - private ByteBuffer castBuffer = ByteBuffer.allocate(8); - - abstract boolean fill() throws IOException; - - final boolean next(UnpackResult result) throws IOException, UnpackException { - if(filled == 0) { - if(!fill()) { - return false; - } - } - - do { - int noffset = super.execute(buffer, offset, filled); - if(noffset <= offset) { - if(!fill()) { - return false; - } - continue; - } - offset = noffset; - } while(!super.isFinished()); - - MessagePackObject obj = super.getData(); - super.reset(); - result.done(obj); - - return true; - } - - private final void more(int require) throws IOException, UnpackException { - while(filled - offset < require) { - if(!fill()) { - // FIXME - throw new UnpackException("insufficient buffer"); - } - } - } - - private final boolean tryMore(int require) throws IOException, UnpackException { - while(filled - offset < require) { - if(!fill()) { - return false; - } - } - return true; - } - - private final void advance(int length) { - offset += length; - } - - final byte unpackByte() throws IOException, MessageTypeException { - int o = unpackInt(); - if(0x7f < o || o < -0x80) { - throw new MessageTypeException(); - } - return (byte)o; - } - - final short unpackShort() throws IOException, MessageTypeException { - int o = unpackInt(); - if(0x7fff < o || o < -0x8000) { - throw new MessageTypeException(); - } - return (short)o; - } - - final int unpackInt() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum - advance(1); - return (int)b; - } - switch(b & 0xff) { - case 0xcc: // unsigned int 8 - more(2); - advance(2); - return (int)((short)(buffer[offset-1]) & 0xff); - case 0xcd: // unsigned int 16 - more(3); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 2); - advance(3); - return (int)((int)castBuffer.getShort(0) & 0xffff); - case 0xce: // unsigned int 32 - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - { - int o = castBuffer.getInt(0); - if(o < 0) { - throw new MessageTypeException(); - } - advance(5); - return o; - } - case 0xcf: // unsigned int 64 - more(9); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 8); - { - long o = castBuffer.getLong(0); - if(o < 0 || o > 0x7fffffffL) { - throw new MessageTypeException(); - } - advance(9); - return (int)o; - } - case 0xd0: // signed int 8 - more(2); - advance(2); - return (int)buffer[offset-1]; - case 0xd1: // signed int 16 - more(3); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 2); - advance(3); - return (int)castBuffer.getShort(0); - case 0xd2: // signed int 32 - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - return (int)castBuffer.getInt(0); - case 0xd3: // signed int 64 - more(9); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 8); - { - long o = castBuffer.getLong(0); - if(0x7fffffffL < o || o < -0x80000000L) { - throw new MessageTypeException(); - } - advance(9); - return (int)o; - } - default: - throw new MessageTypeException(); - } - } - - final long unpackLong() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum - advance(1); - return (long)b; - } - switch(b & 0xff) { - case 0xcc: // unsigned int 8 - more(2); - advance(2); - return (long)((short)(buffer[offset-1]) & 0xff); - case 0xcd: // unsigned int 16 - more(3); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 2); - advance(3); - return (long)((int)castBuffer.getShort(0) & 0xffff); - case 0xce: // unsigned int 32 - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - return ((long)castBuffer.getInt(0) & 0xffffffffL); - case 0xcf: // unsigned int 64 - more(9); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 8); - { - long o = castBuffer.getLong(0); - if(o < 0) { - throw new MessageTypeException(); - } - advance(9); - return o; - } - case 0xd0: // signed int 8 - more(2); - advance(2); - return (long)buffer[offset-1]; - case 0xd1: // signed int 16 - more(3); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 2); - advance(3); - return (long)castBuffer.getShort(0); - case 0xd2: // signed int 32 - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - return (long)castBuffer.getInt(0); - case 0xd3: // signed int 64 - more(9); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 8); - advance(9); - return (long)castBuffer.getLong(0); - default: - throw new MessageTypeException(); - } - } - - final BigInteger unpackBigInteger() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - if((b & 0xff) != 0xcf) { - return BigInteger.valueOf(unpackLong()); - } - - // unsigned int 64 - more(9); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 8); - advance(9); - long o = castBuffer.getLong(0); - if(o < 0) { - return new BigInteger(1, castBuffer.array()); - } else { - return BigInteger.valueOf(o); - } - } - - final float unpackFloat() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - switch(b & 0xff) { - case 0xca: // float - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - return castBuffer.getFloat(0); - case 0xcb: // double - more(9); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 8); - advance(9); - // FIXME overflow check - return (float)castBuffer.getDouble(0); - default: - throw new MessageTypeException(); - } - } - - final double unpackDouble() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - switch(b & 0xff) { - case 0xca: // float - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - return (double)castBuffer.getFloat(0); - case 0xcb: // double - more(9); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 8); - advance(9); - return castBuffer.getDouble(0); - default: - throw new MessageTypeException(); - } - } - - final Object unpackNull() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset] & 0xff; - if(b != 0xc0) { // nil - throw new MessageTypeException(); - } - advance(1); - return null; - } - - final boolean tryUnpackNull() throws IOException { - if(!tryMore(1)) { - return false; - } - int b = buffer[offset] & 0xff; - if(b != 0xc0) { // nil - return false; - } - advance(1); - return true; - } - - final boolean unpackBoolean() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset] & 0xff; - if(b == 0xc2) { // false - advance(1); - return false; - } else if(b == 0xc3) { // true - advance(1); - return true; - } else { - throw new MessageTypeException(); - } - } - - final int unpackArray() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - if((b & 0xf0) == 0x90) { // FixArray - advance(1); - return (int)(b & 0x0f); - } - switch(b & 0xff) { - case 0xdc: // array 16 - more(3); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 2); - advance(3); - return (int)castBuffer.getShort(0) & 0xffff; - case 0xdd: // array 32 - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - // FIXME overflow check - return castBuffer.getInt(0) & 0x7fffffff; - default: - throw new MessageTypeException(); - } - } - - final int unpackMap() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - if((b & 0xf0) == 0x80) { // FixMap - advance(1); - return (int)(b & 0x0f); - } - switch(b & 0xff) { - case 0xde: // map 16 - more(3); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 2); - advance(3); - return (int)castBuffer.getShort(0) & 0xffff; - case 0xdf: // map 32 - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - // FIXME overflow check - return castBuffer.getInt(0) & 0x7fffffff; - default: - throw new MessageTypeException(); - } - } - - final int unpackRaw() throws IOException, MessageTypeException { - more(1); - int b = buffer[offset]; - if((b & 0xe0) == 0xa0) { // FixRaw - advance(1); - return (int)(b & 0x1f); - } - switch(b & 0xff) { - case 0xda: // raw 16 - more(3); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 2); - advance(3); - return (int)castBuffer.getShort(0) & 0xffff; - case 0xdb: // raw 32 - more(5); - castBuffer.rewind(); - castBuffer.put(buffer, offset+1, 4); - advance(5); - // FIXME overflow check - return castBuffer.getInt(0) & 0x7fffffff; - default: - throw new MessageTypeException(); - } - } - - final byte[] unpackRawBody(int length) throws IOException { - more(length); - byte[] bytes = new byte[length]; - System.arraycopy(buffer, offset, bytes, 0, length); - advance(length); - return bytes; - } - - final byte[] unpackByteArray() throws IOException, MessageTypeException { - int length = unpackRaw(); - byte[] body = unpackRawBody(length); - return body; - } - - final ByteBuffer unpackByteBuffer() throws IOException, MessageTypeException { - // TODO zero-copy buffer - int length = unpackRaw(); - more(length); - ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length); - bufferReferenced = true; // TODO fix magical code - advance(length); - return buf; - } - - final String unpackString() throws IOException, MessageTypeException { - int length = unpackRaw(); - more(length); - String s; - try { - s = new String(buffer, offset, length, "UTF-8"); - } catch (Exception e) { - throw new MessageTypeException(); - } - advance(length); - return s; - } - - final MessagePackObject unpackObject() throws IOException { - UnpackResult result = new UnpackResult(); - if(!next(result)) { - super.reset(); - throw new UnpackException("insufficient buffer"); - } - return result.getData(); - } -} - diff --git a/java/src/main/java/org/msgpack/MessageConvertable.java b/java/src/main/java/org/msgpack/MessageConvertable.java deleted file mode 100644 index 8acf1f28..00000000 --- a/java/src/main/java/org/msgpack/MessageConvertable.java +++ /dev/null @@ -1,23 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -public interface MessageConvertable { - public void messageConvert(MessagePackObject obj) throws MessageTypeException; -} - diff --git a/java/src/main/java/org/msgpack/MessageConverter.java b/java/src/main/java/org/msgpack/MessageConverter.java deleted file mode 100644 index 51d5ec1a..00000000 --- a/java/src/main/java/org/msgpack/MessageConverter.java +++ /dev/null @@ -1,23 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -public interface MessageConverter { - Object convert(MessagePackObject from, Object to) throws MessageTypeException; -} - diff --git a/java/src/main/java/org/msgpack/MessagePack.java b/java/src/main/java/org/msgpack/MessagePack.java deleted file mode 100644 index 872f5999..00000000 --- a/java/src/main/java/org/msgpack/MessagePack.java +++ /dev/null @@ -1,160 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.OutputStream; -import java.io.InputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.msgpack.template.TemplateRegistry; -import org.msgpack.template.FieldList; - -public class MessagePack { - public static byte[] pack(Object obj) { - ByteArrayOutputStream out = new ByteArrayOutputStream(1024); - try { - new Packer(out).pack(obj); - } catch (IOException e) { - throw new RuntimeException(e); - } - return out.toByteArray(); - } - - public static void pack(OutputStream out, Object obj) throws IOException { - new Packer(out).pack(obj); - } - - public static byte[] pack(Object obj, Template tmpl) throws MessageTypeException { - ByteArrayOutputStream out = new ByteArrayOutputStream(1024); - try { - new Packer(out).pack(obj, tmpl); - } catch (IOException e) { - throw new RuntimeException(e); - } - return out.toByteArray(); - } - - public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException, MessageTypeException { - new Packer(out).pack(obj, tmpl); - } - - - public static MessagePackObject unpack(byte[] buffer) throws MessageTypeException { - Unpacker pac = new Unpacker(); - pac.wrap(buffer); - try { - return pac.unpackObject(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static Object unpack(byte[] buffer, Template tmpl) throws MessageTypeException { - Unpacker pac = new Unpacker(); - pac.wrap(buffer); - try { - return pac.unpack(tmpl); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static T unpack(byte[] buffer, Template tmpl, T to) throws MessageTypeException { - Unpacker pac = new Unpacker(); - pac.wrap(buffer); - try { - return pac.unpack(tmpl, to); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static T unpack(byte[] buffer, Class klass) throws MessageTypeException { - Unpacker pac = new Unpacker(); - pac.wrap(buffer); - try { - return pac.unpack(klass); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static T unpack(byte[] buffer, T to) throws MessageTypeException { - Unpacker pac = new Unpacker(); - pac.wrap(buffer); - try { - return pac.unpack(to); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static MessagePackObject unpack(InputStream in) throws IOException { - Unpacker pac = new Unpacker(in); - return pac.unpackObject(); - } - - public static Object unpack(InputStream in, Template tmpl) throws IOException, MessageTypeException { - Unpacker pac = new Unpacker(in); - try { - return pac.unpack(tmpl); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static T unpack(InputStream in, Template tmpl, T to) throws IOException, MessageTypeException { - Unpacker pac = new Unpacker(in); - try { - return pac.unpack(tmpl, to); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static T unpack(InputStream in, Class klass) throws IOException, MessageTypeException { - Unpacker pac = new Unpacker(in); - try { - return pac.unpack(klass); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static T unpack(InputStream in, T to) throws IOException, MessageTypeException { - Unpacker pac = new Unpacker(in); - try { - return pac.unpack(to); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static void register(Class target) { - TemplateRegistry.register(target); - } - - public static void register(Class target, FieldList flist) throws NoSuchFieldException { - TemplateRegistry.register(target, flist); - } - - public static void register(Class target, Template tmpl) { - TemplateRegistry.register(target, tmpl); - } -} - diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java deleted file mode 100644 index 59fab62e..00000000 --- a/java/src/main/java/org/msgpack/MessagePackObject.java +++ /dev/null @@ -1,160 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.util.List; -import java.util.Set; -import java.util.Map; -import java.math.BigInteger; -import org.msgpack.template.TemplateRegistry; - -public abstract class MessagePackObject implements Cloneable, MessagePackable { - public boolean isNil() { - return false; - } - - public boolean isBooleanType() { - return false; - } - - public boolean isIntegerType() { - return false; - } - - public boolean isFloatType() { - return false; - } - - public boolean isArrayType() { - return false; - } - - public boolean isMapType() { - return false; - } - - public boolean isRawType() { - return false; - } - - public boolean asBoolean() { - throw new MessageTypeException("type error"); - } - - public byte asByte() { - throw new MessageTypeException("type error"); - } - - public short asShort() { - throw new MessageTypeException("type error"); - } - - public int asInt() { - throw new MessageTypeException("type error"); - } - - public long asLong() { - throw new MessageTypeException("type error"); - } - - public BigInteger asBigInteger() { - throw new MessageTypeException("type error"); - } - - public float asFloat() { - throw new MessageTypeException("type error"); - } - - public double asDouble() { - throw new MessageTypeException("type error"); - } - - public byte[] asByteArray() { - throw new MessageTypeException("type error"); - } - - public String asString() { - throw new MessageTypeException("type error"); - } - - public MessagePackObject[] asArray() { - throw new MessageTypeException("type error"); - } - - public List asList() { - throw new MessageTypeException("type error"); - } - - public Map asMap() { - throw new MessageTypeException("type error"); - } - - public byte byteValue() { - throw new MessageTypeException("type error"); - } - - public short shortValue() { - throw new MessageTypeException("type error"); - } - - public int intValue() { - throw new MessageTypeException("type error"); - } - - public long longValue() { - throw new MessageTypeException("type error"); - } - - public BigInteger bigIntegerValue() { - throw new MessageTypeException("type error"); - } - - public float floatValue() { - throw new MessageTypeException("type error"); - } - - public double doubleValue() { - throw new MessageTypeException("type error"); - } - - abstract public Object clone(); - - public Object convert(Template tmpl) throws MessageTypeException { - return convert(tmpl, null); - } - - public T convert(Template tmpl, T to) throws MessageTypeException { - return (T)tmpl.convert(this, to); - } - - public T convert(Class klass) throws MessageTypeException { - return convert(klass, null); - } - - public T convert(T to) throws MessageTypeException { - return convert((Class)to.getClass(), to); - } - - public T convert(Class klass, T to) throws MessageTypeException { - if(isNil()) { - return null; - } - return (T)convert(TemplateRegistry.lookup(klass), to); - } -} - diff --git a/java/src/main/java/org/msgpack/MessagePackTemplateProvider.java b/java/src/main/java/org/msgpack/MessagePackTemplateProvider.java deleted file mode 100644 index 8f7515b6..00000000 --- a/java/src/main/java/org/msgpack/MessagePackTemplateProvider.java +++ /dev/null @@ -1,22 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -public interface MessagePackTemplateProvider { - Template getTemplate(); -} diff --git a/java/src/main/java/org/msgpack/MessagePackable.java b/java/src/main/java/org/msgpack/MessagePackable.java deleted file mode 100644 index 9e9852a2..00000000 --- a/java/src/main/java/org/msgpack/MessagePackable.java +++ /dev/null @@ -1,25 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; - -public interface MessagePackable { - public void messagePack(Packer pk) throws IOException; -} - diff --git a/java/src/main/java/org/msgpack/MessagePacker.java b/java/src/main/java/org/msgpack/MessagePacker.java deleted file mode 100644 index e5d387ee..00000000 --- a/java/src/main/java/org/msgpack/MessagePacker.java +++ /dev/null @@ -1,25 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; - -public interface MessagePacker { - void pack(Packer pk, Object target) throws IOException; -} - diff --git a/java/src/main/java/org/msgpack/MessageTypeException.java b/java/src/main/java/org/msgpack/MessageTypeException.java deleted file mode 100644 index bd148447..00000000 --- a/java/src/main/java/org/msgpack/MessageTypeException.java +++ /dev/null @@ -1,35 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -public class MessageTypeException extends RuntimeException { - public MessageTypeException() { } - - public MessageTypeException(String s) { - super(s); - } - - public MessageTypeException(String s, Throwable t) { - super(s, t); - } - - public MessageTypeException(Throwable t) { - super(t); - } -} - diff --git a/java/src/main/java/org/msgpack/MessageUnpackable.java b/java/src/main/java/org/msgpack/MessageUnpackable.java deleted file mode 100644 index cc206e7a..00000000 --- a/java/src/main/java/org/msgpack/MessageUnpackable.java +++ /dev/null @@ -1,25 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; - -public interface MessageUnpackable { - public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException; -} - diff --git a/java/src/main/java/org/msgpack/MessageUnpacker.java b/java/src/main/java/org/msgpack/MessageUnpacker.java deleted file mode 100644 index fd9f1ecf..00000000 --- a/java/src/main/java/org/msgpack/MessageUnpacker.java +++ /dev/null @@ -1,25 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; - -public interface MessageUnpacker { - Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException; -} - diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java deleted file mode 100644 index 17a6796a..00000000 --- a/java/src/main/java/org/msgpack/Packer.java +++ /dev/null @@ -1,541 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.OutputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.List; -import java.util.Set; -import java.util.Map; -import java.util.Collection; -import java.math.BigInteger; -import org.msgpack.template.TemplateRegistry; - -/** - * Packer enables you to serialize objects into OutputStream. - * - *
- * // create a packer with output stream
- * Packer pk = new Packer(System.out);
- *
- * // store an object with pack() method.
- * pk.pack(1);
- *
- * // you can store String, List, Map, byte[] and primitive types.
- * pk.pack(new ArrayList());
- * 
- * - * You can serialize objects that implements {@link MessagePackable} interface. - */ -public class Packer { - public static void load() { } - - protected byte[] castBytes = new byte[9]; - //protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes); - protected OutputStream out; - - public Packer(OutputStream out) { - this.out = out; - } - - public Packer packByte(byte d) throws IOException { - if(d < -(1<<5)) { - castBytes[0] = (byte)0xd0; - castBytes[1] = d; - out.write(castBytes, 0, 2); - } else { - out.write(d); - } - return this; - } - - public Packer packShort(short d) throws IOException { - if(d < -(1<<5)) { - if(d < -(1<<7)) { - // signed 16 - castBytes[0] = (byte)0xd1; -// castBuffer.putShort(1, d); - castBytes[1] = (byte)(d >> 8); - castBytes[2] = (byte)(d >> 0); - out.write(castBytes, 0, 3); - } else { - // signed 8 - castBytes[0] = (byte)0xd0; - castBytes[1] = (byte)d; - out.write(castBytes, 0, 2); - } - } else if(d < (1<<7)) { - // fixnum - out.write((byte)d); - } else { - if(d < (1<<8)) { - // unsigned 8 - castBytes[0] = (byte)0xcc; - castBytes[1] = (byte)d; - out.write(castBytes, 0, 2); - } else { - // unsigned 16 - castBytes[0] = (byte)0xcd; -// castBuffer.putShort(1, d); - castBytes[1] = (byte)(d >> 8); - castBytes[2] = (byte)(d >> 0); - out.write(castBytes, 0, 3); - } - } - return this; - } - - public Packer packInt(int d) throws IOException { - if(d < -(1<<5)) { - if(d < -(1<<15)) { - // signed 32 - castBytes[0] = (byte)0xd2; -// castBuffer.putInt(1, d); - castBytes[1] = (byte)(d >> 24); - castBytes[2] = (byte)(d >> 16); - castBytes[3] = (byte)(d >> 8); - castBytes[4] = (byte)(d >> 0); - out.write(castBytes, 0, 5); - } else if(d < -(1<<7)) { - // signed 16 - castBytes[0] = (byte)0xd1; -// castBuffer.putShort(1, (short)d); - castBytes[1] = (byte)(d >> 8); - castBytes[2] = (byte)(d >> 0); - out.write(castBytes, 0, 3); - } else { - // signed 8 - castBytes[0] = (byte)0xd0; - castBytes[1] = (byte)d; - out.write(castBytes, 0, 2); - } - } else if(d < (1<<7)) { - // fixnum - out.write((byte)d); - } else { - if(d < (1<<8)) { - // unsigned 8 - castBytes[0] = (byte)0xcc; - castBytes[1] = (byte)d; - out.write(castBytes, 0, 2); - } else if(d < (1<<16)) { - // unsigned 16 - castBytes[0] = (byte)0xcd; -// castBuffer.putShort(1, (short)d); - castBytes[1] = (byte)(d >> 8); - castBytes[2] = (byte)(d >> 0); - out.write(castBytes, 0, 3); - } else { - // unsigned 32 - castBytes[0] = (byte)0xce; -// castBuffer.putInt(1, d); - castBytes[1] = (byte)(d >> 24); - castBytes[2] = (byte)(d >> 16); - castBytes[3] = (byte)(d >> 8); - castBytes[4] = (byte)(d >> 0); - out.write(castBytes, 0, 5); - } - } - return this; - } - - public Packer packLong(long d) throws IOException { - if(d < -(1L<<5)) { - if(d < -(1L<<15)) { - if(d < -(1L<<31)) { - // signed 64 - castBytes[0] = (byte)0xd3; -// castBuffer.putLong(1, d); - castBytes[1] = (byte)(d >> 56); - castBytes[2] = (byte)(d >> 48); - castBytes[3] = (byte)(d >> 40); - castBytes[4] = (byte)(d >> 32); - castBytes[5] = (byte)(d >> 24); - castBytes[6] = (byte)(d >> 16); - castBytes[7] = (byte)(d >> 8); - castBytes[8] = (byte)(d >> 0); - out.write(castBytes, 0, 9); - } else { - // signed 32 - castBytes[0] = (byte)0xd2; -// castBuffer.putInt(1, (int)d); - castBytes[1] = (byte)(d >> 24); - castBytes[2] = (byte)(d >> 16); - castBytes[3] = (byte)(d >> 8); - castBytes[4] = (byte)(d >> 0); - out.write(castBytes, 0, 5); - } - } else { - if(d < -(1<<7)) { - // signed 16 - castBytes[0] = (byte)0xd1; -// castBuffer.putShort(1, (short)d); - castBytes[1] = (byte)(d >> 8); - castBytes[2] = (byte)(d >> 0); - out.write(castBytes, 0, 3); - } else { - // signed 8 - castBytes[0] = (byte)0xd0; - castBytes[1] = (byte)d; - out.write(castBytes, 0, 2); - } - } - } else if(d < (1<<7)) { - // fixnum - out.write((byte)d); - } else { - if(d < (1L<<16)) { - if(d < (1<<8)) { - // unsigned 8 - castBytes[0] = (byte)0xcc; - castBytes[1] = (byte)d; - out.write(castBytes, 0, 2); - } else { - // unsigned 16 - castBytes[0] = (byte)0xcd; -// castBuffer.putShort(1, (short)d); - castBytes[1] = (byte)((d & 0x0000ff00) >> 8); - castBytes[2] = (byte)((d & 0x000000ff) >> 0); - out.write(castBytes, 0, 3); - //System.out.println("pack uint 16 "+(short)d); - } - } else { - if(d < (1L<<32)) { - // unsigned 32 - castBytes[0] = (byte)0xce; -// castBuffer.putInt(1, (int)d); - castBytes[1] = (byte)((d & 0xff000000) >> 24); - castBytes[2] = (byte)((d & 0x00ff0000) >> 16); - castBytes[3] = (byte)((d & 0x0000ff00) >> 8); - castBytes[4] = (byte)((d & 0x000000ff) >> 0); - out.write(castBytes, 0, 5); - } else { - // unsigned 64 - castBytes[0] = (byte)0xcf; -// castBuffer.putLong(1, d); - castBytes[1] = (byte)(d >> 56); - castBytes[2] = (byte)(d >> 48); - castBytes[3] = (byte)(d >> 40); - castBytes[4] = (byte)(d >> 32); - castBytes[5] = (byte)(d >> 24); - castBytes[6] = (byte)(d >> 16); - castBytes[7] = (byte)(d >> 8); - castBytes[8] = (byte)(d >> 0); - out.write(castBytes, 0, 9); - } - } - } - return this; - } - - public Packer packBigInteger(BigInteger d) throws IOException { - if(d.bitLength() <= 63) { - return packLong(d.longValue()); - } else if(d.bitLength() <= 64 && d.signum() >= 0) { - castBytes[0] = (byte)0xcf; - byte[] barray = d.toByteArray(); - castBytes[1] = barray[barray.length-8]; - castBytes[2] = barray[barray.length-7]; - castBytes[3] = barray[barray.length-6]; - castBytes[4] = barray[barray.length-5]; - castBytes[5] = barray[barray.length-4]; - castBytes[6] = barray[barray.length-3]; - castBytes[7] = barray[barray.length-2]; - castBytes[8] = barray[barray.length-1]; - out.write(castBytes, 0, 9); - return this; - } else { - throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff"); - } - } - - public Packer packFloat(float d) throws IOException { - castBytes[0] = (byte)0xca; -// castBuffer.putFloat(1, d); - int f = Float.floatToRawIntBits(d); - castBytes[1] = (byte)(f >> 24); - castBytes[2] = (byte)(f >> 16); - castBytes[3] = (byte)(f >> 8); - castBytes[4] = (byte)(f >> 0); - out.write(castBytes, 0, 5); - return this; - } - - public Packer packDouble(double d) throws IOException { - castBytes[0] = (byte)0xcb; -// castBuffer.putDouble(1, d); - long f = Double.doubleToRawLongBits(d); - castBytes[1] = (byte)(f >> 56); - castBytes[2] = (byte)(f >> 48); - castBytes[3] = (byte)(f >> 40); - castBytes[4] = (byte)(f >> 32); - castBytes[5] = (byte)(f >> 24); - castBytes[6] = (byte)(f >> 16); - castBytes[7] = (byte)(f >> 8); - castBytes[8] = (byte)(f >> 0); - out.write(castBytes, 0, 9); - return this; - } - - public Packer packNil() throws IOException { - out.write((byte)0xc0); - return this; - } - - public Packer packTrue() throws IOException { - out.write((byte)0xc3); - return this; - } - - public Packer packFalse() throws IOException { - out.write((byte)0xc2); - return this; - } - - public Packer packBoolean(boolean d) throws IOException { - return d ? packTrue() : packFalse(); - } - - public Packer packArray(int n) throws IOException { - if(n < 16) { - final int d = 0x90 | n; - out.write((byte)d); - } else if(n < 65536) { - castBytes[0] = (byte)0xdc; -// castBuffer.putShort(1, (short)n); - castBytes[1] = (byte)(n >> 8); - castBytes[2] = (byte)(n >> 0); - out.write(castBytes, 0, 3); - } else { - castBytes[0] = (byte)0xdd; -// castBuffer.putInt(1, n); - castBytes[1] = (byte)(n >> 24); - castBytes[2] = (byte)(n >> 16); - castBytes[3] = (byte)(n >> 8); - castBytes[4] = (byte)(n >> 0); - out.write(castBytes, 0, 5); - } - return this; - } - - public Packer packMap(int n) throws IOException { - if(n < 16) { - final int d = 0x80 | n; - out.write((byte)d); - } else if(n < 65536) { - castBytes[0] = (byte)0xde; -// castBuffer.putShort(1, (short)n); - castBytes[1] = (byte)(n >> 8); - castBytes[2] = (byte)(n >> 0); - out.write(castBytes, 0, 3); - } else { - castBytes[0] = (byte)0xdf; -// castBuffer.putInt(1, n); - castBytes[1] = (byte)(n >> 24); - castBytes[2] = (byte)(n >> 16); - castBytes[3] = (byte)(n >> 8); - castBytes[4] = (byte)(n >> 0); - out.write(castBytes, 0, 5); - } - return this; - } - - public Packer packRaw(int n) throws IOException { - if(n < 32) { - final int d = 0xa0 | n; - out.write((byte)d); - } else if(n < 65536) { - castBytes[0] = (byte)0xda; -// castBuffer.putShort(1, (short)n); - castBytes[1] = (byte)(n >> 8); - castBytes[2] = (byte)(n >> 0); - out.write(castBytes, 0, 3); - } else { - castBytes[0] = (byte)0xdb; -// castBuffer.putInt(1, n); - castBytes[1] = (byte)(n >> 24); - castBytes[2] = (byte)(n >> 16); - castBytes[3] = (byte)(n >> 8); - castBytes[4] = (byte)(n >> 0); - out.write(castBytes, 0, 5); - } - return this; - } - - public Packer packRawBody(byte[] b) throws IOException { - out.write(b); - return this; - } - - public Packer packRawBody(byte[] b, int off, int length) throws IOException { - out.write(b, off, length); - return this; - } - - - public Packer packByteArray(byte[] b) throws IOException { - packRaw(b.length); - return packRawBody(b, 0, b.length); - } - - public Packer packByteArray(byte[] b, int off, int length) throws IOException { - packRaw(length); - return packRawBody(b, off, length); - } - - public Packer packByteBuffer(ByteBuffer buf) throws IOException { - packRaw(buf.remaining()); - return packRawBody(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); - } - - public Packer packString(String s) throws IOException { - byte[] b = ((String)s).getBytes("UTF-8"); - packRaw(b.length); - return packRawBody(b); - } - - - public Packer pack(boolean o) throws IOException { - if(o) { - return packTrue(); - } else { - return packFalse(); - } - } - - public Packer pack(byte o) throws IOException { - return packByte(o); - } - - public Packer pack(short o) throws IOException { - return packShort(o); - } - - public Packer pack(int o) throws IOException { - return packInt(o); - } - - public Packer pack(long o) throws IOException { - return packLong(o); - } - - public Packer pack(float o) throws IOException { - return packFloat(o); - } - - public Packer pack(double o) throws IOException { - return packDouble(o); - } - - public Packer pack(Boolean o) throws IOException { - if(o == null) { return packNil(); } - if(o) { - return packTrue(); - } else { - return packFalse(); - } - } - - public Packer pack(Byte o) throws IOException { - if(o == null) { return packNil(); } - return packByte(o); - } - - public Packer pack(Short o) throws IOException { - if(o == null) { return packNil(); } - return packShort(o); - } - - public Packer pack(Integer o) throws IOException { - if(o == null) { return packNil(); } - return packInt(o); - } - - public Packer pack(Long o) throws IOException { - if(o == null) { return packNil(); } - return packLong(o); - } - - public Packer pack(BigInteger o) throws IOException { - if(o == null) { return packNil(); } - return packBigInteger(o); - } - - public Packer pack(Float o) throws IOException { - if(o == null) { return packNil(); } - return packFloat(o); - } - - public Packer pack(Double o) throws IOException { - if(o == null) { return packNil(); } - return packDouble(o); - } - - public Packer pack(String o) throws IOException { - if(o == null) { return packNil(); } - return packString(o); - } - - public Packer pack(byte[] o) throws IOException { - if(o == null) { return packNil(); } - packRaw(o.length); - return packRawBody(o); - } - - public Packer pack(ByteBuffer o) throws IOException { - if (o == null) { return packNil(); } - packRaw(o.remaining()); - return packRawBody(o.array(), o.arrayOffset() + o.position(), o.remaining()); - } - - public Packer pack(List o) throws IOException { - if(o == null) { return packNil(); } - packArray(o.size()); - for(Object i : o) { pack(i); } - return this; - } - - public Packer pack(Map o) throws IOException { - if(o == null) { return packNil(); } - packMap(o.size()); - for(Map.Entry e : ((Map)o).entrySet()) { - pack(e.getKey()); - pack(e.getValue()); - } - return this; - } - - public Packer pack(MessagePackable o) throws IOException { - if(o == null) { return packNil(); } - o.messagePack(this); - return this; - } - - public Packer pack(Object o) throws IOException { - if(o == null) { return packNil(); } - TemplateRegistry.lookup(o.getClass()).pack(this, o); - return this; - } - - public Packer pack(Object o, Template tmpl) throws IOException { - tmpl.pack(this, o); - return this; - } -} - diff --git a/java/src/main/java/org/msgpack/Template.java b/java/src/main/java/org/msgpack/Template.java deleted file mode 100644 index 8cce999f..00000000 --- a/java/src/main/java/org/msgpack/Template.java +++ /dev/null @@ -1,21 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -public interface Template extends MessagePacker, MessageUnpacker, MessageConverter { -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/Templates.java b/java/src/main/java/org/msgpack/Templates.java deleted file mode 100644 index f268fa0b..00000000 --- a/java/src/main/java/org/msgpack/Templates.java +++ /dev/null @@ -1,119 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import org.msgpack.template.*; - -public class Templates { - public static Template tNullable(Template elementTemplate) { - return new NullableTemplate(elementTemplate); - } - - - public static final Template TAny = AnyTemplate.getInstance(); - public static Template tAny() { - return TAny; - } - - - public static Template tList(Template elementTemplate) { - return new ListTemplate(elementTemplate); - } - - public static Template tMap(Template keyTemplate, Template valueTemplate) { - return new MapTemplate(keyTemplate, valueTemplate); - } - - public static Template tCollection(Template elementTemplate) { - return new CollectionTemplate(elementTemplate); - } - - public static Template tClass(Class target) { - Template tmpl = TemplateRegistry.lookup(target); - if(tmpl == null) { - // FIXME - } - return tmpl; - } - - public static final Template TByte = ByteTemplate.getInstance(); - public static Template tByte() { - return TByte; - } - - public static final Template TShort = ShortTemplate.getInstance(); - public static Template tShort() { - return TShort; - } - - public static final Template TInteger = IntegerTemplate.getInstance(); - public static Template tInteger() { - return TInteger; - } - - public static final Template TLong = LongTemplate.getInstance(); - public static Template tLong() { - return TLong; - } - - public static final Template TBigInteger = BigIntegerTemplate.getInstance(); - public static Template tBigInteger() { - return TBigInteger; - } - - public static final Template TBigDecimal = BigDecimalTemplate.getInstance(); - public static Template tBigDecimal() { - return TBigDecimal; - } - - public static final Template TFloat = FloatTemplate.getInstance(); - public static Template tFloat() { - return TFloat; - } - - public static final Template TDouble = DoubleTemplate.getInstance(); - public static Template tDouble() { - return TDouble; - } - - public static final Template TBoolean = BooleanTemplate.getInstance(); - public static Template tBoolean() { - return TBoolean; - } - - public static final Template TString = StringTemplate.getInstance(); - public static Template tString() { - return TString; - } - - public static final Template TByteArray = ByteArrayTemplate.getInstance(); - public static Template tByteArray() { - return TByteArray; - } - - public static final Template TByteBuffer = ByteBufferTemplate.getInstance(); - public static Template tByteBuffer() { - return TByteBuffer; - } - - public static final Template TDate = DateTemplate.getInstance(); - public static Template tDate() { - return TDate; - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackException.java b/java/src/main/java/org/msgpack/UnpackException.java deleted file mode 100644 index 35e3e44a..00000000 --- a/java/src/main/java/org/msgpack/UnpackException.java +++ /dev/null @@ -1,29 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; - -public class UnpackException extends IOException { - public UnpackException() { } - - public UnpackException(String s) { - super(s); - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackIterator.java b/java/src/main/java/org/msgpack/UnpackIterator.java deleted file mode 100644 index 8c778b67..00000000 --- a/java/src/main/java/org/msgpack/UnpackIterator.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.IOException; -import java.util.Iterator; -import java.util.NoSuchElementException; - -public class UnpackIterator extends UnpackResult implements Iterator { - private Unpacker pac; - - UnpackIterator(Unpacker pac) { - super(); - this.pac = pac; - } - - public boolean hasNext() { - if(finished) { return true; } - try { - return pac.next(this); - } catch (IOException e) { - return false; - } - } - - public MessagePackObject next() { - if(!finished && !hasNext()) { - throw new NoSuchElementException(); - } - finished = false; - return data; - } - - public void remove() { - throw new UnsupportedOperationException(); - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackResult.java b/java/src/main/java/org/msgpack/UnpackResult.java deleted file mode 100644 index bb981c12..00000000 --- a/java/src/main/java/org/msgpack/UnpackResult.java +++ /dev/null @@ -1,42 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -public class UnpackResult { - protected boolean finished = false; - protected MessagePackObject data = null; - - public boolean isFinished() { - return finished; - } - - public MessagePackObject getData() { - return data; - } - - public void reset() { - finished = false; - data = null; - } - - void done(MessagePackObject obj) { - finished = true; - data = obj; - } -} - diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java deleted file mode 100644 index 01e3ea1b..00000000 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ /dev/null @@ -1,610 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.lang.Iterable; -import java.io.InputStream; -import java.io.IOException; -import java.util.Iterator; -import java.nio.ByteBuffer; -import java.math.BigInteger; -import org.msgpack.template.TemplateRegistry; - -/** - * Unpacker enables you to deserialize objects from stream. - * - * Unpacker provides Buffered API, Unbuffered API and - * Direct Conversion API. - * - * Buffered API uses the internal buffer of the Unpacker. - * Following code uses Buffered API with an InputStream: - *
- * // create an unpacker with input stream
- * Unpacker pac = new Unpacker(System.in);
- *
- * // take a object out using next() method, or ...
- * UnpackResult result = pac.next();
- *
- * // use an iterator.
- * for(MessagePackObject obj : pac) {
- *   // use MessageConvertable interface to convert the
- *   // the generic object to the specific type.
- * }
- * 
- * - * Following code doesn't use the input stream and feeds buffer - * using {@link feed(byte[])} method. This is useful to use - * special stream like zlib or event-driven I/O library. - *
- * // create an unpacker without input stream
- * Unpacker pac = new Unpacker();
- *
- * // feed buffer to the internal buffer.
- * pac.feed(input_bytes);
- *
- * // use next() method or iterators.
- * for(MessagePackObject obj : pac) {
- *   // ...
- * }
- * 
- * - * The combination of {@link reserveBuffer()}, {@link getBuffer()}, - * {@link getBufferOffset()}, {@link getBufferCapacity()} and - * {@link bufferConsumed()} is useful to omit copying. - *
- * // create an unpacker without input stream
- * Unpacker pac = new Unpacker();
- *
- * // reserve internal buffer at least 1024 bytes.
- * pac.reserveBuffer(1024);
- *
- * // feed buffer to the internal buffer upto pac.getBufferCapacity() bytes.
- * System.in.read(pac.getBuffer(), pac.getBufferOffset(), pac.getBufferCapacity());
- *
- * // use next() method or iterators.
- * for(MessagePackObject obj : pac) {
- *     // ...
- * }
- * 
- * - * Unbuffered API doesn't initialize the internal buffer. - * You can manage the buffer manually. - *
- * // create an unpacker with input stream
- * Unpacker pac = new Unpacker(System.in);
- *
- * // manage the buffer manually.
- * byte[] buffer = new byte[1024];
- * int filled = System.in.read(buffer);
- * int offset = 0;
- *
- * // deserialize objects using execute() method.
- * int nextOffset = pac.execute(buffer, offset, filled);
- *
- * // take out object if deserialized object is ready.
- * if(pac.isFinished()) {
- *     MessagePackObject obj = pac.getData();
- *     // ...
- * }
- * 
- */ -public class Unpacker implements Iterable { - // buffer: - // +---------------------------------------------+ - // | [object] | [obje| unparsed ... | unused ...| - // +---------------------------------------------+ - // ^ parsed - // ^ offset - // ^ filled - // ^ buffer.length - - private static final int DEFAULT_BUFFER_SIZE = 32*1024; - - protected int parsed; - protected int bufferReserveSize; - protected InputStream stream; - - final class BufferedUnpackerMixin extends BufferedUnpackerImpl { - boolean fill() throws IOException { - if(stream == null) { - return false; - } - reserveBuffer(bufferReserveSize); - int rl = stream.read(buffer, filled, buffer.length - filled); - // equals: stream.read(getBuffer(), getBufferOffset(), getBufferCapacity()); - if(rl <= 0) { - return false; - } - bufferConsumed(rl); - return true; - } - }; - - final BufferedUnpackerMixin impl = new BufferedUnpackerMixin(); - - - /** - * Calls {@link Unpacker(DEFAULT_BUFFER_SIZE)} - */ - public Unpacker() { - this(DEFAULT_BUFFER_SIZE); - } - - /** - * Calls {@link Unpacker(null, bufferReserveSize)} - */ - public Unpacker(int bufferReserveSize) { - this(null, bufferReserveSize); - } - - /** - * Calls {@link Unpacker(stream, DEFAULT_BUFFER_SIZE)} - */ - public Unpacker(InputStream stream) { - this(stream, DEFAULT_BUFFER_SIZE); - } - - /** - * Constructs the unpacker. - * The stream is used to fill the buffer when more buffer is required by {@link next()} or {@link UnpackIterator#hasNext()} method. - * @param stream input stream to fill the buffer - * @param bufferReserveSize threshold size to expand the size of buffer - */ - public Unpacker(InputStream stream, int bufferReserveSize) { - this.parsed = 0; - this.bufferReserveSize = bufferReserveSize/2; - this.stream = stream; - } - - - /** - * Gets the input stream. - * @return the input stream. it may be null. - */ - public InputStream getStream() { - return this.stream; - } - - /** - * Sets the input stream. - * @param stream the input stream to set. - */ - public void setStream(InputStream stream) { - this.stream = stream; - } - - - /** - * Fills the buffer with the specified buffer. - */ - public void feed(byte[] buffer) { - feed(buffer, 0, buffer.length); - } - - /** - * Fills the buffer with the specified buffer. - */ - public void feed(byte[] buffer, int offset, int length) { - reserveBuffer(length); - System.arraycopy(buffer, offset, impl.buffer, impl.offset, length); - bufferConsumed(length); - } - - /** - * Fills the buffer with the specified buffer. - */ - public void feed(ByteBuffer buffer) { - int length = buffer.remaining(); - if (length == 0) return; - reserveBuffer(length); - buffer.get(impl.buffer, impl.offset, length); - bufferConsumed(length); - } - - /** - * Swaps the internal buffer with the specified buffer. - * This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method. - */ - public void wrap(byte[] buffer) { - wrap(buffer, 0, buffer.length); - } - - /** - * Swaps the internal buffer with the specified buffer. - * This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method. - */ - public void wrap(byte[] buffer, int offset, int length) { - impl.buffer = buffer; - impl.offset = offset; - impl.filled = length; - impl.bufferReferenced = false; // TODO zero-copy buffer - } - - /** - * Fills the internal using the input stream. - * @return false if the stream is null or stream.read returns <= 0. - */ - public boolean fill() throws IOException { - return impl.fill(); - } - - - /** - * Returns the iterator that calls {@link next()} method repeatedly. - */ - public Iterator iterator() { - return new UnpackIterator(this); - } - - /** - * Deserializes one object and returns it. - * @return {@link UnpackResult#isFinished()} returns false if the buffer is insufficient to deserialize one object. - */ - public UnpackResult next() throws IOException, UnpackException { - UnpackResult result = new UnpackResult(); - impl.next(result); - return result; - } - - /** - * Deserializes one object and returns it. - * @return false if the buffer is insufficient to deserialize one object. - */ - public boolean next(UnpackResult result) throws IOException, UnpackException { - return impl.next(result); - } - - - /** - * Reserve free space of the internal buffer at least specified size and expands {@link getBufferCapacity()}. - */ - public void reserveBuffer(int require) { - if(impl.buffer == null) { - int nextSize = (bufferReserveSize < require) ? require : bufferReserveSize; - impl.buffer = new byte[nextSize]; - impl.bufferReferenced = false; // TODO zero-copy buffer - return; - } - - if(!impl.bufferReferenced) { // TODO zero-copy buffer - if(impl.filled <= impl.offset) { - // rewind the buffer - impl.filled = 0; - impl.offset = 0; - } - } - - if(impl.buffer.length - impl.filled >= require) { - return; - } - - int nextSize = impl.buffer.length * 2; - int notParsed = impl.filled - impl.offset; - while(nextSize < require + notParsed) { - nextSize *= 2; - } - - byte[] tmp = new byte[nextSize]; - System.arraycopy(impl.buffer, impl.offset, tmp, 0, impl.filled - impl.offset); - - impl.buffer = tmp; - impl.filled = notParsed; - impl.offset = 0; - impl.bufferReferenced = false; // TODO zero-copy buffer - } - - /** - * Returns the internal buffer. - */ - public byte[] getBuffer() { - return impl.buffer; - } - - /** - * Returns the size of free space of the internal buffer. - */ - public int getBufferCapacity() { - return impl.buffer.length - impl.filled; - } - - /** - * Returns the offset of free space in the internal buffer. - */ - public int getBufferOffset() { - return impl.filled; - } - - /** - * Moves front the offset of the free space in the internal buffer. - * Call this method after fill the buffer manually using {@link reserveBuffer()}, {@link getBuffer()}, {@link getBufferOffset()} and {@link getBufferCapacity()} methods. - */ - public void bufferConsumed(int size) { - impl.filled += size; - } - - /** - * Deserializes one object upto the offset of the internal buffer. - * Call {@link reset()} method before calling this method again. - * @return true if one object is deserialized. Use {@link getData()} to get the deserialized object. - */ - public boolean execute() throws UnpackException { - int noffset = impl.execute(impl.buffer, impl.offset, impl.filled); - if(noffset <= impl.offset) { - return false; - } - parsed += noffset - impl.offset; - impl.offset = noffset; - return impl.isFinished(); - } - - - /** - * Deserializes one object over the specified buffer. - * This method doesn't use the internal buffer. - * Use {@link isFinished()} method to known a object is ready to get. - * Call {@link reset()} method before calling this method again. - * @return offset position that is parsed. - */ - public int execute(byte[] buffer) throws UnpackException { - return execute(buffer, 0, buffer.length); - } - - /** - * Deserializes one object over the specified buffer. - * This method doesn't use the internal buffer. - * Use {@link isFinished()} method to known a object is ready to get. - * Call {@link reset()} method before calling this method again. - * @return offset position that is parsed. - */ - public int execute(byte[] buffer, int offset, int length) throws UnpackException { - int noffset = impl.execute(buffer, offset, length); - if(impl.isFinished()) { - impl.resetState(); - } - return noffset; - } - - /** - * Gets the object deserialized by {@link execute(byte[])} method. - */ - public MessagePackObject getData() { - return impl.getData(); - } - - /** - * Returns true if an object is ready to get with {@link getData()} method. - */ - public boolean isFinished() { - return impl.isFinished(); - } - - /** - * Resets the internal state of the unpacker. - */ - public void reset() { - impl.reset(); - } - - public int getMessageSize() { - return parsed - impl.offset + impl.filled; - } - - public int getParsedSize() { - return parsed; - } - - public int getNonParsedSize() { - return impl.filled - impl.offset; - } - - public void skipNonparsedBuffer(int size) { - impl.offset += size; - } - - public void removeNonparsedBuffer() { - impl.filled = impl.offset; - } - - - /** - * Gets one {@code byte} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code byte}. - */ - public byte unpackByte() throws IOException, MessageTypeException { - return impl.unpackByte(); - } - - /** - * Gets one {@code short} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code short}. - */ - public short unpackShort() throws IOException, MessageTypeException { - return impl.unpackShort(); - } - - /** - * Gets one {@code int} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code int}. - */ - public int unpackInt() throws IOException, MessageTypeException { - return impl.unpackInt(); - } - - /** - * Gets one {@code long} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code long}. - */ - public long unpackLong() throws IOException, MessageTypeException { - return impl.unpackLong(); - } - - /** - * Gets one {@code BigInteger} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code BigInteger}. - */ - public BigInteger unpackBigInteger() throws IOException, MessageTypeException { - return impl.unpackBigInteger(); - } - - /** - * Gets one {@code float} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code float}. - */ - public float unpackFloat() throws IOException, MessageTypeException { - return impl.unpackFloat(); - } - - /** - * Gets one {@code double} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code double}. - */ - public double unpackDouble() throws IOException, MessageTypeException { - return impl.unpackDouble(); - } - - /** - * Gets one {@code null} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code null}. - */ - public Object unpackNull() throws IOException, MessageTypeException { - return impl.unpackNull(); - } - - /** - * Gets one {@code boolean} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code boolean}. - */ - public boolean unpackBoolean() throws IOException, MessageTypeException { - return impl.unpackBoolean(); - } - - /** - * Gets one array header from the buffer. - * This method calls {@link fill()} method if needed. - * @return the length of the map. There are {@code retval} objects to get. - * @throws MessageTypeException the first value of the buffer is not a array. - */ - public int unpackArray() throws IOException, MessageTypeException { - return impl.unpackArray(); - } - - /** - * Gets one map header from the buffer. - * This method calls {@link fill()} method if needed. - * @return the length of the map. There are {@code retval * 2} objects to get. - * @throws MessageTypeException the first value of the buffer is not a map. - */ - public int unpackMap() throws IOException, MessageTypeException { - return impl.unpackMap(); - } - - /** - * Gets one raw header from the buffer. - * This method calls {@link fill()} method if needed. - * @return the length of the raw bytes. There are {@code retval} bytes to get. - * @throws MessageTypeException the first value of the buffer is not a raw bytes. - */ - public int unpackRaw() throws IOException, MessageTypeException { - return impl.unpackRaw(); - } - - /** - * Gets one raw body from the buffer. - * This method calls {@link fill()} method if needed. - */ - public byte[] unpackRawBody(int length) throws IOException { - return impl.unpackRawBody(length); - } - - - /** - * Gets one raw object (header + body) from the buffer. - * This method calls {@link fill()} method if needed. - */ - public byte[] unpackByteArray() throws IOException { - return impl.unpackByteArray(); - } - - /** - * Gets one raw object (header + body) from the buffer. - * This method calls {@link fill()} method if needed. - */ - public ByteBuffer unpackByteBuffer() throws IOException { - return impl.unpackByteBuffer(); - } - - /** - * Gets one {@code String} value from the buffer. - * This method calls {@link fill()} method if needed. - * @throws MessageTypeException the first value of the buffer is not a {@code String}. - */ - final public String unpackString() throws IOException, MessageTypeException { - return impl.unpackString(); - } - - /** - * Gets one {@code Object} value from the buffer. - * This method calls {@link fill()} method if needed. - */ - final public MessagePackObject unpackObject() throws IOException { - return impl.unpackObject(); - } - - final public boolean tryUnpackNull() throws IOException { - return impl.tryUnpackNull(); - } - - final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException { - obj.messageUnpack(this); - } - - //final public MessagePackObject unpack() throws IOException { - // return unpackObject(); - //} - - final public T unpack(T to) throws IOException, MessageTypeException { - return unpack((Class)to.getClass(), to); - } - - final public T unpack(Class klass) throws IOException, MessageTypeException { - return unpack(klass, null); - } - - final public T unpack(Class klass, T to) throws IOException, MessageTypeException { - if(tryUnpackNull()) { return null; } - return (T)TemplateRegistry.lookup(klass).unpack(this, to); - } - - final public Object unpack(Template tmpl) throws IOException, MessageTypeException { - return unpack(tmpl, null); - } - - final public T unpack(Template tmpl, T to) throws IOException, MessageTypeException { - return (T)tmpl.unpack(this, to); - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java deleted file mode 100644 index 30456be9..00000000 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ /dev/null @@ -1,473 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.nio.ByteBuffer; -import java.math.BigInteger; -import org.msgpack.object.*; - -public class UnpackerImpl { - static final int CS_HEADER = 0x00; - static final int CS_FLOAT = 0x0a; - static final int CS_DOUBLE = 0x0b; - static final int CS_UINT_8 = 0x0c; - static final int CS_UINT_16 = 0x0d; - static final int CS_UINT_32 = 0x0e; - static final int CS_UINT_64 = 0x0f; - static final int CS_INT_8 = 0x10; - static final int CS_INT_16 = 0x11; - static final int CS_INT_32 = 0x12; - static final int CS_INT_64 = 0x13; - static final int CS_RAW_16 = 0x1a; - static final int CS_RAW_32 = 0x1b; - static final int CS_ARRAY_16 = 0x1c; - static final int CS_ARRAY_32 = 0x1d; - static final int CS_MAP_16 = 0x1e; - static final int CS_MAP_32 = 0x1f; - static final int ACS_RAW_VALUE = 0x20; - static final int CT_ARRAY_ITEM = 0x00; - static final int CT_MAP_KEY = 0x01; - static final int CT_MAP_VALUE = 0x02; - - static final int MAX_STACK_SIZE = 32; - - private int cs; - private int trail; - private int top; - private int[] stack_ct = new int[MAX_STACK_SIZE]; - private int[] stack_count = new int[MAX_STACK_SIZE]; - private Object[] stack_obj = new Object[MAX_STACK_SIZE]; - private int top_ct; - private int top_count; - private Object top_obj; - private ByteBuffer castBuffer = ByteBuffer.allocate(8); - private boolean finished = false; - private MessagePackObject data = null; - - public UnpackerImpl() - { - reset(); - } - - public final MessagePackObject getData() - { - return data; - } - - public final boolean isFinished() - { - return finished; - } - - public final void resetState() { - cs = CS_HEADER; - top = -1; - top_ct = 0; - top_count = 0; - top_obj = null; - } - - public final void reset() - { - resetState(); - finished = false; - data = null; - } - - @SuppressWarnings("unchecked") - public final int execute(byte[] src, int off, int length) throws UnpackException - { - if(off >= length) { return off; } - - int limit = length; - int i = off; - int count; - - Object obj = null; - - _out: do { - _header_again: { - //System.out.println("while i:"+i+" limit:"+limit); - - int b = src[i]; - - _push: { - _fixed_trail_again: - if(cs == CS_HEADER) { - - if((b & 0x80) == 0) { // Positive Fixnum - //System.out.println("positive fixnum "+b); - obj = IntegerType.create((byte)b); - break _push; - } - - if((b & 0xe0) == 0xe0) { // Negative Fixnum - //System.out.println("negative fixnum "+b); - obj = IntegerType.create((byte)b); - break _push; - } - - if((b & 0xe0) == 0xa0) { // FixRaw - trail = b & 0x1f; - if(trail == 0) { - obj = RawType.create(new byte[0]); - break _push; - } - cs = ACS_RAW_VALUE; - break _fixed_trail_again; - } - - if((b & 0xf0) == 0x90) { // FixArray - if(top >= MAX_STACK_SIZE) { - throw new UnpackException("parse error"); - } - count = b & 0x0f; - //System.out.println("fixarray count:"+count); - obj = new MessagePackObject[count]; - if(count == 0) { - obj = ArrayType.create((MessagePackObject[])obj); - break _push; - } - ++top; - stack_obj[top] = top_obj; - stack_ct[top] = top_ct; - stack_count[top] = top_count; - top_obj = obj; - top_ct = CT_ARRAY_ITEM; - top_count = count; - break _header_again; - } - - if((b & 0xf0) == 0x80) { // FixMap - if(top >= MAX_STACK_SIZE) { - throw new UnpackException("parse error"); - } - count = b & 0x0f; - obj = new MessagePackObject[count*2]; - if(count == 0) { - obj = MapType.create((MessagePackObject[])obj); - break _push; - } - //System.out.println("fixmap count:"+count); - ++top; - stack_obj[top] = top_obj; - stack_ct[top] = top_ct; - stack_count[top] = top_count; - top_obj = obj; - top_ct = CT_MAP_KEY; - top_count = count; - break _header_again; - } - - switch(b & 0xff) { // FIXME - case 0xc0: // nil - obj = NilType.create(); - break _push; - case 0xc2: // false - obj = BooleanType.create(false); - break _push; - case 0xc3: // true - obj = BooleanType.create(true); - break _push; - case 0xca: // float - case 0xcb: // double - case 0xcc: // unsigned int 8 - case 0xcd: // unsigned int 16 - case 0xce: // unsigned int 32 - case 0xcf: // unsigned int 64 - case 0xd0: // signed int 8 - case 0xd1: // signed int 16 - case 0xd2: // signed int 32 - case 0xd3: // signed int 64 - trail = 1 << (b & 0x03); - cs = b & 0x1f; - //System.out.println("a trail "+trail+" cs:"+cs); - break _fixed_trail_again; - case 0xda: // raw 16 - case 0xdb: // raw 32 - case 0xdc: // array 16 - case 0xdd: // array 32 - case 0xde: // map 16 - case 0xdf: // map 32 - trail = 2 << (b & 0x01); - cs = b & 0x1f; - //System.out.println("b trail "+trail+" cs:"+cs); - break _fixed_trail_again; - default: - //System.out.println("unknown b "+(b&0xff)); - throw new UnpackException("parse error"); - } - - } // _fixed_trail_again - - do { - _fixed_trail_again: { - - if(limit - i <= trail) { break _out; } - int n = i + 1; - i += trail; - - switch(cs) { - case CS_FLOAT: - castBuffer.rewind(); - castBuffer.put(src, n, 4); - obj = FloatType.create( castBuffer.getFloat(0) ); - //System.out.println("float "+obj); - break _push; - case CS_DOUBLE: - castBuffer.rewind(); - castBuffer.put(src, n, 8); - obj = FloatType.create( castBuffer.getDouble(0) ); - //System.out.println("double "+obj); - break _push; - case CS_UINT_8: - //System.out.println(n); - //System.out.println(src[n]); - //System.out.println(src[n+1]); - //System.out.println(src[n-1]); - obj = IntegerType.create( (short)((src[n]) & 0xff) ); - //System.out.println("uint8 "+obj); - break _push; - case CS_UINT_16: - //System.out.println(src[n]); - //System.out.println(src[n+1]); - castBuffer.rewind(); - castBuffer.put(src, n, 2); - obj = IntegerType.create( ((int)castBuffer.getShort(0)) & 0xffff ); - //System.out.println("uint 16 "+obj); - break _push; - case CS_UINT_32: - castBuffer.rewind(); - castBuffer.put(src, n, 4); - obj = IntegerType.create( ((long)castBuffer.getInt(0)) & 0xffffffffL ); - //System.out.println("uint 32 "+obj); - break _push; - case CS_UINT_64: - castBuffer.rewind(); - castBuffer.put(src, n, 8); - { - long o = castBuffer.getLong(0); - if(o < 0) { - obj = IntegerType.create(new BigInteger(1, castBuffer.array())); - } else { - obj = IntegerType.create(o); - } - } - break _push; - case CS_INT_8: - obj = IntegerType.create( src[n] ); - break _push; - case CS_INT_16: - castBuffer.rewind(); - castBuffer.put(src, n, 2); - obj = IntegerType.create( castBuffer.getShort(0) ); - break _push; - case CS_INT_32: - castBuffer.rewind(); - castBuffer.put(src, n, 4); - obj = IntegerType.create( castBuffer.getInt(0) ); - break _push; - case CS_INT_64: - castBuffer.rewind(); - castBuffer.put(src, n, 8); - obj = IntegerType.create( castBuffer.getLong(0) ); - break _push; - case CS_RAW_16: - castBuffer.rewind(); - castBuffer.put(src, n, 2); - trail = ((int)castBuffer.getShort(0)) & 0xffff; - if(trail == 0) { - obj = RawType.create(new byte[0]); - break _push; - } - cs = ACS_RAW_VALUE; - break _fixed_trail_again; - case CS_RAW_32: - castBuffer.rewind(); - castBuffer.put(src, n, 4); - // FIXME overflow check - trail = castBuffer.getInt(0) & 0x7fffffff; - if(trail == 0) { - obj = RawType.create(new byte[0]); - break _push; - } - cs = ACS_RAW_VALUE; - break _fixed_trail_again; - case ACS_RAW_VALUE: { - // TODO zero-copy buffer - byte[] raw = new byte[trail]; - System.arraycopy(src, n, raw, 0, trail); - obj = RawType.create(raw); - } - break _push; - case CS_ARRAY_16: - if(top >= MAX_STACK_SIZE) { - throw new UnpackException("parse error"); - } - castBuffer.rewind(); - castBuffer.put(src, n, 2); - count = ((int)castBuffer.getShort(0)) & 0xffff; - obj = new MessagePackObject[count]; - if(count == 0) { - obj = ArrayType.create((MessagePackObject[])obj); - break _push; - } - ++top; - stack_obj[top] = top_obj; - stack_ct[top] = top_ct; - stack_count[top] = top_count; - top_obj = obj; - top_ct = CT_ARRAY_ITEM; - top_count = count; - break _header_again; - case CS_ARRAY_32: - if(top >= MAX_STACK_SIZE) { - throw new UnpackException("parse error"); - } - castBuffer.rewind(); - castBuffer.put(src, n, 4); - // FIXME overflow check - count = castBuffer.getInt(0) & 0x7fffffff; - obj = new MessagePackObject[count]; - if(count == 0) { - obj = ArrayType.create((MessagePackObject[])obj); - break _push; - } - ++top; - stack_obj[top] = top_obj; - stack_ct[top] = top_ct; - stack_count[top] = top_count; - top_obj = obj; - top_ct = CT_ARRAY_ITEM; - top_count = count; - break _header_again; - case CS_MAP_16: - if(top >= MAX_STACK_SIZE) { - throw new UnpackException("parse error"); - } - castBuffer.rewind(); - castBuffer.put(src, n, 2); - count = ((int)castBuffer.getShort(0)) & 0xffff; - obj = new MessagePackObject[count*2]; - if(count == 0) { - obj = MapType.create((MessagePackObject[])obj); - break _push; - } - //System.out.println("fixmap count:"+count); - ++top; - stack_obj[top] = top_obj; - stack_ct[top] = top_ct; - stack_count[top] = top_count; - top_obj = obj; - top_ct = CT_MAP_KEY; - top_count = count; - break _header_again; - case CS_MAP_32: - if(top >= MAX_STACK_SIZE) { - throw new UnpackException("parse error"); - } - castBuffer.rewind(); - castBuffer.put(src, n, 4); - // FIXME overflow check - count = castBuffer.getInt(0) & 0x7fffffff; - obj = new MessagePackObject[count*2]; - if(count == 0) { - obj = MapType.create((MessagePackObject[])obj); - break _push; - } - //System.out.println("fixmap count:"+count); - ++top; - stack_obj[top] = top_obj; - stack_ct[top] = top_ct; - stack_count[top] = top_count; - top_obj = obj; - top_ct = CT_MAP_KEY; - top_count = count; - break _header_again; - default: - throw new UnpackException("parse error"); - } - - } // _fixed_trail_again - } while(true); - } // _push - - do { - _push: { - //System.out.println("push top:"+top); - if(top == -1) { - ++i; - data = (MessagePackObject)obj; - finished = true; - break _out; - } - - switch(top_ct) { - case CT_ARRAY_ITEM: { - //System.out.println("array item "+obj); - Object[] ar = (Object[])top_obj; - ar[ar.length - top_count] = obj; - if(--top_count == 0) { - top_obj = stack_obj[top]; - top_ct = stack_ct[top]; - top_count = stack_count[top]; - obj = ArrayType.create((MessagePackObject[])ar); - stack_obj[top] = null; - --top; - break _push; - } - break _header_again; - } - case CT_MAP_KEY: { - //System.out.println("map key:"+top+" "+obj); - Object[] mp = (Object[])top_obj; - mp[mp.length - top_count*2] = obj; - top_ct = CT_MAP_VALUE; - break _header_again; - } - case CT_MAP_VALUE: { - //System.out.println("map value:"+top+" "+obj); - Object[] mp = (Object[])top_obj; - mp[mp.length - top_count*2 + 1] = obj; - if(--top_count == 0) { - top_obj = stack_obj[top]; - top_ct = stack_ct[top]; - top_count = stack_count[top]; - obj = MapType.create((MessagePackObject[])mp); - stack_obj[top] = null; - --top; - break _push; - } - top_ct = CT_MAP_KEY; - break _header_again; - } - default: - throw new UnpackException("parse error"); - } - } // _push - } while(true); - - } // _header_again - cs = CS_HEADER; - ++i; - } while(i < limit); // _out - - return i; - } -} - diff --git a/java/src/main/java/org/msgpack/annotation/Ignore.java b/java/src/main/java/org/msgpack/annotation/Ignore.java deleted file mode 100644 index e052e58a..00000000 --- a/java/src/main/java/org/msgpack/annotation/Ignore.java +++ /dev/null @@ -1,28 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Ignore { -} diff --git a/java/src/main/java/org/msgpack/annotation/Index.java b/java/src/main/java/org/msgpack/annotation/Index.java deleted file mode 100644 index d3b2181f..00000000 --- a/java/src/main/java/org/msgpack/annotation/Index.java +++ /dev/null @@ -1,29 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Index { - int value(); -} diff --git a/java/src/main/java/org/msgpack/annotation/MessagePackBeans.java b/java/src/main/java/org/msgpack/annotation/MessagePackBeans.java deleted file mode 100644 index bff1b0d2..00000000 --- a/java/src/main/java/org/msgpack/annotation/MessagePackBeans.java +++ /dev/null @@ -1,35 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import org.msgpack.template.FieldOption; - -/** - * Annotation for java beans class - * @author takeshita - * - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface MessagePackBeans { - FieldOption value() default FieldOption.DEFAULT; -} diff --git a/java/src/main/java/org/msgpack/annotation/MessagePackDelegate.java b/java/src/main/java/org/msgpack/annotation/MessagePackDelegate.java deleted file mode 100644 index bb16e2ce..00000000 --- a/java/src/main/java/org/msgpack/annotation/MessagePackDelegate.java +++ /dev/null @@ -1,29 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface MessagePackDelegate { - String value(); -} diff --git a/java/src/main/java/org/msgpack/annotation/MessagePackMessage.java b/java/src/main/java/org/msgpack/annotation/MessagePackMessage.java deleted file mode 100644 index ab3ad0a1..00000000 --- a/java/src/main/java/org/msgpack/annotation/MessagePackMessage.java +++ /dev/null @@ -1,30 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import org.msgpack.template.FieldOption; - -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface MessagePackMessage { - FieldOption value() default FieldOption.DEFAULT; -} diff --git a/java/src/main/java/org/msgpack/annotation/MessagePackOrdinalEnum.java b/java/src/main/java/org/msgpack/annotation/MessagePackOrdinalEnum.java deleted file mode 100644 index eac4767e..00000000 --- a/java/src/main/java/org/msgpack/annotation/MessagePackOrdinalEnum.java +++ /dev/null @@ -1,28 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface MessagePackOrdinalEnum { -} diff --git a/java/src/main/java/org/msgpack/annotation/Nullable.java b/java/src/main/java/org/msgpack/annotation/Nullable.java deleted file mode 100644 index 56aea89e..00000000 --- a/java/src/main/java/org/msgpack/annotation/Nullable.java +++ /dev/null @@ -1,28 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Nullable { -} diff --git a/java/src/main/java/org/msgpack/annotation/Optional.java b/java/src/main/java/org/msgpack/annotation/Optional.java deleted file mode 100644 index a137ec86..00000000 --- a/java/src/main/java/org/msgpack/annotation/Optional.java +++ /dev/null @@ -1,28 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Optional { -} diff --git a/java/src/main/java/org/msgpack/annotation/Required.java b/java/src/main/java/org/msgpack/annotation/Required.java deleted file mode 100644 index de1d816b..00000000 --- a/java/src/main/java/org/msgpack/annotation/Required.java +++ /dev/null @@ -1,28 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Required { -} diff --git a/java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java b/java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java deleted file mode 100644 index 79b9d9eb..00000000 --- a/java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java +++ /dev/null @@ -1,462 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.buffer; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.IOException; -import java.util.List; -import java.util.ArrayList; -import java.nio.ByteBuffer; -import java.nio.channels.GatheringByteChannel; -import java.nio.channels.ScatteringByteChannel; - -public class VectoredByteBuffer implements GatheringByteChannel, ScatteringByteChannel { - private List vec = new ArrayList(); - private ByteBuffer internalBuffer; - private ByteBuffer lastInternalBuffer; - private int chunkSize; - private int referenceThreshold; - - public VectoredByteBuffer() { - this(32*1024); - } - - public VectoredByteBuffer(int chunkSize) { - this(chunkSize, 32); - } - - public VectoredByteBuffer(int chunkSize, int referenceThreshold) { - this.chunkSize = chunkSize; - this.referenceThreshold = referenceThreshold; - internalBuffer = ByteBuffer.allocateDirect(chunkSize); - } - - - public void setChunkSize(int chunkSize) { - this.chunkSize = chunkSize; - } - - public int getChunkSize(int chunkSize) { - return this.chunkSize; - } - - public void setReferenceThreshold(int referenceThreshold) { - this.referenceThreshold = referenceThreshold; - } - - public int getReferenceThreshold(int referenceThreshold) { - return this.referenceThreshold; - } - - - @Override - public void close() { - reset(); - } - - @Override - public boolean isOpen() { - return true; // FIXME? - } - - - public synchronized void reset() { - vec.clear(); - lastInternalBuffer = null; - } - - - public void write(byte[] b) { - write(b, 0, b.length); - } - - public void write(byte[] b, int off, int len) { - if(off < 0 || len < 0 || b.length < off+len) { - throw new IndexOutOfBoundsException(); - } - if(referenceThreshold >= 0 && len > referenceThreshold) { - writeReference(b, off, len); - } else { - writeCopy(b, off, len); - } - } - - public void write(int b) { - byte[] ba = new byte[1]; - ba[0] = (byte)b; - write(ba); - } - - @Override - public int write(ByteBuffer src) { - int slen = src.remaining(); - if(referenceThreshold >= 0 && slen > referenceThreshold) { - writeCopy(src); - } else { - writeReference(src); - } - return slen; - } - - @Override - public synchronized long write(ByteBuffer[] srcs) { - return write(srcs, 0, srcs.length); - } - - @Override - public synchronized long write(ByteBuffer[] srcs, int offset, int length) { - if(offset < 0 || length < 0 || srcs.length < offset+length) { - throw new IndexOutOfBoundsException(); - } - long total = 0; - for(int i=offset; offset < length; offset++) { - ByteBuffer src = srcs[i]; - total += write(src); - } - return total; - } - - private synchronized void writeCopy(byte[] b, int off, int len) { - int ipos = internalBuffer.position(); - if(internalBuffer.capacity() - ipos < len) { - // allocate new buffer - int nsize = chunkSize > len ? chunkSize : len; - internalBuffer = ByteBuffer.allocateDirect(nsize); - ipos = 0; - } else if(internalBuffer == lastInternalBuffer) { - // optimization: concatenates to the last buffer instead - // of adding new reference - ByteBuffer dup = vec.get(vec.size()-1); - internalBuffer.put(b, off, len); - dup.limit(ipos + len); - return; - } - internalBuffer.put(b, off, len); - ByteBuffer dup = internalBuffer.duplicate(); - dup.position(ipos); - dup.mark(); - dup.limit(ipos + len); - vec.add(dup); - lastInternalBuffer = internalBuffer; - } - - private synchronized void writeCopy(ByteBuffer src) { - int slen = src.remaining(); - int ipos = internalBuffer.position(); - if(internalBuffer.capacity() - ipos < slen) { - // allocate new buffer - int nsize = chunkSize > slen ? chunkSize : slen; - internalBuffer = ByteBuffer.allocateDirect(nsize); - ipos = 0; - } else if(internalBuffer == lastInternalBuffer) { - // optimization: concatenates to the last buffer instead - // of adding new reference - ByteBuffer dup = vec.get(vec.size()-1); - int dpos = dup.position(); - internalBuffer.put(src); - ByteBuffer dup2 = internalBuffer.duplicate(); - dup2.position(dpos); - dup2.limit(ipos + slen); - vec.set(vec.size()-1, dup2); - return; - } - internalBuffer.put(src); - ByteBuffer dup = internalBuffer.duplicate(); - dup.position(ipos); - dup.mark(); - dup.limit(ipos + slen); - vec.add(dup); - lastInternalBuffer = internalBuffer; - } - - private synchronized void writeReference(byte[] b, int off, int len) { - ByteBuffer buf = ByteBuffer.wrap(b, off, len); - vec.add(buf); - lastInternalBuffer = null; - } - - private synchronized void writeReference(ByteBuffer src) { - ByteBuffer buf = src.duplicate(); - vec.add(buf); - lastInternalBuffer = null; - } - - - public synchronized void writeTo(java.io.OutputStream out) throws IOException { - byte[] tmpbuf = null; - for(int i=0; i < vec.size(); i++) { - ByteBuffer r = vec.get(i); - int rpos = r.position(); - int rlen = r.limit() - rpos; - if(r.hasArray()) { - byte[] array = r.array(); - out.write(array, rpos, rlen); - } else { - if(tmpbuf == null) { - int max = rlen; - for(int j=i+1; j < vec.size(); j++) { - ByteBuffer c = vec.get(j); - int clen = c.remaining(); - if(max < clen) { - max = clen; - } - } - tmpbuf = new byte[max]; - } - r.get(tmpbuf, 0, rlen); - r.position(rpos); - out.write(tmpbuf, 0, rlen); - } - } - } - - public synchronized byte[] toByteArray() { - byte[] out = new byte[available()]; - int off = 0; - for(ByteBuffer r: vec) { - int rpos = r.position(); - int rlen = r.limit() - rpos; - r.get(out, off, rlen); - r.position(rpos); - off += rlen; - } - return out; - } - - - public synchronized int available() { - int total = 0; - for(ByteBuffer r : vec) { - total += r.remaining(); - } - return total; - } - - public synchronized int read(byte[] b) { - return read(b, 0, b.length); - } - - public synchronized int read(byte[] b, int off, int len) { - if(off < 0 || len < 0 || b.length < off+len) { - throw new IndexOutOfBoundsException(); - } - int start = len; - while(!vec.isEmpty()) { - ByteBuffer r = vec.get(0); - int rlen = r.remaining(); - if(rlen <= len) { - r.get(b, off, rlen); - vec.remove(0); - off += rlen; - len -= rlen; - } else { - r.get(b, off, len); - return start; - } - } - return start - len; - } - - public synchronized int read() { - byte[] ba = new byte[1]; - if(read(ba) >= 1) { - return ba[0]; - } else { - return -1; - } - } - - @Override - public synchronized int read(ByteBuffer dst) { - int len = dst.remaining(); - int start = len; - while(!vec.isEmpty()) { - ByteBuffer r = vec.get(0); - int rlen = r.remaining(); - if(rlen <= len) { - dst.put(r); - vec.remove(0); - len -= rlen; - } else { - int blim = r.limit(); - r.limit(len); - try { - dst.put(r); - } finally { - r.limit(blim); - } - return start; - } - } - return start - len; - } - - @Override - public synchronized long read(ByteBuffer[] dsts) { - return read(dsts, 0, dsts.length); - } - - @Override - public synchronized long read(ByteBuffer[] dsts, int offset, int length) { - if(offset < 0 || length < 0 || dsts.length < offset+length) { - throw new IndexOutOfBoundsException(); - } - long total = 0; - for(int i=offset; i < length; i++) { - ByteBuffer dst = dsts[i]; - int dlen = dst.remaining(); - int rlen = read(dsts[i]); - total += rlen; - if(rlen < dlen) { - return total; - } - } - return total; - } - - public synchronized long read(GatheringByteChannel to) throws IOException { - long total = to.write(vec.toArray(new ByteBuffer[0])); - while(!vec.isEmpty()) { - ByteBuffer r = vec.get(0); - if(r.remaining() == 0) { - vec.remove(0); - } else { - break; - } - } - return total; - } - - public synchronized long skip(long len) { - if(len <= 0) { - return 0; - } - long start = len; - while(!vec.isEmpty()) { - ByteBuffer r = vec.get(0); - int rlen = r.remaining(); - if(rlen <= len) { - r.position(r.position()+rlen); - vec.remove(0); - len -= rlen; - } else { - r.position((int)(r.position()+len)); - return start; - } - } - return start - len; - } - - - public final static class OutputStream extends java.io.OutputStream { - private VectoredByteBuffer vbb; - - OutputStream(VectoredByteBuffer vbb) { - this.vbb = vbb; - } - - @Override - public void write(byte[] b) { - vbb.write(b); - } - - @Override - public void write(byte[] b, int off, int len) { - vbb.write(b, off, len); - } - - @Override - public void write(int b) { - vbb.write(b); - } - - public int write(ByteBuffer src) { - return vbb.write(src); - } - - public long write(ByteBuffer[] srcs) { - return vbb.write(srcs); - } - - public long write(ByteBuffer[] srcs, int offset, int length) { - return vbb.write(srcs, offset, length); - } - - public void writeTo(OutputStream out) throws IOException { - vbb.writeTo(out); - } - - public byte[] toByteArray() { - return vbb.toByteArray(); - } - } - - public final static class InputStream extends java.io.InputStream { - private VectoredByteBuffer vbb; - - InputStream(VectoredByteBuffer vbb) { - this.vbb = vbb; - } - - @Override - public int available() { - return vbb.available(); - } - - @Override - public int read(byte[] b) { - return vbb.read(b); - } - - @Override - public int read(byte[] b, int off, int len) { - return vbb.read(b, off, len); - } - - @Override - public int read() { - return vbb.read(); - } - - public int read(ByteBuffer dst) { - return vbb.read(dst); - } - - public long read(ByteBuffer[] dsts, int offset, int length) { - return vbb.read(dsts, offset, length); - } - - public long read(GatheringByteChannel to) throws IOException { - return vbb.read(to); - } - - public long skip(long len) { - return vbb.skip(len); - } - } - - public OutputStream outputStream() { - return new OutputStream(this); - } - - public InputStream inputStream() { - return new InputStream(this); - } -} - diff --git a/java/src/main/java/org/msgpack/object/ArrayType.java b/java/src/main/java/org/msgpack/object/ArrayType.java deleted file mode 100644 index 36134dc7..00000000 --- a/java/src/main/java/org/msgpack/object/ArrayType.java +++ /dev/null @@ -1,81 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.util.List; -import java.util.Arrays; -import java.io.IOException; -import org.msgpack.*; - -public class ArrayType extends MessagePackObject { - private MessagePackObject[] array; - - ArrayType(MessagePackObject[] array) { - this.array = array; - } - - public static ArrayType create(MessagePackObject[] array) { - return new ArrayType(array); - } - - @Override - public boolean isArrayType() { - return true; - } - - @Override - public MessagePackObject[] asArray() { - return array; - } - - @Override - public List asList() { - return Arrays.asList(array); - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packArray(array.length); - for(int i=0; i < array.length; i++) { - array[i].messagePack(pk); - } - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - return false; - } - return Arrays.equals(((ArrayType)obj).array, array); - } - - @Override - public int hashCode() { - return array.hashCode(); - } - - @Override - public Object clone() { - MessagePackObject[] copy = new MessagePackObject[array.length]; - for(int i=0; i < array.length; i++) { - copy[i] = (MessagePackObject)array[i].clone(); - } - return copy; - } -} - diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java deleted file mode 100644 index b29879f4..00000000 --- a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java +++ /dev/null @@ -1,131 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.math.BigInteger; -import java.io.IOException; -import org.msgpack.*; - -class BigIntegerTypeIMPL extends IntegerType { - private BigInteger value; - - BigIntegerTypeIMPL(BigInteger value) { - this.value = value; - } - - @Override - public byte asByte() { - if(value.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) > 0) { - throw new MessageTypeException("type error"); - } - return value.byteValue(); - } - - @Override - public short asShort() { - if(value.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) > 0) { - throw new MessageTypeException("type error"); - } - return value.shortValue(); - } - - @Override - public int asInt() { - if(value.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) > 0) { - throw new MessageTypeException("type error"); - } - return value.intValue(); - } - - @Override - public long asLong() { - if(value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { - throw new MessageTypeException("type error"); - } - return value.longValue(); - } - - @Override - public BigInteger asBigInteger() { - return value; - } - - @Override - public byte byteValue() { - return value.byteValue(); - } - - @Override - public short shortValue() { - return value.shortValue(); - } - - @Override - public int intValue() { - return value.intValue(); - } - - @Override - public long longValue() { - return value.longValue(); - } - - @Override - public BigInteger bigIntegerValue() { - return value; - } - - @Override - public float floatValue() { - return value.floatValue(); - } - - @Override - public double doubleValue() { - return value.doubleValue(); - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packBigInteger(value); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - if(obj.getClass() == ShortIntegerTypeIMPL.class) { - return BigInteger.valueOf(((ShortIntegerTypeIMPL)obj).longValue()).equals(value); - } else if(obj.getClass() == LongIntegerTypeIMPL.class) { - return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value); - } - return false; - } - return ((BigIntegerTypeIMPL)obj).value.equals(value); - } - - @Override - public int hashCode() { - return value.hashCode(); - } - - @Override - public Object clone() { - return new BigIntegerTypeIMPL(value); - } -} - diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java deleted file mode 100644 index 65bd42b4..00000000 --- a/java/src/main/java/org/msgpack/object/BooleanType.java +++ /dev/null @@ -1,71 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.io.IOException; -import org.msgpack.*; - -public class BooleanType extends MessagePackObject { - private boolean value; - - BooleanType(boolean value) { - this.value = value; - } - - public static BooleanType create(boolean value) { - return new BooleanType(value); - } - - @Override - public boolean isBooleanType() { - return true; - } - - @Override - public boolean asBoolean() { - return value; - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packBoolean(value); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - return false; - } - return ((BooleanType)obj).value == value; - } - - @Override - public int hashCode() { - if(value) { - return 1231; - } else { - return 1237; - } - } - - @Override - public Object clone() { - return new BooleanType(value); - } -} - diff --git a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java deleted file mode 100644 index fd38089e..00000000 --- a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java +++ /dev/null @@ -1,101 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.math.BigInteger; -import java.io.IOException; -import org.msgpack.*; - -class DoubleTypeIMPL extends FloatType { - private double value; - - public DoubleTypeIMPL(double value) { - this.value = value; - } - - @Override - public float asFloat() { - // FIXME check overflow, underflow? - return (float)value; - } - - @Override - public double asDouble() { - return value; - } - - @Override - public byte byteValue() { - return (byte)value; - } - - @Override - public short shortValue() { - return (short)value; - } - - @Override - public int intValue() { - return (int)value; - } - - @Override - public long longValue() { - return (long)value; - } - - @Override - public BigInteger bigIntegerValue() { - return BigInteger.valueOf((long)value); - } - - @Override - public float floatValue() { - return (float)value; - } - - @Override - public double doubleValue() { - return (double)value; - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packDouble(value); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - return false; - } - return ((DoubleTypeIMPL)obj).value == value; - } - - @Override - public int hashCode() { - long v = Double.doubleToLongBits(value); - return (int)(v^(v>>>32)); - } - - @Override - public Object clone() { - return new DoubleTypeIMPL(value); - } -} - diff --git a/java/src/main/java/org/msgpack/object/FloatType.java b/java/src/main/java/org/msgpack/object/FloatType.java deleted file mode 100644 index 514efd5b..00000000 --- a/java/src/main/java/org/msgpack/object/FloatType.java +++ /dev/null @@ -1,36 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import org.msgpack.*; - -public abstract class FloatType extends MessagePackObject { - @Override - public boolean isFloatType() { - return true; - } - - public static FloatType create(float value) { - return new FloatTypeIMPL(value); - } - - public static FloatType create(double value) { - return new DoubleTypeIMPL(value); - } -} - diff --git a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java deleted file mode 100644 index 1d799611..00000000 --- a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java +++ /dev/null @@ -1,94 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.math.BigInteger; -import java.io.IOException; -import org.msgpack.*; - -class FloatTypeIMPL extends FloatType { - private float value; - - public FloatTypeIMPL(float value) { - this.value = value; - } - - @Override - public float asFloat() { - return value; - } - - @Override - public double asDouble() { - return (double)value; - } - - @Override - public byte byteValue() { - return (byte)value; - } - - @Override - public short shortValue() { - return (short)value; - } - - @Override - public int intValue() { - return (int)value; - } - - @Override - public long longValue() { - return (long)value; - } - - @Override - public float floatValue() { - return (float)value; - } - - @Override - public double doubleValue() { - return (double)value; - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packFloat(value); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - return false; - } - return ((FloatTypeIMPL)obj).value == value; - } - - @Override - public int hashCode() { - return Float.floatToIntBits(value); - } - - @Override - public Object clone() { - return new FloatTypeIMPL(value); - } -} - diff --git a/java/src/main/java/org/msgpack/object/IntegerType.java b/java/src/main/java/org/msgpack/object/IntegerType.java deleted file mode 100644 index 43357e8e..00000000 --- a/java/src/main/java/org/msgpack/object/IntegerType.java +++ /dev/null @@ -1,49 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.math.BigInteger; -import org.msgpack.*; - -public abstract class IntegerType extends MessagePackObject { - @Override - public boolean isIntegerType() { - return true; - } - - public static IntegerType create(byte value) { - return new ShortIntegerTypeIMPL((int)value); - } - - public static IntegerType create(short value) { - return new ShortIntegerTypeIMPL((int)value); - } - - public static IntegerType create(int value) { - return new ShortIntegerTypeIMPL(value); - } - - public static IntegerType create(long value) { - return new LongIntegerTypeIMPL(value); - } - - public static IntegerType create(BigInteger value) { - return new BigIntegerTypeIMPL(value); - } -} - diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java deleted file mode 100644 index d052e77a..00000000 --- a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java +++ /dev/null @@ -1,128 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.math.BigInteger; -import java.io.IOException; -import org.msgpack.*; - -class LongIntegerTypeIMPL extends IntegerType { - private long value; - - LongIntegerTypeIMPL(long value) { - this.value = value; - } - - @Override - public byte asByte() { - if(value > (long)Byte.MAX_VALUE) { - throw new MessageTypeException("type error"); - } - return (byte)value; - } - - @Override - public short asShort() { - if(value > (long)Short.MAX_VALUE) { - throw new MessageTypeException("type error"); - } - return (short)value; - } - - @Override - public int asInt() { - if(value > (long)Integer.MAX_VALUE) { - throw new MessageTypeException("type error"); - } - return (int)value; - } - - @Override - public long asLong() { - return value; - } - - @Override - public BigInteger asBigInteger() { - return BigInteger.valueOf(value); - } - - @Override - public byte byteValue() { - return (byte)value; - } - - @Override - public short shortValue() { - return (short)value; - } - - @Override - public int intValue() { - return (int)value; - } - - @Override - public long longValue() { - return (long)value; - } - - @Override - public BigInteger bigIntegerValue() { - return BigInteger.valueOf(value); - } - - @Override - public float floatValue() { - return (float)value; - } - - @Override - public double doubleValue() { - return (double)value; - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packLong(value); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - if(obj.getClass() == ShortIntegerTypeIMPL.class) { - return value == ((ShortIntegerTypeIMPL)obj).longValue(); - } else if(obj.getClass() == BigIntegerTypeIMPL.class) { - return BigInteger.valueOf(value).equals(((BigIntegerTypeIMPL)obj).bigIntegerValue()); - } - return false; - } - return ((LongIntegerTypeIMPL)obj).value == value; - } - - @Override - public int hashCode() { - return (int)(value^(value>>>32)); - } - - @Override - public Object clone() { - return new LongIntegerTypeIMPL(value); - } -} - diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java deleted file mode 100644 index 148c0bf4..00000000 --- a/java/src/main/java/org/msgpack/object/MapType.java +++ /dev/null @@ -1,84 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.util.HashMap; -import java.util.Map; -import java.util.Arrays; -import java.io.IOException; -import org.msgpack.*; - -public class MapType extends MessagePackObject { - private MessagePackObject[] map; - - MapType(MessagePackObject[] map) { - this.map = map; - } - - public static MapType create(MessagePackObject[] map) { - return new MapType(map); - } - - @Override - public boolean isMapType() { - return true; - } - - @Override - public Map asMap() { - HashMap m = new HashMap(map.length / 2); - int i = 0; - while(i < map.length) { - MessagePackObject k = map[i++]; - MessagePackObject v = map[i++]; - m.put(k, v); - } - return m; - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packMap(map.length / 2); - for(int i=0; i < map.length; i++) { - map[i].messagePack(pk); - } - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - return false; - } - return Arrays.equals(((MapType)obj).map, map); - } - - @Override - public int hashCode() { - return map.hashCode(); - } - - @Override - public Object clone() { - MessagePackObject[] copy = new MessagePackObject[map.length]; - for(int i=0; i < map.length; i++) { - copy[i] = (MessagePackObject)map[i].clone(); - } - return copy; - } -} - diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java deleted file mode 100644 index c443db18..00000000 --- a/java/src/main/java/org/msgpack/object/NilType.java +++ /dev/null @@ -1,60 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.io.IOException; -import org.msgpack.*; - -public class NilType extends MessagePackObject { - private final static NilType INSTANCE = new NilType(); - - public static NilType create() { - return INSTANCE; - } - - private NilType() { } - - @Override - public boolean isNil() { - return true; - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packNil(); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - return false; - } - return true; - } - - @Override - public int hashCode() { - return 0; - } - - @Override - public Object clone() { - return INSTANCE; - } -} - diff --git a/java/src/main/java/org/msgpack/object/RawType.java b/java/src/main/java/org/msgpack/object/RawType.java deleted file mode 100644 index f6866c46..00000000 --- a/java/src/main/java/org/msgpack/object/RawType.java +++ /dev/null @@ -1,90 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.util.Arrays; -import java.io.IOException; -import org.msgpack.*; - -public class RawType extends MessagePackObject { - private byte[] bytes; - - RawType(byte[] bytes) { - this.bytes = bytes; - } - - RawType(String str) { - try { - this.bytes = str.getBytes("UTF-8"); - } catch (Exception e) { - throw new MessageTypeException("type error"); - } - } - - public static RawType create(byte[] bytes) { - return new RawType(bytes); - } - - public static RawType create(String str) { - return new RawType(str); - } - - @Override - public boolean isRawType() { - return true; - } - - @Override - public byte[] asByteArray() { - return bytes; - } - - @Override - public String asString() { - try { - return new String(bytes, "UTF-8"); - } catch (Exception e) { - throw new MessageTypeException("type error"); - } - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packRaw(bytes.length); - pk.packRawBody(bytes); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - return false; - } - return Arrays.equals(((RawType)obj).bytes, bytes); - } - - @Override - public int hashCode() { - return Arrays.hashCode(bytes); - } - - @Override - public Object clone() { - return new RawType((byte[])bytes.clone()); - } -} - diff --git a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java deleted file mode 100644 index dbed4263..00000000 --- a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java +++ /dev/null @@ -1,125 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.object; - -import java.math.BigInteger; -import java.io.IOException; -import org.msgpack.*; - -class ShortIntegerTypeIMPL extends IntegerType { - private int value; - - ShortIntegerTypeIMPL(int value) { - this.value = value; - } - - @Override - public byte asByte() { - if(value > (int)Byte.MAX_VALUE) { - throw new MessageTypeException("type error"); - } - return (byte)value; - } - - @Override - public short asShort() { - if(value > (int)Short.MAX_VALUE) { - throw new MessageTypeException("type error"); - } - return (short)value; - } - - @Override - public int asInt() { - return value; - } - - @Override - public long asLong() { - return value; - } - - @Override - public BigInteger asBigInteger() { - return BigInteger.valueOf((long)value); - } - - @Override - public byte byteValue() { - return (byte)value; - } - - @Override - public short shortValue() { - return (short)value; - } - - @Override - public int intValue() { - return (int)value; - } - - @Override - public long longValue() { - return (long)value; - } - - @Override - public BigInteger bigIntegerValue() { - return BigInteger.valueOf((long)value); - } - - @Override - public float floatValue() { - return (float)value; - } - - @Override - public double doubleValue() { - return (double)value; - } - - @Override - public void messagePack(Packer pk) throws IOException { - pk.packInt(value); - } - - @Override - public boolean equals(Object obj) { - if(obj.getClass() != getClass()) { - if(obj.getClass() == LongIntegerTypeIMPL.class) { - return (long)value == ((LongIntegerTypeIMPL)obj).longValue(); - } else if(obj.getClass() == BigIntegerTypeIMPL.class) { - return ((BigIntegerTypeIMPL)obj).bigIntegerValue().equals(BigInteger.valueOf((long)value)); - } - return false; - } - return ((ShortIntegerTypeIMPL)obj).value == value; - } - - @Override - public int hashCode() { - return value; - } - - @Override - public Object clone() { - return new ShortIntegerTypeIMPL(value); - } -} - diff --git a/java/src/main/java/org/msgpack/package-info.java b/java/src/main/java/org/msgpack/package-info.java deleted file mode 100644 index 7e9b8a27..00000000 --- a/java/src/main/java/org/msgpack/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * MessagePack is a binary-based efficient object serialization library. - * It enables to exchange structured objects between many languages like JSON. - * But unlike JSON, it is very fast and small. - * - * Use {@link Packer} to serialize and {@link Unpacker} to deserialize. - */ -package org.msgpack; diff --git a/java/src/main/java/org/msgpack/template/AnyTemplate.java b/java/src/main/java/org/msgpack/template/AnyTemplate.java deleted file mode 100644 index f628388b..00000000 --- a/java/src/main/java/org/msgpack/template/AnyTemplate.java +++ /dev/null @@ -1,54 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class AnyTemplate implements Template { - private AnyTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - if(target instanceof MessagePackObject) { - pk.pack((MessagePackObject)target); - } else if(target == null) { - pk.packNil(); - } else { - TemplateRegistry.lookup(target.getClass()).pack(pk, target); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackObject(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from; - } - - static public AnyTemplate getInstance() { - return instance; - } - - static final AnyTemplate instance = new AnyTemplate(); - - static { - TemplateRegistry.register(MessagePackObject.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/BeansFieldEntry.java b/java/src/main/java/org/msgpack/template/BeansFieldEntry.java deleted file mode 100644 index 5d5ff816..00000000 --- a/java/src/main/java/org/msgpack/template/BeansFieldEntry.java +++ /dev/null @@ -1,143 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.beans.PropertyDescriptor; -import java.lang.reflect.*; -import org.msgpack.*; - -/** - * Field entry for Java beans property. - * @author takeshita - * - */ -public class BeansFieldEntry implements IFieldEntry { - - PropertyDescriptor desc; - FieldOption option = FieldOption.DEFAULT; - - public BeansFieldEntry(PropertyDescriptor desc) { - this.desc = desc; - } - - @Override - public String getName() { - return desc.getDisplayName(); - } - public String getGetterName(){ - return desc.getReadMethod().getName(); - } - public String getSetterName(){ - return desc.getWriteMethod().getName(); - } - - @Override - public Class getType() { - return desc.getPropertyType(); - } - - @Override - public String getJavaTypeName() { - Class type = getType(); - if(type.isArray()) { - return arrayTypeToString(type); - } else { - return type.getName(); - } - } - static String arrayTypeToString(Class type) { - int dim = 1; - Class baseType = type.getComponentType(); - while(baseType.isArray()) { - baseType = baseType.getComponentType(); - dim += 1; - } - StringBuilder sb = new StringBuilder(); - sb.append(baseType.getName()); - for (int i = 0; i < dim; ++i) { - sb.append("[]"); - } - return sb.toString(); - } - - @Override - public Type getGenericType() { - return desc.getReadMethod().getGenericReturnType(); - } - - @Override - public FieldOption getOption() { - return option; - } - public void setOption(FieldOption option){ - this.option = option; - } - - @Override - public boolean isAvailable() { - return option != FieldOption.IGNORE; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#isRequired() - */ - @Override - public boolean isRequired() { - return option == FieldOption.REQUIRED; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#isOptional() - */ - @Override - public boolean isOptional() { - return option == FieldOption.OPTIONAL; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#isNullable() - */ - @Override - public boolean isNullable() { - return option == FieldOption.NULLABLE; - } - - public Object get(Object target){ - try { - return desc.getReadMethod().invoke(target); - } catch (IllegalArgumentException e) { - throw new MessageTypeException(e); - } catch (IllegalAccessException e) { - throw new MessageTypeException(e); - } catch (InvocationTargetException e) { - throw new MessageTypeException(e); - } - } - public void set(Object target , Object value){ - try { - desc.getWriteMethod().invoke(target, value); - } catch (IllegalArgumentException e) { - throw new MessageTypeException(e); - } catch (IllegalAccessException e) { - throw new MessageTypeException(e); - } catch (InvocationTargetException e) { - throw new MessageTypeException(e); - } - } - -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/template/BeansFieldEntryReader.java b/java/src/main/java/org/msgpack/template/BeansFieldEntryReader.java deleted file mode 100644 index 9d3039b2..00000000 --- a/java/src/main/java/org/msgpack/template/BeansFieldEntryReader.java +++ /dev/null @@ -1,188 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.lang.annotation.Annotation; -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; - -import org.msgpack.annotation.Ignore; -import org.msgpack.annotation.Index; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.Nullable; -import org.msgpack.annotation.Optional; -import org.msgpack.annotation.Required; - -/** - * List up Java beans property methods. - * @author takeshita - * - */ -public class BeansFieldEntryReader implements IFieldEntryReader{ - - - public IFieldEntry[] convertFieldEntries(Class targetClass, FieldList flist) throws NoSuchFieldException { - List src = flist.getList(); - FieldEntry[] result = new FieldEntry[src.size()]; - for(int i=0; i < src.size(); i++) { - FieldList.Entry s = src.get(i); - if(s.isAvailable()) { - result[i] = new FieldEntry(targetClass.getDeclaredField(s.getName()), s.getOption()); - } else { - result[i] = new FieldEntry(); - } - } - return result; - } - - @Override - public IFieldEntry[] readFieldEntries(Class targetClass, - FieldOption implicitOption) { - BeanInfo desc; - try { - desc = Introspector.getBeanInfo(targetClass); - } catch (IntrospectionException e1) { - throw new TemplateBuildException("Class must be java beans class:" + targetClass.getName()); - } - - PropertyDescriptor[] props = desc.getPropertyDescriptors(); - ArrayList list = new ArrayList(); - for(int i = 0;i < props.length;i++){ - PropertyDescriptor pd = props[i]; - if(!isIgnoreProp(pd)){ - list.add(pd); - } - } - props = new PropertyDescriptor[list.size()]; - list.toArray(props); - - BeansFieldEntry[] entries = new BeansFieldEntry[props.length]; - for(int i = 0;i < props.length;i++){ - PropertyDescriptor p = props[i]; - int index = readPropIndex(p); - if(index >= 0){ - if(entries[index] != null){ - throw new TemplateBuildException("duplicated index: "+index); - } - if(index >= entries.length){ - throw new TemplateBuildException("invalid index: "+index); - } - entries[index] = new BeansFieldEntry(p); - props[index] = null; - } - } - int insertIndex = 0; - for(int i = 0;i < props.length;i++){ - PropertyDescriptor p = props[i]; - if(p != null){ - while(entries[insertIndex] != null){ - insertIndex++; - } - entries[insertIndex] = new BeansFieldEntry(p); - } - - } - for(int i = 0;i < entries.length;i++){ - BeansFieldEntry e = entries[i]; - FieldOption op = readPropOption(e.desc, implicitOption); - e.setOption(op); - } - return entries; - } - - public FieldOption readImplicitFieldOption(Class targetClass) { - MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class); - if(a == null) { - return FieldOption.DEFAULT; - } - return a.value(); - } - - - private FieldOption readPropOption(PropertyDescriptor desc, FieldOption implicitOption) { - - FieldOption forGetter = readMethodOption(desc.getReadMethod()); - if(forGetter != FieldOption.DEFAULT){ - return forGetter; - } - FieldOption forSetter = readMethodOption(desc.getWriteMethod()); - if(forSetter != FieldOption.DEFAULT){ - return forSetter; - }else{ - return implicitOption; - } - - } - private FieldOption readMethodOption(Method method){ - - if(isAnnotated(method, Ignore.class)) { - return FieldOption.IGNORE; - } else if(isAnnotated(method, Required.class)) { - return FieldOption.REQUIRED; - } else if(isAnnotated(method, Optional.class)) { - return FieldOption.OPTIONAL; - } else if(isAnnotated(method, Nullable.class)) { - if(method.getDeclaringClass().isPrimitive()) { - return FieldOption.REQUIRED; - } else { - return FieldOption.NULLABLE; - } - } - return FieldOption.DEFAULT; - } - - private int readPropIndex(PropertyDescriptor desc) { - - int forGetter = readMethodIndex(desc.getReadMethod()); - if(forGetter >= 0){ - return forGetter; - } - int forSetter = readMethodIndex(desc.getWriteMethod()); - return forSetter; - } - private int readMethodIndex(Method method){ - Index a = method.getAnnotation(Index.class); - if(a == null) { - return -1; - } else { - return a.value(); - } - } - - private boolean isAnnotated(AccessibleObject ao, Class with) { - return ao.getAnnotation(with) != null; - } - boolean isIgnoreProp(PropertyDescriptor desc){ - if(desc == null)return true; - Method getter = desc.getReadMethod(); - Method setter = desc.getWriteMethod(); - return getter == null || - setter == null || - !Modifier.isPublic(getter.getModifiers()) || - !Modifier.isPublic(setter.getModifiers()) || - isAnnotated(getter,Ignore.class) || - isAnnotated(setter, Ignore.class); - } -} diff --git a/java/src/main/java/org/msgpack/template/BigDecimalTemplate.java b/java/src/main/java/org/msgpack/template/BigDecimalTemplate.java deleted file mode 100644 index 7958f825..00000000 --- a/java/src/main/java/org/msgpack/template/BigDecimalTemplate.java +++ /dev/null @@ -1,61 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import java.math.BigDecimal; -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; - -public class BigDecimalTemplate implements Template { - - @Override - public void pack(Packer pk, Object target) throws IOException { - BigDecimal temp = (BigDecimal) target; - try { - pk.packString(temp.toString()); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - @Override - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - String temp = pac.unpackString(); - return new BigDecimal(temp); - } - - @Override - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - String temp = from.asString(); - return new BigDecimal(temp); - } - - static public BigDecimalTemplate getInstance() { - return instance; - } - - static final BigDecimalTemplate instance = new BigDecimalTemplate(); - - static { - TemplateRegistry.register(BigDecimal.class, instance); - } -} diff --git a/java/src/main/java/org/msgpack/template/BigIntegerTemplate.java b/java/src/main/java/org/msgpack/template/BigIntegerTemplate.java deleted file mode 100644 index 65389b8a..00000000 --- a/java/src/main/java/org/msgpack/template/BigIntegerTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import java.math.BigInteger; -import org.msgpack.*; - -public class BigIntegerTemplate implements Template { - private BigIntegerTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packBigInteger((BigInteger)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackBigInteger(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asBigInteger(); - } - - static public BigIntegerTemplate getInstance() { - return instance; - } - - static final BigIntegerTemplate instance = new BigIntegerTemplate(); - - static { - TemplateRegistry.register(BigInteger.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/BooleanArrayTemplate.java b/java/src/main/java/org/msgpack/template/BooleanArrayTemplate.java deleted file mode 100644 index a66e0e1e..00000000 --- a/java/src/main/java/org/msgpack/template/BooleanArrayTemplate.java +++ /dev/null @@ -1,80 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class BooleanArrayTemplate implements Template { - private BooleanArrayTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof boolean[])) { - throw new MessageTypeException(); - } - boolean[] array = (boolean[])target; - try { - pk.packArray(array.length); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - for(boolean a : array) { - pk.pack(a); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - boolean[] array; - if(to != null && to instanceof boolean[] && ((boolean[])to).length == length) { - array = (boolean[])to; - } else { - array = new boolean[length]; - } - for(int i=0; i < length; i++) { - array[i] = pac.unpackBoolean(); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - boolean[] array; - if(to != null && to instanceof boolean[] && ((boolean[])to).length == src.length) { - array = (boolean[])to; - } else { - array = new boolean[src.length]; - } - for(int i=0; i < src.length; i++) { - MessagePackObject s = src[i]; - array[i] = s.asBoolean(); - } - return array; - } - - static public BooleanArrayTemplate getInstance() { - return instance; - } - - static final BooleanArrayTemplate instance = new BooleanArrayTemplate(); - - static { - TemplateRegistry.register(boolean[].class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/BooleanTemplate.java b/java/src/main/java/org/msgpack/template/BooleanTemplate.java deleted file mode 100644 index a756a62d..00000000 --- a/java/src/main/java/org/msgpack/template/BooleanTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class BooleanTemplate implements Template { - private BooleanTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packBoolean((Boolean)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackBoolean(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asBoolean(); - } - - static public BooleanTemplate getInstance() { - return instance; - } - - static final BooleanTemplate instance = new BooleanTemplate(); - - static { - TemplateRegistry.register(Boolean.class, instance); - TemplateRegistry.register(boolean.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/BuiltInTemplateLoader.java b/java/src/main/java/org/msgpack/template/BuiltInTemplateLoader.java deleted file mode 100644 index 4b8d618c..00000000 --- a/java/src/main/java/org/msgpack/template/BuiltInTemplateLoader.java +++ /dev/null @@ -1,49 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -public class BuiltInTemplateLoader { - public static void load() { - AnyTemplate.getInstance(); - BigIntegerTemplate.getInstance(); - BigDecimalTemplate.getInstance(); - BooleanArrayTemplate.getInstance(); - BooleanTemplate.getInstance(); - ByteArrayTemplate.getInstance(); - ByteBufferTemplate.getInstance(); - ByteTemplate.getInstance(); - DoubleArrayTemplate.getInstance(); - DoubleTemplate.getInstance(); - DateTemplate.getInstance(); - FloatArrayTemplate.getInstance(); - FloatTemplate.getInstance(); - IntArrayTemplate.getInstance(); - IntegerTemplate.getInstance(); - LongArrayTemplate.getInstance(); - LongTemplate.getInstance(); - ShortArrayTemplate.getInstance(); - ShortTemplate.getInstance(); - StringTemplate.getInstance(); - - CollectionTemplate.load(); - ListTemplate.load(); - MapTemplate.load(); - NullableTemplate.load(); - } -} - diff --git a/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java b/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java deleted file mode 100644 index 038a5413..00000000 --- a/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java +++ /dev/null @@ -1,52 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class ByteArrayTemplate implements Template { - private ByteArrayTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packByteArray((byte[])target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackByteArray(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asByteArray(); - } - - static public ByteArrayTemplate getInstance() { - return instance; - } - - static final ByteArrayTemplate instance = new ByteArrayTemplate(); - - static { - TemplateRegistry.register(byte[].class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/ByteBufferTemplate.java b/java/src/main/java/org/msgpack/template/ByteBufferTemplate.java deleted file mode 100644 index b84c3da4..00000000 --- a/java/src/main/java/org/msgpack/template/ByteBufferTemplate.java +++ /dev/null @@ -1,56 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.nio.ByteBuffer; -import java.io.IOException; -import org.msgpack.*; - -public class ByteBufferTemplate implements Template { - private ByteBufferTemplate() { - } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packByteBuffer((ByteBuffer) target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackByteBuffer(); - } - - public Object convert(MessagePackObject from, Object to) - throws MessageTypeException { - // FIXME - byte[] bytes = from.asByteArray(); - return ByteBuffer.wrap(bytes); - } - - static public ByteBufferTemplate getInstance() { - return instance; - } - - static final ByteBufferTemplate instance = new ByteBufferTemplate(); - - static { - TemplateRegistry.register(ByteBuffer.class, instance); - } -} diff --git a/java/src/main/java/org/msgpack/template/ByteTemplate.java b/java/src/main/java/org/msgpack/template/ByteTemplate.java deleted file mode 100644 index 7c6048cb..00000000 --- a/java/src/main/java/org/msgpack/template/ByteTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class ByteTemplate implements Template { - private ByteTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packByte((Byte)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackByte(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asByte(); - } - - static public ByteTemplate getInstance() { - return instance; - } - - static final ByteTemplate instance = new ByteTemplate(); - - static { - TemplateRegistry.register(Byte.class, instance); - TemplateRegistry.register(byte.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/CollectionTemplate.java b/java/src/main/java/org/msgpack/template/CollectionTemplate.java deleted file mode 100644 index 31a937ef..00000000 --- a/java/src/main/java/org/msgpack/template/CollectionTemplate.java +++ /dev/null @@ -1,93 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.util.Collection; -import java.util.LinkedList; -import java.io.IOException; - -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; - -public class CollectionTemplate implements Template { - public static void load() { } - - private Template elementTemplate; - - public CollectionTemplate(Template elementTemplate) { - this.elementTemplate = elementTemplate; - } - - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object target) throws IOException { - if (! (target instanceof Collection)) { - if (target == null) { - throw new MessageTypeException(new NullPointerException("target is null.")); - } - throw new MessageTypeException("target is not Collection type: " + target.getClass()); - } - Collection collection = (Collection) target; - pk.packArray(collection.size()); - for(Object element : collection) { - elementTemplate.pack(pk, element); - } - } - - @SuppressWarnings("unchecked") - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - Collection c; - if(to == null) { - c = new LinkedList(); - } else { - // TODO: optimize if list is instanceof ArrayList - c = (Collection) to; - c.clear(); - } - for(; length > 0; length--) { - c.add(elementTemplate.unpack(pac, null)); - } - return c; - } - - @SuppressWarnings("unchecked") - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] array = from.asArray(); - Collection c; - if(to == null) { - c = new LinkedList(); - } else { - // TODO: optimize if list is instanceof ArrayList - c = (Collection) to; - c.clear(); - } - for(MessagePackObject element : array) { - c.add(elementTemplate.convert(element, null)); - } - return c; - } - - static { - TemplateRegistry.registerGeneric(Collection.class, new GenericTemplate1(CollectionTemplate.class)); - TemplateRegistry.register(Collection.class, new CollectionTemplate(AnyTemplate.getInstance())); - } -} - diff --git a/java/src/main/java/org/msgpack/template/DateTemplate.java b/java/src/main/java/org/msgpack/template/DateTemplate.java deleted file mode 100644 index 26adde68..00000000 --- a/java/src/main/java/org/msgpack/template/DateTemplate.java +++ /dev/null @@ -1,57 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import java.util.Date; -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; - -public class DateTemplate implements Template { - - @Override - public void pack(Packer pk, Object target) throws IOException { - Date temp = (Date) target; - pk.packLong(temp.getTime()); - } - - @Override - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - Long temp = pac.unpackLong(); - return new Date(temp); - } - - @Override - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - Long temp = from.asLong(); - return new Date(temp); - } - - static public DateTemplate getInstance() { - return instance; - } - - static final DateTemplate instance = new DateTemplate(); - - static { - TemplateRegistry.register(Date.class, instance); - } -} diff --git a/java/src/main/java/org/msgpack/template/DefaultTemplate.java b/java/src/main/java/org/msgpack/template/DefaultTemplate.java deleted file mode 100644 index 8a5792e1..00000000 --- a/java/src/main/java/org/msgpack/template/DefaultTemplate.java +++ /dev/null @@ -1,96 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import java.lang.reflect.Type; -import org.msgpack.*; - -public class DefaultTemplate implements Template { - private Class targetClass; - private Type lookupType; - private boolean messagePackable; - private boolean messageUnpackable; - private boolean messageConvertable; - - public DefaultTemplate(Class targetClass) { - this(targetClass, (Type)targetClass); - } - - public DefaultTemplate(Class targetClass, Type lookupType) { - this.targetClass = targetClass; - this.lookupType = lookupType; - this.messagePackable = MessagePackable.class.isAssignableFrom(targetClass); - this.messageUnpackable = MessageUnpackable.class.isAssignableFrom(targetClass); - this.messageConvertable = MessageConvertable.class.isAssignableFrom(targetClass); - } - - public void pack(Packer pk, Object target) throws IOException { - if(messagePackable) { - if(target == null) { - throw new MessageTypeException("target is null."); - } - ((MessagePackable)target).messagePack(pk); - return; - } - Template tmpl = TemplateRegistry.tryLookup(lookupType); - if(tmpl == this || tmpl == null) { - throw new MessageTypeException("Template lookup fail: " + lookupType); - } - tmpl.pack(pk, target); - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - if(messageUnpackable) { - if(to == null) { - try { - to = targetClass.newInstance(); - } catch (Exception e) { - throw new MessageTypeException(e); - } - } - ((MessageUnpackable)to).messageUnpack(pac); - return to; - } - Template tmpl = TemplateRegistry.tryLookup(lookupType); - if(tmpl == this || tmpl == null) { - throw new MessageTypeException("Template lookup fail: " + lookupType); - } - return tmpl.unpack(pac, to); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - if(messageConvertable) { - if(to == null) { - try { - to = targetClass.newInstance(); - } catch (Exception e) { - throw new MessageTypeException(e); - } - } - ((MessageConvertable)to).messageConvert(from); - return from; - } - Template tmpl = TemplateRegistry.tryLookup(lookupType); - if(tmpl == this || tmpl == null) { - throw new MessageTypeException("Template lookup fail: " + lookupType); - } - return tmpl.convert(from, to); - } -} - diff --git a/java/src/main/java/org/msgpack/template/DoubleArrayTemplate.java b/java/src/main/java/org/msgpack/template/DoubleArrayTemplate.java deleted file mode 100644 index 087a33bb..00000000 --- a/java/src/main/java/org/msgpack/template/DoubleArrayTemplate.java +++ /dev/null @@ -1,80 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class DoubleArrayTemplate implements Template { - private DoubleArrayTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof double[])) { - throw new MessageTypeException(); - } - double[] array = (double[])target; - try { - pk.packArray(array.length); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - for(double a : array) { - pk.pack(a); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - double[] array; - if(to != null && to instanceof double[] && ((double[])to).length == length) { - array = (double[])to; - } else { - array = new double[length]; - } - for(int i=0; i < length; i++) { - array[i] = pac.unpackDouble(); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - double[] array; - if(to != null && to instanceof double[] && ((double[])to).length == src.length) { - array = (double[])to; - } else { - array = new double[src.length]; - } - for(int i=0; i < src.length; i++) { - MessagePackObject s = src[i]; - array[i] = s.asDouble(); - } - return array; - } - - static public DoubleArrayTemplate getInstance() { - return instance; - } - - static final DoubleArrayTemplate instance = new DoubleArrayTemplate(); - - static { - TemplateRegistry.register(double[].class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/DoubleTemplate.java b/java/src/main/java/org/msgpack/template/DoubleTemplate.java deleted file mode 100644 index b76a307d..00000000 --- a/java/src/main/java/org/msgpack/template/DoubleTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class DoubleTemplate implements Template { - private DoubleTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packDouble(((Double)target)); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackDouble(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asDouble(); - } - - static public DoubleTemplate getInstance() { - return instance; - } - - static final DoubleTemplate instance = new DoubleTemplate(); - - static { - TemplateRegistry.register(Double.class, instance); - TemplateRegistry.register(double.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/FieldEntry.java b/java/src/main/java/org/msgpack/template/FieldEntry.java deleted file mode 100644 index a8437403..00000000 --- a/java/src/main/java/org/msgpack/template/FieldEntry.java +++ /dev/null @@ -1,143 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import java.lang.reflect.*; -import java.lang.annotation.*; -import java.util.List; -import java.util.ArrayList; -import java.util.EnumSet; -import org.msgpack.*; -import org.msgpack.annotation.*; - -public class FieldEntry implements IFieldEntry { - private Field field; - private FieldOption option; - - public FieldEntry() { - this.field = null; - this.option = FieldOption.IGNORE; - } - - public FieldEntry(FieldEntry e) { - this.field = e.field; - this.option = e.option; - } - - public FieldEntry(Field field, FieldOption option) { - this.field = field; - this.option = option; - } - - public Field getField() { - return field; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#getName() - */ - @Override - public String getName() { - return field.getName(); - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#getType() - */ - @Override - public Class getType() { - return field.getType(); - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#getJavaTypeName() - */ - @Override - public String getJavaTypeName() { - Class type = field.getType(); - if(type.isArray()) { - return arrayTypeToString(type); - } else { - return type.getName(); - } - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#getGenericType() - */ - @Override - public Type getGenericType() { - return field.getGenericType(); - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#getOption() - */ - @Override - public FieldOption getOption() { - return option; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#isAvailable() - */ - @Override - public boolean isAvailable() { - return option != FieldOption.IGNORE; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#isRequired() - */ - @Override - public boolean isRequired() { - return option == FieldOption.REQUIRED; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#isOptional() - */ - @Override - public boolean isOptional() { - return option == FieldOption.OPTIONAL; - } - - /* (non-Javadoc) - * @see org.msgpack.template.IFieldEntry#isNullable() - */ - @Override - public boolean isNullable() { - return option == FieldOption.NULLABLE; - } - - static String arrayTypeToString(Class type) { - int dim = 1; - Class baseType = type.getComponentType(); - while(baseType.isArray()) { - baseType = baseType.getComponentType(); - dim += 1; - } - StringBuilder sb = new StringBuilder(); - sb.append(baseType.getName()); - for (int i = 0; i < dim; ++i) { - sb.append("[]"); - } - return sb.toString(); - } -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/template/FieldEntryReader.java b/java/src/main/java/org/msgpack/template/FieldEntryReader.java deleted file mode 100644 index 0815aa2e..00000000 --- a/java/src/main/java/org/msgpack/template/FieldEntryReader.java +++ /dev/null @@ -1,182 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.lang.annotation.Annotation; -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; - -import org.msgpack.annotation.Ignore; -import org.msgpack.annotation.Index; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.Nullable; -import org.msgpack.annotation.Optional; -import org.msgpack.annotation.Required; - -public class FieldEntryReader implements IFieldEntryReader{ - - - public IFieldEntry[] convertFieldEntries(Class targetClass, FieldList flist) throws NoSuchFieldException { - List src = flist.getList(); - FieldEntry[] result = new FieldEntry[src.size()]; - for(int i=0; i < src.size(); i++) { - FieldList.Entry s = src.get(i); - if(s.isAvailable()) { - result[i] = new FieldEntry(targetClass.getDeclaredField(s.getName()), s.getOption()); - } else { - result[i] = new FieldEntry(); - } - } - return result; - } - - @Override - public IFieldEntry[] readFieldEntries(Class targetClass, - FieldOption implicitOption) { - Field[] allFields = readAllFields(targetClass); - - /* index: - * @Index(0) int field_a; // 0 - * int field_b; // 1 - * @Index(3) int field_c; // 3 - * int field_d; // 4 - * @Index(2) int field_e; // 2 - * int field_f; // 5 - */ - List indexed = new ArrayList(); - int maxIndex = -1; - for(Field f : allFields) { - FieldOption opt = readFieldOption(f, implicitOption); - if(opt == FieldOption.IGNORE) { - // skip - continue; - } - - int index = readFieldIndex(f, maxIndex); - - if(indexed.size() > index && indexed.get(index) != null) { - throw new TemplateBuildException("duplicated index: "+index); - } - if(index < 0) { - throw new TemplateBuildException("invalid index: "+index); - } - - while(indexed.size() <= index) { - indexed.add(null); - } - indexed.set(index, new FieldEntry(f, opt)); - - if(maxIndex < index) { - maxIndex = index; - } - } - - FieldEntry[] result = new FieldEntry[maxIndex+1]; - for(int i=0; i < indexed.size(); i++) { - FieldEntry e = indexed.get(i); - if(e == null) { - result[i] = new FieldEntry(); - } else { - result[i] = e; - } - } - - return result; - } - - public FieldOption readImplicitFieldOption(Class targetClass) { - MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class); - if(a == null) { - return FieldOption.DEFAULT; - } - return a.value(); - } - - private Field[] readAllFields(Class targetClass) { - // order: [fields of super class, ..., fields of this class] - List succ = new ArrayList(); - int total = 0; - for(Class c = targetClass; c != Object.class; c = c.getSuperclass()) { - Field[] fields = c.getDeclaredFields(); - total += fields.length; - succ.add(fields); - } - Field[] result = new Field[total]; - int off = 0; - for(int i=succ.size()-1; i >= 0; i--) { - Field[] fields = succ.get(i); - System.arraycopy(fields, 0, result, off, fields.length); - off += fields.length; - } - return result; - } - - private static FieldOption readFieldOption(Field field, FieldOption implicitOption) { - int mod = field.getModifiers(); - if(Modifier.isStatic(mod) || Modifier.isFinal(mod)) { - return FieldOption.IGNORE; - } - - if(isAnnotated(field, Ignore.class)) { - return FieldOption.IGNORE; - } else if(isAnnotated(field, Required.class)) { - return FieldOption.REQUIRED; - } else if(isAnnotated(field, Optional.class)) { - return FieldOption.OPTIONAL; - } else if(isAnnotated(field, Nullable.class)) { - if(field.getType().isPrimitive()) { - return FieldOption.REQUIRED; - } else { - return FieldOption.NULLABLE; - } - } - - if(implicitOption != FieldOption.DEFAULT) { - return implicitOption; - } - - // default mode: - // transient : Ignore - // public : Required - // others : Ignore - if(Modifier.isTransient(mod)) { - return FieldOption.IGNORE; - } else if(Modifier.isPublic(mod)) { - return FieldOption.REQUIRED; - } else { - return FieldOption.IGNORE; - } - } - - private static int readFieldIndex(Field field, int maxIndex) { - Index a = field.getAnnotation(Index.class); - if(a == null) { - return maxIndex + 1; - } else { - return a.value(); - } - } - - private static boolean isAnnotated(AccessibleObject ao, Class with) { - return ao.getAnnotation(with) != null; - } - -} diff --git a/java/src/main/java/org/msgpack/template/FieldList.java b/java/src/main/java/org/msgpack/template/FieldList.java deleted file mode 100644 index a5b141d9..00000000 --- a/java/src/main/java/org/msgpack/template/FieldList.java +++ /dev/null @@ -1,96 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.util.List; -import java.util.ArrayList; - -public class FieldList { - public static class Entry { - public Entry() { - this.name = null; - this.option = FieldOption.IGNORE; - } - - public Entry(String name, FieldOption option) { - this.name = name; - this.option = option; - } - - private String name; - private FieldOption option; - - public String getName() { - return name; - } - - public FieldOption getOption() { - return option; - } - - public boolean isAvailable() { - return this.option != FieldOption.IGNORE; - } - - public boolean isRequired() { - return this.option == FieldOption.REQUIRED; - } - - public boolean isOptional() { - return this.option == FieldOption.OPTIONAL; - } - - public boolean isNullable() { - return this.option == FieldOption.NULLABLE; - } - } - - private ArrayList list; - - public FieldList() { - list = new ArrayList(); - } - - public void add(final String name) { - add(name, FieldOption.REQUIRED); - } - - public void add(final String name, final FieldOption option) { - list.add(new Entry(name, option)); - } - - public void put(int index, final String name) { - put(index, name, FieldOption.REQUIRED); - } - - public void put(int index, final String name, final FieldOption option) { - if(list.size() < index) { - do { - list.add(new Entry()); - } while(list.size() < index); - list.add(new Entry(name, option)); - } else { - list.set(index, new Entry(name, option)); - } - } - - public List getList() { - return list; - } -} - diff --git a/java/src/main/java/org/msgpack/template/FieldOption.java b/java/src/main/java/org/msgpack/template/FieldOption.java deleted file mode 100644 index 41f152ff..00000000 --- a/java/src/main/java/org/msgpack/template/FieldOption.java +++ /dev/null @@ -1,27 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -public enum FieldOption { - IGNORE, - REQUIRED, - OPTIONAL, - NULLABLE, - DEFAULT; -} - diff --git a/java/src/main/java/org/msgpack/template/FloatArrayTemplate.java b/java/src/main/java/org/msgpack/template/FloatArrayTemplate.java deleted file mode 100644 index a0ca1489..00000000 --- a/java/src/main/java/org/msgpack/template/FloatArrayTemplate.java +++ /dev/null @@ -1,80 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class FloatArrayTemplate implements Template { - private FloatArrayTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof float[])) { - throw new MessageTypeException(); - } - float[] array = (float[])target; - try { - pk.packArray(array.length); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - for(float a : array) { - pk.pack(a); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - float[] array; - if(to != null && to instanceof float[] && ((float[])to).length == length) { - array = (float[])to; - } else { - array = new float[length]; - } - for(int i=0; i < length; i++) { - array[i] = pac.unpackFloat(); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - float[] array; - if(to != null && to instanceof float[] && ((float[])to).length == src.length) { - array = (float[])to; - } else { - array = new float[src.length]; - } - for(int i=0; i < src.length; i++) { - MessagePackObject s = src[i]; - array[i] = s.asFloat(); - } - return array; - } - - static public FloatArrayTemplate getInstance() { - return instance; - } - - static final FloatArrayTemplate instance = new FloatArrayTemplate(); - - static { - TemplateRegistry.register(float[].class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/FloatTemplate.java b/java/src/main/java/org/msgpack/template/FloatTemplate.java deleted file mode 100644 index 823d893e..00000000 --- a/java/src/main/java/org/msgpack/template/FloatTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class FloatTemplate implements Template { - private FloatTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packFloat((Float)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackFloat(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asFloat(); - } - - static public FloatTemplate getInstance() { - return instance; - } - - static final FloatTemplate instance = new FloatTemplate(); - - static { - TemplateRegistry.register(Float.class, instance); - TemplateRegistry.register(float.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/GenericTemplate.java b/java/src/main/java/org/msgpack/template/GenericTemplate.java deleted file mode 100644 index 3fa204c9..00000000 --- a/java/src/main/java/org/msgpack/template/GenericTemplate.java +++ /dev/null @@ -1,25 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import org.msgpack.Template; - -public interface GenericTemplate { - public Template build(Template[] params); -} - diff --git a/java/src/main/java/org/msgpack/template/GenericTemplate1.java b/java/src/main/java/org/msgpack/template/GenericTemplate1.java deleted file mode 100644 index ca89b655..00000000 --- a/java/src/main/java/org/msgpack/template/GenericTemplate1.java +++ /dev/null @@ -1,54 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import org.msgpack.Template; - -public class GenericTemplate1 implements GenericTemplate { - Constructor constructor; - - public GenericTemplate1(Class tmpl) { - try { - this.constructor = tmpl.getConstructor(new Class[]{Template.class}); - constructor.newInstance(new Object[]{AnyTemplate.getInstance()}); - } catch (NoSuchMethodException e) { - throw new IllegalArgumentException(e); - } catch (InvocationTargetException e) { - throw new IllegalArgumentException(e); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException(e); - } catch (InstantiationException e) { - throw new IllegalArgumentException(e); - } - } - - public Template build(Template[] params) { - try { - return constructor.newInstance(params); - } catch (InvocationTargetException e) { - throw new IllegalArgumentException(e); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException(e); - } catch (InstantiationException e) { - throw new IllegalArgumentException(e); - } - } -} - diff --git a/java/src/main/java/org/msgpack/template/GenericTemplate2.java b/java/src/main/java/org/msgpack/template/GenericTemplate2.java deleted file mode 100644 index 5f938775..00000000 --- a/java/src/main/java/org/msgpack/template/GenericTemplate2.java +++ /dev/null @@ -1,54 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import org.msgpack.Template; - -public class GenericTemplate2 implements GenericTemplate { - Constructor constructor; - - public GenericTemplate2(Class tmpl) { - try { - this.constructor = tmpl.getConstructor(new Class[]{Template.class, Template.class}); - constructor.newInstance(new Object[]{AnyTemplate.getInstance(), AnyTemplate.getInstance()}); - } catch (NoSuchMethodException e) { - throw new IllegalArgumentException(e); - } catch (InvocationTargetException e) { - throw new IllegalArgumentException(e); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException(e); - } catch (InstantiationException e) { - throw new IllegalArgumentException(e); - } - } - - public Template build(Template[] params) { - try { - return constructor.newInstance(params); - } catch (InvocationTargetException e) { - throw new IllegalArgumentException(e); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException(e); - } catch (InstantiationException e) { - throw new IllegalArgumentException(e); - } - } -} - diff --git a/java/src/main/java/org/msgpack/template/IFieldEntry.java b/java/src/main/java/org/msgpack/template/IFieldEntry.java deleted file mode 100644 index 23700c67..00000000 --- a/java/src/main/java/org/msgpack/template/IFieldEntry.java +++ /dev/null @@ -1,42 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.lang.reflect.Type; - -public interface IFieldEntry { - - public abstract String getName(); - - public abstract Class getType(); - - public abstract String getJavaTypeName(); - - public abstract Type getGenericType(); - - public abstract FieldOption getOption(); - - public abstract boolean isAvailable(); - - public abstract boolean isRequired(); - - public abstract boolean isOptional(); - - public abstract boolean isNullable(); - -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/template/IFieldEntryReader.java b/java/src/main/java/org/msgpack/template/IFieldEntryReader.java deleted file mode 100644 index fe3024a7..00000000 --- a/java/src/main/java/org/msgpack/template/IFieldEntryReader.java +++ /dev/null @@ -1,25 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -public interface IFieldEntryReader { - - public IFieldEntry[] convertFieldEntries(Class targetClass, FieldList flist) throws NoSuchFieldException; - public IFieldEntry[] readFieldEntries(Class targetClass, FieldOption implicitOption); - public FieldOption readImplicitFieldOption(Class targetClass) ; -} diff --git a/java/src/main/java/org/msgpack/template/IntArrayTemplate.java b/java/src/main/java/org/msgpack/template/IntArrayTemplate.java deleted file mode 100644 index 76e6176b..00000000 --- a/java/src/main/java/org/msgpack/template/IntArrayTemplate.java +++ /dev/null @@ -1,80 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class IntArrayTemplate implements Template { - private IntArrayTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof int[])) { - throw new MessageTypeException(); - } - int[] array = (int[])target; - try { - pk.packArray(array.length); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - for(int a : array) { - pk.pack(a); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - int[] array; - if(to != null && to instanceof int[] && ((int[])to).length == length) { - array = (int[])to; - } else { - array = new int[length]; - } - for(int i=0; i < length; i++) { - array[i] = pac.unpackInt(); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - int[] array; - if(to != null && to instanceof int[] && ((int[])to).length == src.length) { - array = (int[])to; - } else { - array = new int[src.length]; - } - for(int i=0; i < src.length; i++) { - MessagePackObject s = src[i]; - array[i] = s.asInt(); - } - return array; - } - - static public IntArrayTemplate getInstance() { - return instance; - } - - static final IntArrayTemplate instance = new IntArrayTemplate(); - - static { - TemplateRegistry.register(int[].class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/IntegerTemplate.java b/java/src/main/java/org/msgpack/template/IntegerTemplate.java deleted file mode 100644 index f20d36cf..00000000 --- a/java/src/main/java/org/msgpack/template/IntegerTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class IntegerTemplate implements Template { - private IntegerTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packInt((Integer)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackInt(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asInt(); - } - - static public IntegerTemplate getInstance() { - return instance; - } - - static final IntegerTemplate instance = new IntegerTemplate(); - - static { - TemplateRegistry.register(Integer.class, instance); - TemplateRegistry.register(int.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/ListTemplate.java b/java/src/main/java/org/msgpack/template/ListTemplate.java deleted file mode 100644 index 385d7b0f..00000000 --- a/java/src/main/java/org/msgpack/template/ListTemplate.java +++ /dev/null @@ -1,91 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.util.List; -import java.util.ArrayList; -import java.io.IOException; -import org.msgpack.*; - -public class ListTemplate implements Template { - static void load() { } - - private Template elementTemplate; - - public ListTemplate(Template elementTemplate) { - this.elementTemplate = elementTemplate; - } - - public Template getElementTemplate() { - return elementTemplate; - } - - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object target) throws IOException { - if (! (target instanceof List)) { - if (target == null) { - throw new MessageTypeException(new NullPointerException("target is null.")); - } - throw new MessageTypeException("target is not List type: " + target.getClass()); - } - List list = (List)target; - pk.packArray(list.size()); - for(Object element : list) { - elementTemplate.pack(pk, element); - } - } - - @SuppressWarnings("unchecked") - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - List list; - if(to == null) { - list = new ArrayList(length); - } else { - list = (List) to; - list.clear(); - } - for(; length > 0; length--) { - list.add( elementTemplate.unpack(pac, null) ); - } - return list; - } - - @SuppressWarnings("unchecked") - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] array = from.asArray(); - List list; - if(to == null) { - list = new ArrayList(array.length); - } else { - // TODO: optimize if list is instanceof ArrayList - list = (List) to; - list.clear(); - } - for(MessagePackObject element : array) { - list.add( elementTemplate.convert(element, null) ); - } - return list; - } - - static { - TemplateRegistry.registerGeneric(List.class, new GenericTemplate1(ListTemplate.class)); - TemplateRegistry.register(List.class, new ListTemplate(AnyTemplate.getInstance())); - } -} - diff --git a/java/src/main/java/org/msgpack/template/LongArrayTemplate.java b/java/src/main/java/org/msgpack/template/LongArrayTemplate.java deleted file mode 100644 index 2854697e..00000000 --- a/java/src/main/java/org/msgpack/template/LongArrayTemplate.java +++ /dev/null @@ -1,80 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class LongArrayTemplate implements Template { - private LongArrayTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof long[])) { - throw new MessageTypeException(); - } - long[] array = (long[])target; - try { - pk.packArray(array.length); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - for(long a : array) { - pk.pack(a); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - long[] array; - if(to != null && to instanceof long[] && ((long[])to).length == length) { - array = (long[])to; - } else { - array = new long[length]; - } - for(int i=0; i < length; i++) { - array[i] = pac.unpackLong(); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - long[] array; - if(to != null && to instanceof long[] && ((long[])to).length == src.length) { - array = (long[])to; - } else { - array = new long[src.length]; - } - for(int i=0; i < src.length; i++) { - MessagePackObject s = src[i]; - array[i] = s.asLong(); - } - return array; - } - - static public LongArrayTemplate getInstance() { - return instance; - } - - static final LongArrayTemplate instance = new LongArrayTemplate(); - - static { - TemplateRegistry.register(long[].class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/LongTemplate.java b/java/src/main/java/org/msgpack/template/LongTemplate.java deleted file mode 100644 index 28bad53c..00000000 --- a/java/src/main/java/org/msgpack/template/LongTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class LongTemplate implements Template { - private LongTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packLong((Long)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackLong(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asLong(); - } - - static public LongTemplate getInstance() { - return instance; - } - - static final LongTemplate instance = new LongTemplate(); - - static { - TemplateRegistry.register(Long.class, instance); - TemplateRegistry.register(long.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/MapTemplate.java b/java/src/main/java/org/msgpack/template/MapTemplate.java deleted file mode 100644 index 9ad74fa8..00000000 --- a/java/src/main/java/org/msgpack/template/MapTemplate.java +++ /dev/null @@ -1,101 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.util.Map; -import java.util.HashMap; -import java.io.IOException; -import org.msgpack.*; - -public class MapTemplate implements Template { - static void load() { } - - private Template keyTemplate; - private Template valueTemplate; - - public MapTemplate(Template keyTemplate, Template valueTemplate) { - this.keyTemplate = keyTemplate; - this.valueTemplate = valueTemplate; - } - - public Template getKeyTemplate() { - return keyTemplate; - } - - public Template getValueTemplate() { - return valueTemplate; - } - - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof Map)) { - if (target == null) { - throw new MessageTypeException(new NullPointerException("target is null.")); - } - throw new MessageTypeException("target is not Map type: " + target.getClass()); - } - Map map = (Map) target; - pk.packMap(map.size()); - for(Map.Entry pair : map.entrySet()) { - keyTemplate.pack(pk, pair.getKey()); - valueTemplate.pack(pk, pair.getValue()); - } - } - - @SuppressWarnings("unchecked") - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackMap(); - Map map; - if(to == null) { - map = new HashMap(length); - } else { - map = (Map) to; - map.clear(); - } - for(; length > 0; length--) { - Object key = keyTemplate.unpack(pac, null); - Object value = valueTemplate.unpack(pac, null); - map.put(key, value); - } - return map; - } - - @SuppressWarnings("unchecked") - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - Map src = from.asMap(); - Map map; - if(to == null) { - map = new HashMap(src.size()); - } else { - map = (Map) to; - map.clear(); - } - for(Map.Entry pair : src.entrySet()) { - Object key = keyTemplate.convert(pair.getKey(), null); - Object value = valueTemplate.convert(pair.getValue(), null); - map.put(key, value); - } - return map; - } - - static { - TemplateRegistry.registerGeneric(Map.class, new GenericTemplate2(MapTemplate.class)); - TemplateRegistry.register(Map.class, new MapTemplate(AnyTemplate.getInstance(), AnyTemplate.getInstance())); - } -} - diff --git a/java/src/main/java/org/msgpack/template/NullableTemplate.java b/java/src/main/java/org/msgpack/template/NullableTemplate.java deleted file mode 100644 index 431da596..00000000 --- a/java/src/main/java/org/msgpack/template/NullableTemplate.java +++ /dev/null @@ -1,58 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class NullableTemplate implements Template { - static void load() { } - - private Template elementTemplate; - - public NullableTemplate(Template elementTemplate) { - this.elementTemplate = elementTemplate; - } - - public Template getElementTemplate() { - return elementTemplate; - } - - public void pack(Packer pk, Object target) throws IOException { - if(target == null) { - pk.packNil(); - } else { - elementTemplate.pack(pk, target); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - if(pac.tryUnpackNull()) { - return null; - } - return elementTemplate.unpack(pac, to); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - if(from.isNil()) { - return null; - } - return elementTemplate.convert(from, to); - } -} - diff --git a/java/src/main/java/org/msgpack/template/OptionalTemplate.java b/java/src/main/java/org/msgpack/template/OptionalTemplate.java deleted file mode 100644 index 9e519e71..00000000 --- a/java/src/main/java/org/msgpack/template/OptionalTemplate.java +++ /dev/null @@ -1,62 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class OptionalTemplate implements Template { - private Template elementTemplate; - private Object defaultObject; - - public OptionalTemplate(Template elementTemplate) { - this(elementTemplate, null); - } - - public OptionalTemplate(Template elementTemplate, Object defaultObject) { - this.elementTemplate = elementTemplate; - this.defaultObject = defaultObject; - } - - public Template getElementTemplate() { - return elementTemplate; - } - - public void pack(Packer pk, Object target) throws IOException { - if(target == null) { - pk.pack(defaultObject); - } else { - elementTemplate.pack(pk, target); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - if(pac.tryUnpackNull()) { - return defaultObject; // FIXME return to? - } - return elementTemplate.unpack(pac, to); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - if(from.isNil()) { - return defaultObject; // FIXME return to? - } - return elementTemplate.convert(from, to); - } -} - diff --git a/java/src/main/java/org/msgpack/template/ShortArrayTemplate.java b/java/src/main/java/org/msgpack/template/ShortArrayTemplate.java deleted file mode 100644 index 2beb61be..00000000 --- a/java/src/main/java/org/msgpack/template/ShortArrayTemplate.java +++ /dev/null @@ -1,80 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class ShortArrayTemplate implements Template { - private ShortArrayTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof short[])) { - throw new MessageTypeException(); - } - short[] array = (short[])target; - try { - pk.packArray(array.length); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - for(short a : array) { - pk.pack(a); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - short[] array; - if(to != null && to instanceof short[] && ((short[])to).length == length) { - array = (short[])to; - } else { - array = new short[length]; - } - for(int i=0; i < length; i++) { - array[i] = pac.unpackShort(); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - short[] array; - if(to != null && to instanceof short[] && ((short[])to).length == src.length) { - array = (short[])to; - } else { - array = new short[src.length]; - } - for(int i=0; i < src.length; i++) { - MessagePackObject s = src[i]; - array[i] = s.asShort(); - } - return array; - } - - static public ShortArrayTemplate getInstance() { - return instance; - } - - static final ShortArrayTemplate instance = new ShortArrayTemplate(); - - static { - TemplateRegistry.register(short[].class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/ShortTemplate.java b/java/src/main/java/org/msgpack/template/ShortTemplate.java deleted file mode 100644 index 9daba10c..00000000 --- a/java/src/main/java/org/msgpack/template/ShortTemplate.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class ShortTemplate implements Template { - private ShortTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packShort((Short)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackShort(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asShort(); - } - - static public ShortTemplate getInstance() { - return instance; - } - - static final ShortTemplate instance = new ShortTemplate(); - - static { - TemplateRegistry.register(Short.class, instance); - TemplateRegistry.register(short.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/StringTemplate.java b/java/src/main/java/org/msgpack/template/StringTemplate.java deleted file mode 100644 index 0cb63810..00000000 --- a/java/src/main/java/org/msgpack/template/StringTemplate.java +++ /dev/null @@ -1,52 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.io.IOException; -import org.msgpack.*; - -public class StringTemplate implements Template { - private StringTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packString((String)target); - } catch (NullPointerException e) { - throw new MessageTypeException("target is null.", e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return pac.unpackString(); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return from.asString(); - } - - static public StringTemplate getInstance() { - return instance; - } - - static final StringTemplate instance = new StringTemplate(); - - static { - TemplateRegistry.register(String.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/template/TemplateBuildException.java b/java/src/main/java/org/msgpack/template/TemplateBuildException.java deleted file mode 100644 index f8560d1f..00000000 --- a/java/src/main/java/org/msgpack/template/TemplateBuildException.java +++ /dev/null @@ -1,35 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -@SuppressWarnings("serial") -public class TemplateBuildException extends RuntimeException { - - public TemplateBuildException(String reason) { - super(reason); - } - - public TemplateBuildException(String reason, Throwable t) { - super(reason, t); - } - - public TemplateBuildException(Throwable t) { - super(t); - } -} - diff --git a/java/src/main/java/org/msgpack/template/TemplateRegistry.java b/java/src/main/java/org/msgpack/template/TemplateRegistry.java deleted file mode 100644 index 412b3b45..00000000 --- a/java/src/main/java/org/msgpack/template/TemplateRegistry.java +++ /dev/null @@ -1,208 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template; - -import java.util.Map; -import java.util.HashMap; -import java.lang.reflect.Type; -import java.lang.reflect.ParameterizedType; -import org.msgpack.template.builder.BuilderSelectorRegistry; -import org.msgpack.template.builder.CustomTemplateBuilder; -import org.msgpack.template.builder.TemplateBuilder; -import org.msgpack.Template; - -public class TemplateRegistry { - private static Map map; - private static Map genericMap; - private static BuilderSelectorRegistry builderSelectorRegistry; - - static { - map = new HashMap(); - genericMap = new HashMap(); - BuiltInTemplateLoader.load(); - builderSelectorRegistry = BuilderSelectorRegistry.getInstance(); - } - - public static void register(Class target) { - TemplateBuilder builder = builderSelectorRegistry.select(target); - if (builder != null) { - register(target,builder.buildTemplate(target)); - } else { - register(target,builderSelectorRegistry.getForceBuilder().buildTemplate(target)); - } - } - - public static void register(Class target, FieldOption implicitOption) { - TemplateBuilder builder = builderSelectorRegistry.select(target); - if (builder != null && builder instanceof CustomTemplateBuilder) { - register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, implicitOption)); - } else { - throw new TemplateBuildException("Cannot build template with filed option"); - } - } - - public static void register(Class target, FieldList flist) throws NoSuchFieldException { - TemplateBuilder builder = builderSelectorRegistry.select(target); - if (builder != null && builder instanceof CustomTemplateBuilder) { - register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, flist)); - } else { - throw new TemplateBuildException("Cannot build template with filed list"); - } - } - - public static synchronized void register(Type rawType, Template tmpl) { - if (rawType instanceof ParameterizedType) { - rawType = ((ParameterizedType)rawType).getRawType(); - } - map.put(rawType, tmpl); - } - - public static synchronized boolean unregister(Class target) { - Template tmpl = map.remove(target); - return tmpl != null; - } - - public static synchronized void registerGeneric(Type rawType, GenericTemplate gtmpl) { - if(rawType instanceof ParameterizedType) { - rawType = ((ParameterizedType)rawType).getRawType(); - } - genericMap.put(rawType, gtmpl); - } - - public static synchronized Template lookup(Type targetType) { - return lookupImpl(targetType, true, false, true); - } - - public static synchronized Template lookup(Type targetType, boolean forceBuild) { - return lookupImpl(targetType, true, forceBuild, true); - } - - public static synchronized Template lookup(Type targetType, boolean forceLoad, boolean forceBuild) { - return lookupImpl(targetType, forceLoad, forceBuild, true); - } - - public static synchronized Template tryLookup(Type targetType) { - return lookupImpl(targetType, true, false, false); - } - - public static synchronized Template tryLookup(Type targetType, boolean forceBuild) { - return lookupImpl(targetType, true, forceBuild, false); - } - - private static synchronized Template lookupImpl(Type targetType, - boolean forceLoad, boolean forceBuild, boolean fallbackDefault) { - Template tmpl; - - if(targetType instanceof ParameterizedType) { - // ParameterizedType is not a Class? - tmpl = lookupGenericImpl((ParameterizedType)targetType); - if(tmpl != null) { - return tmpl; - } - targetType = ((ParameterizedType)targetType).getRawType(); - } - - tmpl = map.get(targetType); - if(tmpl != null) { - return tmpl; - } - - // find match TemplateBuilder - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetType); - if (builder != null) { - if (forceLoad) { - tmpl = builder.loadTemplate(targetType); - if (tmpl != null) { - register(targetType, tmpl); - return tmpl; - } - } - - tmpl = builder.buildTemplate(targetType); - if (tmpl != null) { - register(targetType, tmpl); - return tmpl; - } - } - - Class target = (Class)targetType; - - for(Class i : target.getInterfaces()) { - tmpl = map.get(i); - if(tmpl != null) { - register(target, tmpl); - return tmpl; - } - } - - Class c = target.getSuperclass(); - if(c != null) { - for(; c != Object.class; c = c.getSuperclass()) { - tmpl = map.get(c); - if(tmpl != null) { - register(target, tmpl); - return tmpl; - } - } - - if(forceBuild) { - tmpl = builderSelectorRegistry.getForceBuilder().buildTemplate(target); - register(target, tmpl); - return tmpl; - } - } - - if(fallbackDefault) { - tmpl = new DefaultTemplate((Class)target); - register(target, tmpl); - return tmpl; - } else { - return null; - } - } - - public static synchronized Template lookupGeneric(Type targetType) { - if(targetType instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType)targetType; - Template tmpl = lookupGenericImpl(parameterizedType); - if(tmpl != null) { - return tmpl; - } - return new DefaultTemplate((Class)parameterizedType.getRawType(), parameterizedType); - } else { - throw new IllegalArgumentException("Actual types of the generic type are erased: "+targetType); - } - } - - private static synchronized Template lookupGenericImpl(ParameterizedType type) { - Type rawType = type.getRawType(); - GenericTemplate gtmpl = genericMap.get(rawType); - if(gtmpl == null) { - return null; - } - - Type[] types = type.getActualTypeArguments(); - Template[] tmpls = new Template[types.length]; - for(int i=0; i < types.length; i++) { - tmpls[i] = lookup(types[i]); - } - - return gtmpl.build(tmpls); - } -} - diff --git a/java/src/main/java/org/msgpack/template/builder/AnnotationTemplateBuilderSelector.java b/java/src/main/java/org/msgpack/template/builder/AnnotationTemplateBuilderSelector.java deleted file mode 100644 index 10bf5e62..00000000 --- a/java/src/main/java/org/msgpack/template/builder/AnnotationTemplateBuilderSelector.java +++ /dev/null @@ -1,54 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; - -import org.msgpack.annotation.MessagePackMessage; - -public class AnnotationTemplateBuilderSelector implements BuilderSelector{ - - public static final String NAME = "AnnotationTemplateBuilder"; - - TemplateBuilder builder; - - public AnnotationTemplateBuilderSelector(TemplateBuilder builder){ - this.builder = builder; - } - - @Override - public String getName(){ - return NAME; - } - - @Override - public boolean matchType(Type targetType) { - Class targetClass = (Class)targetType; - return isAnnotated(targetClass, MessagePackMessage.class); - } - - @Override - public TemplateBuilder getTemplateBuilder(Type targetType) { - return builder; - } - - public static boolean isAnnotated(Class targetClass, Class with) { - return targetClass.getAnnotation(with) != null; - } -} diff --git a/java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilder.java b/java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilder.java deleted file mode 100644 index 24e277ac..00000000 --- a/java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilder.java +++ /dev/null @@ -1,194 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.io.IOException; -import java.lang.reflect.Array; -import java.lang.reflect.GenericArrayType; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -import org.msgpack.AbstractTemplate; -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.template.BooleanArrayTemplate; -import org.msgpack.template.DoubleArrayTemplate; -import org.msgpack.template.FloatArrayTemplate; -import org.msgpack.template.IntArrayTemplate; -import org.msgpack.template.LongArrayTemplate; -import org.msgpack.template.ShortArrayTemplate; -import org.msgpack.template.TemplateRegistry; - -public class ArrayTemplateBuilder implements TemplateBuilder { - - static class ReflectionObjectArrayTemplate extends AbstractTemplate { - private Class componentClass; - private Template elementTemplate; - - public ReflectionObjectArrayTemplate(Class componentClass, Template elementTemplate) { - this.componentClass = componentClass; - this.elementTemplate = elementTemplate; - } - - public void pack(Packer pk, Object target) throws IOException { - if(!(target instanceof Object[]) || !componentClass.isAssignableFrom(target.getClass().getComponentType())) { - throw new MessageTypeException(); - } - Object[] array = (Object[])target; - int length = array.length; - pk.packArray(length); - for(int i=0; i < length; i++) { - elementTemplate.pack(pk, array[i]); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException { - int length = pac.unpackArray(); - Object[] array = (Object[])Array.newInstance(componentClass, length); - for(int i=0; i < length; i++) { - array[i] = elementTemplate.unpack(pac, null); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - int length = src.length; - Object[] array = (Object[])Array.newInstance(componentClass, length); - for(int i=0; i < length; i++) { - array[i] = elementTemplate.convert(src[i], null); - } - return array; - } - } - - static class ReflectionMultidimentionalArrayTemplate extends AbstractTemplate { - private Class componentClass; - private Template componentTemplate; - - public ReflectionMultidimentionalArrayTemplate(Class componentClass, Template componentTemplate) { - this.componentClass = componentClass; - this.componentTemplate = componentTemplate; - } - - Class getComponentClass() { - return componentClass; - } - - public void pack(Packer pk, Object target) throws IOException { - Object[] array = (Object[])target; - int length = array.length; - pk.packArray(length); - for(int i=0; i < length; i++) { - componentTemplate.pack(pk, array[i]); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - Object[] array = (Object[])Array.newInstance(componentClass, length); - for(int i=0; i < length; i++) { - array[i] = componentTemplate.unpack(pac, null); - } - return array; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - MessagePackObject[] src = from.asArray(); - int length = src.length; - Object[] array = (Object[])Array.newInstance(componentClass, length); - for(int i=0; i < length; i++) { - array[i] = componentTemplate.convert(src[i], null); - } - return array; - } - } - - @Override - public Template buildTemplate(Type arrayType) { - Type baseType; - Class baseClass; - int dim = 1; - if(arrayType instanceof GenericArrayType) { - GenericArrayType type = (GenericArrayType)arrayType; - baseType = type.getGenericComponentType(); - while(baseType instanceof GenericArrayType) { - baseType = ((GenericArrayType)baseType).getGenericComponentType(); - dim += 1; - } - if(baseType instanceof ParameterizedType) { - baseClass = (Class)((ParameterizedType)baseType).getRawType(); - } else { - baseClass = (Class)baseType; - } - } else { - Class type = (Class)arrayType; - baseClass = type.getComponentType(); - while(baseClass.isArray()) { - baseClass = baseClass.getComponentType(); - dim += 1; - } - baseType = baseClass; - } - return toTemplate(arrayType, baseType, baseClass, dim); - - } - - private Template toTemplate(Type arrayType, Type genericBaseType, Class baseClass, int dim) { - if(dim == 1) { - if(baseClass == boolean.class) { - return BooleanArrayTemplate.getInstance(); - } else if(baseClass == short.class) { - return ShortArrayTemplate.getInstance(); - } else if(baseClass == int.class) { - return IntArrayTemplate.getInstance(); - } else if(baseClass == long.class) { - return LongArrayTemplate.getInstance(); - } else if(baseClass == float.class) { - return FloatArrayTemplate.getInstance(); - } else if(baseClass == double.class) { - return DoubleArrayTemplate.getInstance(); - } else { - Template baseTemplate = TemplateRegistry.lookup(genericBaseType); - return new ReflectionObjectArrayTemplate(baseClass, baseTemplate); - } - } else if(dim == 2) { - Class componentClass = Array.newInstance(baseClass, 0).getClass(); - Template componentTemplate = toTemplate(arrayType, genericBaseType, baseClass, dim-1); - return new ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate); - } else { - ReflectionMultidimentionalArrayTemplate componentTemplate = (ReflectionMultidimentionalArrayTemplate) - toTemplate(arrayType, genericBaseType, baseClass, dim-1); - Class componentClass = Array.newInstance(componentTemplate.getComponentClass(), 0).getClass(); - return new ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate); - } - } - - @Override - public void writeTemplate(Type targetType, String directoryName) { - throw new UnsupportedOperationException(targetType.toString()); - } - - @Override - public Template loadTemplate(Type targetType) { - return null; - } -} diff --git a/java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilderSelector.java b/java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilderSelector.java deleted file mode 100644 index eeefd7ec..00000000 --- a/java/src/main/java/org/msgpack/template/builder/ArrayTemplateBuilderSelector.java +++ /dev/null @@ -1,49 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.GenericArrayType; -import java.lang.reflect.Type; - -public class ArrayTemplateBuilderSelector implements BuilderSelector { - - public static final String NAME = "ArrayTemplateBuilder"; - - ArrayTemplateBuilder templateBuilder = new ArrayTemplateBuilder(); - - @Override - public String getName(){ - return NAME; - } - - @Override - public boolean matchType(Type targetType) { - if(targetType instanceof GenericArrayType){ - return true; - } - Class targetClass = (Class)targetType; - return targetClass.isArray(); - } - - @Override - public TemplateBuilder getTemplateBuilder(Type target) { - return templateBuilder; - } - - -} diff --git a/java/src/main/java/org/msgpack/template/builder/BeansBuildContext.java b/java/src/main/java/org/msgpack/template/builder/BeansBuildContext.java deleted file mode 100644 index 652a34f0..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BeansBuildContext.java +++ /dev/null @@ -1,285 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -import org.msgpack.*; -import org.msgpack.template.*; - -import javassist.CannotCompileException; -import javassist.CtClass; -import javassist.CtConstructor; -import javassist.CtNewConstructor; -import javassist.NotFoundException; - -public class BeansBuildContext extends BuildContextBase { - protected BeansFieldEntry[] entries; - protected Class origClass; - protected String origName; - protected Template[] templates; - protected int minimumArrayLength; - - public BeansBuildContext(JavassistTemplateBuilder director) { - super(director); - } - - public Template buildTemplate(Class targetClass, BeansFieldEntry[] entries, Template[] templates) { - this.entries = entries; - this.templates = templates; - this.origClass = targetClass; - this.origName = this.origClass.getName(); - return build(this.origName); - } - - protected void setSuperClass() throws CannotCompileException, NotFoundException { - this.tmplCtClass.setSuperclass( - director.getCtClass(JavassistTemplateBuilder.JavassistTemplate.class.getName())); - } - - protected void buildConstructor() throws CannotCompileException, NotFoundException { - // Constructor(Class targetClass, Template[] templates) - CtConstructor newCtCons = CtNewConstructor.make( - new CtClass[] { - director.getCtClass(Class.class.getName()), - director.getCtClass(Template.class.getName()+"[]") - }, - new CtClass[0], - this.tmplCtClass); - this.tmplCtClass.addConstructor(newCtCons); - } - - protected Template buildInstance(Class c) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { - Constructor cons = c.getConstructor(new Class[] { - Class.class, - Template[].class - }); - Object tmpl = cons.newInstance(new Object[] { - this.origClass, - this.templates - }); - return (Template)tmpl; - } - - protected void buildMethodInit() { - this.minimumArrayLength = 0; - for(int i=0; i < entries.length; i++) { - IFieldEntry e = entries[i]; - if(e.isRequired() || e.isNullable()) { - this.minimumArrayLength = i+1; - } - } - } - - protected String buildPackMethodBody() { - resetStringBuilder(); - buildString("{"); - buildString("%s _$$_t = (%s)$2;", this.origName, this.origName); - buildString("$1.packArray(%d);", entries.length); - for(int i=0; i < entries.length; i++) { - BeansFieldEntry e = entries[i]; - if(!e.isAvailable()) { - buildString("$1.packNil();"); - continue; - } - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("$1.%s(_$$_t.%s());", primitivePackName(type), e.getGetterName()); - } else { - buildString("if(_$$_t.%s() == null) {", e.getGetterName()); - if(!e.isNullable() && !e.isOptional()) { - buildString("throw new %s();", MessageTypeException.class.getName()); - } else { - buildString("$1.packNil();"); - } - buildString("} else {"); - buildString(" this.templates[%d].pack($1, _$$_t.%s());", i, e.getGetterName()); - buildString("}"); - } - } - buildString("}"); - return getBuiltString(); - } - - protected String buildUnpackMethodBody() { - resetStringBuilder(); - buildString("{ "); - - buildString("%s _$$_t;", this.origName); - buildString("if($2 == null) {"); - buildString(" _$$_t = new %s();", this.origName); - buildString("} else {"); - buildString(" _$$_t = (%s)$2;", this.origName); - buildString("}"); - - buildString("int length = $1.unpackArray();"); - buildString("if(length < %d) {", this.minimumArrayLength); - buildString(" throw new %s();", MessageTypeException.class.getName()); - buildString("}"); - - int i; - for(i=0; i < this.minimumArrayLength; i++) { - BeansFieldEntry e = entries[i]; - if(!e.isAvailable()) { - buildString("$1.unpackObject();"); - continue; - } - - buildString("if($1.tryUnpackNull()) {"); - if(e.isRequired()) { - // Required + nil => exception - buildString("throw new %s();", MessageTypeException.class.getName()); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - buildString("_$$_t.%s(null);", e.getSetterName()); - } - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.set%s( $1.%s() );", e.getName(), primitiveUnpackName(type)); - } else { - buildString("_$$_t.set%s( (%s)this.templates[%d].unpack($1, _$$_t.get%s()) );", e.getName(), e.getJavaTypeName(), i, e.getName()); - } - buildString("}"); - } - - for(; i < entries.length; i++) { - buildString("if(length <= %d) { return _$$_t; }", i); - - BeansFieldEntry e = entries[i]; - if(!e.isAvailable()) { - buildString("$1.unpackObject();"); - continue; - } - - buildString("if($1.tryUnpackNull()) {"); - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.%s( $1.%s() );", e.getSetterName(), primitiveUnpackName(type)); - } else { - buildString("_$$_t.%s( (%s)this.templates[%d].unpack($1, _$$_t.%s()) );", e.getSetterName(), e.getJavaTypeName(), i, e.getGetterName()); - } - buildString("}"); - } - - // latter entries are all Optional + nil => keep default value - - buildString("for(int i=%d; i < length; i++) {", i); - buildString(" $1.unpackObject();"); - buildString("}"); - - buildString("return _$$_t;"); - - buildString("}"); - return getBuiltString(); - } - - protected String buildConvertMethodBody() { - resetStringBuilder(); - buildString("{ "); - - buildString("%s _$$_t;", this.origName); - buildString("if($2 == null) {"); - buildString(" _$$_t = new %s();", this.origName); - buildString("} else {"); - buildString(" _$$_t = (%s)$2;", this.origName); - buildString("}"); - - buildString("%s[] array = $1.asArray();", MessagePackObject.class.getName()); - buildString("int length = array.length;"); - buildString("if(length < %d) {", this.minimumArrayLength); - buildString(" throw new %s();", MessageTypeException.class.getName()); - buildString("}"); - - buildString("%s obj;", MessagePackObject.class.getName()); - - int i; - for(i=0; i < this.minimumArrayLength; i++) { - BeansFieldEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - buildString("obj = array[%d];", i); - buildString("if(obj.isNil()) {"); - if(e.isRequired()) { - // Required + nil => exception - buildString("throw new %s();", MessageTypeException.class.getName()); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - buildString("_$$_t.%s( null );", e.getSetterName()); - } - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.%s( obj.%s() );", e.getSetterName(), primitiveConvertName(type)); - } else { - buildString("_$$_t.%s( (%s)this.templates[%d].convert(obj, _$$_t.%s()) );", e.getSetterName(), e.getJavaTypeName(), i, e.getGetterName()); - } - buildString("}"); - } - - for(; i < entries.length; i++) { - buildString("if(length <= %d) { return _$$_t; }", i); - - BeansFieldEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - buildString("obj = array[%d];", i); - buildString("if(obj.isNil()) {"); - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.%s( obj.%s() );", e.getSetterName(), primitiveConvertName(type)); - } else { - buildString("_$$_t.%s( (%s)this.templates[%d].convert(obj, _$$_t.%s()) );", e.getSetterName(), e.getJavaTypeName(), i, e.getGetterName()); - } - buildString("}"); - } - - // latter entries are all Optional + nil => keep default value - - buildString("return _$$_t;"); - - buildString("}"); - return getBuiltString(); - } - - @Override - public void writeTemplate(Class targetClass, BeansFieldEntry[] entries, - Template[] templates, String directoryName) { - throw new UnsupportedOperationException(targetClass.getName()); - } - - @Override - public Template loadTemplate(Class targetClass, BeansFieldEntry[] entries, Template[] templates) { - return null; - } -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilder.java b/java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilder.java deleted file mode 100644 index cc81f6b1..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilder.java +++ /dev/null @@ -1,328 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.io.IOException; -import java.lang.reflect.Type; - -import org.msgpack.AbstractTemplate; -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.template.BeansFieldEntry; -import org.msgpack.template.BeansFieldEntryReader; -import org.msgpack.template.IFieldEntry; -import org.msgpack.template.IFieldEntryReader; -import org.msgpack.template.TemplateRegistry; - -/** - * Class for building java reflection template builder for java beans class. - * @author takeshita - * - */ -public class BeansTemplateBuilder extends CustomTemplateBuilder{ - - IFieldEntryReader reader = new BeansFieldEntryReader(); - - public BeansTemplateBuilder(){} - - @Override - public IFieldEntryReader getFieldEntryReader(){ - return reader; - } - - static class ReflectionEntry{ - BeansFieldEntry entry; - public ReflectionEntry(BeansFieldEntry entry){ - this.entry = entry; - } - - public void pack(Object value , Packer packer) throws IOException{ - packer.pack(value); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - entry.set(target, obj.convert(entry.getType())); - } - - public void unpack(Object target, Unpacker unpacker) throws IOException, MessageTypeException, IllegalAccessException { - entry.set(target, unpacker.unpack(entry.getType())); - } - - public void setNull(Object target){ - entry.set(target, null); - } - - public boolean isRequired(){ - return entry.isRequired(); - } - public boolean isNullable(){ - return entry.isNullable(); - } - public boolean isAvailable(){ - return entry.isAvailable(); - } - public boolean isOptional(){ - return entry.isOptional(); - } - public Object get(Object target){ - return entry.get(target); - } - - } - - static class ObjectFieldEntry extends ReflectionEntry{ - Template template; - public ObjectFieldEntry(BeansFieldEntry entry,Template template){ - super(entry); - this.template = template; - } - public void pack(Object value , Packer packer) throws IOException{ - template.pack(packer,value); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - Class type = (Class)entry.getType(); - Object fieldReference = entry.get(target); - Object valueReference = template.convert(obj, fieldReference); - if(valueReference != fieldReference) { - entry.set(target, valueReference); - } - } - - public void unpack(Object target, Unpacker unpacker) throws IOException, MessageTypeException, IllegalAccessException { - - Class type = (Class)entry.getType(); - Object fieldReference = entry.get(target); - Object valueReference = template.unpack(unpacker, fieldReference); - if(valueReference != fieldReference) { - entry.set(target, valueReference); - } - } - } - - static class BeansReflectionTemplate extends AbstractTemplate{ - - Class targetClass; - ReflectionEntry[] entries = null; - protected int minimumArrayLength; - - public BeansReflectionTemplate( - Class targetClass, - ReflectionEntry[] entries){ - this.targetClass = targetClass; - this.entries = entries; - this.minimumArrayLength = 0; - for(int i=0; i < entries.length; i++) { - ReflectionEntry e = entries[i]; - if(e.isRequired() || e.isNullable()) { - this.minimumArrayLength = i+1; - } - } - } - - - @Override - public void pack(Packer pk, Object target) throws IOException { - - pk.packArray(entries.length); - for(ReflectionEntry e : entries){ - if(!e.isAvailable()){ - pk.packNil(); - continue; - } - Object obj = e.get(target); - if(obj == null) { - if(!e.isNullable() && !e.isOptional()) { - throw new MessageTypeException(); - } - pk.packNil(); - } else { - e.pack(obj, pk); - } - } - - } - @Override - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - try { - if(to == null) { - to = targetClass.newInstance(); - } - - int length = pac.unpackArray(); - if(length < minimumArrayLength) { - throw new MessageTypeException(); - } - - int i; - for(i=0; i < minimumArrayLength; i++) { - ReflectionEntry e = entries[i]; - if(!e.isAvailable()) { - pac.unpackObject(); - continue; - } - - if(pac.tryUnpackNull()) { - if(e.isRequired()) { - // Required + nil => exception - throw new MessageTypeException(); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - e.setNull(to); - } - } else { - e.unpack(to,pac); - //e.set(to, pac.unpack(e.getType())); - } - } - - int max = length < entries.length ? length : entries.length; - for(; i < max; i++) { - ReflectionEntry e = entries[i]; - if(!e.isAvailable()) { - pac.unpackObject(); - continue; - } - - if(pac.tryUnpackNull()) { - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - } else { - e.unpack(to, pac); - //e.set(to, pac.unpack(e.getType())); - } - } - - // latter entries are all Optional + nil => keep default value - - for(; i < length; i++) { - pac.unpackObject(); - } - - return to; - - } catch (MessageTypeException e) { - throw e; - } catch (IOException e) { - throw e; - } catch (Exception e) { - throw new MessageTypeException(e); - } - } - - @Override - public Object convert(MessagePackObject from, Object to) - throws MessageTypeException { - try { - if(to == null) { - to = targetClass.newInstance(); - } - - MessagePackObject[] array = from.asArray(); - int length = array.length; - if(length < minimumArrayLength) { - throw new MessageTypeException(); - } - - int i; - for(i=0; i < minimumArrayLength; i++) { - ReflectionEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - MessagePackObject obj = array[i]; - if(obj.isNil()) { - if(e.isRequired()) { - // Required + nil => exception - throw new MessageTypeException(); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - e.setNull(to); - //e.set(to,null); - } - } else { - e.convert(to, obj); - //e.set(to, from.convert(e.getType())); - } - } - - int max = length < entries.length ? length : entries.length; - for(; i < max; i++) { - ReflectionEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - MessagePackObject obj = array[i]; - if(obj.isNil()) { - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - } else { - e.convert(to, obj); - //e.set(to, obj.convert(e.getType())); - } - } - - // latter entries are all Optional + nil => keep default value - - return to; - - } catch (MessageTypeException e) { - throw e; - } catch (Exception e) { - throw new MessageTypeException(e); - } - } - - } - - - @Override - public Template buildTemplate(Class targetClass, IFieldEntry[] entries) { - ReflectionEntry[] refEntries = new ReflectionEntry[entries.length]; - for(int i = 0;i < entries.length;i++){ - BeansFieldEntry e = (BeansFieldEntry)entries[i]; - Class type = e.getType(); - if(type.equals(boolean.class)) { - refEntries[i] = new ReflectionEntry(e); - } else if(type.equals(byte.class)) { - refEntries[i] = new ReflectionEntry(e); - } else if(type.equals(short.class)) { - refEntries[i] = new ReflectionEntry(e); - } else if(type.equals(int.class)) { - refEntries[i] = new ReflectionEntry(e); - } else if(type.equals(long.class)) { - refEntries[i] = new ReflectionEntry(e); - } else if(type.equals(float.class)) { - refEntries[i] = new ReflectionEntry(e); - } else if(type.equals(double.class)) { - refEntries[i] = new ReflectionEntry(e); - } else { - Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true); - refEntries[i] = new ObjectFieldEntry(e, tmpl); - } - } - return new BeansReflectionTemplate(targetClass,refEntries); - } -} diff --git a/java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilderSelector.java b/java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilderSelector.java deleted file mode 100644 index b875c2f7..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BeansTemplateBuilderSelector.java +++ /dev/null @@ -1,49 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Type; - -import org.msgpack.annotation.MessagePackBeans; - -public class BeansTemplateBuilderSelector implements BuilderSelector{ - - public static final String NAME = "BeansTemplateBuilder"; - - TemplateBuilder builder; - - public BeansTemplateBuilderSelector(TemplateBuilder builder){ - this.builder = builder; - } - - @Override - public String getName(){ - return NAME; - } - - @Override - public boolean matchType(Type targetType) { - Class targetClass = (Class)targetType; - return AnnotationTemplateBuilderSelector.isAnnotated(targetClass, MessagePackBeans.class); - } - - @Override - public TemplateBuilder getTemplateBuilder(Type targetType) { - return builder; - } -} diff --git a/java/src/main/java/org/msgpack/template/builder/BuildContext.java b/java/src/main/java/org/msgpack/template/builder/BuildContext.java deleted file mode 100644 index ef843a96..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BuildContext.java +++ /dev/null @@ -1,293 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -import org.msgpack.*; -import org.msgpack.template.*; - -import javassist.CannotCompileException; -import javassist.CtClass; -import javassist.CtConstructor; -import javassist.CtNewConstructor; -import javassist.NotFoundException; - -public class BuildContext extends BuildContextBase { - protected IFieldEntry[] entries; - protected Class origClass; - protected String origName; - protected Template[] templates; - protected int minimumArrayLength; - - public BuildContext(JavassistTemplateBuilder director) { - super(director); - } - - public Template buildTemplate(Class targetClass, FieldEntry[] entries, Template[] templates) { - this.entries = entries; - this.templates = templates; - this.origClass = targetClass; - this.origName = this.origClass.getName(); - return build(this.origName); - } - - protected void setSuperClass() throws CannotCompileException, NotFoundException { - this.tmplCtClass.setSuperclass( - director.getCtClass(JavassistTemplateBuilder.JavassistTemplate.class.getName())); - } - - protected void buildConstructor() throws CannotCompileException, NotFoundException { - // Constructor(Class targetClass, Template[] templates) - CtConstructor newCtCons = CtNewConstructor.make( - new CtClass[] { - director.getCtClass(Class.class.getName()), - director.getCtClass(Template.class.getName()+"[]") - }, - new CtClass[0], - this.tmplCtClass); - this.tmplCtClass.addConstructor(newCtCons); - } - - protected Template buildInstance(Class c) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { - Constructor cons = c.getConstructor(new Class[] { - Class.class, - Template[].class - }); - Object tmpl = cons.newInstance(new Object[] { - this.origClass, - this.templates - }); - return (Template)tmpl; - } - - protected void buildMethodInit() { - this.minimumArrayLength = 0; - for(int i=0; i < entries.length; i++) { - IFieldEntry e = entries[i]; - if(e.isRequired() || e.isNullable()) { - this.minimumArrayLength = i+1; - } - } - } - - protected String buildPackMethodBody() { - resetStringBuilder(); - buildString("{"); - buildString("%s _$$_t = (%s)$2;", this.origName, this.origName); - buildString("$1.packArray(%d);", entries.length); - for(int i=0; i < entries.length; i++) { - IFieldEntry e = entries[i]; - if(!e.isAvailable()) { - buildString("$1.packNil();"); - continue; - } - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("$1.%s(_$$_t.%s);", primitivePackName(type), e.getName()); - } else { - buildString("if(_$$_t.%s == null) {", e.getName()); - if(!e.isNullable() && !e.isOptional()) { - buildString("throw new %s();", MessageTypeException.class.getName()); - } else { - buildString("$1.packNil();"); - } - buildString("} else {"); - buildString(" this.templates[%d].pack($1, _$$_t.%s);", i, e.getName()); - buildString("}"); - } - } - buildString("}"); - return getBuiltString(); - } - - protected String buildUnpackMethodBody() { - resetStringBuilder(); - buildString("{ "); - - buildString("%s _$$_t;", this.origName); - buildString("if($2 == null) {"); - buildString(" _$$_t = new %s();", this.origName); - buildString("} else {"); - buildString(" _$$_t = (%s)$2;", this.origName); - buildString("}"); - - buildString("int length = $1.unpackArray();"); - buildString("if(length < %d) {", this.minimumArrayLength); - buildString(" throw new %s();", MessageTypeException.class.getName()); - buildString("}"); - - int i; - for(i=0; i < this.minimumArrayLength; i++) { - IFieldEntry e = entries[i]; - if(!e.isAvailable()) { - buildString("$1.unpackObject();"); - continue; - } - - buildString("if($1.tryUnpackNull()) {"); - if(e.isRequired()) { - // Required + nil => exception - buildString("throw new %s();", MessageTypeException.class.getName()); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - buildString("_$$_t.%s = null;", e.getName()); - } - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.%s = $1.%s();", e.getName(), primitiveUnpackName(type)); - } else { - buildString("_$$_t.%s = (%s)this.templates[%d].unpack($1, _$$_t.%s);", e.getName(), e.getJavaTypeName(), i, e.getName()); - } - buildString("}"); - } - - for(; i < entries.length; i++) { - buildString("if(length <= %d) { return _$$_t; }", i); - - IFieldEntry e = entries[i]; - if(!e.isAvailable()) { - buildString("$1.unpackObject();"); - continue; - } - - buildString("if($1.tryUnpackNull()) {"); - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.%s = $1.%s();", e.getName(), primitiveUnpackName(type)); - } else { - buildString("_$$_t.%s = (%s)this.templates[%d].unpack($1, _$$_t.%s);", e.getName(), e.getJavaTypeName(), i, e.getName()); - } - buildString("}"); - } - - // latter entries are all Optional + nil => keep default value - - buildString("for(int i=%d; i < length; i++) {", i); - buildString(" $1.unpackObject();"); - buildString("}"); - - buildString("return _$$_t;"); - - buildString("}"); - return getBuiltString(); - } - - protected String buildConvertMethodBody() { - resetStringBuilder(); - buildString("{ "); - - buildString("%s _$$_t;", this.origName); - buildString("if($2 == null) {"); - buildString(" _$$_t = new %s();", this.origName); - buildString("} else {"); - buildString(" _$$_t = (%s)$2;", this.origName); - buildString("}"); - - buildString("%s[] array = $1.asArray();", MessagePackObject.class.getName()); - buildString("int length = array.length;"); - buildString("if(length < %d) {", this.minimumArrayLength); - buildString(" throw new %s();", MessageTypeException.class.getName()); - buildString("}"); - - buildString("%s obj;", MessagePackObject.class.getName()); - - int i; - for(i=0; i < this.minimumArrayLength; i++) { - IFieldEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - buildString("obj = array[%d];", i); - buildString("if(obj.isNil()) {"); - if(e.isRequired()) { - // Required + nil => exception - buildString("throw new %s();", MessageTypeException.class.getName()); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - buildString("_$$_t.%s = null;", e.getName()); - } - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.%s = obj.%s();", e.getName(), primitiveConvertName(type)); - } else { - buildString("_$$_t.%s = (%s)this.templates[%d].convert(obj, _$$_t.%s);", e.getName(), e.getJavaTypeName(), i, e.getName()); - } - buildString("}"); - } - - for(; i < entries.length; i++) { - buildString("if(length <= %d) { return _$$_t; }", i); - - IFieldEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - buildString("obj = array[%d];", i); - buildString("if(obj.isNil()) {"); - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - buildString("} else {"); - Class type = e.getType(); - if(type.isPrimitive()) { - buildString("_$$_t.%s = obj.%s();", e.getName(), primitiveConvertName(type)); - } else { - buildString("_$$_t.%s = (%s)this.templates[%d].convert(obj, _$$_t.%s);", e.getName(), e.getJavaTypeName(), i, e.getName()); - } - buildString("}"); - } - - // latter entries are all Optional + nil => keep default value - - buildString("return _$$_t;"); - - buildString("}"); - return getBuiltString(); - } - - @Override - public void writeTemplate(Class targetClass, FieldEntry[] entries, - Template[] templates, String directoryName) { - this.entries = entries; - this.templates = templates; - this.origClass = targetClass; - this.origName = this.origClass.getName(); - write(this.origName, directoryName); - } - - @Override - public Template loadTemplate(Class targetClass, FieldEntry[] entries, Template[] templates) { - this.entries = entries; - this.templates = templates; - this.origClass = targetClass; - this.origName = this.origClass.getName(); - return load(this.origName); - } -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/template/builder/BuildContextBase.java b/java/src/main/java/org/msgpack/template/builder/BuildContextBase.java deleted file mode 100644 index 229f1c8c..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BuildContextBase.java +++ /dev/null @@ -1,285 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; - -import org.msgpack.*; -import org.msgpack.template.*; - -import javassist.CannotCompileException; -import javassist.CtClass; -import javassist.CtMethod; -import javassist.CtNewMethod; -import javassist.NotFoundException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public abstract class BuildContextBase { - - private static Logger LOG = LoggerFactory.getLogger(JavassistTemplateBuilder.class); - - protected JavassistTemplateBuilder director; - - protected String tmplName; - - protected CtClass tmplCtClass; - - protected abstract Template buildTemplate(Class targetClass, T[] entries, Template[] templates); - - protected abstract void setSuperClass() throws CannotCompileException, NotFoundException; - - protected abstract void buildConstructor() throws CannotCompileException, NotFoundException; - - protected void buildMethodInit() { } - - protected abstract String buildPackMethodBody(); - - protected abstract String buildUnpackMethodBody(); - - protected abstract String buildConvertMethodBody(); - - protected abstract Template buildInstance(Class c) - throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException; - - protected abstract void writeTemplate(Class targetClass, T[] entries, - Template[] templates, String directoryName); - - protected abstract Template loadTemplate(Class targetClass, T[] entries, Template[] templates); - - public BuildContextBase(JavassistTemplateBuilder director) { - this.director = director; - } - - protected Template build(final String className) { - try { - reset(className, false); - buildClass(); - buildConstructor(); - buildMethodInit(); - buildPackMethod(); - buildUnpackMethod(); - buildConvertMethod(); - return buildInstance(createClass()); - } catch (Exception e) { - String code = getBuiltString(); - if(code != null) { - LOG.error("builder: " + code, e); - throw new TemplateBuildException("Cannot compile: " + code, e); - } else { - throw new TemplateBuildException(e); - } - } - } - - protected void reset(String className, boolean isWritten) { - String tmplName = null; - if (!isWritten) { - tmplName = className + "_$$_Template" + director.nextSeqId(); - } else { - tmplName = className + "_$$_Template"; - } - tmplCtClass = director.makeCtClass(tmplName); - } - - protected void buildClass() throws CannotCompileException, NotFoundException { - setSuperClass(); - tmplCtClass.addInterface(director.getCtClass(Template.class.getName())); - } - - protected void buildPackMethod() throws CannotCompileException, NotFoundException { - String mbody = buildPackMethodBody(); - int mod = javassist.Modifier.PUBLIC; - CtClass returnType = CtClass.voidType; - String mname = "pack"; - CtClass[] paramTypes = new CtClass[] { - director.getCtClass(Packer.class.getName()), - director.getCtClass(Object.class.getName()) - }; - CtClass[] exceptTypes = new CtClass[] { - director.getCtClass(IOException.class.getName()) - }; - CtMethod newCtMethod = CtNewMethod.make( - mod, returnType, mname, - paramTypes, exceptTypes, mbody, tmplCtClass); - tmplCtClass.addMethod(newCtMethod); - } - - protected void buildUnpackMethod() throws CannotCompileException, NotFoundException { - String mbody = buildUnpackMethodBody(); - int mod = javassist.Modifier.PUBLIC; - CtClass returnType = director.getCtClass(Object.class.getName()); - String mname = "unpack"; - CtClass[] paramTypes = new CtClass[] { - director.getCtClass(Unpacker.class.getName()), - director.getCtClass(Object.class.getName()) - }; - CtClass[] exceptTypes = new CtClass[] { - director.getCtClass(MessageTypeException.class.getName()) - }; - CtMethod newCtMethod = CtNewMethod.make( - mod, returnType, mname, - paramTypes, exceptTypes, mbody, tmplCtClass); - tmplCtClass.addMethod(newCtMethod); - } - - protected void buildConvertMethod() throws CannotCompileException, NotFoundException { - String mbody = buildConvertMethodBody(); - int mod = javassist.Modifier.PUBLIC; - CtClass returnType = director.getCtClass(Object.class.getName()); - String mname = "convert"; - CtClass[] paramTypes = new CtClass[] { - director.getCtClass(MessagePackObject.class.getName()), - director.getCtClass(Object.class.getName()) - }; - CtClass[] exceptTypes = new CtClass[] { - director.getCtClass(MessageTypeException.class.getName()) - }; - CtMethod newCtMethod = CtNewMethod.make( - mod, returnType, mname, - paramTypes, exceptTypes, mbody, tmplCtClass); - tmplCtClass.addMethod(newCtMethod); - } - - protected Class createClass() throws CannotCompileException { - return (Class) tmplCtClass.toClass(null, null); - } - - protected void saveClass(final String directoryName) throws CannotCompileException, IOException { - tmplCtClass.writeFile(directoryName); - } - - protected StringBuilder stringBuilder = null; - - protected void resetStringBuilder() { - stringBuilder = new StringBuilder(); - } - - protected void buildString(String str) { - stringBuilder.append(str); - } - - protected void buildString(String format, Object... args) { - stringBuilder.append(String.format(format, args)); - } - - protected String getBuiltString() { - if(stringBuilder == null) { - return null; - } - return stringBuilder.toString(); - } - - protected String primitivePackName(Class type) { - if(type == boolean.class) { - return "packBoolean"; - } else if(type == byte.class) { - return "packByte"; - } else if(type == short.class) { - return "packShort"; - } else if(type == int.class) { - return "packInt"; - } else if(type == long.class) { - return "packLong"; - } else if(type == float.class) { - return "packFloat"; - } else if(type == double.class) { - return "packDouble"; - } - return null; - } - - protected String primitiveUnpackName(Class type) { - if(type == boolean.class) { - return "unpackBoolean"; - } else if(type == byte.class) { - return "unpackByte"; - } else if(type == short.class) { - return "unpackShort"; - } else if(type == int.class) { - return "unpackInt"; - } else if(type == long.class) { - return "unpackLong"; - } else if(type == float.class) { - return "unpackFloat"; - } else if(type == double.class) { - return "unpackDouble"; - } - return null; - } - - protected String primitiveConvertName(Class type) { - if(type == boolean.class) { - return "asBoolean"; - } else if(type == byte.class) { - return "asByte"; - } else if(type == short.class) { - return "asShort"; - } else if(type == int.class) { - return "asInt"; - } else if(type == long.class) { - return "asLong"; - } else if(type == float.class) { - return "asFloat"; - } else if(type == double.class) { - return "asDouble"; - } - return null; - } - - protected void write(final String className, final String directoryName) { - try { - reset(className, true); - buildClass(); - buildConstructor(); - buildMethodInit(); - buildPackMethod(); - buildUnpackMethod(); - buildConvertMethod(); - saveClass(directoryName); - } catch (Exception e) { - String code = getBuiltString(); - if(code != null) { - LOG.error("builder: " + code, e); - throw new TemplateBuildException("Cannot compile: " + code, e); - } else { - throw new TemplateBuildException(e); - } - } - } - - protected Template load(final String className) { - String tmplName = className + "_$$_Template"; - try { - Class tmplClass = getClass().getClassLoader().loadClass(tmplName); - return buildInstance(tmplClass); - } catch (ClassNotFoundException e) { - return null; - } catch (Exception e) { - String code = getBuiltString(); - if(code != null) { - LOG.error("builder: " + code, e); - throw new TemplateBuildException("Cannot compile: " + code, e); - } else { - throw new TemplateBuildException(e); - } - } - } -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/template/builder/BuildContextFactory.java b/java/src/main/java/org/msgpack/template/builder/BuildContextFactory.java deleted file mode 100644 index 5974ef7f..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BuildContextFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - - -public interface BuildContextFactory { - - public BuildContextBase createBuildContext(JavassistTemplateBuilder builder); - -} diff --git a/java/src/main/java/org/msgpack/template/builder/BuilderSelector.java b/java/src/main/java/org/msgpack/template/builder/BuilderSelector.java deleted file mode 100644 index 8ca94b44..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BuilderSelector.java +++ /dev/null @@ -1,33 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Type; - -/** - * Match condition for TemplateBuilder. - * @author takeshita - * - */ -public interface BuilderSelector { - String getName(); - - boolean matchType(Type targetType); - - TemplateBuilder getTemplateBuilder(Type targetType); -} diff --git a/java/src/main/java/org/msgpack/template/builder/BuilderSelectorRegistry.java b/java/src/main/java/org/msgpack/template/builder/BuilderSelectorRegistry.java deleted file mode 100644 index 5ec0552e..00000000 --- a/java/src/main/java/org/msgpack/template/builder/BuilderSelectorRegistry.java +++ /dev/null @@ -1,212 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Type; -import java.util.LinkedList; -import java.util.List; - -import org.msgpack.template.BeansFieldEntryReader; - -/** - * Registry for BuilderSelectors. - * You can modify BuilderSelector chain throw this class. - * - * @author takeshita - * - */ -public class BuilderSelectorRegistry { - - private static BuilderSelectorRegistry instance = new BuilderSelectorRegistry(); - - static{ - initForJava(); - } - - public static BuilderSelectorRegistry getInstance(){ - return instance; - } - - TemplateBuilder forceBuilder; - - List builderSelectors = new LinkedList(); - - private BuilderSelectorRegistry(){ - } - - /** - * initialize BuilderSelectors for basic java enviroment. - */ - private static void initForJava(){ - instance.append(new ArrayTemplateBuilderSelector()); - if(isSupportJavassist()){ - instance.append(new AnnotationTemplateBuilderSelector(new JavassistTemplateBuilder())); - instance.forceBuilder = new JavassistTemplateBuilder(); - instance.append(new BeansTemplateBuilderSelector( - new JavassistTemplateBuilder( - new BeansFieldEntryReader(), - new BuildContextFactory() { - @Override - public BuildContextBase createBuildContext(JavassistTemplateBuilder builder) { - return new BeansBuildContext(builder); - } - } - ))); - }else{ - instance.append(new AnnotationTemplateBuilderSelector(new ReflectionTemplateBuilder())); - instance.forceBuilder = new ReflectionTemplateBuilder(); - instance.append(new BeansTemplateBuilderSelector(new BeansTemplateBuilder())); - } - instance.append(new OrdinalEnumTemplateBuilderSelector()); - instance.append(new EnumTemplateBuilderSelector()); - } - - public static boolean isSupportJavassist(){ - try { - return !System.getProperty("java.vm.name").equals("Dalvik"); - } catch (Exception e) { - return true; - } - } - - /** - * Check whether same name BuilderSelector is registered. - * @param builderSelectorName - * @return - */ - public boolean contains(String builderSelectorName){ - for(BuilderSelector bs : builderSelectors){ - if(bs.getName().equals(builderSelectorName)){ - return true; - } - } - return false; - } - /** - * Append BuilderSelector to tail - * @param builderSelector - */ - public void append(BuilderSelector builderSelector){ - if(contains(builderSelector.getName())){ - throw new RuntimeException("Duplicate BuilderSelector name:" + builderSelector.getName()); - } - this.builderSelectors.add(builderSelector); - } - /** - * Insert BuiderSelector to head - * @param builderSelector - */ - public void prepend(BuilderSelector builderSelector){ - if(contains(builderSelector.getName())){ - throw new RuntimeException("Duplicate BuilderSelector name:" + builderSelector.getName()); - } - if(builderSelectors.size() > 0){ - this.builderSelectors.add(0, builderSelector); - }else{ - this.builderSelectors.add(builderSelector); - } - } - - /** - * Insert BuilderSelector - * @param index - * @param builderSelector - */ - public void insert(int index,BuilderSelector builderSelector){ - if(contains(builderSelector.getName())){ - throw new RuntimeException("Duplicate BuilderSelector name:" + builderSelector.getName()); - } - if(builderSelectors.size() > 0){ - this.builderSelectors.add(index, builderSelector); - - }else{ - this.builderSelectors.add(builderSelector); - } - } - /** - * Replace same name BuilderSelector - * @param builderSelector - */ - public void replace(BuilderSelector builderSelector){ - String name = builderSelector.getName(); - int index = getIndex(name); - builderSelectors.add(index, builderSelector); - builderSelectors.remove(index + 1); - } - - /** - * Insert the BuilderSelector before BuilderSelector named "builderSelectorName". - * @param builderSelectorName - * @param builderSelector - */ - public void insertBefore(String builderSelectorName,BuilderSelector builderSelector){ - int index = getIndex(builderSelectorName); - - builderSelectors.add(index,builderSelector); - } - /** - * Insert the BuilderSelector after BuilderSelector named "builderSelectorName". - * @param builderSelectorName - * @param builderSelector - */ - public void insertAfter(String builderSelectorName,BuilderSelector builderSelector){ - int index = getIndex(builderSelectorName); - if(index + 1 == builderSelectors.size()){ - builderSelectors.add(builderSelector); - }else{ - builderSelectors.add(index + 1 , builderSelector); - } - } - private int getIndex(String builderSelectorName){ - int index = 0; - for(BuilderSelector bs : builderSelectors){ - if(bs.getName().equals(builderSelectorName)){ - break; - } - index++; - } - if(index >= builderSelectors.size()){ - throw new RuntimeException( - String.format("BuilderSelector named %s does not exist",builderSelectorName)); - } - return index; - } - - - public TemplateBuilder select(Type target){ - for(BuilderSelector selector : builderSelectors){ - if(selector.matchType(target)){ - return selector.getTemplateBuilder(target); - } - } - return null; - } - - public TemplateBuilder getForceBuilder() { - return forceBuilder; - } - - - public void setForceBuilder(TemplateBuilder forceBuilder) { - this.forceBuilder = forceBuilder; - } - - - - -} diff --git a/java/src/main/java/org/msgpack/template/builder/CustomTemplateBuilder.java b/java/src/main/java/org/msgpack/template/builder/CustomTemplateBuilder.java deleted file mode 100644 index 8d97689f..00000000 --- a/java/src/main/java/org/msgpack/template/builder/CustomTemplateBuilder.java +++ /dev/null @@ -1,77 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Type; - -import org.msgpack.Template; -import org.msgpack.template.FieldList; -import org.msgpack.template.FieldOption; -import org.msgpack.template.IFieldEntry; -import org.msgpack.template.IFieldEntryReader; -import org.msgpack.template.TemplateBuildException; - -public abstract class CustomTemplateBuilder implements TemplateBuilder { - - public abstract IFieldEntryReader getFieldEntryReader(); - - public abstract Template buildTemplate(Class targetClass , IFieldEntry[] entries); - - public Template buildTemplate(Class targetClass, FieldOption implicitOption ){ - checkValidation(targetClass); - return buildTemplate(targetClass, - getFieldEntryReader().readFieldEntries(targetClass, implicitOption)); - } - - public Template buildTemplate(Class targetClass, FieldList flist) throws NoSuchFieldException { - checkValidation(targetClass); - return buildTemplate(targetClass, getFieldEntryReader().convertFieldEntries(targetClass, flist)); - } - - @Override - public Template buildTemplate(Type targetType) { - Class targetClass = (Class)targetType; - IFieldEntryReader reader = getFieldEntryReader(); - FieldOption implicitOption = reader.readImplicitFieldOption(targetClass); - checkValidation(targetClass); - IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption); - return buildTemplate(targetClass, entries); - } - - protected void checkValidation(Class targetClass) { - if(targetClass.isInterface()) { - throw new TemplateBuildException("Cannot build template of interface"); - } - if(targetClass.isArray()) { - throw new TemplateBuildException("Cannot build template of array class"); - } - if(targetClass.isPrimitive()) { - throw new TemplateBuildException("Cannot build template of primitive type"); - } - } - - @Override - public void writeTemplate(Type targetType, String directoryName) { - throw new UnsupportedOperationException(targetType.toString()); - } - - @Override - public Template loadTemplate(Type targetType) { - return null; - } -} \ No newline at end of file diff --git a/java/src/main/java/org/msgpack/template/builder/EnumTemplateBuilderSelector.java b/java/src/main/java/org/msgpack/template/builder/EnumTemplateBuilderSelector.java deleted file mode 100644 index 816a2faf..00000000 --- a/java/src/main/java/org/msgpack/template/builder/EnumTemplateBuilderSelector.java +++ /dev/null @@ -1,42 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Type; - -public class EnumTemplateBuilderSelector implements BuilderSelector { - - public static final String NAME = "EnumTemplateBuilder"; - - OrdinalEnumTemplateBuilder builder = new OrdinalEnumTemplateBuilder(); - - @Override - public String getName(){ - return NAME; - } - - @Override - public boolean matchType(Type targetType) { - return ((Class)targetType).isEnum(); - } - - @Override - public TemplateBuilder getTemplateBuilder(Type targetType) { - return builder; - } -} diff --git a/java/src/main/java/org/msgpack/template/builder/JavassistTemplateBuilder.java b/java/src/main/java/org/msgpack/template/builder/JavassistTemplateBuilder.java deleted file mode 100644 index ae50f257..00000000 --- a/java/src/main/java/org/msgpack/template/builder/JavassistTemplateBuilder.java +++ /dev/null @@ -1,207 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.Thread; -import java.lang.reflect.Type; - -import org.msgpack.*; - -import javassist.ClassPool; -import javassist.CtClass; -import javassist.LoaderClassPath; -import javassist.NotFoundException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.msgpack.template.FieldEntryReader; -import org.msgpack.template.FieldOption; -import org.msgpack.template.IFieldEntry; -import org.msgpack.template.IFieldEntryReader; -import org.msgpack.template.TemplateRegistry; - -public class JavassistTemplateBuilder extends CustomTemplateBuilder { - public static abstract class JavassistTemplate extends AbstractTemplate { - public Class targetClass; - public Template[] templates; - - public JavassistTemplate(Class targetClass, Template[] templates) { - this.targetClass = targetClass; - this.templates = templates; - } - } - - private static Logger LOG = LoggerFactory.getLogger(JavassistTemplateBuilder.class); - - private static JavassistTemplateBuilder instance; - - public synchronized static JavassistTemplateBuilder getInstance() { - if(instance == null) { - instance = new JavassistTemplateBuilder(); - } - return instance; - } - - public static void addClassLoader(ClassLoader cl) { - getInstance().pool.appendClassPath(new LoaderClassPath(cl)); - } - - IFieldEntryReader reader = new FieldEntryReader(); - - public void setFieldEntryReader(IFieldEntryReader reader){ - this.reader = reader; - } - - BuildContextFactory buildContextFactory = new BuildContextFactory() { - @Override - public BuildContextBase createBuildContext(JavassistTemplateBuilder builder) { - - return new BuildContext(builder); - } - }; - - public void setBuildContextFactory(BuildContextFactory factory){ - this.buildContextFactory = factory; - } - - public JavassistTemplateBuilder() { - pool = new ClassPool(); - boolean appended = false; - ClassLoader cl = null; - try { - cl = Thread.currentThread().getContextClassLoader(); - if (cl != null) { - pool.appendClassPath(new LoaderClassPath(cl)); - appended = true; - } - } catch (SecurityException e) { - LOG.debug("Cannot append a search path of context classloader", e); - } - try { - ClassLoader cl2 = getClass().getClassLoader(); - if (cl2 != null && cl2 != cl) { - pool.appendClassPath(new LoaderClassPath(cl2)); - appended = true; - } - } catch (SecurityException e) { - LOG.debug("Cannot append a search path of classloader", e); - } - if (!appended) { - pool.appendSystemPath(); - } - } - - /** - * Replace FieldEntryReader and BuilderContextFactory. - * you can replace field entry rules and generated codes easily. - * @param reader - * @param buildContextFactory - */ - public JavassistTemplateBuilder(IFieldEntryReader reader, BuildContextFactory buildContextFactory ){ - this(); - this.reader = reader; - this.buildContextFactory = buildContextFactory; - } - - protected ClassPool pool; - - private int seqId = 0; - - public CtClass makeCtClass(String className) { - return pool.makeClass(className); - } - - public CtClass getCtClass(String className) throws NotFoundException { - return pool.get(className); - } - - public int nextSeqId() { - return seqId++; - } - - @Override - public Template buildTemplate(Class targetClass, IFieldEntry[] entries) { - // FIXME private / packagefields - //for(FieldEntry e : entries) { - // Field f = e.getField(); - // int mod = f.getModifiers(); - // if(!Modifier.isPublic(mod)) { - // f.setAccessible(true); - // } - //} - Template[] tmpls = toTemplate(entries); - BuildContextBase bc = getBuildContextFacotry().createBuildContext(this); - return bc.buildTemplate(targetClass, entries, tmpls); - } - - private static Template[] toTemplate(IFieldEntry[] from) { - Template[] tmpls = new Template[from.length]; - for(int i=0; i < from.length; i++) { - IFieldEntry e = from[i]; - if(!e.isAvailable()) { - tmpls[i] = null; - } else { - Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true); - tmpls[i] = tmpl; - } - } - return tmpls; - } - - @Override - public IFieldEntryReader getFieldEntryReader() { - return reader; - } - - public BuildContextFactory getBuildContextFacotry() { - return buildContextFactory; - } - - @Override - public void writeTemplate(Type targetType, String directoryName) { - Class targetClass = (Class)targetType; - IFieldEntryReader reader = getFieldEntryReader(); - FieldOption implicitOption = reader.readImplicitFieldOption(targetClass); - checkValidation(targetClass); - IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption); - writeTemplate(targetClass, entries, directoryName); - } - - private void writeTemplate(Class targetClass, IFieldEntry[] entries, String directoryName) { - Template[] tmpls = toTemplate(entries); - BuildContextBase bc = getBuildContextFacotry().createBuildContext(this); - bc.writeTemplate(targetClass, entries, tmpls, directoryName); - } - - @Override - public Template loadTemplate(Type targetType) { - Class targetClass = (Class)targetType; - IFieldEntryReader reader = getFieldEntryReader(); - FieldOption implicitOption = reader.readImplicitFieldOption(targetClass); - checkValidation(targetClass); - IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption); - return loadTemplate(targetClass, entries); - } - - private Template loadTemplate(Class targetClass, IFieldEntry[] entries) { - Template[] tmpls = toTemplate(entries); - BuildContextBase bc = getBuildContextFacotry().createBuildContext(this); - return bc.loadTemplate(targetClass, entries, tmpls); - } -} - diff --git a/java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilder.java b/java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilder.java deleted file mode 100644 index 61f29838..00000000 --- a/java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilder.java +++ /dev/null @@ -1,96 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.io.IOException; -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; - -import org.msgpack.AbstractTemplate; -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.template.TemplateBuildException; - -public class OrdinalEnumTemplateBuilder implements TemplateBuilder{ - - static class ReflectionOrdinalEnumTemplate extends AbstractTemplate { - protected Enum[] entries; - protected Map, Integer> reverse; - - ReflectionOrdinalEnumTemplate(Enum[] entries) { - this.entries = entries; - this.reverse = new HashMap, Integer>(); - for(int i=0; i < entries.length; i++) { - this.reverse.put(entries[i], i); - } - } - - public void pack(Packer pk, Object target) throws IOException { - Integer ord = reverse.get(target); - if(ord == null) { - throw new MessageTypeException(); - } - pk.pack((int)ord); - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - int ord = pac.unpackInt(); - if(entries.length <= ord) { - throw new MessageTypeException(); - } - return entries[ord]; - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - int ord = from.asInt(); - if(entries.length <= ord) { - throw new MessageTypeException(); - } - return entries[ord]; - } - } - - @Override - public Template buildTemplate(Type targetType) { - Class targetClass = (Class)targetType; - checkOrdinalEnumValidation(targetClass); - Enum[] entries = (Enum[])targetClass.getEnumConstants(); - - return new ReflectionOrdinalEnumTemplate(entries); - } - - private void checkOrdinalEnumValidation(Class targetClass) { - if(!targetClass.isEnum()) { - throw new TemplateBuildException("tried to build ordinal enum template of non-enum class"); - } - } - - @Override - public void writeTemplate(Type targetType, String directoryName) { - throw new UnsupportedOperationException(targetType.toString()); - } - - @Override - public Template loadTemplate(Type targetType) { - return null; - } -} diff --git a/java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilderSelector.java b/java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilderSelector.java deleted file mode 100644 index 38ff7c4f..00000000 --- a/java/src/main/java/org/msgpack/template/builder/OrdinalEnumTemplateBuilderSelector.java +++ /dev/null @@ -1,45 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Type; - -import org.msgpack.annotation.MessagePackOrdinalEnum; - -public class OrdinalEnumTemplateBuilderSelector implements BuilderSelector { - - public static final String NAME = "OrdinalEnumTemplateBuilder"; - - OrdinalEnumTemplateBuilder builder = new OrdinalEnumTemplateBuilder(); - - @Override - public String getName(){ - return NAME; - } - - @Override - public boolean matchType(Type targetType) { - Class targetClass = (Class)targetType; - return AnnotationTemplateBuilderSelector.isAnnotated(targetClass, MessagePackOrdinalEnum.class); - } - - @Override - public TemplateBuilder getTemplateBuilder(Type targetType) { - return builder; - } -} diff --git a/java/src/main/java/org/msgpack/template/builder/ReflectionTemplateBuilder.java b/java/src/main/java/org/msgpack/template/builder/ReflectionTemplateBuilder.java deleted file mode 100644 index 6d875015..00000000 --- a/java/src/main/java/org/msgpack/template/builder/ReflectionTemplateBuilder.java +++ /dev/null @@ -1,423 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; - -import org.msgpack.AbstractTemplate; -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.template.FieldEntry; -import org.msgpack.template.FieldEntryReader; -import org.msgpack.template.IFieldEntry; -import org.msgpack.template.IFieldEntryReader; -import org.msgpack.template.TemplateRegistry; - -public class ReflectionTemplateBuilder extends CustomTemplateBuilder { - IFieldEntryReader reader = new FieldEntryReader(); - - @Override - public IFieldEntryReader getFieldEntryReader(){ - return reader; - } - - public ReflectionTemplateBuilder() { - } - - static abstract class ReflectionFieldEntry extends FieldEntry { - ReflectionFieldEntry(FieldEntry e) { - super(e.getField(), e.getOption()); - } - - public abstract void pack(Object target, Packer pac) throws IOException; - - public abstract void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException; - - public abstract void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException; - - public void setNull(Object target) throws IllegalAccessException { - getField().set(target, null); - } - } - - static class NullFieldEntry extends ReflectionFieldEntry { - NullFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { } - } - - static class ObjectFieldEntry extends ReflectionFieldEntry { - private Template template; - - ObjectFieldEntry(FieldEntry e, Template template) { - super(e); - this.template = template; - } - - public void pack(Object target, Packer pac) throws IOException { - template.pack(pac, target); - } - - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - Field f = getField(); - Class type = (Class)f.getType(); - Object fieldReference = f.get(target); - Object valueReference = template.convert(obj, fieldReference); - if(valueReference != fieldReference) { - f.set(target, valueReference); - } - } - - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - Field f = getField(); - Class type = (Class)f.getType(); - Object fieldReference = f.get(target); - Object valueReference = template.unpack(pac, fieldReference); - if(valueReference != fieldReference) { - f.set(target, valueReference); - } - } - } - - static class BooleanFieldEntry extends ReflectionFieldEntry { - BooleanFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { - pac.pack((boolean)(Boolean)target); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - getField().setBoolean(target, obj.asBoolean()); - } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - getField().setBoolean(target, pac.unpackBoolean()); - } - } - - static class ByteFieldEntry extends ReflectionFieldEntry { - ByteFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { - pac.pack((byte)(Byte)target); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - getField().setByte(target, obj.asByte()); - } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - getField().setByte(target, pac.unpackByte()); - } - } - - static class ShortFieldEntry extends ReflectionFieldEntry { - ShortFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { - pac.pack((short)(Short)target); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - getField().setShort(target, obj.asShort()); - } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - getField().setShort(target, pac.unpackShort()); - } - } - - static class IntFieldEntry extends ReflectionFieldEntry { - IntFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { - pac.pack((int)(Integer)target); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - getField().setInt(target, obj.asInt()); - } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - getField().setInt(target, pac.unpackInt()); - } - } - - static class LongFieldEntry extends ReflectionFieldEntry { - LongFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { - pac.pack((long)(Long)target); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - getField().setLong(target, obj.asLong()); - } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - getField().setLong(target, pac.unpackLong()); - } - } - - static class FloatFieldEntry extends ReflectionFieldEntry { - FloatFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { - pac.pack((float)(Float)target); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - getField().setFloat(target, obj.asFloat()); - } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - getField().setFloat(target, pac.unpackFloat()); - } - } - - static class DoubleFieldEntry extends ReflectionFieldEntry { - DoubleFieldEntry(FieldEntry e) { - super(e); - } - public void pack(Object target, Packer pac) throws IOException { - pac.pack((double)(Double)target); - } - public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { - getField().setDouble(target, obj.asDouble()); - } - public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { - getField().setDouble(target, pac.unpackDouble()); - } - } - - static class ReflectionTemplate extends AbstractTemplate { - protected Class targetClass; - protected ReflectionFieldEntry[] entries; - protected int minimumArrayLength; - - ReflectionTemplate(Class targetClass, ReflectionFieldEntry[] entries) { - this.targetClass = targetClass; - this.entries = entries; - this.minimumArrayLength = 0; - for(int i=0; i < entries.length; i++) { - ReflectionFieldEntry e = entries[i]; - if(e.isRequired() || e.isNullable()) { - this.minimumArrayLength = i+1; - } - } - } - - public void pack(Packer pk, Object target) throws IOException { - try { - pk.packArray(entries.length); - for(ReflectionFieldEntry e : entries) { - if(!e.isAvailable()) { - pk.packNil(); - continue; - } - Object obj = e.getField().get(target); - if(obj == null) { - if(!e.isNullable() && !e.isOptional()) { - throw new MessageTypeException(); - } - pk.packNil(); - } else { - e.pack(obj, pk); - } - } - - } catch (MessageTypeException e) { - throw e; - } catch (IOException e) { - throw e; - } catch (Exception e) { - throw new MessageTypeException(e); - } - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - try { - if(to == null) { - to = targetClass.newInstance(); - } - - int length = pac.unpackArray(); - if(length < minimumArrayLength) { - throw new MessageTypeException(); - } - - int i; - for(i=0; i < minimumArrayLength; i++) { - ReflectionFieldEntry e = entries[i]; - if(!e.isAvailable()) { - pac.unpackObject(); - continue; - } - - if(pac.tryUnpackNull()) { - if(e.isRequired()) { - // Required + nil => exception - throw new MessageTypeException(); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - e.setNull(to); - } - } else { - e.unpack(to, pac); - } - } - - int max = length < entries.length ? length : entries.length; - for(; i < max; i++) { - ReflectionFieldEntry e = entries[i]; - if(!e.isAvailable()) { - pac.unpackObject(); - continue; - } - - if(pac.tryUnpackNull()) { - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - } else { - e.unpack(to, pac); - } - } - - // latter entries are all Optional + nil => keep default value - - for(; i < length; i++) { - pac.unpackObject(); - } - - return to; - - } catch (MessageTypeException e) { - throw e; - } catch (IOException e) { - throw e; - } catch (Exception e) { - throw new MessageTypeException(e); - } - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - try { - if(to == null) { - to = targetClass.newInstance(); - } - - MessagePackObject[] array = from.asArray(); - int length = array.length; - if(length < minimumArrayLength) { - throw new MessageTypeException(); - } - - int i; - for(i=0; i < minimumArrayLength; i++) { - ReflectionFieldEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - MessagePackObject obj = array[i]; - if(obj.isNil()) { - if(e.isRequired()) { - // Required + nil => exception - throw new MessageTypeException(); - } else if(e.isOptional()) { - // Optional + nil => keep default value - } else { // Nullable - // Nullable + nil => set null - e.setNull(to); - } - } else { - e.convert(to, obj); - } - } - - int max = length < entries.length ? length : entries.length; - for(; i < max; i++) { - ReflectionFieldEntry e = entries[i]; - if(!e.isAvailable()) { - continue; - } - - MessagePackObject obj = array[i]; - if(obj.isNil()) { - // this is Optional field becaue i >= minimumArrayLength - // Optional + nil => keep default value - } else { - e.convert(to, obj); - } - } - - // latter entries are all Optional + nil => keep default value - - return to; - - } catch (MessageTypeException e) { - throw e; - } catch (Exception e) { - throw new MessageTypeException(e); - } - } - } - - public Template buildTemplate(Class targetClass, IFieldEntry[] entries) { - // TODO Now it is simply cast. - for(IFieldEntry e : entries) { - Field f = ((FieldEntry)e).getField(); - int mod = f.getModifiers(); - if(!Modifier.isPublic(mod)) { - f.setAccessible(true); - } - } - - ReflectionFieldEntry[] res = new ReflectionFieldEntry[entries.length]; - for(int i=0; i < entries.length; i++) { - FieldEntry e = (FieldEntry)entries[i]; - Class type = e.getType(); - if(!e.isAvailable()) { - res[i] = new NullFieldEntry(e); - } else if(type.equals(boolean.class)) { - res[i] = new BooleanFieldEntry(e); - } else if(type.equals(byte.class)) { - res[i] = new ByteFieldEntry(e); - } else if(type.equals(short.class)) { - res[i] = new ShortFieldEntry(e); - } else if(type.equals(int.class)) { - res[i] = new IntFieldEntry(e); - } else if(type.equals(long.class)) { - res[i] = new LongFieldEntry(e); - } else if(type.equals(float.class)) { - res[i] = new FloatFieldEntry(e); - } else if(type.equals(double.class)) { - res[i] = new DoubleFieldEntry(e); - } else { - Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true); - res[i] = new ObjectFieldEntry(e, tmpl); - } - } - return new ReflectionTemplate(targetClass, res); - } -} - diff --git a/java/src/main/java/org/msgpack/template/builder/TemplateBuilder.java b/java/src/main/java/org/msgpack/template/builder/TemplateBuilder.java deleted file mode 100644 index 0846ac41..00000000 --- a/java/src/main/java/org/msgpack/template/builder/TemplateBuilder.java +++ /dev/null @@ -1,31 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.template.builder; - -import java.lang.reflect.Type; - -import org.msgpack.Template; - -public interface TemplateBuilder { - Template buildTemplate(Type targetType); - - void writeTemplate(Type targetType, String directoryName); - - Template loadTemplate(Type targetType); -} - diff --git a/java/src/main/java/org/msgpack/type/Raw.java b/java/src/main/java/org/msgpack/type/Raw.java deleted file mode 100644 index cdd0c220..00000000 --- a/java/src/main/java/org/msgpack/type/Raw.java +++ /dev/null @@ -1,69 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.type; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.nio.ByteBuffer; -import org.msgpack.*; - -public final class Raw { - private byte[] bytes; - private String string; - - public Raw(byte[] bytes) { - this.bytes = bytes; - this.string = null; - } - - public Raw(String string) { - this.bytes = null; - this.string = string; - } - - public String toString() { - if(string == null) { - try { - string = new String(bytes, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - return string; - } - - public byte[] toByteArray() { - if(bytes == null) { - try { - bytes = string.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - return bytes; - } - - public ByteBuffer toByteBuffer() { - return ByteBuffer.wrap(toByteArray()); - } - - static { - RawTemplate.load(); - } -} - diff --git a/java/src/main/java/org/msgpack/type/RawTemplate.java b/java/src/main/java/org/msgpack/type/RawTemplate.java deleted file mode 100644 index 85fb5e46..00000000 --- a/java/src/main/java/org/msgpack/type/RawTemplate.java +++ /dev/null @@ -1,51 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.type; - -import java.io.IOException; -import org.msgpack.*; -import org.msgpack.template.TemplateRegistry; - -public class RawTemplate implements Template { - static void load() { } - - private RawTemplate() { } - - public void pack(Packer pk, Object target) throws IOException { - pk.packByteArray(((Raw)target).toByteArray()); - } - - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - return new Raw(pac.unpackByteArray()); - } - - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { - return new Raw(from.asByteArray()); - } - - static public RawTemplate getInstance() { - return instance; - } - - static final RawTemplate instance = new RawTemplate(); - - static { - TemplateRegistry.register(Raw.class, instance); - } -} - diff --git a/java/src/main/java/org/msgpack/util/TemplatePrecompiler.java b/java/src/main/java/org/msgpack/util/TemplatePrecompiler.java deleted file mode 100644 index 36414f5c..00000000 --- a/java/src/main/java/org/msgpack/util/TemplatePrecompiler.java +++ /dev/null @@ -1,133 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2011 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.util; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.tools.DiagnosticCollector; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileManager; -import javax.tools.JavaFileObject; -import javax.tools.StandardLocation; -import javax.tools.ToolProvider; - -import org.msgpack.template.builder.JavassistTemplateBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This class is a template precompiler, which is used for saving templates - * that TemplateBuilder generated. It saves templates as .class files. - * Application enables to load the .class files and use templates. - * - */ -public class TemplatePrecompiler { - - private static final Logger LOG = LoggerFactory.getLogger(TemplatePrecompiler.class); - - public static final String DEST = "msgpack.template.destdir"; - - public static final String DEFAULT_DEST = "."; - - public static void saveTemplates(final String[] classNames) throws IOException, ClassNotFoundException { - List ret = new ArrayList(); - for (String className : classNames) { - matchClassNames(ret, className); - } - List> ret0 = toClass(ret); - for (Class c : ret0) { - saveTemplateClass(c); - } - } - - @SuppressWarnings("serial") - private static void matchClassNames(List ret, final String className) throws IOException { - String packageName = className.substring(0, className.lastIndexOf('.')); - String relativedName = className.substring(className.lastIndexOf('.') + 1, className.length()); - String patName = relativedName.replace("*", "(\\w+)"); - Pattern pat = Pattern.compile(patName); - - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - JavaFileManager fm = compiler.getStandardFileManager( - new DiagnosticCollector(), null, null); - HashSet kind = new HashSet(){{ - add(JavaFileObject.Kind.CLASS); - }}; - - for (JavaFileObject f : fm.list(StandardLocation.PLATFORM_CLASS_PATH, packageName, kind, false)) { - String relatived0 = f.getName(); - String name0 = relatived0.substring(0, relatived0.length() - ".class".length()); - Matcher m = pat.matcher(name0); - if (m.matches()) { - String name = packageName + '.' + name0; - if (!ret.contains(name)) { - ret.add(name); - } - } - } - } - - private static List> toClass(List classNames) throws ClassNotFoundException { - List> ret = new ArrayList>(classNames.size()); - ClassLoader cl = TemplatePrecompiler.class.getClassLoader(); - for (String className : classNames) { - Class c = cl.loadClass(className); - ret.add(c); - } - return ret; - } - - public static void saveTemplateClasses(Class[] targetClasses) throws IOException { - for (Class c : targetClasses) { - saveTemplateClass(c); - } - } - - public static void saveTemplateClass(Class targetClass) throws IOException { - LOG.info("Saving template of " + targetClass.getName() + "..."); - Properties props = System.getProperties(); - String distDirName = getDirName(props, DEST, DEFAULT_DEST); - if (targetClass.isEnum()) { - throw new UnsupportedOperationException("Not supported enum type yet: " + targetClass.getName()); - } else { - new JavassistTemplateBuilder().writeTemplate(targetClass, distDirName); - } - LOG.info("Saved .class file of template class of " + targetClass.getName()); - } - - private static String getDirName(Properties props, String dirName, String defaultDirName) - throws IOException { - String dName = props.getProperty(dirName, defaultDirName); - File d = new File(dName); - if (!d.isDirectory() && !d.exists()) { - throw new IOException("Directory not exists: " + dName); - } - return d.getAbsolutePath(); - } - - public static void main(final String[] args) throws Exception { - TemplatePrecompiler.saveTemplates(args); - } -} diff --git a/java/src/main/resources/log4j.properties b/java/src/main/resources/log4j.properties deleted file mode 100644 index 6fae205c..00000000 --- a/java/src/main/resources/log4j.properties +++ /dev/null @@ -1,14 +0,0 @@ -### direct log messages to stdout ### -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target=System.out -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1} - %m%n - -### direct messages to file mylog.log ### -log4j.appender.file=org.apache.log4j.FileAppender -log4j.appender.file.File=mylog.log -log4j.appender.file.Append=true -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d %5p %c{1} - %m%n - -log4j.rootLogger=info, stdout diff --git a/java/src/test/java/org/msgpack/Image.java b/java/src/test/java/org/msgpack/Image.java deleted file mode 100644 index 3fcfe388..00000000 --- a/java/src/test/java/org/msgpack/Image.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import java.io.*; -import java.util.*; - -public class Image implements MessagePackable, MessageUnpackable { - public String uri = ""; - public String title = ""; - public int width = 0; - public int height = 0; - public int size = 0; - - public void messagePack(Packer pk) throws IOException { - pk.packArray(5); - pk.pack(uri); - pk.pack(title); - pk.pack(width); - pk.pack(height); - pk.pack(size); - } - - public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException { - int length = pac.unpackArray(); - if(length != 5) { - throw new MessageTypeException(); - } - uri = pac.unpackString(); - title = pac.unpackString(); - width = pac.unpackInt(); - height = pac.unpackInt(); - size = pac.unpackInt(); - } - - public boolean equals(Image obj) { - return uri.equals(obj.uri) && - title.equals(obj.title) && - width == obj.width && - height == obj.height && - size == obj.size; - } - - public boolean equals(Object obj) { - if(obj.getClass() != Image.class) { - return false; - } - return equals((Image)obj); - } -} - diff --git a/java/src/test/java/org/msgpack/TestAnnotations.java b/java/src/test/java/org/msgpack/TestAnnotations.java deleted file mode 100644 index 657ca28f..00000000 --- a/java/src/test/java/org/msgpack/TestAnnotations.java +++ /dev/null @@ -1,150 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import org.msgpack.object.*; -import org.msgpack.annotation.*; -import static org.msgpack.Templates.*; - -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import junit.framework.TestCase; - -public class TestAnnotations extends TestCase { - - @MessagePackMessage - public static class MyClassVersion1 { - // required field, not nullable. - public String name; - - // required and nullable field. - @Nullable - public String nickname; - } - - - @MessagePackMessage - public static class MyClassVersion2 { - public String name; - - @Nullable - public String nickname; - - // adds an optional field on version 2. - @Optional - public int age = -1; - } - - - @MessagePackMessage - public static class MyClassVersion3 { - public String name; - - @Nullable - public String nickname; - - // adds required fields on version 3, then - // this class is NOT compatible with version 1. - public int age; - - // optional field is nullable. - @Optional - public String school; - } - - - @Test - public void testBackwardCompatibility() throws Exception { - MyClassVersion1 v1 = new MyClassVersion1(); - v1.name = "Sadayuki Furuhashi"; - v1.nickname = "frsyuki"; - - byte[] bytes = MessagePack.pack(v1); - - MyClassVersion2 v2 = MessagePack.unpack(bytes, MyClassVersion2.class); - - assertEquals(v1.name, v2.name); - assertEquals(v1.nickname, v2.nickname); - assertEquals(v2.age, -1); - } - - @Test - public void testForwardCompatibility() throws Exception { - MyClassVersion2 v2 = new MyClassVersion2(); - v2.name = "Sadayuki Furuhashi"; - v2.nickname = "frsyuki"; - v2.age = 23; - - byte[] bytes = MessagePack.pack(v2); - - MyClassVersion1 v1 = MessagePack.unpack(bytes, MyClassVersion1.class); - - assertEquals(v2.name, v1.name); - assertEquals(v2.nickname, v1.nickname); - } - - @Test - public void testNullFields01() throws Exception { - MyClassVersion1 src = new MyClassVersion1(); - src.name = "Sadayuki Furuhashi"; - src.nickname = null; - - byte[] bytes = MessagePack.pack(src); - - MyClassVersion1 dst = MessagePack.unpack(bytes, MyClassVersion1.class); - - assertEquals(dst.name, src.name); - assertEquals(dst.nickname, src.nickname); - } - - @Test - public void testNullFields02() throws Exception { - MyClassVersion1 src = new MyClassVersion1(); - src.name = null; - src.nickname = "frsyuki"; - - try { - byte[] bytes = MessagePack.pack(src); - } catch (Exception e) { - assertTrue(true); - return; - } - assertTrue(false); - } - - @Test - public void testNullFields03() throws Exception { - List src = new ArrayList(); - src.add(null); - src.add("frsyuki"); - - byte[] bytes = MessagePack.pack(src); - - try { - MyClassVersion1 dst = MessagePack.unpack(bytes, MyClassVersion1.class); - } catch (Exception e) { - assertTrue(true); - return; - } - assertTrue(false); - } - - @Test - public void testNullFields04() throws Exception { - MyClassVersion3 src = new MyClassVersion3(); - src.name = "Sadayuki Furuhashi"; - src.nickname = null; - src.age = 23; - src.school = null; - - byte[] bytes = MessagePack.pack(src); - - MyClassVersion3 dst = MessagePack.unpack(bytes, MyClassVersion3.class); - - assertEquals(dst.name, src.name); - assertEquals(dst.nickname, src.nickname); - } -} - diff --git a/java/src/test/java/org/msgpack/TestArrays.java b/java/src/test/java/org/msgpack/TestArrays.java deleted file mode 100644 index 40fa5d69..00000000 --- a/java/src/test/java/org/msgpack/TestArrays.java +++ /dev/null @@ -1,359 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import org.msgpack.object.*; -import org.msgpack.annotation.*; -import static org.msgpack.Templates.*; - -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import junit.framework.TestCase; - -public class TestArrays extends TestCase { - - - - - @MessagePackMessage - public static class PrimitiveTest { - public PrimitiveTest() { } - public boolean[] b = new boolean[0]; - public short[] s = new short[0]; - public int[] i = new int[0]; - //public long[] l = new long[0]; // FIXME javassist? - public float[] f = new float[0]; - //public double[] d = new double[0]; // FIXME javassist? - } - - @Test - public void testPrimitive() { - PrimitiveTest t = new PrimitiveTest(); - t.b = new boolean[] {true, false}; - t.s = new short[] {0, 1}; - t.i = new int[] {2, 3}; - //t.l = new long[] {4, 5}; - t.f = new float[] {2.0f, 4.0f}; - //t.d = new double[] {8.0, 16.0}; - - byte[] raw = MessagePack.pack(t); - - PrimitiveTest u = MessagePack.unpack(raw, PrimitiveTest.class); - assertEquals(t.b.length, u.b.length); - for(int i=0; i < t.b.length; i++) { assertEquals(t.b[i], u.b[i]); } - assertEquals(t.s.length, u.s.length); - for(int i=0; i < t.s.length; i++) { assertEquals(t.s[i], u.s[i]); } - assertEquals(t.i.length, u.i.length); - for(int i=0; i < t.i.length; i++) { assertEquals(t.i[i], u.i[i]); } - //assertEquals(t.l.length, u.l.length); - //for(int i=0; i < t.l.length; i++) { assertEquals(t.l[i], u.l[i]); } - assertEquals(t.f.length, u.f.length); - for(int i=0; i < t.f.length; i++) { assertEquals(t.f[i], u.f[i]); } - //assertEquals(t.d.length, u.d.length); - //for(int i=0; i < t.d.length; i++) { assertEquals(t.d[i], u.d[i]); } - - PrimitiveTest c = MessagePack.unpack(raw).convert(PrimitiveTest.class); - assertEquals(t.b.length, c.b.length); - for(int i=0; i < t.b.length; i++) { assertEquals(t.b[i], c.b[i]); } - assertEquals(t.s.length, c.s.length); - for(int i=0; i < t.s.length; i++) { assertEquals(t.s[i], c.s[i]); } - assertEquals(t.i.length, c.i.length); - for(int i=0; i < t.i.length; i++) { assertEquals(t.i[i], c.i[i]); } - //assertEquals(t.l.length, c.l.length); - //for(int i=0; i < t.l.length; i++) { assertEquals(t.l[i], c.l[i]); } - assertEquals(t.f.length, c.f.length); - for(int i=0; i < t.f.length; i++) { assertEquals(t.f[i], c.f[i]); } - //assertEquals(t.d.length, c.d.length); - //for(int i=0; i < t.d.length; i++) { assertEquals(t.d[i], c.d[i]); } - } - - @MessagePackMessage - public static class ReferenceTest { - public ReferenceTest() { } - public Boolean[] b; - public Short[] s; - public Integer[] i; - public Long[] l; - public Float[] f; - public Double[] d; - public String[] str; - } - - @Test - public void testReference() { - ReferenceTest t = new ReferenceTest(); - t.b = new Boolean[] {true, false}; - t.s = new Short[] {0, 1}; - t.i = new Integer[] {2, 3}; - t.l = new Long[] {4l, 5l}; - t.f = new Float[] {2.0f, 4.0f}; - t.d = new Double[] {8.0, 16.0}; - t.str = new String[] {"furuhashi", "java"}; - - byte[] raw = MessagePack.pack(t); - - ReferenceTest u = MessagePack.unpack(raw, ReferenceTest.class); - assertEquals(t.b.length, u.b.length); - for(int i=0; i < t.b.length; i++) { assertEquals(t.b[i], u.b[i]); } - assertEquals(t.s.length, u.s.length); - for(int i=0; i < t.s.length; i++) { assertEquals(t.s[i], u.s[i]); } - assertEquals(t.i.length, u.i.length); - for(int i=0; i < t.i.length; i++) { assertEquals(t.i[i], u.i[i]); } - assertEquals(t.l.length, u.l.length); - for(int i=0; i < t.l.length; i++) { assertEquals(t.l[i], u.l[i]); } - assertEquals(t.f.length, u.f.length); - for(int i=0; i < t.f.length; i++) { assertEquals(t.f[i], u.f[i]); } - assertEquals(t.d.length, u.d.length); - for(int i=0; i < t.d.length; i++) { assertEquals(t.d[i], u.d[i]); } - assertEquals(t.str.length, u.str.length); - for(int i=0; i < t.str.length; i++) { assertEquals(t.str[i], u.str[i]); } - - ReferenceTest c = MessagePack.unpack(raw).convert(ReferenceTest.class); - assertEquals(t.b.length, c.b.length); - for(int i=0; i < t.b.length; i++) { assertEquals(t.b[i], c.b[i]); } - assertEquals(t.s.length, c.s.length); - for(int i=0; i < t.s.length; i++) { assertEquals(t.s[i], c.s[i]); } - assertEquals(t.i.length, c.i.length); - for(int i=0; i < t.i.length; i++) { assertEquals(t.i[i], c.i[i]); } - assertEquals(t.l.length, c.l.length); - for(int i=0; i < t.l.length; i++) { assertEquals(t.l[i], c.l[i]); } - assertEquals(t.f.length, c.f.length); - for(int i=0; i < t.f.length; i++) { assertEquals(t.f[i], c.f[i]); } - assertEquals(t.d.length, c.d.length); - for(int i=0; i < t.d.length; i++) { assertEquals(t.d[i], c.d[i]); } - assertEquals(t.str.length, c.str.length); - for(int i=0; i < t.str.length; i++) { assertEquals(t.str[i], c.str[i]); } - } - - @MessagePackMessage - public static class GenericsTest { - public GenericsTest() { } - public List[] slist; - public Map[] imap; - } - - @Test - public void testGenerics() { - GenericsTest t = new GenericsTest(); - t.slist = new List[2]; - t.slist[0] = new ArrayList(); - t.slist[0].add("aa"); - t.slist[0].add("bb"); - t.slist[1] = new ArrayList(); - t.slist[1].add("cc"); - t.imap = new Map[2]; - t.imap[0] = new HashMap(); - t.imap[0].put("aa", 1); - t.imap[0].put("bb", 2); - t.imap[1] = new HashMap(); - t.imap[1].put("cc", 3); - - byte[] raw = MessagePack.pack(t); - - GenericsTest u = MessagePack.unpack(raw, GenericsTest.class); - assertEquals(t.slist.length, u.slist.length); - for(int i=0; i < t.slist.length; i++) { - assertEquals(t.slist[i].size(), u.slist[i].size()); - for(int j=0; j < t.slist[i].size(); j++) { - assertEquals(t.slist[i].get(j), u.slist[i].get(j)); - } - } - for(int i=0; i < t.imap.length; i++) { - assertEquals(t.imap[i].size(), u.imap[i].size()); - for(String j : t.imap[i].keySet()) { - assertEquals(t.imap[i].get(j), u.imap[i].get(j)); - } - } - - GenericsTest c = MessagePack.unpack(raw).convert(GenericsTest.class); - assertEquals(t.slist.length, c.slist.length); - for(int i=0; i < t.slist.length; i++) { - assertEquals(t.slist[i].size(), c.slist[i].size()); - for(int j=0; j < t.slist[i].size(); j++) { - assertEquals(t.slist[i].get(j), c.slist[i].get(j)); - } - } - for(int i=0; i < t.imap.length; i++) { - assertEquals(t.imap[i].size(), c.imap[i].size()); - for(String j : t.imap[i].keySet()) { - assertEquals(t.imap[i].get(j), c.imap[i].get(j)); - } - } - } - - @MessagePackMessage - public static class Dim2Test { - public Dim2Test() { } - public int[][] i; - public String[][] str; - public List[][] slist; - } - - @Test - public void testDim2() { - Dim2Test t = new Dim2Test(); - t.i = new int[2][]; - t.i[0] = new int[] {0, 1}; - t.i[1] = new int[] {2, 3, 4}; - t.str = new String[2][]; - t.str[0] = new String[] {"aa", "bb"}; - t.str[1] = new String[] {"cc", "dd", "ee"}; - t.slist = new List[2][]; - t.slist[0] = new List[1]; - t.slist[0][0] = new ArrayList(); - t.slist[0][0].add("ff"); - t.slist[0][0].add("gg"); - t.slist[1] = new List[2]; - t.slist[1][0] = new ArrayList(); - t.slist[1][0].add("hh"); - t.slist[1][0].add("ii"); - t.slist[1][1] = new ArrayList(); - t.slist[1][1].add("jj"); - t.slist[1][1].add("kk"); - - byte[] raw = MessagePack.pack(t); - - Dim2Test u = MessagePack.unpack(raw, Dim2Test.class); - assertEquals(t.i.length, t.i.length); - for(int i=0; i < t.i.length; i++) { - assertEquals(t.i[i].length, u.i[i].length); - for(int j=0; j < t.i[i].length; j++) { - assertEquals(t.i[i][j], u.i[i][j]); - } - } - assertEquals(t.str.length, t.str.length); - for(int i=0; i < t.str.length; i++) { - assertEquals(t.str[i].length, u.str[i].length); - for(int j=0; j < t.str[i].length; j++) { - assertEquals(t.str[i][j], u.str[i][j]); - } - } - assertEquals(t.slist.length, t.slist.length); - for(int i=0; i < t.slist.length; i++) { - assertEquals(t.slist[i].length, u.slist[i].length); - for(int j=0; j < t.slist[i].length; j++) { - assertEquals(t.slist[i][j].size(), u.slist[i][j].size()); - for(int k=0; k < t.slist[i][j].size(); k++) { - assertEquals(t.slist[i][j].get(k), u.slist[i][j].get(k)); - } - } - } - } - - @MessagePackMessage - public static class Dim3Test { - public Dim3Test() { } - public int[][][] i; - public String[][][] str; - public List[][][] slist; - } - - @Test - public void testDim3() { - Dim3Test t = new Dim3Test(); - t.i = new int[2][][]; - t.i[0] = new int[2][]; - t.i[0][0] = new int[] {0, 1}; - t.i[0][1] = new int[] {2, 3, 4}; - t.i[1] = new int[1][]; - t.i[1][0] = new int[] {5}; - t.str = new String[2][][]; - t.str[0] = new String[1][]; - t.str[0][0] = new String[] {"aa", "bb"}; - t.str[1] = new String[2][]; - t.str[1][0] = new String[] {"cc", "dd", "ee"}; - t.str[1][1] = new String[] {"ff"}; - t.slist = new List[2][][]; - t.slist[0] = new List[2][]; - t.slist[0][0] = new List[1]; - t.slist[0][0][0] = new ArrayList(); - t.slist[0][0][0].add("ff"); - t.slist[0][0][0].add("gg"); - t.slist[0][1] = new List[2]; - t.slist[0][1][0] = new ArrayList(); - t.slist[0][1][0].add("hh"); - t.slist[0][1][0].add("ii"); - t.slist[0][1][1] = new ArrayList(); - t.slist[0][1][1].add("jj"); - t.slist[0][1][1].add("kk"); - t.slist[1] = new List[1][]; - t.slist[1][0] = new List[0]; - - byte[] raw = MessagePack.pack(t); - - Dim3Test u = MessagePack.unpack(raw, Dim3Test.class); - assertEquals(t.i.length, t.i.length); - for(int i=0; i < t.i.length; i++) { - assertEquals(t.i[i].length, u.i[i].length); - for(int j=0; j < t.i[i].length; j++) { - for(int k=0; k < t.i[i].length; k++) { - assertEquals(t.i[i][j][k], u.i[i][j][k]); - } - } - } - assertEquals(t.str.length, t.str.length); - for(int i=0; i < t.str.length; i++) { - assertEquals(t.str[i].length, u.str[i].length); - for(int j=0; j < t.str[i].length; j++) { - assertEquals(t.str[i][j].length, u.str[i][j].length); - for(int k=0; k < t.str[i][j].length; k++) { - assertEquals(t.str[i][j][k], u.str[i][j][k]); - } - } - } - assertEquals(t.slist.length, t.slist.length); - for(int i=0; i < t.slist.length; i++) { - assertEquals(t.slist[i].length, u.slist[i].length); - for(int j=0; j < t.slist[i].length; j++) { - assertEquals(t.slist[i][j].length, u.slist[i][j].length); - for(int k=0; k < t.slist[i][j].length; k++) { - assertEquals(t.slist[i][j][k].size(), u.slist[i][j][k].size()); - for(int l=0; l < t.slist[i][j][k].size(); l++) { - assertEquals(t.slist[i][j][k].get(l), u.slist[i][j][k].get(l)); - } - } - } - } - } - - @Test - public void testLocal() throws IOException { - int[][][] src = new int[10][20][30]; - for (int i = 0; i < 10; ++i) { - for (int j = 0; j < 20; ++j) { - for (int k = 0; k < 30; ++k) { - src[i][j][k] = (int) (Math.random() * 100); - } - } - } - - byte[] raw = MessagePack.pack(src); - - int[][][] u = MessagePack.unpack(raw, int[][][].class); - assertEquals(src.length, u.length); - for(int i = 0; i < src.length; ++i) { - assertEquals(src[i].length, u[i].length); - for(int j = 0; j < src[i].length; ++j) { - assertEquals(src[i][j].length, u[i][j].length); - for(int k = 0; k < src[i][j].length; ++k) { - assertEquals(src[i][j][k], u[i][j][k]); - } - } - } - - int[][][] c = MessagePack.unpack(raw).convert(int[][][].class); - assertEquals(src.length, c.length); - for(int i = 0; i < src.length; ++i) { - assertEquals(src[i].length, c[i].length); - for(int j = 0; j < src[i].length; ++j) { - assertEquals(src[i][j].length, c[i][j].length); - for(int k = 0; k < src[i][j].length; ++k) { - assertEquals(src[i][j][k], c[i][j][k]); - } - } - } - } -} - diff --git a/java/src/test/java/org/msgpack/TestCases.java b/java/src/test/java/org/msgpack/TestCases.java deleted file mode 100644 index 38c1d1c2..00000000 --- a/java/src/test/java/org/msgpack/TestCases.java +++ /dev/null @@ -1,210 +0,0 @@ -package org.msgpack; - -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestCases { - public void feedFile(Unpacker pac, String path) throws Exception { - FileInputStream input = new FileInputStream(path); - byte[] buffer = new byte[32*1024]; - while(true) { - int count = input.read(buffer); - if(count < 0) { - break; - } - pac.feed(buffer, 0, count); - } - } - - @Test - public void testDynamicType() throws Exception { - Unpacker pac = new Unpacker(); - Unpacker pac_compact = new Unpacker(); - - feedFile(pac, "src/test/resources/cases.mpac"); - feedFile(pac_compact, "src/test/resources/cases_compact.mpac"); - - UnpackResult result = new UnpackResult(); - UnpackResult result_compact = new UnpackResult(); - while(pac.next(result)) { - assertTrue( pac_compact.next(result_compact) ); - assertTrue( result.getData().equals(result_compact.getData()) ); - } - - assertFalse( pac_compact.next(result) ); - } - - @Test - public void testDirectConversion() throws Exception { - Unpacker pac = new Unpacker(); - Unpacker pac_compact = new Unpacker(); - - feedFile(pac, "src/test/resources/cases.mpac"); - feedFile(pac_compact, "src/test/resources/cases_compact.mpac"); - - UnpackResult result_compact = new UnpackResult(); - while(pac_compact.next(result_compact)) { - MessagePackObject obj = result_compact.getData(); - testDirectConversionRecursive(pac, obj); - } - - assertFalse( pac_compact.next(result_compact) ); - } - - private void testDirectConversionRecursive(Unpacker pac, MessagePackObject obj) throws Exception { - if(obj.isBooleanType()) { - boolean expect = obj.asBoolean(); - boolean actual = pac.unpackBoolean(); - assertEquals(expect, actual); - - } else if(obj.isIntegerType()) { - BigInteger expect = obj.asBigInteger(); - if(BigInteger.valueOf((long)Byte.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) <= 0) { - byte actual = pac.unpackByte(); - assertEquals(expect.byteValue(), actual); - } else if(BigInteger.valueOf((long)Short.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) <= 0) { - short actual = pac.unpackShort(); - assertEquals(expect.shortValue(), actual); - } else if(BigInteger.valueOf((long)Integer.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) <= 0) { - int actual = pac.unpackInt(); - assertEquals(expect.intValue(), actual); - } else if(BigInteger.valueOf(Long.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) { - long actual = pac.unpackLong(); - assertEquals(expect.longValue(), actual); - } else { - BigInteger actual = pac.unpackBigInteger(); - assertEquals(expect, actual); - } - - } else if(obj.isFloatType()) { - double expect = obj.asFloat(); - double actual = pac.unpackDouble(); - assertEquals(expect, actual, 0.01); - - } else if(obj.isArrayType()) { - MessagePackObject[] expect = obj.asArray(); - int length = pac.unpackArray(); - assertEquals(expect.length, length); - for(int i=0; i < length; i++) { - testDirectConversionRecursive(pac, expect[i]); - } - - } else if(obj.isMapType()) { - Map expect = obj.asMap(); - int size = pac.unpackMap(); - assertEquals(expect.size(), size); - for(int i=0; i < size; i++) { - MessagePackObject key = pac.unpackObject(); - MessagePackObject value = expect.get(key); - assertNotNull(value); - testDirectConversionRecursive(pac, value); - } - - } else if(obj.isRawType()) { - byte[] expect = obj.asByteArray(); - int length = pac.unpackRaw(); - assertEquals(expect.length, length); - byte[] actual = pac.unpackRawBody(length); - assertTrue(Arrays.equals(expect, actual)); - - } else if(obj.isNil()) { - pac.unpackNull(); - - } else { - fail("unexpected object: "+obj); - } - } - - @Test - public void testIndirectConversion() throws Exception { - Unpacker pac = new Unpacker(); - Unpacker pac_compact = new Unpacker(); - - feedFile(pac, "src/test/resources/cases.mpac"); - feedFile(pac_compact, "src/test/resources/cases_compact.mpac"); - - UnpackResult result = new UnpackResult(); - UnpackResult result_compact = new UnpackResult(); - while(pac.next(result)) { - assertTrue( pac_compact.next(result_compact) ); - testIndirectConversionRecursive(result.getData(), result_compact.getData()); - } - - assertFalse( pac_compact.next(result) ); - } - - private void testIndirectConversionRecursive(MessagePackObject target, MessagePackObject obj) { - if(obj.isBooleanType()) { - boolean expect = obj.asBoolean(); - boolean actual = target.asBoolean(); - assertEquals(expect, actual); - - } else if(obj.isIntegerType()) { - BigInteger expect = obj.asBigInteger(); - if(BigInteger.valueOf((long)Byte.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) <= 0) { - byte actual = target.asByte(); - assertEquals(expect.byteValue(), actual); - } else if(BigInteger.valueOf((long)Short.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) <= 0) { - short actual = target.asShort(); - assertEquals(expect.shortValue(), actual); - } else if(BigInteger.valueOf((long)Integer.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) <= 0) { - int actual = target.asInt(); - assertEquals(expect.intValue(), actual); - } else if(BigInteger.valueOf(Long.MIN_VALUE).compareTo(expect) <= 0 && - expect.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) { - long actual = target.asLong(); - assertEquals(expect.longValue(), actual); - } else { - BigInteger actual = target.asBigInteger(); - assertEquals(expect, actual); - } - - } else if(obj.isFloatType()) { - double expect = obj.asFloat(); - double actual = target.asDouble(); - assertEquals(expect, actual, 0.01); - - } else if(obj.isArrayType()) { - MessagePackObject[] expect = obj.asArray(); - MessagePackObject[] actual = target.asArray(); - assertEquals(expect.length, actual.length); - for(int i=0; i < expect.length; i++) { - testIndirectConversionRecursive(actual[i], expect[i]); - } - - } else if(obj.isMapType()) { - Map expect = obj.asMap(); - Map actual = target.asMap(); - assertEquals(expect.size(), actual.size()); - for(Map.Entry pair : expect.entrySet()) { - MessagePackObject value = actual.get(pair.getKey()); - assertNotNull(value); - testIndirectConversionRecursive(value, pair.getValue()); - } - - } else if(obj.isRawType()) { - byte[] expect = obj.asByteArray(); - byte[] actual = obj.asByteArray(); - assertEquals(expect.length, actual.length); - assertTrue(Arrays.equals(expect, actual)); - - } else if(obj.isNil()) { - assertTrue(target.isNil()); - - } else { - fail("unexpected object: "+obj); - } - } -}; - diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java deleted file mode 100644 index 1822ecb4..00000000 --- a/java/src/test/java/org/msgpack/TestDirectConversion.java +++ /dev/null @@ -1,279 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestDirectConversion { - @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testInt(rand.nextInt()); - } - public void testInt(int val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(val, pac.unpackInt()); - } - - @Test - public void testLong() throws Exception { - testLong(0); - testLong(-1); - testLong(1); - testLong(Integer.MIN_VALUE); - testLong(Integer.MAX_VALUE); - testLong(Long.MIN_VALUE); - testLong(Long.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testLong(rand.nextLong()); - } - public void testLong(long val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(val, pac.unpackLong()); - } - - @Test - public void testBigInteger() throws Exception { - testBigInteger(BigInteger.valueOf(0)); - testBigInteger(BigInteger.valueOf(-1)); - testBigInteger(BigInteger.valueOf(1)); - testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); - testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); - testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); - testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); - BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); - testBigInteger(max); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); - } - public void testBigInteger(BigInteger val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(val, pac.unpackBigInteger()); - } - - @Test - public void testFloat() throws Exception { - testFloat((float)0.0); - testFloat((float)-0.0); - testFloat((float)1.0); - testFloat((float)-1.0); - testFloat((float)Float.MAX_VALUE); - testFloat((float)Float.MIN_VALUE); - testFloat((float)Float.NaN); - testFloat((float)Float.NEGATIVE_INFINITY); - testFloat((float)Float.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testFloat(rand.nextFloat()); - } - public void testFloat(float val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(val, pac.unpackFloat(), 10e-10); - } - - @Test - public void testDouble() throws Exception { - testDouble((double)0.0); - testDouble((double)-0.0); - testDouble((double)1.0); - testDouble((double)-1.0); - testDouble((double)Double.MAX_VALUE); - testDouble((double)Double.MIN_VALUE); - testDouble((double)Double.NaN); - testDouble((double)Double.NEGATIVE_INFINITY); - testDouble((double)Double.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testDouble(rand.nextDouble()); - } - public void testDouble(double val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(val, pac.unpackDouble(), 10e-10); - } - - @Test - public void testNil() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).packNil(); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(null, pac.unpackNull()); - } - - @Test - public void testBoolean() throws Exception { - testBoolean(false); - testBoolean(true); - } - public void testBoolean(boolean val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(val, pac.unpackBoolean()); - } - - @Test - public void testString() throws Exception { - testString(""); - testString("a"); - testString("ab"); - testString("abc"); - - // small size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 31 + 1; - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - - // medium size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 15); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - - // large size string - for (int i = 0; i < 10; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 31); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - } - public void testString(String val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - assertEquals(val, pac.unpackString()); - } - - @Test - public void testArray() throws Exception { - List emptyList = new ArrayList(); - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(emptyList); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - int ulen = pac.unpackArray(); - assertEquals(0, ulen); - } - - for (int i = 0; i < 1000; i++) { - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(j); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(l); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - int ulen = pac.unpackArray(); - assertEquals(len, ulen); - for (int j = 0; j < len; j++) { - assertEquals(l.get(j).intValue(), pac.unpackInt()); - } - } - - for (int i = 0; i < 1000; i++) { - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(Integer.toString(j)); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(l); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - int ulen = pac.unpackArray(); - assertEquals(len, ulen); - for (int j = 0; j < len; j++) { - assertEquals(l.get(j), pac.unpackString()); - } - } - } - - @Test - public void testMap() throws Exception { - Map emptyMap = new HashMap(); - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(emptyMap); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - int ulen = pac.unpackMap(); - assertEquals(0, ulen); - } - - for (int i = 0; i < 1000; i++) { - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(j, j); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(m); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - int ulen = pac.unpackMap(); - assertEquals(len, ulen); - for (int j = 0; j < len; j++) { - Integer val = m.get(pac.unpackInt()); - assertNotNull(val); - assertEquals(val.intValue(), pac.unpackInt()); - } - } - - for (int i = 0; i < 1000; i++) { - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(Integer.toString(j), j); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(m); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - int ulen = pac.unpackMap(); - assertEquals(len, ulen); - for (int j = 0; j < len; j++) { - Integer val = m.get(pac.unpackString()); - assertNotNull(val); - assertEquals(val.intValue(), pac.unpackInt()); - } - } - } -}; - diff --git a/java/src/test/java/org/msgpack/TestEnums.java b/java/src/test/java/org/msgpack/TestEnums.java deleted file mode 100644 index 84a390cf..00000000 --- a/java/src/test/java/org/msgpack/TestEnums.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import org.msgpack.object.*; -import org.msgpack.annotation.*; -import static org.msgpack.Templates.*; - -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import junit.framework.TestCase; - -public class TestEnums extends TestCase { - public static enum ProvidedEnum { - RED, - GREEN, - BLUE - } - - @MessagePackOrdinalEnum - public static enum UserDefinedEnum { - CYAN, - MAGENTA, - YELLOW - } - - @MessagePackOrdinalEnum - public static enum UserDefinedEnumVersion2 { - CYAN, - MAGENTA, - YELLOW, - KEY - } - - - { - // provided classes need registration - MessagePack.register(ProvidedEnum.class); - // annotated classes don't need registration - } - - @Test - public void testRegisteredEnum() { - byte[] rout = MessagePack.pack(ProvidedEnum.RED); - byte[] gout = MessagePack.pack(ProvidedEnum.GREEN); - byte[] bout = MessagePack.pack(ProvidedEnum.BLUE); - - ProvidedEnum r = MessagePack.unpack(rout, ProvidedEnum.class); - ProvidedEnum g = MessagePack.unpack(gout, ProvidedEnum.class); - ProvidedEnum b = MessagePack.unpack(bout, ProvidedEnum.class); - - assertEquals(r, ProvidedEnum.RED); - assertEquals(g, ProvidedEnum.GREEN); - assertEquals(b, ProvidedEnum.BLUE); - } - - @Test - public void testAnnotatedEnum() { - byte[] cout = MessagePack.pack(UserDefinedEnum.CYAN); - byte[] mout = MessagePack.pack(UserDefinedEnum.MAGENTA); - byte[] yout = MessagePack.pack(UserDefinedEnum.YELLOW); - - UserDefinedEnum c = MessagePack.unpack(cout, UserDefinedEnum.class); - UserDefinedEnum m = MessagePack.unpack(mout, UserDefinedEnum.class); - UserDefinedEnum y = MessagePack.unpack(yout, UserDefinedEnum.class); - - assertEquals(c, UserDefinedEnum.CYAN); - assertEquals(m, UserDefinedEnum.MAGENTA); - assertEquals(y, UserDefinedEnum.YELLOW); - } - - @Test - public void testBackwardCompatibility() { - byte[] cout = MessagePack.pack(UserDefinedEnum.CYAN); - byte[] mout = MessagePack.pack(UserDefinedEnum.MAGENTA); - byte[] yout = MessagePack.pack(UserDefinedEnum.YELLOW); - - UserDefinedEnumVersion2 c = MessagePack.unpack(cout, UserDefinedEnumVersion2.class); - UserDefinedEnumVersion2 m = MessagePack.unpack(mout, UserDefinedEnumVersion2.class); - UserDefinedEnumVersion2 y = MessagePack.unpack(yout, UserDefinedEnumVersion2.class); - - assertEquals(c, UserDefinedEnumVersion2.CYAN); - assertEquals(m, UserDefinedEnumVersion2.MAGENTA); - assertEquals(y, UserDefinedEnumVersion2.YELLOW); - } - - @Test - public void testForwardCompatibility() { - byte[] cout = MessagePack.pack(UserDefinedEnumVersion2.CYAN); - byte[] mout = MessagePack.pack(UserDefinedEnumVersion2.MAGENTA); - byte[] yout = MessagePack.pack(UserDefinedEnumVersion2.YELLOW); - byte[] kout = MessagePack.pack(UserDefinedEnumVersion2.KEY); - - UserDefinedEnum c = MessagePack.unpack(cout, UserDefinedEnum.class); - UserDefinedEnum m = MessagePack.unpack(mout, UserDefinedEnum.class); - UserDefinedEnum y = MessagePack.unpack(yout, UserDefinedEnum.class); - - assertEquals(c, UserDefinedEnum.CYAN); - assertEquals(m, UserDefinedEnum.MAGENTA); - assertEquals(y, UserDefinedEnum.YELLOW); - - try { - MessagePack.unpack(kout, UserDefinedEnum.class); - } catch (Exception e) { - assertTrue(true); - return; - } - assertTrue(false); - } -} - diff --git a/java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java b/java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java deleted file mode 100644 index 7503d0ea..00000000 --- a/java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java +++ /dev/null @@ -1,319 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import org.msgpack.object.*; -import org.msgpack.annotation.*; -import static org.msgpack.Templates.*; - -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import junit.framework.TestCase; - -public class TestMessagePackStaticMethods extends TestCase { - public static class ProvidedClass { - public boolean bool; - public String str; - public List list; - - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof ProvidedClass)) { - return false; - } - ProvidedClass o = (ProvidedClass)obj; - return bool == o.bool && str.equals(o.str) && list.equals(o.list); - } - - public String toString() { - return "ProvidedClass"; - } - } - - @MessagePackMessage - public static class UserDefinedClass { - public boolean bool; - public String str; - public List list; - - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof UserDefinedClass)) { - return false; - } - UserDefinedClass o = (UserDefinedClass)obj; - return bool == o.bool && str.equals(o.str) && list.equals(o.list); - } - - public String toString() { - return "UserDefinedClass"; - } - } - - static { - // provided classes need registration - MessagePack.register(ProvidedClass.class); - // annotated classes don't need registration - } - - - @Test - public void testCheckedPackToByteArray() throws Exception { - byte[] a = MessagePack.pack("msgpack", TString); - byte[] b = MessagePack.pack((Object)1, TInteger); - byte[] c = MessagePack.pack((Object)null, TAny); - byte[] d = MessagePack.pack(createStringList(), tList(TString)); - byte[] e = MessagePack.pack(createProvidedClass(), tClass(ProvidedClass.class)); - byte[] f = MessagePack.pack(createUserDefinedClass(), tClass(UserDefinedClass.class)); - - { - Object aobj = MessagePack.unpack(a, TString); - Object bobj = MessagePack.unpack(b, TInteger); - Object cobj = MessagePack.unpack(c, tNullable(TAny)); - Object dobj = MessagePack.unpack(d, tList(TString)); - Object eobj = MessagePack.unpack(e, tClass(ProvidedClass.class)); - Object fobj = MessagePack.unpack(f, tClass(UserDefinedClass.class)); - - assertEquals(aobj, "msgpack"); - assertEquals(bobj, 1); - assertEquals(cobj, null); - assertEquals(dobj, createStringList()); - assertEquals(eobj, createProvidedClass()); - assertEquals(fobj, createUserDefinedClass()); - } - - { - String aobj = MessagePack.unpack(a, String.class); - Integer bobj = MessagePack.unpack(b, Integer.class); - Object cobj = MessagePack.unpack(c, Object.class); - // Generics are not supported on unpack(Class klass) interface - ProvidedClass eobj = MessagePack.unpack(e, ProvidedClass.class); - UserDefinedClass fobj = MessagePack.unpack(f, UserDefinedClass.class); - - assertEquals(aobj, "msgpack"); - assertEquals(bobj, (Integer)1); - assertEquals(cobj, null); - assertEquals(eobj, createProvidedClass()); - assertEquals(fobj, createUserDefinedClass()); - } - - { - ProvidedClass eobj = MessagePack.unpack(e, createProvidedClass()); - UserDefinedClass fobj = MessagePack.unpack(f, createUserDefinedClass()); - - assertEquals(eobj, createProvidedClass()); - assertEquals(fobj, createUserDefinedClass()); - } - } - - @Test - public void testCheckedPackToStream() throws Exception { - ByteArrayOutputStream aout = new ByteArrayOutputStream(); - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ByteArrayOutputStream cout = new ByteArrayOutputStream(); - ByteArrayOutputStream dout = new ByteArrayOutputStream(); - ByteArrayOutputStream eout = new ByteArrayOutputStream(); - ByteArrayOutputStream fout = new ByteArrayOutputStream(); - - MessagePack.pack(aout, "msgpack"); - MessagePack.pack(bout, (Object)1); - MessagePack.pack(cout, (Object)null); - MessagePack.pack(dout, createStringList()); - MessagePack.pack(eout, createProvidedClass()); - MessagePack.pack(fout, createUserDefinedClass()); - - { - InputStream ain = new ByteArrayInputStream(aout.toByteArray()); - InputStream bin = new ByteArrayInputStream(bout.toByteArray()); - InputStream cin = new ByteArrayInputStream(cout.toByteArray()); - InputStream din = new ByteArrayInputStream(dout.toByteArray()); - InputStream ein = new ByteArrayInputStream(eout.toByteArray()); - InputStream fin = new ByteArrayInputStream(fout.toByteArray()); - - Object aobj = MessagePack.unpack(ain, TString); - Object bobj = MessagePack.unpack(bin, TInteger); - Object cobj = MessagePack.unpack(cin, tNullable(TAny)); - Object dobj = MessagePack.unpack(din, tList(TString)); - Object eobj = MessagePack.unpack(ein, tClass(ProvidedClass.class)); - Object fobj = MessagePack.unpack(fin, tClass(UserDefinedClass.class)); - - assertEquals(aobj, "msgpack"); - assertEquals(bobj, 1); - assertEquals(cobj, null); - assertEquals(dobj, createStringList()); - assertEquals(eobj, createProvidedClass()); - assertEquals(fobj, createUserDefinedClass()); - } - - { - InputStream ain = new ByteArrayInputStream(aout.toByteArray()); - InputStream bin = new ByteArrayInputStream(bout.toByteArray()); - InputStream cin = new ByteArrayInputStream(cout.toByteArray()); - InputStream ein = new ByteArrayInputStream(eout.toByteArray()); - // - InputStream fin = new ByteArrayInputStream(fout.toByteArray()); - - String aobj = MessagePack.unpack(ain, String.class); - Integer bobj = MessagePack.unpack(bin, Integer.class); - Object cobj = MessagePack.unpack(cin, Object.class); - // Generics are not supported on unpack(Class klass) interface - ProvidedClass eobj = MessagePack.unpack(ein, ProvidedClass.class); - UserDefinedClass fobj = MessagePack.unpack(fin, UserDefinedClass.class); - - assertEquals(aobj, "msgpack"); - assertEquals(bobj, (Integer)1); - assertEquals(cobj, null); - assertEquals(eobj, createProvidedClass()); - assertEquals(fobj, createUserDefinedClass()); - } - - { - InputStream ein = new ByteArrayInputStream(eout.toByteArray()); - InputStream fin = new ByteArrayInputStream(fout.toByteArray()); - - ProvidedClass eobj = MessagePack.unpack(ein, createProvidedClass()); - UserDefinedClass fobj = MessagePack.unpack(fin, createUserDefinedClass()); - - assertEquals(eobj, createProvidedClass()); - assertEquals(fobj, createUserDefinedClass()); - } - } - - @Test - public void testPackToByteArray() throws Exception { - byte[] a = MessagePack.pack("msgpack"); - byte[] b = MessagePack.pack((Object)1); - byte[] c = MessagePack.pack((Object)null); - byte[] d = MessagePack.pack(createStringList()); - byte[] e = MessagePack.pack(createProvidedClass()); - byte[] f = MessagePack.pack(createUserDefinedClass_dynamic()); - - { - MessagePackObject aobj = MessagePack.unpack(a); - MessagePackObject bobj = MessagePack.unpack(b); - MessagePackObject cobj = MessagePack.unpack(c); - MessagePackObject dobj = MessagePack.unpack(d); - MessagePackObject eobj = MessagePack.unpack(e); - MessagePackObject fobj = MessagePack.unpack(f); - - assertEquals(aobj, RawType.create("msgpack")); - assertEquals(bobj, IntegerType.create(1)); - assertEquals(cobj, NilType.create()); - assertEquals(dobj, createStringList_dynamic()); - assertEquals(eobj, createProvidedClass_dynamic()); - assertEquals(fobj, createUserDefinedClass_dynamic()); - } - } - - @Test - public void testPackToStream() throws Exception { - ByteArrayOutputStream aout = new ByteArrayOutputStream(); - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ByteArrayOutputStream cout = new ByteArrayOutputStream(); - ByteArrayOutputStream dout = new ByteArrayOutputStream(); - ByteArrayOutputStream eout = new ByteArrayOutputStream(); - ByteArrayOutputStream fout = new ByteArrayOutputStream(); - - MessagePack.pack(aout, "msgpack"); - MessagePack.pack(bout, (Object)1); - MessagePack.pack(cout, (Object)null); - MessagePack.pack(dout, createStringList()); - MessagePack.pack(eout, createProvidedClass()); - MessagePack.pack(fout, createUserDefinedClass()); - - { - InputStream ain = new ByteArrayInputStream(aout.toByteArray()); - InputStream bin = new ByteArrayInputStream(bout.toByteArray()); - InputStream cin = new ByteArrayInputStream(cout.toByteArray()); - InputStream din = new ByteArrayInputStream(dout.toByteArray()); - InputStream ein = new ByteArrayInputStream(eout.toByteArray()); - InputStream fin = new ByteArrayInputStream(fout.toByteArray()); - - MessagePackObject aobj = MessagePack.unpack(ain); - MessagePackObject bobj = MessagePack.unpack(bin); - MessagePackObject cobj = MessagePack.unpack(cin); - MessagePackObject dobj = MessagePack.unpack(din); - MessagePackObject eobj = MessagePack.unpack(ein); - MessagePackObject fobj = MessagePack.unpack(fin); - - assertEquals(aobj, RawType.create("msgpack")); - assertEquals(bobj, IntegerType.create(1)); - assertEquals(cobj, NilType.create()); - assertEquals(dobj, createStringList_dynamic()); - assertEquals(eobj, createProvidedClass_dynamic()); - assertEquals(fobj, createUserDefinedClass_dynamic()); - } - } - - - private List createStringList() { - List list = new ArrayList(); - list.add("frsyuki"); - list.add("kumofs"); - list.add("gem-compile"); - return list; - } - - private MessagePackObject createStringList_dynamic() { - MessagePackObject[] array = new MessagePackObject[3]; - array[0] = RawType.create("frsyuki"); - array[1] = RawType.create("kumofs"); - array[2] = RawType.create("gem-compile"); - return ArrayType.create(array); - } - - - private ProvidedClass createProvidedClass() { - ProvidedClass obj = new ProvidedClass(); - obj.bool = true; - obj.str = "viver"; - obj.list = new ArrayList(); - obj.list.add(1); - obj.list.add(2); - obj.list.add(3); - return obj; - } - - private MessagePackObject createProvidedClass_dynamic() { - MessagePackObject[] obj = new MessagePackObject[3]; - obj[0] = BooleanType.create(true); - obj[1] = RawType.create("viver"); - MessagePackObject[] list = new MessagePackObject[3]; - list[0] = IntegerType.create(1); - list[1] = IntegerType.create(2); - list[2] = IntegerType.create(3); - obj[2] = ArrayType.create(list); - return ArrayType.create(obj); - } - - - private UserDefinedClass createUserDefinedClass() { - UserDefinedClass obj = new UserDefinedClass(); - obj.bool = false; - obj.str = "muga"; - obj.list = new ArrayList(); - obj.list.add(9); - obj.list.add(10); - obj.list.add(11); - return obj; - } - - private MessagePackObject createUserDefinedClass_dynamic() { - MessagePackObject[] obj = new MessagePackObject[3]; - obj[0] = BooleanType.create(false); - obj[1] = RawType.create("muga"); - MessagePackObject[] list = new MessagePackObject[3]; - list[0] = IntegerType.create(9); - list[1] = IntegerType.create(10); - list[2] = IntegerType.create(11); - obj[2] = ArrayType.create(list); - return ArrayType.create(obj); - } -} - diff --git a/java/src/test/java/org/msgpack/TestMessageUnpackable.java b/java/src/test/java/org/msgpack/TestMessageUnpackable.java deleted file mode 100644 index 32917c7a..00000000 --- a/java/src/test/java/org/msgpack/TestMessageUnpackable.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestMessageUnpackable { - @Test - public void testImage() throws Exception { - Image src = new Image(); - src.title = "msgpack"; - src.uri = "http://msgpack.org/"; - src.width = 2560; - src.height = 1600; - src.size = 4096000; - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - src.messagePack(new Packer(out)); - - Image dst = new Image(); - - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - dst.messageUnpack(new Unpacker(in)); - - assertEquals(src, dst); - } -} - diff --git a/java/src/test/java/org/msgpack/TestObjectEquals.java b/java/src/test/java/org/msgpack/TestObjectEquals.java deleted file mode 100644 index 25fe9271..00000000 --- a/java/src/test/java/org/msgpack/TestObjectEquals.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import org.msgpack.object.*; -import java.math.BigInteger; -import java.util.*; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestObjectEquals { - @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testInt(rand.nextInt()); - } - public void testInt(int val) throws Exception { - MessagePackObject objInt = IntegerType.create(val); - MessagePackObject objLong = IntegerType.create((long)val); - MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf((long)val)); - assertTrue(objInt.equals(objInt)); - assertTrue(objInt.equals(objLong)); - assertTrue(objInt.equals(objBigInt)); - assertTrue(objLong.equals(objInt)); - assertTrue(objLong.equals(objLong)); - assertTrue(objLong.equals(objBigInt)); - assertTrue(objBigInt.equals(objInt)); - assertTrue(objBigInt.equals(objLong)); - assertTrue(objBigInt.equals(objBigInt)); - } - - @Test - public void testLong() throws Exception { - testLong(0); - testLong(-1); - testLong(1); - testLong(Integer.MIN_VALUE); - testLong(Integer.MAX_VALUE); - testLong(Long.MIN_VALUE); - testLong(Long.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testLong(rand.nextLong()); - } - public void testLong(long val) throws Exception { - MessagePackObject objInt = IntegerType.create((int)val); - MessagePackObject objLong = IntegerType.create(val); - MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf(val)); - if(val > (long)Integer.MAX_VALUE || val < (long)Integer.MIN_VALUE) { - assertTrue(objInt.equals(objInt)); - assertFalse(objInt.equals(objLong)); - assertFalse(objInt.equals(objBigInt)); - assertFalse(objLong.equals(objInt)); - assertTrue(objLong.equals(objLong)); - assertTrue(objLong.equals(objBigInt)); - assertFalse(objBigInt.equals(objInt)); - assertTrue(objBigInt.equals(objLong)); - assertTrue(objBigInt.equals(objBigInt)); - } else { - assertTrue(objInt.equals(objInt)); - assertTrue(objInt.equals(objLong)); - assertTrue(objInt.equals(objBigInt)); - assertTrue(objLong.equals(objInt)); - assertTrue(objLong.equals(objLong)); - assertTrue(objLong.equals(objBigInt)); - assertTrue(objBigInt.equals(objInt)); - assertTrue(objBigInt.equals(objLong)); - assertTrue(objBigInt.equals(objBigInt)); - } - } - - @Test - public void testNil() throws Exception { - assertTrue(NilType.create().equals(NilType.create())); - assertFalse(NilType.create().equals(IntegerType.create(0))); - assertFalse(NilType.create().equals(BooleanType.create(false))); - } - - @Test - public void testString() throws Exception { - testString(""); - testString("a"); - testString("ab"); - testString("abc"); - } - public void testString(String str) throws Exception { - assertTrue(RawType.create(str).equals(RawType.create(str))); - } -} - diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java deleted file mode 100644 index 494c8a8a..00000000 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ /dev/null @@ -1,275 +0,0 @@ -package org.msgpack; - -import org.msgpack.*; -import java.io.*; -import java.util.*; -import java.math.BigInteger; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestPackUnpack { - public MessagePackObject unpackOne(ByteArrayOutputStream out) { - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertEquals(true, it.hasNext()); - MessagePackObject obj = it.next(); - assertEquals(false, it.hasNext()); - return obj; - } - - @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testInt(rand.nextInt()); - } - public void testInt(int val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asInt()); - } - - @Test - public void testLong() throws Exception { - testLong(0); - testLong(-1); - testLong(1); - testLong(Integer.MIN_VALUE); - testLong(Integer.MAX_VALUE); - testLong(Long.MIN_VALUE); - testLong(Long.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testLong(rand.nextLong()); - } - public void testLong(long val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asLong()); - } - - @Test - public void testBigInteger() throws Exception { - testBigInteger(BigInteger.valueOf(0)); - testBigInteger(BigInteger.valueOf(-1)); - testBigInteger(BigInteger.valueOf(1)); - testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); - testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); - testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); - testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); - BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); - testBigInteger(max); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); - } - public void testBigInteger(BigInteger val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asBigInteger()); - } - - @Test - public void testFloat() throws Exception { - testFloat((float)0.0); - testFloat((float)-0.0); - testFloat((float)1.0); - testFloat((float)-1.0); - testFloat((float)Float.MAX_VALUE); - testFloat((float)Float.MIN_VALUE); - testFloat((float)Float.NaN); - testFloat((float)Float.NEGATIVE_INFINITY); - testFloat((float)Float.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testFloat(rand.nextFloat()); - } - public void testFloat(float val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - float f = obj.asFloat(); - assertEquals(val, f, 10e-10); - } - - @Test - public void testDouble() throws Exception { - testDouble((double)0.0); - testDouble((double)-0.0); - testDouble((double)1.0); - testDouble((double)-1.0); - testDouble((double)Double.MAX_VALUE); - testDouble((double)Double.MIN_VALUE); - testDouble((double)Double.NaN); - testDouble((double)Double.NEGATIVE_INFINITY); - testDouble((double)Double.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testDouble(rand.nextDouble()); - } - public void testDouble(double val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - double f = obj.asDouble(); - assertEquals(val, f, 10e-10); - } - - @Test - public void testNil() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).packNil(); - MessagePackObject obj = unpackOne(out); - assertTrue(obj.isNil()); - } - - @Test - public void testBoolean() throws Exception { - testBoolean(false); - testBoolean(true); - } - public void testBoolean(boolean val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asBoolean()); - } - - @Test - public void testString() throws Exception { - testString(""); - testString("a"); - testString("ab"); - testString("abc"); - - // small size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 31 + 1; - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - - // medium size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 15); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - - // large size string - for (int i = 0; i < 10; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 31); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - } - public void testString(String val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asString()); - } - - @Test - public void testArray() throws Exception { - List emptyList = new ArrayList(); - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(emptyList); - MessagePackObject obj = unpackOne(out); - assertEquals(emptyList, obj.asList()); - } - - for (int i = 0; i < 1000; i++) { - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(j); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(l); - MessagePackObject obj = unpackOne(out); - List list = obj.asList(); - assertEquals(l.size(), list.size()); - for (int j = 0; j < len; j++) { - assertEquals(l.get(j).intValue(), list.get(j).asInt()); - } - } - - for (int i = 0; i < 1000; i++) { - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(Integer.toString(j)); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(l); - MessagePackObject obj = unpackOne(out); - List list = obj.asList(); - assertEquals(l.size(), list.size()); - for (int j = 0; j < len; j++) { - assertEquals(l.get(j), list.get(j).asString()); - } - } - } - - @Test - public void testMap() throws Exception { - Map emptyMap = new HashMap(); - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(emptyMap); - MessagePackObject obj = unpackOne(out); - assertEquals(emptyMap, obj.asMap()); - } - - for (int i = 0; i < 1000; i++) { - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(j, j); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(m); - MessagePackObject obj = unpackOne(out); - Map map = obj.asMap(); - assertEquals(m.size(), map.size()); - for (Map.Entry pair : map.entrySet()) { - Integer val = m.get(pair.getKey().asInt()); - assertNotNull(val); - assertEquals(val.intValue(), pair.getValue().asInt()); - } - } - - for (int i = 0; i < 1000; i++) { - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(Integer.toString(j), j); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(m); - MessagePackObject obj = unpackOne(out); - Map map = obj.asMap(); - assertEquals(m.size(), map.size()); - for (Map.Entry pair : map.entrySet()) { - Integer val = m.get(pair.getKey().asString()); - assertNotNull(val); - assertEquals(val.intValue(), pair.getValue().asInt()); - } - } - } -}; - diff --git a/java/src/test/java/org/msgpack/TestSample.java b/java/src/test/java/org/msgpack/TestSample.java deleted file mode 100644 index d0e3bae8..00000000 --- a/java/src/test/java/org/msgpack/TestSample.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.msgpack; - -import org.junit.Test; -import static org.junit.Assert.assertEquals; - -public class TestSample { - @Test - public void testNull() throws Exception { - assertEquals("aiueo", 0, 0); - } -}; diff --git a/java/src/test/java/org/msgpack/Util.java b/java/src/test/java/org/msgpack/Util.java deleted file mode 100644 index 442b4d67..00000000 --- a/java/src/test/java/org/msgpack/Util.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.msgpack; - -import static org.junit.Assert.assertEquals; - -import java.io.ByteArrayInputStream; -import java.util.Iterator; - -public class Util { - - public static MessagePackObject unpackOne(byte[] bytes) { - ByteArrayInputStream in = new ByteArrayInputStream(bytes); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertEquals(true, it.hasNext()); - MessagePackObject obj = it.next(); - assertEquals(false, it.hasNext()); - return obj; - } -} diff --git a/java/src/test/java/org/msgpack/buffer/VectoredByteBufferTest.java b/java/src/test/java/org/msgpack/buffer/VectoredByteBufferTest.java deleted file mode 100644 index 5c9c8228..00000000 --- a/java/src/test/java/org/msgpack/buffer/VectoredByteBufferTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.msgpack.buffer; - -import org.msgpack.*; -import org.msgpack.object.*; -import java.io.*; -import java.util.*; -import java.util.concurrent.*; -import java.net.*; -import junit.framework.*; -import org.junit.Test; - -public class VectoredByteBufferTest extends TestCase { - public VectoredByteBufferTest() { - } - - @Test - public void testIO() throws Exception { - VectoredByteBuffer v = new VectoredByteBuffer(); - ByteArrayOutputStream bo = new ByteArrayOutputStream(); - byte[] ref = new byte[40]; - byte[] copy = new byte[3]; - ref[0] = 10; - ref[1] = 20; - ref[2] = 30; - copy[0] = 40; - copy[1] = 50; - copy[2] = 60; - - byte[][] src = new byte[][] { - copy, ref, ref, copy, ref, copy, copy, ref - }; - - for(byte[] s : src) { - bo.write(s); - v.write(s); - } - - ByteArrayOutputStream check = new ByteArrayOutputStream(); - v.writeTo(check); - - assertEquals(bo.size(), check.size()); - assertTrue(Arrays.equals(bo.toByteArray(), check.toByteArray())); - } -} - diff --git a/java/src/test/java/org/msgpack/template/BeansEntryReaderTest.java b/java/src/test/java/org/msgpack/template/BeansEntryReaderTest.java deleted file mode 100644 index 78880582..00000000 --- a/java/src/test/java/org/msgpack/template/BeansEntryReaderTest.java +++ /dev/null @@ -1,133 +0,0 @@ -package org.msgpack.template; - -import java.beans.BeanDescriptor; -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.msgpack.template.BeansFieldEntryReader; - -import static org.junit.Assert.assertThat; -import static org.hamcrest.CoreMatchers.*; - -/** - * - * @author takeshita - * - */ -public class BeansEntryReaderTest { - - public static class VariableProps{ - - public int getCollect(){ - return 0; - } - public void setCollect(int v){} - - public int getOnlyGetter(){ - return 0; - } - - public void setOnlySetter(int v){} - - public boolean isBoolean(){ - return true; - } - public void setBoolean(boolean b){} - - - private int getPrivateBoth(){return 1;} - private void setPrivateBoth(int v){} - - private int getPrivateGetter(){return 1;} - public void setPrivateGetter(int v){} - - public int getPrivateSetter(){return 1;} - private void setPrivateSetter(int v){} - - protected int getProtected(){return 1;} - protected void setProtected(int v){} - - int getInternal(){return 1;} - void setInternal(int v){} - - public int getWrongGetter(int v){return 1;} - public void setWrongGetter(int v){} - - public void getWrongGetter2(){} - public void setWrongGetter2(int v){} - - public int isWrongGetter3(){return 1;} - public void setWrongGetter3(int v){} - - public int getWrongSetter(){return 1;} - public int setWrongSetter(int v){return 1;} - - public int getWrongSetter2(){return 1;} - public void setWrongSetter2(){} - } - - @Before - public void before(){ - reader = new BeansFieldEntryReader(); - - try { - info = Introspector.getBeanInfo(VariableProps.class); - } catch (IntrospectionException e) { - e.printStackTrace(); - Assert.fail(); - } - } - BeansFieldEntryReader reader; - BeanInfo info; - @Test - public void testIgnorePropertyDesc(){ - BeanDescriptor desc = info.getBeanDescriptor(); - - assertThat(reader.isIgnoreProp(getProp(info,"collect")),is(false)); - assertThat(reader.isIgnoreProp(getProp(info,"boolean")),is(false)); - - - assertThat(reader.isIgnoreProp(getProp(info,"onlyGetter")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"onlySetter")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"privateBoth")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"privateGetter")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"privateSetter")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"protected")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"internal")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"wrongGetter")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"wrongGetter2")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"wrongGetter3")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"wrongSetter")),is(true)); - assertThat(reader.isIgnoreProp(getProp(info,"wrongSetter2")),is(true)); - - } - @Test - public void testReadEntries(){ - - IFieldEntry[] entries = reader.readFieldEntries(VariableProps.class, FieldOption.DEFAULT); - - assertThat(entries.length, is(2)); - - - } - - - public PropertyDescriptor getProp(BeanInfo info , String name){ - PropertyDescriptor[] props = info.getPropertyDescriptors(); - for(int i = 0;i < props.length;i++){ - PropertyDescriptor d = props[i]; - if(d.getDisplayName().equalsIgnoreCase(name)){ - return d; - } - } - return null; - } - - - -} diff --git a/java/src/test/java/org/msgpack/template/BeansEquals.java b/java/src/test/java/org/msgpack/template/BeansEquals.java deleted file mode 100644 index e07a224d..00000000 --- a/java/src/test/java/org/msgpack/template/BeansEquals.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.msgpack.template; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.HashSet; - -import org.hamcrest.BaseMatcher; -import org.hamcrest.CoreMatchers; -import org.hamcrest.Description; -import org.junit.Assert; - -/** - * This matcher compares all get***() methods(except getClass) - * @author takeshita - * - */ -public class BeansEquals extends BaseMatcher{ - - Object expected; - - HashSet ignoreNames = new HashSet(); - - public BeansEquals(Object expected){ - this.expected = expected; - } - public BeansEquals(Object expected,String[] ignoreNames){ - this.expected = expected; - for(int i = 0;i < ignoreNames.length;i++){ - this.ignoreNames.add(ignoreNames[i]); - } - } - - static String errorMessage = "hoge"; - - @Override - public boolean matches(Object actual) { - if(expected == actual){ - return true; - } - if(!actual.getClass().equals(expected.getClass())){ - errorMessage = String.format("Expected class is %s but actual %s", - expected.getClass().getName(), - actual.getClass().getName()); - return false; - } - - for(Method m : expected.getClass().getMethods()){ - String n = m.getName(); - if(n.startsWith("get") && - !n.equals("getClass") && - !ignoreNames.contains(n)){ - - if(m.getParameterTypes().length == 0 && - !m.getReturnType().equals(void.class)){ - try { - Object exp = m.invoke(expected); - Object act = m.invoke(actual); - - Assert.assertThat("@" + n,act, CoreMatchers.is(exp)); - - } catch (Exception e) { - throw new RuntimeException(String.format( - "Exception occured while comparing %s",n), e); - } - - } - - } - - } - - return true; - } - - - @Override - public void describeTo(Description desc) { - - desc.appendText(errorMessage); - } - - -} diff --git a/java/src/test/java/org/msgpack/template/TestPackConvert.java b/java/src/test/java/org/msgpack/template/TestPackConvert.java deleted file mode 100644 index f69b0ce2..00000000 --- a/java/src/test/java/org/msgpack/template/TestPackConvert.java +++ /dev/null @@ -1,829 +0,0 @@ -package org.msgpack.template; - -import java.io.ByteArrayOutputStream; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Random; - -import org.junit.Test; -import org.msgpack.MessagePackObject; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Util; - -import junit.framework.TestCase; - -public class TestPackConvert extends TestCase { - - @Test - public void testInteger() throws Exception { - _testInteger(0); - _testInteger(-1); - _testInteger(1); - _testInteger(Integer.MIN_VALUE); - _testInteger(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testInteger(rand.nextInt()); - } - } - - static void _testInteger(Integer src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = IntegerTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - Integer dst = (Integer) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testNullInteger() throws Exception { - Integer src = null; - Template tmpl = IntegerTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - new Packer(out).pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(IntegerTemplate.getInstance()); - Integer dst = (Integer) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testLong() throws Exception { - _testLong((long) 0); - _testLong((long) -1); - _testLong((long) 1); - _testLong((long) Integer.MIN_VALUE); - _testLong((long) Integer.MAX_VALUE); - _testLong(Long.MIN_VALUE); - _testLong(Long.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testLong(rand.nextLong()); - } - } - - public void _testLong(Long src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = LongTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - Long dst = (Long) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testNullLong() throws Exception { - Long src = null; - Template tmpl = LongTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(LongTemplate.getInstance()); - Long dst = (Long) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testBiginteger() throws Exception { - _testBigInteger(BigInteger.valueOf(0)); - _testBigInteger(BigInteger.valueOf(-1)); - _testBigInteger(BigInteger.valueOf(1)); - _testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); - _testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); - _testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); - _testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); - BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); - _testBigInteger(max); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testBigInteger(max.subtract(BigInteger.valueOf(Math.abs(rand.nextLong())))); - } - } - - static void _testBigInteger(BigInteger src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = BigIntegerTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - BigInteger dst = (BigInteger) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testNullBigInteger() throws Exception { - Long src = null; - Template tmpl = BigIntegerTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(BigIntegerTemplate.getInstance()); - BigInteger dst = (BigInteger) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testFloat() throws Exception { - _testFloat((float) 0.0); - _testFloat((float) -0.0); - _testFloat((float) 1.0); - _testFloat((float) -1.0); - _testFloat((float) Float.MAX_VALUE); - _testFloat((float) Float.MIN_VALUE); - _testFloat((float) Float.NaN); - _testFloat((float) Float.NEGATIVE_INFINITY); - _testFloat((float) Float.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testFloat(rand.nextFloat()); - } - } - - static void _testFloat(Float src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = FloatTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - Float dst = (Float) tmpl.convert(obj, null); - assertEquals(src, dst, 10e-10); - } - - @Test - public void testNullFloat() throws Exception { - Long src = null; - Template tmpl = FloatTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(FloatTemplate.getInstance()); - Float dst = (Float) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testDouble() throws Exception { - _testDouble((double) 0.0); - _testDouble((double) -0.0); - _testDouble((double) 1.0); - _testDouble((double) -1.0); - _testDouble((double) Double.MAX_VALUE); - _testDouble((double) Double.MIN_VALUE); - _testDouble((double) Double.NaN); - _testDouble((double) Double.NEGATIVE_INFINITY); - _testDouble((double) Double.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testDouble(rand.nextDouble()); - } - } - - static void _testDouble(Double src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = DoubleTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - Double dst = (Double) tmpl.convert(obj, null); - assertEquals(src, dst, 10e-10); - } - - @Test - public void testNullDouble() throws Exception { - Long src = null; - Template tmpl = DoubleTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(DoubleTemplate.getInstance()); - Double dst = (Double) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testBoolean() throws Exception { - _testBoolean(false); - _testBoolean(true); - } - - static void _testBoolean(Boolean src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = BooleanTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - Boolean dst = (Boolean) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testNullBoolean() throws Exception { - Long src = null; - Template tmpl = BooleanTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(BooleanTemplate.getInstance()); - Boolean dst = (Boolean) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testByteArray() throws Exception { - Random rand = new Random(System.currentTimeMillis()); - byte[] b0 = new byte[0]; - _testByteArray(b0); - byte[] b1 = new byte[10]; - rand.nextBytes(b1); - _testByteArray(b1); - byte[] b2 = new byte[1024]; - rand.nextBytes(b2); - _testByteArray(b2); - } - - static void _testByteArray(byte[] src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = ByteArrayTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - byte[] dst = (byte[]) tmpl.convert(obj, null); - assertEquals(src.length, dst.length); - for (int i = 0; i < src.length; ++i) { - assertEquals(src[i], dst[i]); - } - } - - @Test - public void testNullByteArray() throws Exception { - byte[] src = null; - Template tmpl = ByteArrayTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - tmpl = new NullableTemplate(ByteArrayTemplate.getInstance()); - obj = Util.unpackOne(out.toByteArray()); - byte[] dst = (byte[]) tmpl.convert(obj, null); - assertEquals(null, dst); - } - - @Test - public void testByteBuffer() throws Exception {// FIXME - Random rand = new Random(System.currentTimeMillis()); - byte[] b0 = new byte[0]; - ByteBuffer bb0 = ByteBuffer.wrap(b0); - _testByteBuffer(bb0); - bb0.clear(); - byte[] b1 = new byte[10]; - rand.nextBytes(b1); - ByteBuffer bb1 = ByteBuffer.wrap(b1); - _testByteBuffer(bb1); - bb1.clear(); - byte[] b2 = new byte[2048]; - rand.nextBytes(b2); - ByteBuffer bb2 = ByteBuffer.wrap(b2); - _testByteBuffer(bb2); - bb2.clear(); - } - - static void _testByteBuffer(ByteBuffer src) throws Exception { - Template tmpl = ByteBufferTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - ByteBuffer dst = (ByteBuffer) tmpl.convert(obj, null); - assertEquals(src.limit() - src.position(), dst.limit() - dst.position()); - int dst_pos = dst.position(); - for (int i = src.position(); i < src.limit(); ++i) { - assertEquals(src.get(i), dst.get(dst_pos)); - dst_pos++; - } - } - - @Test - public void testNullByteBuffer() throws Exception { - ByteBuffer src = null; - Template tmpl = ByteBufferTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(BooleanTemplate.getInstance()); - ByteBuffer dst = (ByteBuffer) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testString() throws Exception { - _testString(""); - _testString("a"); - _testString("ab"); - _testString("abc"); - - // small size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int) Math.random() % 31 + 1; - for (int j = 0; j < len; j++) { - sb.append('a' + ((int) Math.random()) & 26); - } - _testString(sb.toString()); - } - - // medium size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int) Math.random() % 100 + (1 << 15); - for (int j = 0; j < len; j++) { - sb.append('a' + ((int) Math.random()) & 26); - } - _testString(sb.toString()); - } - - // large size string - for (int i = 0; i < 10; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int) Math.random() % 100 + (1 << 31); - for (int j = 0; j < len; j++) { - sb.append('a' + ((int) Math.random()) & 26); - } - _testString(sb.toString()); - } - } - - static void _testString(String src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = StringTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - String dst = (String) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testNullString() throws Exception { - String src = null; - Template tmpl = StringTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(StringTemplate.getInstance()); - String dst = (String) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @SuppressWarnings("unchecked") - @Test - public void testList() throws Exception { - List src = new ArrayList(); - Template tmpl = new ListTemplate(IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - List dst = (List) tmpl.convert(obj, null); - assertEquals(src.size(), dst.size()); - Integer[] src_array = src.toArray(new Integer[0]); - Integer[] dst_array = dst.toArray(new Integer[0]); - for (int i = 0; i < src_array.length; ++i) { - assertEquals(src_array[i], dst_array[i]); - } - src.clear(); - } - - // otherwise - { - int len = (int) (Math.random() * 1000); - for (int i = 0; i < len; i++) { - src.add(i); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - List dst = (List) tmpl.convert(obj, null); - assertEquals(src.size(), dst.size()); - Integer[] src_array = src.toArray(new Integer[0]); - Integer[] dst_array = dst.toArray(new Integer[0]); - for (int i = 0; i < src_array.length; ++i) { - assertEquals(src_array[i], dst_array[i]); - } - src.clear(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testNullList() throws Exception { - List src = null; - Template tmpl = new ListTemplate(StringTemplate.getInstance()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(new ListTemplate(StringTemplate.getInstance())); - List dst = (List) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @SuppressWarnings("unchecked") - @Test - public void testMap() throws Exception { - Map src = new HashMap(); - Template tmpl = new MapTemplate( - IntegerTemplate.getInstance(), - IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - Map dst = (Map) tmpl.convert(obj, null); - assertEquals(src.size(), src.size()); - for (Map.Entry pair : dst.entrySet()) { - Integer val = src.get(pair.getKey()); - assertNotNull(val); - assertEquals(val, pair.getValue()); - } - src.clear(); - } - - // otherwise - { - int len = (int) (Math.random() * 1000); - for (int j = 0; j < len; j++) { - src.put(j, j); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - Map dst = (Map) tmpl.convert(obj, null); - assertEquals(src.size(), dst.size()); - for (Map.Entry pair : dst.entrySet()) { - Integer val = src.get(pair.getKey()); - assertNotNull(val); - assertEquals(val, pair.getValue()); - } - } - } - - @SuppressWarnings("unchecked") - @Test - public void testNullMap() throws Exception { - Map src = null; - Template tmpl = new MapTemplate( - StringTemplate.getInstance(), - StringTemplate.getInstance()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(new MapTemplate( - StringTemplate.getInstance(), - StringTemplate.getInstance())); - Map dst = (Map) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @SuppressWarnings("unchecked") - @Test - public void testCollectionLinkedList() throws Exception { - LinkedList src = new LinkedList(); - Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - LinkedList dst = (LinkedList) - tmpl.convert(obj, new LinkedList()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - src.clear(); - } - - // otherwise - { - int len = (int) Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) { - src.add(j); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - LinkedList dst = (LinkedList) - tmpl.convert(obj, new LinkedList()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - for (int j = 0; j < len; j++) { - assertEquals(src.get(j), dst.get(j)); - } - src.clear(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testCollectionHashSet() throws Exception { - HashSet src = new HashSet(); - Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - HashSet dst = (HashSet) - tmpl.convert(obj, new HashSet()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - src.clear(); - } - - // otherwise - { - int len = (int) Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) { - src.add(j); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - HashSet dst = (HashSet) - tmpl.convert(obj, new HashSet()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - Integer[] src_array = src.toArray(new Integer[0]); - Integer[] dst_array = dst.toArray(new Integer[0]); - for (int j = 0; j < len; j++) { - assertEquals(src_array[j], dst_array[j]); - } - src.clear(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testNullCollection() throws Exception { - Collection src = null; - Template tmpl = new CollectionTemplate(StringTemplate.getInstance()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Throwable t) { - assertTrue(t instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Throwable t) { - assertTrue(t instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(new CollectionTemplate(StringTemplate.getInstance())); - Collection dst = (Collection) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testBigDecimal() throws Exception { - // String - _testBigDecimal(new BigDecimal("0")); - _testBigDecimal(new BigDecimal("-0")); - _testBigDecimal(new BigDecimal("1")); - _testBigDecimal(new BigDecimal("-1")); - _testBigDecimal(new BigDecimal("123.456")); - _testBigDecimal(new BigDecimal("-123.456")); - _testBigDecimal(new BigDecimal("0.123456789")); - _testBigDecimal(new BigDecimal("-0.123456789")); - - // char array - char[] zero = {'0'}; - _testBigDecimal(new BigDecimal(zero)); - char[] one = {'1'}; - _testBigDecimal(new BigDecimal(one)); - char[] minusOne = {'-', '1'}; - _testBigDecimal(new BigDecimal(minusOne)); - char[] decimal = {'1', '2', '3', '.', '4', '5', '6'}; - _testBigDecimal(new BigDecimal(decimal)); - char[] minusDecimal = {'-', '1', '2', '3', '.', '4', '5', '6'}; - _testBigDecimal(new BigDecimal(minusDecimal)); - char[] oneOrLessDecimal = {'0', '.', '1', '2', '3'}; - _testBigDecimal(new BigDecimal(oneOrLessDecimal)); - char[] minusOneOrLessDecimal = {'-', '0', '.', '1', '2', '3'}; - _testBigDecimal(new BigDecimal(minusOneOrLessDecimal)); - - // int - _testBigDecimal(new BigDecimal(0)); - _testBigDecimal(new BigDecimal(-0)); - _testBigDecimal(new BigDecimal(1)); - _testBigDecimal(new BigDecimal(-1)); - _testBigDecimal(new BigDecimal(Integer.MAX_VALUE)); - _testBigDecimal(new BigDecimal(Integer.MIN_VALUE)); - - // double - _testBigDecimal(new BigDecimal((double) 0.0)); - _testBigDecimal(new BigDecimal((double) -0.0)); - _testBigDecimal(new BigDecimal((double) 1.0)); - _testBigDecimal(new BigDecimal((double) -1.0)); - _testBigDecimal(new BigDecimal((double) 123.456)); - _testBigDecimal(new BigDecimal((double) -123.456)); - _testBigDecimal(new BigDecimal((double) 0.123456789)); - _testBigDecimal(new BigDecimal((double) -0.123456789)); - _testBigDecimal(new BigDecimal(Double.MAX_VALUE)); - _testBigDecimal(new BigDecimal(Double.MIN_VALUE)); - } - - static void _testBigDecimal(BigDecimal src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = BigDecimalTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - BigDecimal dst = (BigDecimal) tmpl.convert(obj, null); - assertEquals(src, dst); - } - - @Test - public void testNullBigDecimal() throws Exception { - BigDecimal src = null; - Template tmpl = BigDecimalTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - MessagePackObject obj = Util.unpackOne(out.toByteArray()); - try { - tmpl.convert(obj, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - obj = Util.unpackOne(out.toByteArray()); - tmpl = new NullableTemplate(BigDecimalTemplate.getInstance()); - BigDecimal dst = (BigDecimal) tmpl.convert(obj, null); - assertEquals(src, dst); - } -} diff --git a/java/src/test/java/org/msgpack/template/TestPackUnpack.java b/java/src/test/java/org/msgpack/template/TestPackUnpack.java deleted file mode 100644 index 0855d986..00000000 --- a/java/src/test/java/org/msgpack/template/TestPackUnpack.java +++ /dev/null @@ -1,858 +0,0 @@ -package org.msgpack.template; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Random; - -import org.junit.Test; -import org.msgpack.MessageTypeException; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; - -import junit.framework.TestCase; - -public class TestPackUnpack extends TestCase { - @Test - public void testInteger() throws Exception { - _testInteger(0); - _testInteger(-1); - _testInteger(1); - _testInteger(Integer.MIN_VALUE); - _testInteger(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testInteger(rand.nextInt()); - } - } - - static void _testInteger(Integer src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = IntegerTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Integer dst = (Integer) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst); - } - - @Test - public void testNullInteger() throws Exception { - Template tmpl = IntegerTemplate.getInstance(); - Integer src = null; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(IntegerTemplate.getInstance()); - Integer dst = (Integer) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @Test - public void testLong() throws Exception { - _testLong((long) 0); - _testLong((long) -1); - _testLong((long) 1); - _testLong((long) Integer.MIN_VALUE); - _testLong((long) Integer.MAX_VALUE); - _testLong(Long.MIN_VALUE); - _testLong(Long.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testLong(rand.nextLong()); - } - } - - static void _testLong(Long src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = LongTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Long dst = (Long) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst); - } - - @Test - public void testNullLong() throws Exception { - Long src = null; - Template tmpl = LongTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(LongTemplate.getInstance()); - Long dst = (Long) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @Test - public void testBigInteger() throws Exception { - _testBigInteger(BigInteger.valueOf(0)); - _testBigInteger(BigInteger.valueOf(-1)); - _testBigInteger(BigInteger.valueOf(1)); - _testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); - _testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); - _testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); - _testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); - BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); - _testBigInteger(max); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testBigInteger(max.subtract(BigInteger.valueOf(Math.abs(rand - .nextLong())))); - } - } - - static void _testBigInteger(BigInteger src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = BigIntegerTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - BigInteger dst = (BigInteger) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst); - } - - @Test - public void testNullBigInteger() throws Exception { - BigInteger src = null; - Template tmpl = BigIntegerTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(BigIntegerTemplate.getInstance()); - BigInteger dst = (BigInteger) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @Test - public void testFloat() throws Exception { - _testFloat((float) 0.0); - _testFloat((float) -0.0); - _testFloat((float) 1.0); - _testFloat((float) -1.0); - _testFloat((float) Float.MAX_VALUE); - _testFloat((float) Float.MIN_VALUE); - _testFloat((float) Float.NaN); - _testFloat((float) Float.NEGATIVE_INFINITY); - _testFloat((float) Float.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testFloat(rand.nextFloat()); - } - } - - static void _testFloat(Float src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = FloatTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Float dst = (Float) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst, 10e-10); - } - - @Test - public void testNullFloat() throws Exception { - Double src = null; - Template tmpl = FloatTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(FloatTemplate.getInstance()); - Float dst = (Float) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @Test - public void testDouble() throws Exception { - _testDouble((double) 0.0); - _testDouble((double) -0.0); - _testDouble((double) 1.0); - _testDouble((double) -1.0); - _testDouble((double) Double.MAX_VALUE); - _testDouble((double) Double.MIN_VALUE); - _testDouble((double) Double.NaN); - _testDouble((double) Double.NEGATIVE_INFINITY); - _testDouble((double) Double.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) { - _testDouble(rand.nextDouble()); - } - } - - static void _testDouble(Double src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = DoubleTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Double dst = (Double) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst, 10e-10); - } - - @Test - public void testNullDouble() throws Exception { - Double src = null; - Template tmpl = DoubleTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(DoubleTemplate.getInstance()); - Double dst = (Double) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @Test - public void testBoolean() throws Exception { - _testBoolean(false); - _testBoolean(true); - } - - static void _testBoolean(Boolean src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = BooleanTemplate.getInstance(); - Packer packer = new Packer(out); - tmpl.pack(packer, src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Boolean dst = (Boolean) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst); - } - - @Test - public void testNullBoolean() throws Exception { - Boolean src = null; - Template tmpl = BooleanTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(BooleanTemplate.getInstance()); - Boolean dst = (Boolean) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @Test - public void testByteArray() throws Exception { - Random rand = new Random(System.currentTimeMillis()); - byte[] b0 = new byte[0]; - _testByteArray(b0); - byte[] b1 = new byte[10]; - rand.nextBytes(b1); - _testByteArray(b1); - byte[] b2 = new byte[1024]; - rand.nextBytes(b2); - _testByteArray(b2); - } - - static void _testByteArray(byte[] src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = ByteArrayTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - byte[] dst = (byte[]) tmpl.unpack(new Unpacker(in), null); - assertEquals(src.length, dst.length); - for (int i = 0; i < src.length; ++i) { - assertEquals(src[i], dst[i]); - } - } - - @Test - public void testNullByteArray() throws Exception { - byte[] src = null; - Template tmpl = ByteArrayTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(BooleanTemplate.getInstance()); - byte[] dst = (byte[]) tmpl.unpack(unpacker, null); - assertEquals(null, dst); - } - - @Test - public void testByteBuffer() throws Exception { - Random rand = new Random(System.currentTimeMillis()); - byte[] b0 = new byte[0]; - ByteBuffer bb0 = ByteBuffer.wrap(b0); - _testByteBuffer(bb0); - bb0.clear(); - byte[] b1 = new byte[10]; - rand.nextBytes(b1); - ByteBuffer bb1 = ByteBuffer.wrap(b1); - _testByteBuffer(bb1); - bb1.clear(); - byte[] b2 = new byte[2048]; - rand.nextBytes(b2); - ByteBuffer bb2 = ByteBuffer.wrap(b2); - _testByteBuffer(bb2); - bb2.clear(); - } - - static void _testByteBuffer(ByteBuffer src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = ByteBufferTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - ByteBuffer dst = (ByteBuffer) tmpl.unpack(new Unpacker(in), null); - assertEquals(src.limit() - src.position(), dst.limit() - dst.position()); - int dst_pos = dst.position(); - for (int i = src.position(); i < src.limit(); ++i) { - assertEquals(src.get(i), dst.get(dst_pos)); - dst_pos++; - } - } - - @Test - public void testNullByteBuffer() throws Exception { - ByteBuffer src = null; - Template tmpl = ByteBufferTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(BooleanTemplate.getInstance()); - ByteBuffer dst = (ByteBuffer) tmpl.unpack(unpacker, null); - assertEquals(null, dst); - } - - @Test - public void testString() throws Exception { - _testString(""); - _testString("a"); - _testString("ab"); - _testString("abc"); - - // small size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int) Math.random() % 31 + 1; - for (int j = 0; j < len; j++) { - sb.append('a' + ((int) Math.random()) & 26); - } - _testString(sb.toString()); - } - - // medium size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int) Math.random() % 100 + (1 << 15); - for (int j = 0; j < len; j++) { - sb.append('a' + ((int) Math.random()) & 26); - } - _testString(sb.toString()); - } - - // large size string - for (int i = 0; i < 10; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int) Math.random() % 100 + (1 << 31); - for (int j = 0; j < len; j++) { - sb.append('a' + ((int) Math.random()) & 26); - } - _testString(sb.toString()); - } - } - - static void _testString(String src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = StringTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - String dst = (String) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst); - } - - @Test - public void testNullString() throws Exception { - String src = null; - Template tmpl = StringTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(StringTemplate.getInstance()); - String dst = (String) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @SuppressWarnings("unchecked") - @Test - public void testList() throws Exception { - List src = new ArrayList(); - Template tmpl = new ListTemplate(IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - List dst = (List) tmpl.unpack(new Unpacker(in), null); - assertEquals(src.size(), dst.size()); - Integer[] src_array = src.toArray(new Integer[0]); - Integer[] dst_array = dst.toArray(new Integer[0]); - for (int i = 0; i < src_array.length; ++i) { - assertEquals(src_array[i], dst_array[i]); - } - src.clear(); - } - - // otherwise - { - int len = (int) (Math.random() * 1000); - for (int i = 0; i < len; i++) { - src.add(i); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - List dst = (List) tmpl.unpack(new Unpacker(in), null); - assertEquals(src.size(), dst.size()); - Integer[] src_array = src.toArray(new Integer[0]); - Integer[] dst_array = dst.toArray(new Integer[0]); - for (int i = 0; i < src_array.length; ++i) { - assertEquals(src_array[i], dst_array[i]); - } - src.clear(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testNullList() throws Exception { - List src = null; - Template tmpl = new ListTemplate(StringTemplate.getInstance()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(new ListTemplate(StringTemplate.getInstance())); - List dst = (List) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @SuppressWarnings("unchecked") - @Test - public void testMap() throws Exception { - Map src = new HashMap(); - Template tmpl = new MapTemplate( - IntegerTemplate.getInstance(), - IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Map dst = (Map) - tmpl.unpack(new Unpacker(in), null); - assertEquals(src.size(), src.size()); - for (Map.Entry pair : dst.entrySet()) { - Integer val = src.get(pair.getKey()); - assertNotNull(val); - assertEquals(val, pair.getValue()); - } - src.clear(); - } - - // otherwise - { - int len = (int) (Math.random() * 1000); - for (int j = 0; j < len; j++) { - src.put(j, j); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Map dst = (Map) - tmpl.unpack(new Unpacker(in), null); - assertEquals(src.size(), dst.size()); - for (Map.Entry pair : dst.entrySet()) { - Integer val = src.get(pair.getKey()); - assertNotNull(val); - assertEquals(val, pair.getValue()); - } - } - } - - @SuppressWarnings("unchecked") - @Test - public void testNullMap() throws Exception { - Map src = null; - Template tmpl = new MapTemplate( - StringTemplate.getInstance(), - StringTemplate.getInstance()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(new MapTemplate(StringTemplate - .getInstance(), StringTemplate.getInstance())); - Map dst = (Map) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @SuppressWarnings("unchecked") - @Test - public void testCollectionLinkedList() throws Exception { - LinkedList src = new LinkedList(); - Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - LinkedList dst = (LinkedList) - tmpl.unpack(new Unpacker(in), new LinkedList()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - src.clear(); - } - - // otherwise - { - int len = (int) Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) { - src.add(j); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - LinkedList dst = (LinkedList) - tmpl.unpack(new Unpacker(in), new LinkedList()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - for (int j = 0; j < len; j++) { - assertEquals(src.get(j), dst.get(j)); - } - src.clear(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testCollectionHashSet() throws Exception { - HashSet src = new HashSet(); - Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance()); - // size is zero - { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - HashSet dst = (HashSet) - tmpl.unpack(new Unpacker(in), new HashSet()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - src.clear(); - } - - // otherwise - { - int len = (int) Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) { - src.add(j); - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - HashSet dst = (HashSet) - tmpl.unpack(new Unpacker(in), new HashSet()); - assertEquals(src.getClass(), dst.getClass()); - assertEquals(src.size(), dst.size()); - Integer[] src_array = src.toArray(new Integer[0]); - Integer[] dst_array = dst.toArray(new Integer[0]); - for (int j = 0; j < len; j++) { - assertEquals(src_array[j], dst_array[j]); - } - src.clear(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testNullCollection() throws Exception { - Collection src = null; - Template tmpl = new CollectionTemplate(StringTemplate.getInstance()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Throwable t) { - assertTrue(t instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - unpacker.wrap(bytes); - try { - tmpl.unpack(unpacker, null); - fail(); - } catch (Throwable t) { - assertTrue(t instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(new CollectionTemplate(StringTemplate.getInstance())); - Collection dst = (Collection) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } - - @Test - public void testBigDecimal() throws Exception { - // String - _testBigDecimal(new BigDecimal("0")); - _testBigDecimal(new BigDecimal("-0")); - _testBigDecimal(new BigDecimal("1")); - _testBigDecimal(new BigDecimal("-1")); - _testBigDecimal(new BigDecimal("123.456")); - _testBigDecimal(new BigDecimal("-123.456")); - _testBigDecimal(new BigDecimal("0.123456789")); - _testBigDecimal(new BigDecimal("-0.123456789")); - - // char array - char[] zero = {'0'}; - _testBigDecimal(new BigDecimal(zero)); - char[] one = {'1'}; - _testBigDecimal(new BigDecimal(one)); - char[] minusOne = {'-', '1'}; - _testBigDecimal(new BigDecimal(minusOne)); - char[] decimal = {'1', '2', '3', '.', '4', '5', '6'}; - _testBigDecimal(new BigDecimal(decimal)); - char[] minusDecimal = {'-', '1', '2', '3', '.', '4', '5', '6'}; - _testBigDecimal(new BigDecimal(minusDecimal)); - char[] oneOrLessDecimal = {'0', '.', '1', '2', '3'}; - _testBigDecimal(new BigDecimal(oneOrLessDecimal)); - char[] minusOneOrLessDecimal = {'-', '0', '.', '1', '2', '3'}; - _testBigDecimal(new BigDecimal(minusOneOrLessDecimal)); - - // int - _testBigDecimal(new BigDecimal(0)); - _testBigDecimal(new BigDecimal(-0)); - _testBigDecimal(new BigDecimal(1)); - _testBigDecimal(new BigDecimal(-1)); - _testBigDecimal(new BigDecimal(Integer.MAX_VALUE)); - _testBigDecimal(new BigDecimal(Integer.MIN_VALUE)); - - // double - _testBigDecimal(new BigDecimal((double) 0.0)); - _testBigDecimal(new BigDecimal((double) -0.0)); - _testBigDecimal(new BigDecimal((double) 1.0)); - _testBigDecimal(new BigDecimal((double) -1.0)); - _testBigDecimal(new BigDecimal((double) 123.456)); - _testBigDecimal(new BigDecimal((double) -123.456)); - _testBigDecimal(new BigDecimal((double) 0.123456789)); - _testBigDecimal(new BigDecimal((double) -0.123456789)); - _testBigDecimal(new BigDecimal(Double.MAX_VALUE)); - _testBigDecimal(new BigDecimal(Double.MIN_VALUE)); - } - - static void _testBigDecimal(BigDecimal src) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Template tmpl = BigDecimalTemplate.getInstance(); - tmpl.pack(new Packer(out), src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - BigDecimal dst = (BigDecimal) tmpl.unpack(new Unpacker(in), null); - assertEquals(src, dst); - } - - @Test - public void testNullBigDecimal() throws Exception { - BigDecimal src = null; - Template tmpl = BigDecimalTemplate.getInstance(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Packer packer = new Packer(out); - try { - tmpl.pack(packer, src); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - packer.pack(src); - byte[] bytes = out.toByteArray(); - Unpacker unpacker = new Unpacker(); - try { - unpacker.wrap(bytes); - tmpl.unpack(unpacker, null); - fail(); - } catch (Exception e) { - assertTrue(e instanceof MessageTypeException); - } - unpacker.wrap(bytes); - tmpl = new NullableTemplate(BigDecimalTemplate.getInstance()); - BigDecimal dst = (BigDecimal) tmpl.unpack(unpacker, null); - assertEquals(src, dst); - } -} diff --git a/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderJavaBeansPackUnpack.java b/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderJavaBeansPackUnpack.java deleted file mode 100644 index e894ad65..00000000 --- a/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderJavaBeansPackUnpack.java +++ /dev/null @@ -1,2200 +0,0 @@ -package org.msgpack.template; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import org.hamcrest.CoreMatchers; -import org.hamcrest.Matcher; -import org.junit.Ignore; -import org.junit.Test; - -import org.msgpack.MessagePack; -import org.msgpack.MessagePackable; -import org.msgpack.MessagePacker; -import org.msgpack.MessageTypeException; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.annotation.MessagePackBeans; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.MessagePackOrdinalEnum; -import org.msgpack.annotation.Optional; -import org.msgpack.template.TestTemplateBuilderPackConvert.SampleInterface; -import org.msgpack.template.builder.BeansTemplateBuilder; -import org.msgpack.template.builder.BuilderSelectorRegistry; -import org.msgpack.template.builder.BeansTemplateBuilderSelector; -import org.msgpack.template.builder.AnnotationTemplateBuilderSelector; -import org.msgpack.template.builder.ReflectionTemplateBuilder; -import org.msgpack.template.builder.TemplateBuilder; - -import org.junit.Assert; -import junit.framework.TestCase; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.assertThat; - -public class TestReflectionTemplateBuilderJavaBeansPackUnpack extends TestCase { - static { - //Replace template selectors from javassist to reflection. - BuilderSelectorRegistry instance = BuilderSelectorRegistry.getInstance(); - - instance.replace( - new AnnotationTemplateBuilderSelector( - new ReflectionTemplateBuilder())); - instance.setForceBuilder( new ReflectionTemplateBuilder()); - instance.replace(new BeansTemplateBuilderSelector( - new BeansTemplateBuilder())); - - MessagePack.register(PrimitiveTypeFieldsClass.class); - MessagePack.register(OptionalPrimitiveTypeFieldsClass.class); - MessagePack.register(GeneralReferenceTypeFieldsClass.class); - MessagePack.register(GeneralOptionalReferenceTypeFieldsClass.class); - MessagePack.register(SampleListTypes.class); - MessagePack.register(SampleOptionalListTypes.class); - MessagePack.register(SampleMapTypes.class); - MessagePack.register(SampleOptionalMapTypes.class); - MessagePack.register(SampleEnumFieldClass.class); - MessagePack.register(SampleOptionalEnumFieldClass.class); - MessagePack.register(FieldModifiersClass.class); - MessagePack.register(OptionalFieldModifiersClass.class); - MessagePack.register(BaseClass.class); - MessagePack.register(NestedClass.class); - MessagePack.register(BaseClass2.class); - MessagePack.register(OptionalBaseClass.class); - MessagePack.register(OptionalNestedClass.class); - MessagePack.register(OptionalBaseClass2.class); - MessagePack.register(SampleSubClass.class); - MessagePack.register(SampleSuperClass.class); - MessagePack.register(SampleOptionalSubClass.class); - MessagePack.register(SampleOptionalSuperClass.class); - MessagePack.register(BaseMessagePackableUnpackableClass.class); - MessagePack.register(MessagePackableUnpackableClass.class); - MessagePack.register(OptionalBaseMessagePackableUnpackableClass.class); - MessagePack.register(OptionalMessagePackableUnpackableClass.class); - } - - Matcher beansEquals(Object actual){ - return new BeansEquals(actual); - } - Matcher beansEquals(Object actual,String[] ignoreNames){ - return new BeansEquals(actual,ignoreNames); - } - String[] ignoring(String ... strings){ - return strings; - } - - @Test - public void testPrimitiveTypeFields00() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - - - assertThat(dst,beansEquals(src)); - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - - assertThat(dst,beansEquals(src)); - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - PrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - - assertThat(dst,beansEquals(src)); - } - - @MessagePackBeans - public static class PrimitiveTypeFieldsClass { - byte f0; - short f1; - int f2; - long f3; - float f4; - double f5; - boolean f6; - - public byte getF0() { - return f0; - } - - public void setF0(byte f0) { - this.f0 = f0; - } - - public short getF1() { - return f1; - } - - public void setF1(short f1) { - this.f1 = f1; - } - - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public long getF3() { - return f3; - } - - public void setF3(long f3) { - this.f3 = f3; - } - - public float getF4() { - return f4; - } - - public void setF4(float f4) { - this.f4 = f4; - } - - public double getF5() { - return f5; - } - - public void setF5(double f5) { - this.f5 = f5; - } - - public boolean isF6() { - return f6; - } - - public void setF6(boolean f6) { - this.f6 = f6; - } - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - - - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - - - assertThat(dst,beansEquals(src)); - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - OptionalPrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - @Optional - public byte getF0() { - return f0; - } - - @Optional - public void setF0(byte f0) { - this.f0 = f0; - } - - @Optional - public short getF1() { - return f1; - } - - public void setF1(short f1) { - this.f1 = f1; - } - - public int getF2() { - return f2; - } - - @Optional - public void setF2(int f2) { - this.f2 = f2; - } - - @Optional - public long getF3() { - return f3; - } - - public void setF3(long f3) { - this.f3 = f3; - } - - @Optional - public float getF4() { - return f4; - } - - public void setF4(float f4) { - this.f4 = f4; - } - - @Optional - public double getF5() { - return f5; - } - - public void setF5(double f5) { - this.f5 = f5; - } - - @Optional - public boolean isF6() { - return f6; - } - - public void setF6(boolean f6) { - this.f6 = f6; - } - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - - - assertThat(dst,beansEquals(src)); - - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public Byte getF0() { - return f0; - } - - public void setF0(Byte f0) { - this.f0 = f0; - } - - public Short getF1() { - return f1; - } - - public void setF1(Short f1) { - this.f1 = f1; - } - - public Integer getF2() { - return f2; - } - - public void setF2(Integer f2) { - this.f2 = f2; - } - - public Long getF3() { - return f3; - } - - public void setF3(Long f3) { - this.f3 = f3; - } - - public Float getF4() { - return f4; - } - - public void setF4(Float f4) { - this.f4 = f4; - } - - public Double getF5() { - return f5; - } - - public void setF5(Double f5) { - this.f5 = f5; - } - - public Boolean getF6() { - return f6; - } - - public void setF6(Boolean f6) { - this.f6 = f6; - } - - public BigInteger getF7() { - return f7; - } - - public void setF7(BigInteger f7) { - this.f7 = f7; - } - - public String getF8() { - return f8; - } - - public void setF8(String f8) { - this.f8 = f8; - } - - public byte[] getF9() { - return f9; - } - - public void setF9(byte[] f9) { - this.f9 = f9; - } - - public ByteBuffer getF10() { - return f10; - } - - public void setF10(ByteBuffer f10) { - this.f10 = f10; - } - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - - - assertThat(dst,beansEquals(src)); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - - - assertThat(dst,beansEquals(src)); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - @Optional - public Byte getF0() { - return f0; - } - - @Optional - public void setF0(Byte f0) { - this.f0 = f0; - } - - @Optional - public Short getF1() { - return f1; - } - - public void setF1(Short f1) { - this.f1 = f1; - } - - public Integer getF2() { - return f2; - } - - @Optional - public void setF2(Integer f2) { - this.f2 = f2; - } - - @Optional - public Long getF3() { - return f3; - } - - public void setF3(Long f3) { - this.f3 = f3; - } - - @Optional - public Float getF4() { - return f4; - } - - public void setF4(Float f4) { - this.f4 = f4; - } - - @Optional - public Double getF5() { - return f5; - } - - public void setF5(Double f5) { - this.f5 = f5; - } - - @Optional - public Boolean getF6() { - return f6; - } - - public void setF6(Boolean f6) { - this.f6 = f6; - } - - @Optional - public BigInteger getF7() { - return f7; - } - - public void setF7(BigInteger f7) { - this.f7 = f7; - } - - @Optional - public String getF8() { - return f8; - } - - public void setF8(String f8) { - this.f8 = f8; - } - - @Optional - public byte[] getF9() { - return f9; - } - - public void setF9(byte[] f9) { - this.f9 = f9; - } - - @Optional - public ByteBuffer getF10() { - return f10; - } - - public void setF10(ByteBuffer f10) { - this.f10 = f10; - } - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - SampleListTypes src = new SampleListTypes(); - src.integerListSize0 = new ArrayList(); - src.integerList = new ArrayList(); - src.integerList.add(1); - src.integerList.add(2); - src.integerList.add(3); - src.stringList = new ArrayList(); - src.stringList.add("e1"); - src.stringList.add("e2"); - src.stringList.add("e3"); - src.stringListList = new ArrayList>(); - src.stringListList.add(src.stringList); - src.sampleListNestedTypeList = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.sampleListNestedTypeList.add(slnt); - src.byteBufferList = new ArrayList(); - src.byteBufferList.add(ByteBuffer.wrap("e1".getBytes())); - src.byteBufferList.add(ByteBuffer.wrap("e2".getBytes())); - src.byteBufferList.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - - //ignore sampleListNestedTypeList, - //because SampleListNestedType is not implemented equals() correctly. - assertThat(dst,beansEquals(src, ignoring("getF4"))); - - assertEquals(src.sampleListNestedTypeList.size(), dst.sampleListNestedTypeList.size()); - for (int i = 0; i < src.sampleListNestedTypeList.size(); ++i) { - SampleListNestedType s = src.sampleListNestedTypeList.get(i); - SampleListNestedType d = dst.sampleListNestedTypeList.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - } - - @Test - public void testListTypes01() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleListTypes { - public List integerListSize0; - public List integerList; - public List stringList; - public List> stringListList; - public List sampleListNestedTypeList; - public List byteBufferList; - - public List getF0() { - return integerListSize0; - } - - public void setF0(List f0) { - this.integerListSize0 = f0; - } - - public List getF1() { - return integerList; - } - - public void setF1(List f1) { - this.integerList = f1; - } - - public List getF2() { - return stringList; - } - - public void setF2(List f2) { - this.stringList = f2; - } - - public List> getF3() { - return stringListList; - } - - public void setF3(List> f3) { - this.stringListList = f3; - } - - public List getF4() { - return sampleListNestedTypeList; - } - - public void setF4(List f4) { - this.sampleListNestedTypeList = f4; - } - - public List getF5() { - return byteBufferList; - } - - public void setF5(List f5) { - this.byteBufferList = f5; - } - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } - - @Test - public void testOptionalListTypes02() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - @Optional - public List getF0() { - return f0; - } - - @Optional - public void setF0(List f0) { - this.f0 = f0; - } - - @Optional - public List getF1() { - return f1; - } - - public void setF1(List f1) { - this.f1 = f1; - } - - public List getF2() { - return f2; - } - - @Optional - public void setF2(List f2) { - this.f2 = f2; - } - - @Optional - public List> getF3() { - return f3; - } - - public void setF3(List> f3) { - this.f3 = f3; - } - - @Optional - public List getF4() { - return f4; - } - - public void setF4(List f4) { - this.f4 = f4; - } - - @Optional - public List getF5() { - return f5; - } - - public void setF5(List f5) { - this.f5 = f5; - } - - public SampleOptionalListTypes() { - } - } - - @MessagePackBeans - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - @Optional - public byte[] getF0() { - return f0; - } - - public void setF0(byte[] f0) { - this.f0 = f0; - } - - @Optional - public String getF1() { - return f1; - } - - public void setF1(String f1) { - this.f1 = f1; - } - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testMapTypes01() throws Exception { - SampleMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public Map getF0() { - return f0; - } - - public void setF0(Map f0) { - this.f0 = f0; - } - - public Map getF1() { - return f1; - } - - public void setF1(Map f1) { - this.f1 = f1; - } - - public Map getF2() { - return f2; - } - - public void setF2(Map f2) { - this.f2 = f2; - } - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - Iterator dstf2 = dst.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } - - @Test - public void testOptionalMapTypes02() throws Exception { - SampleOptionalMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - @Optional - public Map getF0() { - return f0; - } - - public void setF0(Map f0) { - this.f0 = f0; - } - - @Optional - public Map getF1() { - return f1; - } - - public void setF1(Map f1) { - this.f1 = f1; - } - - @Optional - public Map getF2() { - return f2; - } - - public void setF2(Map f2) { - this.f2 = f2; - } - - public SampleOptionalMapTypes() { - } - } - - - @MessagePackBeans - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - @Test - public void testInterfaceType01() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - public interface SampleInterface { - } - - @Test - public void testEnumTypeForOrdinal00() throws Exception { - SampleEnumFieldClass src = new SampleEnumFieldClass(); - src.f0 = 23; - src.f1 = SampleEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - Assert.assertThat(dst.f0, is(src.f0)); - Assert.assertThat(dst.f1, is(src.f1)); - } - - @Test - public void testEnumTypeForOrdinal01() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - Assert.assertThat(dst,is(src)); - } - - @MessagePackBeans - public static class SampleEnumFieldClass { - public int f0; - - public SampleEnum f1; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public SampleEnum getF1() { - return f1; - } - - public void setF1(SampleEnum f1) { - this.f1 = f1; - } - - public SampleEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleEnum { - ONE, TWO, THREE; - } - - @Test - public void testOptionalEnumTypeForOrdinal00() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleOptionalEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal01() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal02() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalEnumFieldClass { - @Optional - public int f0; - - @Optional - public SampleOptionalEnum f1; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public SampleOptionalEnum getF1() { - return f1; - } - - public void setF1(SampleOptionalEnum f1) { - this.f1 = f1; - } - - public SampleOptionalEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleOptionalEnum { - ONE, TWO, THREE; - } - - @Test - public void testFieldModifiers() throws Exception { - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - FieldModifiersClass dst = - MessagePack.unpack(raw, FieldModifiersClass.class); - Assert.assertEquals(src.f1,dst.f1); - Assert.assertThat(dst.f2, is( not(src.f2))); - Assert.assertThat(dst.f3, is( not(src.f3))); - Assert.assertThat(dst.f4, is( not(src.f4))); - } - - @MessagePackBeans - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - private int getF2() { - return f2; - } - - private void setF2(int f2) { - this.f2 = f2; - } - - public int getF3() { - return f3; - } - - protected void setF3(int f3) { - this.f3 = f3; - } - - public int getF4() { - return f4; - } - - void setF4(int f4) { - this.f4 = f4; - } - - public int getF1() { - return f1; - } - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - OptionalFieldModifiersClass dst = - MessagePack.unpack(raw, OptionalFieldModifiersClass.class); - Assert.assertThat(dst.f0, is(src.f0)); - Assert.assertThat(dst.f1, is(src.f1)); - Assert.assertThat(dst.f2, is(not(src.f2))); - Assert.assertThat(dst.f3, is(not(src.f3))); - Assert.assertThat(dst.f4, is(not(src.f4))); - } - - @MessagePackBeans - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - private int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - protected int getF3() { - return f3; - } - - protected void setF3(int f3) { - this.f3 = f3; - } - - public int getF4() { - return f4; - } - - void setF4(int f4) { - this.f4 = f4; - } - - public int getF1() { - return f1; - } - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testNestedFieldClass01() throws Exception { - BaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class BaseClass { - public int f0; - public NestedClass f1; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public NestedClass getF1() { - return f1; - } - - public void setF1(NestedClass f1) { - this.f1 = f1; - } - - public BaseClass() { - } - } - - @MessagePackBeans - public static class NestedClass { - public int f2; - - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - OptionalBaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public OptionalNestedClass getF1() { - return f1; - } - - public void setF1(OptionalNestedClass f1) { - this.f1 = f1; - } - - public OptionalBaseClass() { - } - } - - @MessagePackBeans - public static class OptionalNestedClass { - @Optional - public int f2; - - @Optional - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - BaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public MessagePackMessageClass2 getF1() { - return f1; - } - - public void setF1(MessagePackMessageClass2 f1) { - this.f1 = f1; - } - - public BaseClass2() { - } - } - - @MessagePackBeans - public static class MessagePackMessageClass2 { - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - OptionalBaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public OptionalMessagePackMessageClass2 getF1() { - return f1; - } - - public void setF1(OptionalMessagePackMessageClass2 f1) { - this.f1 = f1; - } - - public OptionalBaseClass2() { - } - } - - @MessagePackBeans - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - @Optional - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public int getF1() { - return f1; - } - - public SampleSubClass() { - } - } - - @MessagePackBeans - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public int getF5() { - return f5; - } - - public void setF5(int f5) { - this.f5 = f5; - } - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - SampleOptionalSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public SampleOptionalSubClass() { - } - } - - @MessagePackBeans - public static class SampleOptionalSuperClass { - @Optional - public int getF5() { - return f5; - } - - public void setF5(int f5) { - this.f5 = f5; - } - - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } - - @Test - public void testMessagePackableUnpackableClass00() throws Exception { - BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass(); - MessagePackableUnpackableClass src1 = new MessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testMessagePackableUnpackableClass01() throws Exception { - BaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class BaseMessagePackableUnpackableClass { - public MessagePackableUnpackableClass f0; - public int f1; - public List f2; - - public MessagePackableUnpackableClass getF0() { - return f0; - } - - public void setF0(MessagePackableUnpackableClass f0) { - this.f0 = f0; - } - - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - public List getF2() { - return f2; - } - - public void setF2(List f2) { - this.f2 = f2; - } - - public BaseMessagePackableUnpackableClass() { - } - } - - @MessagePackBeans - public static class MessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - public int f0; - public int f1; - - public MessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } - - @Test - public void testOptionalMessagePackableUnpackableClass00() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - OptionalMessagePackableUnpackableClass src1 = new OptionalMessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testOptionalMessagePackableUnpackableClass01() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - src.f0 = null; - src.f1 = 1; - src.f2 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - } - - @Test - public void testOptionalMessagePackableUnpackableClass02() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalBaseMessagePackableUnpackableClass { - @Optional - public OptionalMessagePackableUnpackableClass f0; - @Optional - public int f1; - @Optional - public List f2; - - @Optional - public OptionalMessagePackableUnpackableClass getF0() { - return f0; - } - - public void setF0(OptionalMessagePackableUnpackableClass f0) { - this.f0 = f0; - } - - @Optional - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - @Optional - public List getF2() { - return f2; - } - - public void setF2(List f2) { - this.f2 = f2; - } - - public OptionalBaseMessagePackableUnpackableClass() { - } - } - - @MessagePackBeans - public static class OptionalMessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - @Optional - public int f0; - @Optional - public int f1; - - public OptionalMessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } -} - diff --git a/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackConvert.java b/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackConvert.java deleted file mode 100644 index a514216d..00000000 --- a/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackConvert.java +++ /dev/null @@ -1,1409 +0,0 @@ -package org.msgpack.template; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.junit.Test; - -import org.msgpack.MessagePack; -import org.msgpack.MessagePackable; -import org.msgpack.MessagePacker; -import org.msgpack.MessageTypeException; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.MessagePackOrdinalEnum; -import org.msgpack.annotation.Optional; -import org.msgpack.template.builder.BeansTemplateBuilder; -import org.msgpack.template.builder.BuilderSelectorRegistry; -import org.msgpack.template.builder.BeansTemplateBuilderSelector; -import org.msgpack.template.builder.AnnotationTemplateBuilderSelector; -import org.msgpack.template.builder.ReflectionTemplateBuilder; -import org.msgpack.template.builder.TemplateBuilder; - -import junit.framework.Assert; -import junit.framework.TestCase; - -public class TestReflectionTemplateBuilderPackConvert extends TestCase { - static { - //Replace template selectors from javassist to reflection. - BuilderSelectorRegistry instance = BuilderSelectorRegistry.getInstance(); - - instance.replace( - new AnnotationTemplateBuilderSelector( - new ReflectionTemplateBuilder())); - instance.setForceBuilder( new ReflectionTemplateBuilder()); - instance.replace(new BeansTemplateBuilderSelector( - new BeansTemplateBuilder())); - - MessagePack.register(PrimitiveTypeFieldsClass.class); - MessagePack.register(OptionalPrimitiveTypeFieldsClass.class); - MessagePack.register(GeneralReferenceTypeFieldsClass.class); - MessagePack.register(GeneralOptionalReferenceTypeFieldsClass.class); - MessagePack.register(SampleListTypes.class); - MessagePack.register(SampleOptionalListTypes.class); - MessagePack.register(SampleMapTypes.class); - MessagePack.register(SampleOptionalMapTypes.class); - MessagePack.register(SampleEnumFieldClass.class); - MessagePack.register(SampleOptionalEnumFieldClass.class); - MessagePack.register(FieldModifiersClass.class); - MessagePack.register(OptionalFieldModifiersClass.class); - MessagePack.register(BaseClass.class); - MessagePack.register(NestedClass.class); - MessagePack.register(BaseClass2.class); - MessagePack.register(OptionalBaseClass.class); - MessagePack.register(OptionalNestedClass.class); - MessagePack.register(OptionalBaseClass2.class); - MessagePack.register(SampleSubClass.class); - MessagePack.register(SampleSuperClass.class); - MessagePack.register(SampleOptionalSubClass.class); - MessagePack.register(SampleOptionalSuperClass.class); - MessagePack.register(BaseMessagePackableUnpackableClass.class); - MessagePack.register(MessagePackableUnpackableClass.class); - MessagePack.register(OptionalBaseMessagePackableUnpackableClass.class); - MessagePack.register(OptionalMessagePackableUnpackableClass.class); - } - - @Test - public void testPrimitiveTypeFields00() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - PrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class PrimitiveTypeFieldsClass { - public byte f0; - public short f1; - public int f2; - public long f3; - public float f4; - public double f5; - public boolean f6; - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - OptionalPrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9, dst.f9); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - SampleListTypes src = new SampleListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw).convert(SampleListTypes.class); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleListNestedType s = src.f4.get(i); - SampleListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testListTypes01() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw).convert(SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - public List> f3; - public List f4; - public List f5; - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } - - @Test - public void testOptionalListTypes02() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw).convert(SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - public SampleOptionalListTypes() { - } - } - - @MessagePackMessage - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw).convert(SampleMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testMapTypes01() throws Exception { - SampleMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw).convert(SampleMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } - - @Test - public void testOptionalMapTypes02() throws Exception { - SampleOptionalMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - public SampleOptionalMapTypes() { - } - } - - @Test - public void testFinalClass() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(FinalModifierClass.class); - Assert.assertNull(builder);// no available builder - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(FinalModifierClass.class); - assertTrue(true); - } catch (TemplateBuildException e) { - fail(); - } - assertTrue(true); - } - - public final static class FinalModifierClass { - } - - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder);// no available builder - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - @Test - public void testInterfaceType01() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder);// no available builder - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - public interface SampleInterface { - } - - @Test - public void testEnumTypeForOrdinal00() throws Exception { - SampleEnumFieldClass src = new SampleEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleEnumFieldClass.class); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testEnumTypeForOrdinal01() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleEnumFieldClass { - public int f0; - - public SampleEnum f1; - - public SampleEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleEnum { - ONE, TWO, THREE; - } - - @Test - public void testOptionalEnumTypeForOrdinal00() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleOptionalEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal01() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal02() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalEnumFieldClass { - @Optional - public int f0; - - @Optional - public SampleOptionalEnum f1; - - public SampleOptionalEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleOptionalEnum { - ONE, TWO, THREE; - } - - @Test - public void testFieldModifiers() throws Exception { - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - FieldModifiersClass dst = - MessagePack.unpack(raw).convert(FieldModifiersClass.class); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - OptionalFieldModifiersClass dst = - MessagePack.unpack(raw).convert(OptionalFieldModifiersClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw).convert(BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testNestedFieldClass01() throws Exception { - BaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw).convert(BaseClass.class); - assertEquals(src, dst); - } - - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw).convert(OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw).convert(OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - OptionalBaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw).convert(OptionalBaseClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - public OptionalBaseClass() { - } - } - - public static class OptionalNestedClass { - @Optional - public int f2; - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw).convert(BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - BaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw).convert(BaseClass2.class); - assertEquals(src, dst); - } - - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public BaseClass2() { - } - } - - @MessagePackMessage - public static class MessagePackMessageClass2 { - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw).convert(OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw).convert(OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - OptionalBaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw).convert(OptionalBaseClass2.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - public OptionalBaseClass2() { - } - } - - @MessagePackMessage - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw).convert(SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw).convert(SampleSubClass.class); - assertEquals(src, dst); - } - - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleSubClass() { - } - } - - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw).convert(SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - SampleOptionalSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw).convert(SampleOptionalSubClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleOptionalSubClass() { - } - } - - public static class SampleOptionalSuperClass { - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } - - @Test - public void testMessagePackableUnpackableClass00() throws Exception { - BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass(); - MessagePackableUnpackableClass src1 = new MessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(BaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testMessagePackableUnpackableClass01() throws Exception { - BaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(BaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class BaseMessagePackableUnpackableClass { - public MessagePackableUnpackableClass f0; - public int f1; - public List f2; - - public BaseMessagePackableUnpackableClass() { - } - } - - public static class MessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - public int f0; - public int f1; - - public MessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } - - @Test - public void testOptionalMessagePackableUnpackableClass00() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - OptionalMessagePackableUnpackableClass src1 = new OptionalMessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testOptionalMessagePackableUnpackableClass01() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - src.f0 = null; - src.f1 = 1; - src.f2 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - } - - @Test - public void testOptionalMessagePackableUnpackableClass02() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseMessagePackableUnpackableClass { - @Optional - public OptionalMessagePackableUnpackableClass f0; - @Optional - public int f1; - @Optional - public List f2; - - public OptionalBaseMessagePackableUnpackableClass() { - } - } - - public static class OptionalMessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - @Optional - public int f0; - @Optional - public int f1; - - public OptionalMessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } -} - diff --git a/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackUnpack.java b/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackUnpack.java deleted file mode 100644 index 53e48913..00000000 --- a/java/src/test/java/org/msgpack/template/TestReflectionTemplateBuilderPackUnpack.java +++ /dev/null @@ -1,1414 +0,0 @@ -package org.msgpack.template; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.junit.Test; - -import org.msgpack.MessagePack; -import org.msgpack.MessagePackable; -import org.msgpack.MessagePacker; -import org.msgpack.MessageTypeException; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.MessagePackOrdinalEnum; -import org.msgpack.annotation.Optional; -import org.msgpack.template.TestTemplateBuilderPackConvert.SampleInterface; -import org.msgpack.template.builder.BeansTemplateBuilder; -import org.msgpack.template.builder.BuilderSelectorRegistry; -import org.msgpack.template.builder.BeansTemplateBuilderSelector; -import org.msgpack.template.builder.AnnotationTemplateBuilderSelector; -import org.msgpack.template.builder.ReflectionTemplateBuilder; -import org.msgpack.template.builder.TemplateBuilder; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import static org.junit.Assert.assertThat; - -public class TestReflectionTemplateBuilderPackUnpack extends TestCase { - static { - //Replace template selectors from javassist to reflection. - BuilderSelectorRegistry instance = BuilderSelectorRegistry.getInstance(); - - instance.replace( - new AnnotationTemplateBuilderSelector( - new ReflectionTemplateBuilder())); - instance.setForceBuilder( new ReflectionTemplateBuilder()); - instance.replace(new BeansTemplateBuilderSelector( - new BeansTemplateBuilder())); - - - MessagePack.register(PrimitiveTypeFieldsClass.class); - MessagePack.register(OptionalPrimitiveTypeFieldsClass.class); - MessagePack.register(GeneralReferenceTypeFieldsClass.class); - MessagePack.register(GeneralOptionalReferenceTypeFieldsClass.class); - MessagePack.register(SampleListTypes.class); - MessagePack.register(SampleOptionalListTypes.class); - MessagePack.register(SampleMapTypes.class); - MessagePack.register(SampleOptionalMapTypes.class); - MessagePack.register(SampleEnumFieldClass.class); - MessagePack.register(SampleOptionalEnumFieldClass.class); - MessagePack.register(FieldModifiersClass.class); - MessagePack.register(OptionalFieldModifiersClass.class); - MessagePack.register(BaseClass.class); - MessagePack.register(NestedClass.class); - MessagePack.register(BaseClass2.class); - MessagePack.register(OptionalBaseClass.class); - MessagePack.register(OptionalNestedClass.class); - MessagePack.register(OptionalBaseClass2.class); - MessagePack.register(SampleSubClass.class); - MessagePack.register(SampleSuperClass.class); - MessagePack.register(SampleOptionalSubClass.class); - MessagePack.register(SampleOptionalSuperClass.class); - MessagePack.register(BaseMessagePackableUnpackableClass.class); - MessagePack.register(MessagePackableUnpackableClass.class); - MessagePack.register(OptionalBaseMessagePackableUnpackableClass.class); - MessagePack.register(OptionalMessagePackableUnpackableClass.class); - } - - @Test - public void testPrimitiveTypeFields00() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - PrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class PrimitiveTypeFieldsClass { - public byte f0; - public short f1; - public int f2; - public long f3; - public float f4; - public double f5; - public boolean f6; - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - OptionalPrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9, dst.f9); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - SampleListTypes src = new SampleListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleListNestedType s = src.f4.get(i); - SampleListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testListTypes01() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - public List> f3; - public List f4; - public List f5; - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } - - @Test - public void testOptionalListTypes02() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - public SampleOptionalListTypes() { - } - } - - @MessagePackMessage - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testMapTypes01() throws Exception { - SampleMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } - - @Test - public void testOptionalMapTypes02() throws Exception { - SampleOptionalMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - public SampleOptionalMapTypes() { - } - } - - @Test - public void testFinalClass() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(FinalModifierClass.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(FinalModifierClass.class); - assertTrue(true); - } catch (TemplateBuildException e) { - fail(); - } - assertTrue(true); - } - - public final static class FinalModifierClass { - } - - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - @Test - public void testInterfaceType01() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - public interface SampleInterface { - } - - @Test - public void testEnumTypeForOrdinal00() throws Exception { - SampleEnumFieldClass src = new SampleEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testEnumTypeForOrdinal01() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleEnumFieldClass { - public int f0; - - public SampleEnum f1; - - public SampleEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleEnum { - ONE, TWO, THREE; - } - - @Test - public void testOptionalEnumTypeForOrdinal00() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleOptionalEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal01() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal02() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalEnumFieldClass { - @Optional - public int f0; - - @Optional - public SampleOptionalEnum f1; - - public SampleOptionalEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleOptionalEnum { - ONE, TWO, THREE; - } - - @Test - public void testFieldModifiers() throws Exception { - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - FieldModifiersClass dst = - MessagePack.unpack(raw, FieldModifiersClass.class); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - OptionalFieldModifiersClass dst = - MessagePack.unpack(raw, OptionalFieldModifiersClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testNestedFieldClass01() throws Exception { - BaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertEquals(src, dst); - } - - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - OptionalBaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - public OptionalBaseClass() { - } - } - - public static class OptionalNestedClass { - @Optional - public int f2; - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - BaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertEquals(src, dst); - } - - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public BaseClass2() { - } - } - - @MessagePackMessage - public static class MessagePackMessageClass2 { - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - OptionalBaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - public OptionalBaseClass2() { - } - } - - @MessagePackMessage - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertEquals(src, dst); - } - - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleSubClass() { - } - } - - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - SampleOptionalSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleOptionalSubClass() { - } - } - - public static class SampleOptionalSuperClass { - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } - - @Test - public void testMessagePackableUnpackableClass00() throws Exception { - BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass(); - MessagePackableUnpackableClass src1 = new MessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testMessagePackableUnpackableClass01() throws Exception { - BaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class BaseMessagePackableUnpackableClass { - public MessagePackableUnpackableClass f0; - public int f1; - public List f2; - - public BaseMessagePackableUnpackableClass() { - } - } - - public static class MessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - public int f0; - public int f1; - - public MessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } - - @Test - public void testOptionalMessagePackableUnpackableClass00() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - OptionalMessagePackableUnpackableClass src1 = new OptionalMessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testOptionalMessagePackableUnpackableClass01() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - src.f0 = null; - src.f1 = 1; - src.f2 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - } - - @Test - public void testOptionalMessagePackableUnpackableClass02() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseMessagePackableUnpackableClass { - @Optional - public OptionalMessagePackableUnpackableClass f0; - @Optional - public int f1; - @Optional - public List f2; - - public OptionalBaseMessagePackableUnpackableClass() { - } - } - - public static class OptionalMessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - @Optional - public int f0; - @Optional - public int f1; - - public OptionalMessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } -} - diff --git a/java/src/test/java/org/msgpack/template/TestTemplateBuilderJavaBeansPackUnpack.java b/java/src/test/java/org/msgpack/template/TestTemplateBuilderJavaBeansPackUnpack.java deleted file mode 100644 index b5262ac3..00000000 --- a/java/src/test/java/org/msgpack/template/TestTemplateBuilderJavaBeansPackUnpack.java +++ /dev/null @@ -1,2226 +0,0 @@ -package org.msgpack.template; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.hamcrest.CoreMatchers; -import org.junit.Ignore; -import org.junit.Test; - -import org.msgpack.MessagePack; -import org.msgpack.MessagePackable; -import org.msgpack.MessagePacker; -import org.msgpack.MessageTypeException; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.annotation.MessagePackBeans; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.MessagePackOrdinalEnum; -import org.msgpack.annotation.Optional; -import org.msgpack.template.TestTemplateBuilderPackConvert.SampleInterface; -import org.msgpack.template.builder.BuilderSelectorRegistry; -import org.msgpack.template.builder.TemplateBuilder; - -import org.junit.Assert; -import junit.framework.TestCase; -import static org.hamcrest.CoreMatchers.*; - -public class TestTemplateBuilderJavaBeansPackUnpack extends TestCase { - static { - MessagePack.register(PrimitiveTypeFieldsClass.class); - MessagePack.register(OptionalPrimitiveTypeFieldsClass.class); - MessagePack.register(GeneralReferenceTypeFieldsClass.class); - MessagePack.register(GeneralOptionalReferenceTypeFieldsClass.class); - MessagePack.register(SampleListTypes.class); - MessagePack.register(SampleOptionalListTypes.class); - MessagePack.register(SampleMapTypes.class); - MessagePack.register(SampleOptionalMapTypes.class); - MessagePack.register(SampleEnumFieldClass.class); - MessagePack.register(SampleOptionalEnumFieldClass.class); - MessagePack.register(FieldModifiersClass.class); - MessagePack.register(OptionalFieldModifiersClass.class); - MessagePack.register(BaseClass.class); - MessagePack.register(NestedClass.class); - MessagePack.register(BaseClass2.class); - MessagePack.register(OptionalBaseClass.class); - MessagePack.register(OptionalNestedClass.class); - MessagePack.register(OptionalBaseClass2.class); - MessagePack.register(SampleSubClass.class); - MessagePack.register(SampleSuperClass.class); - MessagePack.register(SampleOptionalSubClass.class); - MessagePack.register(SampleOptionalSuperClass.class); - MessagePack.register(BaseMessagePackableUnpackableClass.class); - MessagePack.register(MessagePackableUnpackableClass.class); - MessagePack.register(OptionalBaseMessagePackableUnpackableClass.class); - MessagePack.register(OptionalMessagePackableUnpackableClass.class); - } - - @Test - public void testPrimitiveTypeFields00() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - PrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class PrimitiveTypeFieldsClass { - byte f0; - short f1; - int f2; - long f3; - float f4; - double f5; - boolean f6; - - public byte getF0() { - return f0; - } - - public void setF0(byte f0) { - this.f0 = f0; - } - - public short getF1() { - return f1; - } - - public void setF1(short f1) { - this.f1 = f1; - } - - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public long getF3() { - return f3; - } - - public void setF3(long f3) { - this.f3 = f3; - } - - public float getF4() { - return f4; - } - - public void setF4(float f4) { - this.f4 = f4; - } - - public double getF5() { - return f5; - } - - public void setF5(double f5) { - this.f5 = f5; - } - - public boolean isF6() { - return f6; - } - - public void setF6(boolean f6) { - this.f6 = f6; - } - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - OptionalPrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - @Optional - public byte getF0() { - return f0; - } - - @Optional - public void setF0(byte f0) { - this.f0 = f0; - } - - @Optional - public short getF1() { - return f1; - } - - public void setF1(short f1) { - this.f1 = f1; - } - - public int getF2() { - return f2; - } - - @Optional - public void setF2(int f2) { - this.f2 = f2; - } - - @Optional - public long getF3() { - return f3; - } - - public void setF3(long f3) { - this.f3 = f3; - } - - @Optional - public float getF4() { - return f4; - } - - public void setF4(float f4) { - this.f4 = f4; - } - - @Optional - public double getF5() { - return f5; - } - - public void setF5(double f5) { - this.f5 = f5; - } - - @Optional - public boolean isF6() { - return f6; - } - - public void setF6(boolean f6) { - this.f6 = f6; - } - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public Byte getF0() { - return f0; - } - - public void setF0(Byte f0) { - this.f0 = f0; - } - - public Short getF1() { - return f1; - } - - public void setF1(Short f1) { - this.f1 = f1; - } - - public Integer getF2() { - return f2; - } - - public void setF2(Integer f2) { - this.f2 = f2; - } - - public Long getF3() { - return f3; - } - - public void setF3(Long f3) { - this.f3 = f3; - } - - public Float getF4() { - return f4; - } - - public void setF4(Float f4) { - this.f4 = f4; - } - - public Double getF5() { - return f5; - } - - public void setF5(Double f5) { - this.f5 = f5; - } - - public Boolean getF6() { - return f6; - } - - public void setF6(Boolean f6) { - this.f6 = f6; - } - - public BigInteger getF7() { - return f7; - } - - public void setF7(BigInteger f7) { - this.f7 = f7; - } - - public String getF8() { - return f8; - } - - public void setF8(String f8) { - this.f8 = f8; - } - - public byte[] getF9() { - return f9; - } - - public void setF9(byte[] f9) { - this.f9 = f9; - } - - public ByteBuffer getF10() { - return f10; - } - - public void setF10(ByteBuffer f10) { - this.f10 = f10; - } - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9, dst.f9); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - @Optional - public Byte getF0() { - return f0; - } - - @Optional - public void setF0(Byte f0) { - this.f0 = f0; - } - - @Optional - public Short getF1() { - return f1; - } - - public void setF1(Short f1) { - this.f1 = f1; - } - - public Integer getF2() { - return f2; - } - - @Optional - public void setF2(Integer f2) { - this.f2 = f2; - } - - @Optional - public Long getF3() { - return f3; - } - - public void setF3(Long f3) { - this.f3 = f3; - } - - @Optional - public Float getF4() { - return f4; - } - - public void setF4(Float f4) { - this.f4 = f4; - } - - @Optional - public Double getF5() { - return f5; - } - - public void setF5(Double f5) { - this.f5 = f5; - } - - @Optional - public Boolean getF6() { - return f6; - } - - public void setF6(Boolean f6) { - this.f6 = f6; - } - - @Optional - public BigInteger getF7() { - return f7; - } - - public void setF7(BigInteger f7) { - this.f7 = f7; - } - - @Optional - public String getF8() { - return f8; - } - - public void setF8(String f8) { - this.f8 = f8; - } - - @Optional - public byte[] getF9() { - return f9; - } - - public void setF9(byte[] f9) { - this.f9 = f9; - } - - @Optional - public ByteBuffer getF10() { - return f10; - } - - public void setF10(ByteBuffer f10) { - this.f10 = f10; - } - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - SampleListTypes src = new SampleListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleListNestedType s = src.f4.get(i); - SampleListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testListTypes01() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - public List> f3; - public List f4; - public List f5; - - public List getF0() { - return f0; - } - - public void setF0(List f0) { - this.f0 = f0; - } - - public List getF1() { - return f1; - } - - public void setF1(List f1) { - this.f1 = f1; - } - - public List getF2() { - return f2; - } - - public void setF2(List f2) { - this.f2 = f2; - } - - public List> getF3() { - return f3; - } - - public void setF3(List> f3) { - this.f3 = f3; - } - - public List getF4() { - return f4; - } - - public void setF4(List f4) { - this.f4 = f4; - } - - public List getF5() { - return f5; - } - - public void setF5(List f5) { - this.f5 = f5; - } - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } - - @Test - public void testOptionalListTypes02() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - @Optional - public List getF0() { - return f0; - } - - @Optional - public void setF0(List f0) { - this.f0 = f0; - } - - @Optional - public List getF1() { - return f1; - } - - public void setF1(List f1) { - this.f1 = f1; - } - - public List getF2() { - return f2; - } - - @Optional - public void setF2(List f2) { - this.f2 = f2; - } - - @Optional - public List> getF3() { - return f3; - } - - public void setF3(List> f3) { - this.f3 = f3; - } - - @Optional - public List getF4() { - return f4; - } - - public void setF4(List f4) { - this.f4 = f4; - } - - @Optional - public List getF5() { - return f5; - } - - public void setF5(List f5) { - this.f5 = f5; - } - - public SampleOptionalListTypes() { - } - } - - @MessagePackBeans - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - @Optional - public byte[] getF0() { - return f0; - } - - public void setF0(byte[] f0) { - this.f0 = f0; - } - - @Optional - public String getF1() { - return f1; - } - - public void setF1(String f1) { - this.f1 = f1; - } - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testMapTypes01() throws Exception { - SampleMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public Map getF0() { - return f0; - } - - public void setF0(Map f0) { - this.f0 = f0; - } - - public Map getF1() { - return f1; - } - - public void setF1(Map f1) { - this.f1 = f1; - } - - public Map getF2() { - return f2; - } - - public void setF2(Map f2) { - this.f2 = f2; - } - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - Iterator dstf1 = dst.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - Iterator dstf2 = dst.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } - - @Test - public void testOptionalMapTypes02() throws Exception { - SampleOptionalMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - @Optional - public Map getF0() { - return f0; - } - - public void setF0(Map f0) { - this.f0 = f0; - } - - @Optional - public Map getF1() { - return f1; - } - - public void setF1(Map f1) { - this.f1 = f1; - } - - @Optional - public Map getF2() { - return f2; - } - - public void setF2(Map f2) { - this.f2 = f2; - } - - public SampleOptionalMapTypes() { - } - } - - - @MessagePackBeans - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - @Test - public void testInterfaceType01() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - public interface SampleInterface { - } - - @Test - public void testEnumTypeForOrdinal00() throws Exception { - SampleEnumFieldClass src = new SampleEnumFieldClass(); - src.f0 = 23; - src.f1 = SampleEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - Assert.assertThat(dst.f0, is(src.f0)); - Assert.assertThat(dst.f1, is(src.f1)); - } - - @Test - public void testEnumTypeForOrdinal01() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - Assert.assertThat(dst,is(src)); - } - - @MessagePackBeans - public static class SampleEnumFieldClass { - public int f0; - - public SampleEnum f1; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public SampleEnum getF1() { - return f1; - } - - public void setF1(SampleEnum f1) { - this.f1 = f1; - } - - public SampleEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleEnum { - ONE, TWO, THREE; - } - - @Test - public void testOptionalEnumTypeForOrdinal00() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleOptionalEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal01() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal02() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalEnumFieldClass { - @Optional - public int f0; - - @Optional - public SampleOptionalEnum f1; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public SampleOptionalEnum getF1() { - return f1; - } - - public void setF1(SampleOptionalEnum f1) { - this.f1 = f1; - } - - public SampleOptionalEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleOptionalEnum { - ONE, TWO, THREE; - } - - @Test - public void testFieldModifiers() throws Exception { - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - FieldModifiersClass dst = - MessagePack.unpack(raw, FieldModifiersClass.class); - Assert.assertEquals(src.f1,dst.f1); - Assert.assertThat(dst.f2, is( not(src.f2))); - Assert.assertThat(dst.f3, is( not(src.f3))); - Assert.assertThat(dst.f4, is( not(src.f4))); - } - - @MessagePackBeans - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - private int getF2() { - return f2; - } - - private void setF2(int f2) { - this.f2 = f2; - } - - public int getF3() { - return f3; - } - - protected void setF3(int f3) { - this.f3 = f3; - } - - public int getF4() { - return f4; - } - - void setF4(int f4) { - this.f4 = f4; - } - - public int getF1() { - return f1; - } - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - OptionalFieldModifiersClass dst = - MessagePack.unpack(raw, OptionalFieldModifiersClass.class); - Assert.assertThat(dst.f0, is(src.f0)); - Assert.assertThat(dst.f1, is(src.f1)); - Assert.assertThat(dst.f2, is(not(src.f2))); - Assert.assertThat(dst.f3, is(not(src.f3))); - Assert.assertThat(dst.f4, is(not(src.f4))); - } - - @MessagePackBeans - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - private int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - protected int getF3() { - return f3; - } - - protected void setF3(int f3) { - this.f3 = f3; - } - - public int getF4() { - return f4; - } - - void setF4(int f4) { - this.f4 = f4; - } - - public int getF1() { - return f1; - } - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testNestedFieldClass01() throws Exception { - BaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class BaseClass { - public int f0; - public NestedClass f1; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public NestedClass getF1() { - return f1; - } - - public void setF1(NestedClass f1) { - this.f1 = f1; - } - - public BaseClass() { - } - } - - @MessagePackBeans - public static class NestedClass { - public int f2; - - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - OptionalBaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public OptionalNestedClass getF1() { - return f1; - } - - public void setF1(OptionalNestedClass f1) { - this.f1 = f1; - } - - public OptionalBaseClass() { - } - } - - @MessagePackBeans - public static class OptionalNestedClass { - @Optional - public int f2; - - @Optional - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - BaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public MessagePackMessageClass2 getF1() { - return f1; - } - - public void setF1(MessagePackMessageClass2 f1) { - this.f1 = f1; - } - - public BaseClass2() { - } - } - - @MessagePackBeans - public static class MessagePackMessageClass2 { - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - OptionalBaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public OptionalMessagePackMessageClass2 getF1() { - return f1; - } - - public void setF1(OptionalMessagePackMessageClass2 f1) { - this.f1 = f1; - } - - public OptionalBaseClass2() { - } - } - - @MessagePackBeans - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - @Optional - public int getF2() { - return f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public int getF1() { - return f1; - } - - public SampleSubClass() { - } - } - - @MessagePackBeans - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public int getF5() { - return f5; - } - - public void setF5(int f5) { - this.f5 = f5; - } - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - SampleOptionalSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public SampleOptionalSubClass() { - } - } - - @MessagePackBeans - public static class SampleOptionalSuperClass { - @Optional - public int getF5() { - return f5; - } - - public void setF5(int f5) { - this.f5 = f5; - } - - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } - - @Test - public void testMessagePackableUnpackableClass00() throws Exception { - BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass(); - MessagePackableUnpackableClass src1 = new MessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testMessagePackableUnpackableClass01() throws Exception { - BaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class BaseMessagePackableUnpackableClass { - public MessagePackableUnpackableClass f0; - public int f1; - public List f2; - - public MessagePackableUnpackableClass getF0() { - return f0; - } - - public void setF0(MessagePackableUnpackableClass f0) { - this.f0 = f0; - } - - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - public List getF2() { - return f2; - } - - public void setF2(List f2) { - this.f2 = f2; - } - - public BaseMessagePackableUnpackableClass() { - } - } - - @MessagePackBeans - public static class MessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - public int f0; - public int f1; - - public MessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } - - @Test - public void testOptionalMessagePackableUnpackableClass00() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - OptionalMessagePackableUnpackableClass src1 = new OptionalMessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testOptionalMessagePackableUnpackableClass01() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - src.f0 = null; - src.f1 = 1; - src.f2 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - } - - @Test - public void testOptionalMessagePackableUnpackableClass02() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - @MessagePackBeans - public static class OptionalBaseMessagePackableUnpackableClass { - @Optional - public OptionalMessagePackableUnpackableClass f0; - @Optional - public int f1; - @Optional - public List f2; - - @Optional - public OptionalMessagePackableUnpackableClass getF0() { - return f0; - } - - public void setF0(OptionalMessagePackableUnpackableClass f0) { - this.f0 = f0; - } - - @Optional - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - @Optional - public List getF2() { - return f2; - } - - public void setF2(List f2) { - this.f2 = f2; - } - - public OptionalBaseMessagePackableUnpackableClass() { - } - } - - @MessagePackBeans - public static class OptionalMessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - @Optional - public int getF0() { - return f0; - } - - public void setF0(int f0) { - this.f0 = f0; - } - - @Optional - public int getF1() { - return f1; - } - - public void setF1(int f1) { - this.f1 = f1; - } - - @Optional - public int f0; - @Optional - public int f1; - - public OptionalMessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } -} - diff --git a/java/src/test/java/org/msgpack/template/TestTemplateBuilderPackConvert.java b/java/src/test/java/org/msgpack/template/TestTemplateBuilderPackConvert.java deleted file mode 100644 index d741c9bf..00000000 --- a/java/src/test/java/org/msgpack/template/TestTemplateBuilderPackConvert.java +++ /dev/null @@ -1,1395 +0,0 @@ -package org.msgpack.template; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.junit.Test; - -import org.msgpack.MessagePack; -import org.msgpack.MessagePackable; -import org.msgpack.MessagePacker; -import org.msgpack.MessageTypeException; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.MessagePackOrdinalEnum; -import org.msgpack.annotation.Optional; -import org.msgpack.template.builder.BuilderSelectorRegistry; -import org.msgpack.template.builder.TemplateBuilder; - -import junit.framework.Assert; -import junit.framework.TestCase; - -public class TestTemplateBuilderPackConvert extends TestCase { - static { - MessagePack.register(PrimitiveTypeFieldsClass.class); - MessagePack.register(OptionalPrimitiveTypeFieldsClass.class); - MessagePack.register(GeneralReferenceTypeFieldsClass.class); - MessagePack.register(GeneralOptionalReferenceTypeFieldsClass.class); - MessagePack.register(SampleListTypes.class); - MessagePack.register(SampleOptionalListTypes.class); - MessagePack.register(SampleMapTypes.class); - MessagePack.register(SampleOptionalMapTypes.class); - MessagePack.register(SampleEnumFieldClass.class); - MessagePack.register(SampleOptionalEnumFieldClass.class); - MessagePack.register(FieldModifiersClass.class); - MessagePack.register(OptionalFieldModifiersClass.class); - MessagePack.register(BaseClass.class); - MessagePack.register(NestedClass.class); - MessagePack.register(BaseClass2.class); - MessagePack.register(OptionalBaseClass.class); - MessagePack.register(OptionalNestedClass.class); - MessagePack.register(OptionalBaseClass2.class); - MessagePack.register(SampleSubClass.class); - MessagePack.register(SampleSuperClass.class); - MessagePack.register(SampleOptionalSubClass.class); - MessagePack.register(SampleOptionalSuperClass.class); - MessagePack.register(BaseMessagePackableUnpackableClass.class); - MessagePack.register(MessagePackableUnpackableClass.class); - MessagePack.register(OptionalBaseMessagePackableUnpackableClass.class); - MessagePack.register(OptionalMessagePackableUnpackableClass.class); - } - - @Test - public void testPrimitiveTypeFields00() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - PrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class PrimitiveTypeFieldsClass { - public byte f0; - public short f1; - public int f2; - public long f3; - public float f4; - public double f5; - public boolean f6; - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - OptionalPrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw).convert(OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9, dst.f9); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - SampleListTypes src = new SampleListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw).convert(SampleListTypes.class); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleListNestedType s = src.f4.get(i); - SampleListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testListTypes01() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw).convert(SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - public List> f3; - public List f4; - public List f5; - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } - - @Test - public void testOptionalListTypes02() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw).convert(SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - public SampleOptionalListTypes() { - } - } - - @MessagePackMessage - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw).convert(SampleMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testMapTypes01() throws Exception { - SampleMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw).convert(SampleMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } - - @Test - public void testOptionalMapTypes02() throws Exception { - SampleOptionalMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - public SampleOptionalMapTypes() { - } - } - - @Test - public void testFinalClass() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(FinalModifierClass.class); - Assert.assertNull(builder);// no available builder - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(FinalModifierClass.class); - assertTrue(true); - } catch (TemplateBuildException e) { - fail(); - } - assertTrue(true); - } - - public final static class FinalModifierClass { - } - - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder);// no available builder - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - @Test - public void testInterfaceType01() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder);// no available builder - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - public interface SampleInterface { - } - - @Test - public void testEnumTypeForOrdinal00() throws Exception { - SampleEnumFieldClass src = new SampleEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleEnumFieldClass.class); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testEnumTypeForOrdinal01() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleEnumFieldClass { - public int f0; - - public SampleEnum f1; - - public SampleEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleEnum { - ONE, TWO, THREE; - } - - @Test - public void testOptionalEnumTypeForOrdinal00() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleOptionalEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal01() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal02() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw).convert(SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalEnumFieldClass { - @Optional - public int f0; - - @Optional - public SampleOptionalEnum f1; - - public SampleOptionalEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleOptionalEnum { - ONE, TWO, THREE; - } - - @Test - public void testFieldModifiers() throws Exception { - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - FieldModifiersClass dst = - MessagePack.unpack(raw).convert(FieldModifiersClass.class); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - OptionalFieldModifiersClass dst = - MessagePack.unpack(raw).convert(OptionalFieldModifiersClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw).convert(BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testNestedFieldClass01() throws Exception { - BaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw).convert(BaseClass.class); - assertEquals(src, dst); - } - - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw).convert(OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw).convert(OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - OptionalBaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw).convert(OptionalBaseClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - public OptionalBaseClass() { - } - } - - public static class OptionalNestedClass { - @Optional - public int f2; - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw).convert(BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - BaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw).convert(BaseClass2.class); - assertEquals(src, dst); - } - - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public BaseClass2() { - } - } - - @MessagePackMessage - public static class MessagePackMessageClass2 { - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw).convert(OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw).convert(OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - OptionalBaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw).convert(OptionalBaseClass2.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - public OptionalBaseClass2() { - } - } - - @MessagePackMessage - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw).convert(SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw).convert(SampleSubClass.class); - assertEquals(src, dst); - } - - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleSubClass() { - } - } - - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw).convert(SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - SampleOptionalSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw).convert(SampleOptionalSubClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleOptionalSubClass() { - } - } - - public static class SampleOptionalSuperClass { - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } - - @Test - public void testMessagePackableUnpackableClass00() throws Exception { - BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass(); - MessagePackableUnpackableClass src1 = new MessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(BaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testMessagePackableUnpackableClass01() throws Exception { - BaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(BaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class BaseMessagePackableUnpackableClass { - public MessagePackableUnpackableClass f0; - public int f1; - public List f2; - - public BaseMessagePackableUnpackableClass() { - } - } - - public static class MessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - public int f0; - public int f1; - - public MessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } - - @Test - public void testOptionalMessagePackableUnpackableClass00() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - OptionalMessagePackableUnpackableClass src1 = new OptionalMessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testOptionalMessagePackableUnpackableClass01() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - src.f0 = null; - src.f1 = 1; - src.f2 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - } - - @Test - public void testOptionalMessagePackableUnpackableClass02() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw).convert(OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseMessagePackableUnpackableClass { - @Optional - public OptionalMessagePackableUnpackableClass f0; - @Optional - public int f1; - @Optional - public List f2; - - public OptionalBaseMessagePackableUnpackableClass() { - } - } - - public static class OptionalMessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - @Optional - public int f0; - @Optional - public int f1; - - public OptionalMessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } -} - diff --git a/java/src/test/java/org/msgpack/template/TestTemplateBuilderPackUnpack.java b/java/src/test/java/org/msgpack/template/TestTemplateBuilderPackUnpack.java deleted file mode 100644 index 7fcf0d7c..00000000 --- a/java/src/test/java/org/msgpack/template/TestTemplateBuilderPackUnpack.java +++ /dev/null @@ -1,1391 +0,0 @@ -package org.msgpack.template; - -import java.io.IOException; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.junit.Test; - -import org.msgpack.MessagePack; -import org.msgpack.MessagePackable; -import org.msgpack.MessageTypeException; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Unpacker; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.MessagePackOrdinalEnum; -import org.msgpack.annotation.Optional; -import org.msgpack.template.builder.BuilderSelectorRegistry; -import org.msgpack.template.builder.TemplateBuilder; - -import junit.framework.Assert; -import junit.framework.TestCase; - -public class TestTemplateBuilderPackUnpack extends TestCase { - static { - MessagePack.register(PrimitiveTypeFieldsClass.class); - MessagePack.register(OptionalPrimitiveTypeFieldsClass.class); - MessagePack.register(GeneralReferenceTypeFieldsClass.class); - MessagePack.register(GeneralOptionalReferenceTypeFieldsClass.class); - MessagePack.register(SampleListTypes.class); - MessagePack.register(SampleOptionalListTypes.class); - MessagePack.register(SampleMapTypes.class); - MessagePack.register(SampleOptionalMapTypes.class); - MessagePack.register(SampleEnumFieldClass.class); - MessagePack.register(SampleOptionalEnumFieldClass.class); - MessagePack.register(FieldModifiersClass.class); - MessagePack.register(OptionalFieldModifiersClass.class); - MessagePack.register(BaseClass.class); - MessagePack.register(NestedClass.class); - MessagePack.register(BaseClass2.class); - MessagePack.register(OptionalBaseClass.class); - MessagePack.register(OptionalNestedClass.class); - MessagePack.register(OptionalBaseClass2.class); - MessagePack.register(SampleSubClass.class); - MessagePack.register(SampleSuperClass.class); - MessagePack.register(SampleOptionalSubClass.class); - MessagePack.register(SampleOptionalSuperClass.class); - MessagePack.register(BaseMessagePackableUnpackableClass.class); - MessagePack.register(MessagePackableUnpackableClass.class); - MessagePack.register(OptionalBaseMessagePackableUnpackableClass.class); - MessagePack.register(OptionalMessagePackableUnpackableClass.class); - } - - @Test - public void testPrimitiveTypeFields00() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - PrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class PrimitiveTypeFieldsClass { - public byte f0; - public short f1; - public int f2; - public long f3; - public float f4; - public double f5; - public boolean f6; - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - byte[] raw = MessagePack.pack(src); - - PrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - OptionalPrimitiveTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9, dst.f9); - assertEquals(src.f10, dst.f10); - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() - throws Exception { - GeneralOptionalReferenceTypeFieldsClass src = null; - - byte[] raw = MessagePack.pack(src); - - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } - - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - SampleListTypes src = new SampleListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleListNestedType s = src.f4.get(i); - SampleListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testListTypes01() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - public List> f3; - public List f4; - public List f5; - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = - MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } - - @Test - public void testOptionalListTypes02() throws Exception { - SampleListTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - public SampleOptionalListTypes() { - } - } - - @MessagePackMessage - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testMapTypes01() throws Exception { - SampleMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = - MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } - - @Test - public void testOptionalMapTypes02() throws Exception { - SampleOptionalMapTypes src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = - MessagePack.unpack(raw, SampleOptionalMapTypes.class); - assertEquals(src, dst); - } - - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - public SampleOptionalMapTypes() { - } - } - - @Test - public void testFinalClass() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(FinalModifierClass.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(FinalModifierClass.class); - assertTrue(true); - } catch (TemplateBuildException e) { - fail(); - } - assertTrue(true); - } - - public final static class FinalModifierClass { - } - - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - @Test - public void testInterfaceType01() throws Exception { - try { - TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(SampleInterface.class); - Assert.assertNull(builder); - BuilderSelectorRegistry.getInstance().getForceBuilder().buildTemplate(SampleInterface.class); - fail(); - } catch (TemplateBuildException e) { - assertTrue(true); - } - assertTrue(true); - } - - public interface SampleInterface { - } - - @Test - public void testEnumTypeForOrdinal00() throws Exception { - SampleEnumFieldClass src = new SampleEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testEnumTypeForOrdinal01() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleEnumFieldClass { - public int f0; - - public SampleEnum f1; - - public SampleEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleEnum { - ONE, TWO, THREE; - } - - @Test - public void testOptionalEnumTypeForOrdinal00() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f0 = 0; - src.f1 = SampleOptionalEnum.ONE; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal01() throws Exception { - SampleOptionalEnumFieldClass src = new SampleOptionalEnumFieldClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalEnumFieldClass dst = - MessagePack.unpack(raw, SampleOptionalEnumFieldClass.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalEnumTypeForOrdinal02() throws Exception { - SampleEnumFieldClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleEnumFieldClass dst = - MessagePack.unpack(raw, SampleEnumFieldClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalEnumFieldClass { - @Optional - public int f0; - - @Optional - public SampleOptionalEnum f1; - - public SampleOptionalEnumFieldClass() { - } - } - - @MessagePackOrdinalEnum - public enum SampleOptionalEnum { - ONE, TWO, THREE; - } - - @Test - public void testFieldModifiers() throws Exception { - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - FieldModifiersClass dst = - MessagePack.unpack(raw, FieldModifiersClass.class); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - byte[] raw = MessagePack.pack(src); - - OptionalFieldModifiersClass dst = - MessagePack.unpack(raw, OptionalFieldModifiersClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } - - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testNestedFieldClass01() throws Exception { - BaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass dst = - MessagePack.unpack(raw, BaseClass.class); - assertEquals(src, dst); - } - - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - OptionalBaseClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass dst = - MessagePack.unpack(raw, OptionalBaseClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - public OptionalBaseClass() { - } - } - - public static class OptionalNestedClass { - @Optional - public int f2; - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - BaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - BaseClass2 dst = - MessagePack.unpack(raw, BaseClass2.class); - assertEquals(src, dst); - } - - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public BaseClass2() { - } - } - - @MessagePackMessage - public static class MessagePackMessageClass2 { - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - OptionalBaseClass2 src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseClass2 dst = - MessagePack.unpack(raw, OptionalBaseClass2.class); - assertEquals(src, dst); - } - - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - public OptionalBaseClass2() { - } - } - - @MessagePackMessage - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleSubClass dst = - MessagePack.unpack(raw, SampleSubClass.class); - assertEquals(src, dst); - } - - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleSubClass() { - } - } - - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - SampleOptionalSubClass src = null; - - byte[] raw = MessagePack.pack(src); - - SampleOptionalSubClass dst = - MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertEquals(src, dst); - } - - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleOptionalSubClass() { - } - } - - public static class SampleOptionalSuperClass { - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } - - @Test - public void testMessagePackableUnpackableClass00() throws Exception { - BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass(); - MessagePackableUnpackableClass src1 = new MessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testMessagePackableUnpackableClass01() throws Exception { - BaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - BaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, BaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class BaseMessagePackableUnpackableClass { - public MessagePackableUnpackableClass f0; - public int f1; - public List f2; - - public BaseMessagePackableUnpackableClass() { - } - } - - public static class MessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - public int f0; - public int f1; - - public MessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } - - @Test - public void testOptionalMessagePackableUnpackableClass00() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - OptionalMessagePackableUnpackableClass src1 = new OptionalMessagePackableUnpackableClass(); - List src2 = new ArrayList(); - src1.f0 = 0; - src1.f1 = 1; - src.f0 = src1; - src.f1 = 1; - src2.add(src1); - src.f2 = src2; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0.f0, dst.f0.f0); - assertEquals(src.f0.f1, dst.f0.f1); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0); - assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1); - } - - @Test - public void testOptionalMessagePackableUnpackableClass01() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = new OptionalBaseMessagePackableUnpackableClass(); - src.f0 = null; - src.f1 = 1; - src.f2 = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - } - - @Test - public void testOptionalMessagePackableUnpackableClass02() throws Exception { - OptionalBaseMessagePackableUnpackableClass src = null; - - byte[] raw = MessagePack.pack(src); - - OptionalBaseMessagePackableUnpackableClass dst = - MessagePack.unpack(raw, OptionalBaseMessagePackableUnpackableClass.class); - assertEquals(src, dst); - } - - public static class OptionalBaseMessagePackableUnpackableClass { - @Optional - public OptionalMessagePackableUnpackableClass f0; - @Optional - public int f1; - @Optional - public List f2; - - public OptionalBaseMessagePackableUnpackableClass() { - } - } - - public static class OptionalMessagePackableUnpackableClass implements - MessagePackable, MessageUnpackable { - @Optional - public int f0; - @Optional - public int f1; - - public OptionalMessagePackableUnpackableClass() { - } - - @Override - public void messagePack(Packer packer) throws IOException { - packer.packArray(2); - packer.pack(f0); - packer.pack(f1); - } - - @Override - public void messageUnpack(Unpacker unpacker) throws IOException, - MessageTypeException { - if (unpacker.tryUnpackNull()) { - return; - } - unpacker.unpackArray(); - f0 = unpacker.unpackInt(); - f1 = unpacker.unpackInt(); - } - } -} - diff --git a/java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackConvert.java b/java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackConvert.java deleted file mode 100644 index 455f0f3c..00000000 --- a/java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackConvert.java +++ /dev/null @@ -1,1360 +0,0 @@ -package org.msgpack.util; - -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.junit.Test; -import org.msgpack.MessagePack; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.Optional; -import org.msgpack.template.TemplateBuildException; -import org.msgpack.template.TemplateRegistry; - -import junit.framework.TestCase; - -public class TestTemplatePrecompilerPackConvert extends TestCase { - - @Test - public void testPrimitiveTypeFields00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class); - - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - try { - byte[] raw = MessagePack.pack(src); - PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw).convert( - PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(PrimitiveTypeFieldsClass.class); - } - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class); - - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - try { - byte[] raw = MessagePack.pack(src); - PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw).convert( - PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(PrimitiveTypeFieldsClass.class); - } - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class); - - PrimitiveTypeFieldsClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw).convert( - PrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(PrimitiveTypeFieldsClass.class); - } - } - - @MessagePackMessage - public static class PrimitiveTypeFieldsClass { - public byte f0; - public short f1; - public int f2; - public long f3; - public float f4; - public double f5; - public boolean f6; - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalPrimitiveTypeFieldsClass.class); - - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - try { - byte[] raw = MessagePack.pack(src); - PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw).convert( - PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(OptionalPrimitiveTypeFieldsClass.class); - } - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalPrimitiveTypeFieldsClass.class); - - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - try { - byte[] raw = MessagePack.pack(src); - OptionalPrimitiveTypeFieldsClass dst = MessagePack.unpack(raw) - .convert(OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(OptionalPrimitiveTypeFieldsClass.class); - } - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalPrimitiveTypeFieldsClass.class); - - OptionalPrimitiveTypeFieldsClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalPrimitiveTypeFieldsClass dst = MessagePack.unpack(raw) - .convert(OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(OptionalPrimitiveTypeFieldsClass.class); - } - } - - @MessagePackMessage - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(GeneralReferenceTypeFieldsClass.class); - - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - try { - byte[] raw = MessagePack.pack(src); - GeneralReferenceTypeFieldsClass dst = MessagePack.unpack(raw) - .convert(GeneralReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } finally { - TemplateRegistry.unregister(GeneralReferenceTypeFieldsClass.class); - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(GeneralReferenceTypeFieldsClass.class); - - GeneralReferenceTypeFieldsClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - GeneralReferenceTypeFieldsClass dst = MessagePack.unpack(raw) - .convert(GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(GeneralReferenceTypeFieldsClass.class); - } - } - - @MessagePackMessage - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(GeneralOptionalReferenceTypeFieldsClass.class); - - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - try { - byte[] raw = MessagePack.pack(src); - GeneralOptionalReferenceTypeFieldsClass dst = MessagePack.unpack( - raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } finally { - TemplateRegistry.unregister(GeneralOptionalReferenceTypeFieldsClass.class); - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(GeneralOptionalReferenceTypeFieldsClass.class); - - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - try { - byte[] raw = MessagePack.pack(src); - GeneralOptionalReferenceTypeFieldsClass dst = MessagePack.unpack( - raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9, dst.f9); - assertEquals(src.f10, dst.f10); - } finally { - TemplateRegistry.unregister(GeneralOptionalReferenceTypeFieldsClass.class); - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(GeneralOptionalReferenceTypeFieldsClass.class); - - GeneralOptionalReferenceTypeFieldsClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - GeneralOptionalReferenceTypeFieldsClass dst = MessagePack.unpack( - raw).convert(GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(GeneralOptionalReferenceTypeFieldsClass.class); - } - } - - @MessagePackMessage - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleListTypes.class); - - SampleListTypes src = new SampleListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - try { - byte[] raw = MessagePack.pack(src); - SampleListTypes dst = MessagePack.unpack(raw).convert( - SampleListTypes.class); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleListNestedType s = src.f4.get(i); - SampleListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } finally { - TemplateRegistry.unregister(SampleListNestedType.class); - TemplateRegistry.unregister(SampleListTypes.class); - } - } - - @Test - public void testListTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleListTypes.class); - - SampleListTypes src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleListTypes dst = MessagePack.unpack(raw).convert( - SampleListTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleListNestedType.class); - TemplateRegistry.unregister(SampleListTypes.class); - } - } - - @MessagePackMessage - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - public List> f3; - public List f4; - public List f5; - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListTypes.class); - - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalListTypes dst = MessagePack.unpack(raw).convert( - SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } finally { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListTypes.class); - - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalListTypes dst = MessagePack.unpack(raw).convert( - SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } finally { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - } - } - - @Test - public void testOptionalListTypes02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListTypes.class); - - SampleListTypes src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleListTypes dst = MessagePack.unpack(raw).convert( - SampleListTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - } - } - - @MessagePackMessage - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - public SampleOptionalListTypes() { - } - } - - @MessagePackMessage - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleMapTypes.class); - - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - try { - byte[] raw = MessagePack.pack(src); - SampleMapTypes dst = MessagePack.unpack(raw).convert( - SampleMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } finally { - TemplateRegistry.unregister(SampleMapTypes.class); - } - } - - @Test - public void testMapTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleMapTypes.class); - - SampleMapTypes src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleMapTypes dst = MessagePack.unpack(raw).convert(SampleMapTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleMapTypes.class); - } - } - - @MessagePackMessage - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalMapTypes.class); - - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalMapTypes dst = MessagePack.unpack(raw).convert( - SampleOptionalMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } finally { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalMapTypes.class); - - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalMapTypes dst = MessagePack.unpack(raw).convert( - SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } finally { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - } - } - - @Test - public void testOptionalMapTypes02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalMapTypes.class); - - SampleOptionalMapTypes src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalMapTypes dst = MessagePack.unpack(raw).convert( - SampleOptionalMapTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - } - } - - @MessagePackMessage - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - public SampleOptionalMapTypes() { - } - } - - @Test - public void testFinalClass() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - try { - TemplatePrecompiler.saveTemplateClass(FinalModifierClass.class); - assertTrue(true); - } catch (TemplateBuildException e) { - fail(); - } finally { - TemplateRegistry.unregister(FinalModifierClass.class); - } - } - - @MessagePackMessage - public final static class FinalModifierClass { - } - - @MessagePackMessage - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - try { - TemplatePrecompiler.saveTemplateClass(SampleInterface.class); - fail(); - } catch (Throwable t) { - assertTrue(t instanceof TemplateBuildException); - } finally { - TemplateRegistry.unregister(SampleInterface.class); - } - } - - @MessagePackMessage - public interface SampleInterface { - } - - @Test - public void testFieldModifiers() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(FieldModifiersClass.class); - - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - try { - byte[] raw = MessagePack.pack(src); - FieldModifiersClass dst = MessagePack.unpack(raw).convert( - FieldModifiersClass.class); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } finally { - TemplateRegistry.unregister(FieldModifiersClass.class); - } - } - - @MessagePackMessage - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalFieldModifiersClass.class); - - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - try { - byte[] raw = MessagePack.pack(src); - OptionalFieldModifiersClass dst = MessagePack.unpack(raw).convert( - OptionalFieldModifiersClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } finally { - TemplateRegistry.unregister(OptionalFieldModifiersClass.class); - } - } - - @MessagePackMessage - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(NestedClass.class); - TemplatePrecompiler.saveTemplateClass(BaseClass.class); - - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass dst = MessagePack.unpack(raw).convert(BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(NestedClass.class); - TemplateRegistry.unregister(BaseClass.class); - } - } - - @Test - public void testNestedFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(NestedClass.class); - TemplatePrecompiler.saveTemplateClass(BaseClass.class); - - BaseClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass dst = MessagePack.unpack(raw).convert(BaseClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(NestedClass.class); - TemplateRegistry.unregister(BaseClass.class); - } - } - - @MessagePackMessage - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - @MessagePackMessage - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalNestedClass.class); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass.class); - - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass dst = MessagePack.unpack(raw).convert( - OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(OptionalNestedClass.class); - TemplateRegistry.unregister(OptionalBaseClass.class); - } - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalNestedClass.class); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass.class); - - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass dst = MessagePack.unpack(raw).convert( - OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } finally { - TemplateRegistry.unregister(OptionalNestedClass.class); - TemplateRegistry.unregister(OptionalBaseClass.class); - } - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalNestedClass.class); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass.class); - - OptionalBaseClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass dst = MessagePack.unpack(raw).convert( - OptionalBaseClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(OptionalNestedClass.class); - TemplateRegistry.unregister(OptionalBaseClass.class); - } - } - - @MessagePackMessage - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - public OptionalBaseClass() { - } - } - - @MessagePackMessage - public static class OptionalNestedClass { - @Optional - public int f2; - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(MessagePackMessageClass2.class); - TemplatePrecompiler.saveTemplateClass(BaseClass2.class); - - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass2 dst = MessagePack.unpack(raw).convert(BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(MessagePackMessageClass2.class); - TemplateRegistry.unregister(BaseClass2.class); - } - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(MessagePackMessageClass2.class); - TemplatePrecompiler.saveTemplateClass(BaseClass2.class); - - BaseClass2 src = null; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass2 dst = MessagePack.unpack(raw).convert(BaseClass2.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(MessagePackMessageClass2.class); - TemplateRegistry.unregister(BaseClass2.class); - } - } - - @MessagePackMessage - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public BaseClass2() { - } - } - - @MessagePackMessage - public static class MessagePackMessageClass2 { - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass2.class); - TemplatePrecompiler.saveTemplateClass(OptionalMessagePackMessageClass2.class); - - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass2 dst = MessagePack.unpack(raw).convert( - OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(OptionalBaseClass2.class); - TemplateRegistry.unregister(OptionalMessagePackMessageClass2.class); - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass2.class); - TemplatePrecompiler.saveTemplateClass(OptionalMessagePackMessageClass2.class); - - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass2 dst = MessagePack.unpack(raw).convert( - OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } finally { - TemplateRegistry.unregister(OptionalBaseClass2.class); - TemplateRegistry.unregister(OptionalMessagePackMessageClass2.class); - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass2.class); - TemplatePrecompiler.saveTemplateClass(OptionalMessagePackMessageClass2.class); - - OptionalBaseClass2 src = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass2 dst = MessagePack.unpack(raw).convert( - OptionalBaseClass2.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(OptionalBaseClass2.class); - TemplateRegistry.unregister(OptionalMessagePackMessageClass2.class); - } - } - - @MessagePackMessage - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - public OptionalBaseClass2() { - } - } - - @MessagePackMessage - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleSubClass.class); - - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - try { - byte[] raw = MessagePack.pack(src); - SampleSubClass dst = MessagePack.unpack(raw).convert( - SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } finally { - TemplateRegistry.unregister(SampleSuperClass.class); - TemplateRegistry.unregister(SampleSubClass.class); - } - } - - @Test - public void testExtendedClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleSubClass.class); - - SampleSubClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleSubClass dst = MessagePack.unpack(raw).convert( - SampleSubClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleSuperClass.class); - TemplateRegistry.unregister(SampleSubClass.class); - } - } - - @MessagePackMessage - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleSubClass() { - } - } - - @MessagePackMessage - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSubClass.class); - - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalSubClass dst = MessagePack.unpack(raw).convert( - SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } finally { - TemplateRegistry.unregister(SampleOptionalSuperClass.class); - TemplateRegistry.unregister(SampleOptionalSubClass.class); - } - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSubClass.class); - - SampleOptionalSubClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalSubClass dst = MessagePack.unpack(raw).convert( - SampleOptionalSubClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleOptionalSuperClass.class); - TemplateRegistry.unregister(SampleOptionalSubClass.class); - } - } - - @MessagePackMessage - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleOptionalSubClass() { - } - } - - @MessagePackMessage - public static class SampleOptionalSuperClass { - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } -} diff --git a/java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackUnpack.java b/java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackUnpack.java deleted file mode 100644 index 7b0a7885..00000000 --- a/java/src/test/java/org/msgpack/util/TestTemplatePrecompilerPackUnpack.java +++ /dev/null @@ -1,1422 +0,0 @@ -package org.msgpack.util; - -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import junit.framework.TestCase; - -import org.junit.Test; -import org.msgpack.MessagePack; -import org.msgpack.annotation.MessagePackMessage; -import org.msgpack.annotation.Optional; -import org.msgpack.template.TemplateBuildException; -import org.msgpack.template.TemplateRegistry; - -public class TestTemplatePrecompilerPackUnpack extends TestCase { - - @Test - public void testPrimitiveTypeFields00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = PrimitiveTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class); - - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(c); - } - } - - @Test - public void testPrimitiveTypeFields01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = PrimitiveTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class); - - PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass(); - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(c); - } - } - - @Test - public void testPrimitiveTypeFields02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = PrimitiveTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class); - - PrimitiveTypeFieldsClass src = null; - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(c); - } - } - - @MessagePackMessage - public static class PrimitiveTypeFieldsClass { - public byte f0; - public short f1; - public int f2; - public long f3; - public float f4; - public double f5; - public boolean f6; - - public PrimitiveTypeFieldsClass() { - } - } - - @Test - public void testOptionalPrimitiveTypeFields00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = OptionalPrimitiveTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(OptionalPrimitiveTypeFieldsClass.class); - - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(c); - } - } - - @Test - public void testOptionalPrimitiveTypeFields01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = OptionalPrimitiveTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(c); - - OptionalPrimitiveTypeFieldsClass src = new OptionalPrimitiveTypeFieldsClass(); - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - } finally { - TemplateRegistry.unregister(c); - } - } - - @Test - public void testOptionalPrimitiveTypeFields02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = OptionalPrimitiveTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(c); - - OptionalPrimitiveTypeFieldsClass src = null; - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - OptionalPrimitiveTypeFieldsClass dst = - MessagePack.unpack(raw, OptionalPrimitiveTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(c); - } - } - - @MessagePackMessage - public static class OptionalPrimitiveTypeFieldsClass { - @Optional - public byte f0; - @Optional - public short f1; - @Optional - public int f2; - @Optional - public long f3; - @Optional - public float f4; - @Optional - public double f5; - @Optional - public boolean f6; - - public OptionalPrimitiveTypeFieldsClass() { - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = GeneralReferenceTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(c); - - GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } finally { - TemplateRegistry.unregister(c); - } - } - - @Test - public void testGeneralReferenceTypeFieldsClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = GeneralReferenceTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(c); - - GeneralReferenceTypeFieldsClass src = null; - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - GeneralReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(c); - } - } - - @MessagePackMessage - public static class GeneralReferenceTypeFieldsClass { - public Byte f0; - public Short f1; - public Integer f2; - public Long f3; - public Float f4; - public Double f5; - public Boolean f6; - public BigInteger f7; - public String f8; - public byte[] f9; - public ByteBuffer f10; - - public GeneralReferenceTypeFieldsClass() { - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = GeneralOptionalReferenceTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(c); - - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = (long) 3; - src.f4 = (float) 4; - src.f5 = (double) 5; - src.f6 = false; - src.f7 = new BigInteger("7"); - src.f8 = "8"; - src.f9 = new byte[] { 0x01, 0x02 }; - src.f10 = ByteBuffer.wrap("muga".getBytes()); - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9[0], dst.f9[0]); - assertEquals(src.f9[1], dst.f9[1]); - assertEquals(src.f10, dst.f10); - } finally { - TemplateRegistry.unregister(c); - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = GeneralOptionalReferenceTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(c); - - GeneralOptionalReferenceTypeFieldsClass src = new GeneralOptionalReferenceTypeFieldsClass(); - src.f0 = null; - src.f1 = null; - src.f2 = null; - src.f3 = null; - src.f4 = null; - src.f5 = null; - src.f6 = null; - src.f7 = null; - src.f8 = null; - src.f9 = null; - src.f10 = null; - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src.f0, dst.f0); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2, dst.f2); - assertEquals(src.f3, dst.f3); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5, dst.f5); - assertEquals(src.f6, dst.f6); - assertEquals(src.f7, dst.f7); - assertEquals(src.f8, dst.f8); - assertEquals(src.f9, dst.f9); - assertEquals(src.f10, dst.f10); - } finally { - TemplateRegistry.unregister(c); - } - } - - @Test - public void testGeneralOptionalReferenceTypeFieldsClass02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - Class c = GeneralOptionalReferenceTypeFieldsClass.class; - TemplatePrecompiler.saveTemplateClass(c); - - GeneralOptionalReferenceTypeFieldsClass src = null; - - try { - TemplateRegistry.unregister(c); - TemplateRegistry.lookup(c); - byte[] raw = MessagePack.pack(src); - GeneralOptionalReferenceTypeFieldsClass dst = - MessagePack.unpack(raw, GeneralOptionalReferenceTypeFieldsClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(c); - } - } - - @MessagePackMessage - public static class GeneralOptionalReferenceTypeFieldsClass { - @Optional - public Byte f0; - @Optional - public Short f1; - @Optional - public Integer f2; - @Optional - public Long f3; - @Optional - public Float f4; - @Optional - public Double f5; - @Optional - public Boolean f6; - @Optional - public BigInteger f7; - @Optional - public String f8; - @Optional - public byte[] f9; - @Optional - public ByteBuffer f10; - - public GeneralOptionalReferenceTypeFieldsClass() { - } - } - - @Test - public void testListTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleListTypes.class); - - SampleListTypes src = new SampleListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleListNestedType slnt = new SampleListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - try { - TemplateRegistry.unregister(SampleListNestedType.class); - TemplateRegistry.unregister(SampleListTypes.class); - TemplateRegistry.lookup(SampleListNestedType.class); - TemplateRegistry.lookup(SampleListTypes.class); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = MessagePack - .unpack(raw, SampleListTypes.class); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleListNestedType s = src.f4.get(i); - SampleListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } finally { - TemplateRegistry.unregister(SampleListNestedType.class); - TemplateRegistry.unregister(SampleListTypes.class); - } - } - - @Test - public void testListTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleListTypes.class); - - SampleListTypes src = null; - - try { - TemplateRegistry.unregister(SampleListNestedType.class); - TemplateRegistry.unregister(SampleListTypes.class); - TemplateRegistry.lookup(SampleListNestedType.class); - TemplateRegistry.lookup(SampleListTypes.class); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleListNestedType.class); - TemplateRegistry.unregister(SampleListTypes.class); - } - } - - @MessagePackMessage - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - public List> f3; - public List f4; - public List f5; - - public SampleListTypes() { - } - } - - @MessagePackMessage - public static class SampleListNestedType { - public byte[] f0; - public String f1; - - public SampleListNestedType() { - } - } - - @Test - public void testOptionalListTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListTypes.class); - - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = new ArrayList(); - src.f1.add(1); - src.f1.add(2); - src.f1.add(3); - src.f2 = new ArrayList(); - src.f2.add("e1"); - src.f2.add("e2"); - src.f2.add("e3"); - src.f3 = new ArrayList>(); - src.f3.add(src.f2); - src.f4 = new ArrayList(); - SampleOptionalListNestedType slnt = new SampleOptionalListNestedType(); - slnt.f0 = new byte[] { 0x01, 0x02 }; - slnt.f1 = "muga"; - src.f4.add(slnt); - src.f5 = new ArrayList(); - src.f5.add(ByteBuffer.wrap("e1".getBytes())); - src.f5.add(ByteBuffer.wrap("e2".getBytes())); - src.f5.add(ByteBuffer.wrap("e3".getBytes())); - - try { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListTypes.class); - TemplateRegistry.lookup(SampleOptionalListNestedType.class); - TemplateRegistry.lookup(SampleOptionalListTypes.class); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalListTypes dst = MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - for (int i = 0; i < src.f1.size(); ++i) { - assertEquals(src.f1.get(i), dst.f1.get(i)); - } - assertEquals(src.f2.size(), dst.f2.size()); - for (int i = 0; i < src.f2.size(); ++i) { - assertEquals(src.f2.get(i), dst.f2.get(i)); - } - assertEquals(src.f3.size(), dst.f3.size()); - for (int i = 0; i < src.f3.size(); ++i) { - List srclist = src.f3.get(i); - List dstlist = dst.f3.get(i); - assertEquals(srclist.size(), dstlist.size()); - for (int j = 0; j < srclist.size(); ++j) { - assertEquals(srclist.get(j), dstlist.get(j)); - } - } - assertEquals(src.f4.size(), dst.f4.size()); - for (int i = 0; i < src.f4.size(); ++i) { - SampleOptionalListNestedType s = src.f4.get(i); - SampleOptionalListNestedType d = dst.f4.get(i); - assertEquals(s.f0[0], d.f0[0]); - assertEquals(s.f0[1], d.f0[1]); - assertEquals(s.f1, d.f1); - } - assertEquals(src.f5.size(), dst.f5.size()); - for (int i = 0; i < src.f5.size(); ++i) { - ByteBuffer s = src.f5.get(i); - ByteBuffer d = dst.f5.get(i); - assertEquals(s, d); - } - } finally { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListTypes.class); - } - } - - @Test - public void testOptionalListTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListTypes.class); - - SampleOptionalListTypes src = new SampleOptionalListTypes(); - src.f0 = new ArrayList(); - src.f1 = null; - src.f2 = new ArrayList(); - src.f3 = new ArrayList>(); - src.f4 = null; - src.f5 = new ArrayList(); - - try { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListTypes.class); - TemplateRegistry.lookup(SampleOptionalListNestedType.class); - TemplateRegistry.lookup(SampleOptionalListTypes.class); - - byte[] raw = MessagePack.pack(src); - SampleOptionalListTypes dst = MessagePack.unpack(raw, SampleOptionalListTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - assertEquals(src.f3.size(), dst.f3.size()); - assertEquals(src.f4, dst.f4); - assertEquals(src.f5.size(), dst.f5.size()); - } finally { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListTypes.class); - } - } - - @Test - public void testOptionalListTypes02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListNestedType.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalListTypes.class); - - SampleListTypes src = null; - - try { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListTypes.class); - TemplateRegistry.lookup(SampleOptionalListNestedType.class); - TemplateRegistry.lookup(SampleOptionalListTypes.class); - - byte[] raw = MessagePack.pack(src); - - SampleListTypes dst = - MessagePack.unpack(raw, SampleListTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleOptionalListNestedType.class); - TemplateRegistry.unregister(SampleOptionalListTypes.class); - } - } - - @MessagePackMessage - public static class SampleOptionalListTypes { - @Optional - public List f0; - @Optional - public List f1; - @Optional - public List f2; - @Optional - public List> f3; - @Optional - public List f4; - @Optional - public List f5; - - public SampleOptionalListTypes() { - } - } - - @MessagePackMessage - public static class SampleOptionalListNestedType { - @Optional - public byte[] f0; - @Optional - public String f1; - - public SampleOptionalListNestedType() { - } - } - - @Test - public void testMapTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleMapTypes.class); - - SampleMapTypes src = new SampleMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - try { - TemplateRegistry.unregister(SampleMapTypes.class); - TemplateRegistry.lookup(SampleMapTypes.class); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } finally { - TemplateRegistry.unregister(SampleMapTypes.class); - } - } - - @Test - public void testMapTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleMapTypes.class); - - SampleMapTypes src = null; - - try { - TemplateRegistry.unregister(SampleMapTypes.class); - TemplateRegistry.lookup(SampleMapTypes.class); - - byte[] raw = MessagePack.pack(src); - - SampleMapTypes dst = MessagePack.unpack(raw, SampleMapTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleMapTypes.class); - } - } - - @MessagePackMessage - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testOptionalMapTypes00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalMapTypes.class); - - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = new HashMap(); - src.f1.put(1, 1); - src.f1.put(2, 2); - src.f1.put(3, 3); - src.f2 = new HashMap(); - src.f2.put("k1", 1); - src.f2.put("k2", 2); - src.f2.put("k3", 3); - - try { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - TemplateRegistry.lookup(SampleOptionalMapTypes.class); - - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = MessagePack.unpack(raw, - SampleOptionalMapTypes.class); - assertEquals(0, dst.f0.size()); - assertEquals(src.f1.size(), dst.f1.size()); - Iterator srcf1 = src.f1.keySet().iterator(); - while (srcf1.hasNext()) { - Integer s1 = srcf1.next(); - assertEquals(src.f1.get(s1), dst.f1.get(s1)); - } - assertEquals(src.f2.size(), dst.f2.size()); - Iterator srcf2 = src.f2.keySet().iterator(); - while (srcf2.hasNext()) { - String s2 = srcf2.next(); - assertEquals(src.f2.get(s2), dst.f2.get(s2)); - } - } finally { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - } - } - - @Test - public void testOptionalMapTypes01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalMapTypes.class); - - SampleOptionalMapTypes src = new SampleOptionalMapTypes(); - src.f0 = new HashMap(); - src.f1 = null; - src.f2 = new HashMap(); - - try { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - TemplateRegistry.lookup(SampleOptionalMapTypes.class); - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = MessagePack.unpack(raw, - SampleOptionalMapTypes.class); - assertEquals(src.f0.size(), dst.f0.size()); - assertEquals(src.f1, dst.f1); - assertEquals(src.f2.size(), dst.f2.size()); - } finally { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - } - } - - @Test - public void testOptionalMapTypes02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalMapTypes.class); - - SampleOptionalMapTypes src = null; - try { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - TemplateRegistry.lookup(SampleOptionalMapTypes.class); - byte[] raw = MessagePack.pack(src); - - SampleOptionalMapTypes dst = MessagePack.unpack(raw, - SampleOptionalMapTypes.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleOptionalMapTypes.class); - } - } - - @MessagePackMessage - public static class SampleOptionalMapTypes { - @Optional - public Map f0; - @Optional - public Map f1; - @Optional - public Map f2; - - public SampleOptionalMapTypes() { - } - } - - @Test - public void testFinalClass() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - try { - TemplatePrecompiler.saveTemplateClass(FinalModifierClass.class); - assertTrue(true); - } catch (TemplateBuildException e) { - fail(); - } finally { - TemplateRegistry.unregister(FinalModifierClass.class); - } - } - - @MessagePackMessage - public final static class FinalModifierClass { - } - - @MessagePackMessage - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceType00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - try { - TemplatePrecompiler.saveTemplateClass(SampleInterface.class); - fail(); - } catch (Throwable t) { - assertTrue(t instanceof TemplateBuildException); - } finally { - TemplateRegistry.unregister(SampleInterface.class); - } - } - - @MessagePackMessage - public interface SampleInterface { - } - - @Test - public void testFieldModifiers() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(FieldModifiersClass.class); - FieldModifiersClass src = new FieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - try { - byte[] raw = MessagePack.pack(src); - FieldModifiersClass dst = MessagePack.unpack(raw, FieldModifiersClass.class); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } finally { - TemplateRegistry.unregister(FieldModifiersClass.class); - } - } - - @MessagePackMessage - public static class FieldModifiersClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public FieldModifiersClass() { - } - } - - @Test - public void testOptionalFieldModifiers() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalFieldModifiersClass.class); - - OptionalFieldModifiersClass src = new OptionalFieldModifiersClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - - try { - byte[] raw = MessagePack.pack(src); - OptionalFieldModifiersClass dst = MessagePack.unpack(raw, - OptionalFieldModifiersClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - } finally { - TemplateRegistry.unregister(OptionalFieldModifiersClass.class); - } - } - - @MessagePackMessage - public static class OptionalFieldModifiersClass { - @Optional - public int f0; - @Optional - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public OptionalFieldModifiersClass() { - } - } - - @Test - public void testNestedFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(NestedClass.class); - TemplatePrecompiler.saveTemplateClass(BaseClass.class); - - BaseClass src = new BaseClass(); - NestedClass src2 = new NestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass dst = MessagePack.unpack(raw, BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(NestedClass.class); - TemplateRegistry.unregister(BaseClass.class); - } - } - - @Test - public void testNestedFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(NestedClass.class); - TemplatePrecompiler.saveTemplateClass(BaseClass.class); - - BaseClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass dst = MessagePack.unpack(raw, BaseClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(NestedClass.class); - TemplateRegistry.unregister(BaseClass.class); - } - } - - @MessagePackMessage - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - @MessagePackMessage - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testOptionalNestedFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalNestedClass.class); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass.class); - - OptionalBaseClass src = new OptionalBaseClass(); - OptionalNestedClass src2 = new OptionalNestedClass(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass dst = MessagePack.unpack(raw, OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(OptionalNestedClass.class); - TemplateRegistry.unregister(OptionalBaseClass.class); - } - } - - @Test - public void testOptionalNestedFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalNestedClass.class); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass.class); - - OptionalBaseClass src = new OptionalBaseClass(); - src.f1 = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass dst = MessagePack.unpack(raw, - OptionalBaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - } finally { - TemplateRegistry.unregister(OptionalNestedClass.class); - TemplateRegistry.unregister(OptionalBaseClass.class); - } - } - - @Test - public void testOptionalNestedFieldClass02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalNestedClass.class); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass.class); - - OptionalBaseClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass dst = MessagePack.unpack(raw, OptionalBaseClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(OptionalNestedClass.class); - TemplateRegistry.unregister(OptionalBaseClass.class); - } - } - - @MessagePackMessage - public static class OptionalBaseClass { - @Optional - public int f0; - @Optional - public OptionalNestedClass f1; - - public OptionalBaseClass() { - } - } - - @MessagePackMessage - public static class OptionalNestedClass { - @Optional - public int f2; - - public OptionalNestedClass() { - } - } - - @Test - public void testMessagePackMessageFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(MessagePackMessageClass2.class); - TemplatePrecompiler.saveTemplateClass(BaseClass2.class); - - BaseClass2 src = new BaseClass2(); - MessagePackMessageClass2 src2 = new MessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass2 dst = MessagePack.unpack(raw, BaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(MessagePackMessageClass2.class); - TemplateRegistry.unregister(BaseClass2.class); - } - } - - @Test - public void testMessagePackMessageFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(MessagePackMessageClass2.class); - TemplatePrecompiler.saveTemplateClass(BaseClass2.class); - - BaseClass2 src = null; - - try { - byte[] raw = MessagePack.pack(src); - BaseClass2 dst = MessagePack.unpack(raw, BaseClass2.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(MessagePackMessageClass2.class); - TemplateRegistry.unregister(BaseClass2.class); - } - } - - @MessagePackMessage - public static class BaseClass2 { - public int f0; - public MessagePackMessageClass2 f1; - - public BaseClass2() { - } - } - - @MessagePackMessage - public static class MessagePackMessageClass2 { - public int f2; - - public MessagePackMessageClass2() { - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass2.class); - TemplatePrecompiler.saveTemplateClass(OptionalMessagePackMessageClass2.class); - - OptionalBaseClass2 src = new OptionalBaseClass2(); - OptionalMessagePackMessageClass2 src2 = new OptionalMessagePackMessageClass2(); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass2 dst = MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1.f2 == dst.f1.f2); - } finally { - TemplateRegistry.unregister(OptionalBaseClass2.class); - TemplateRegistry.unregister(OptionalMessagePackMessageClass2.class); - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass2.class); - TemplatePrecompiler.saveTemplateClass(OptionalMessagePackMessageClass2.class); - - OptionalBaseClass2 src = new OptionalBaseClass2(); - src.f1 = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass2 dst = MessagePack.unpack(raw, OptionalBaseClass2.class); - assertTrue(src.f0 == dst.f0); - assertEquals(src.f1, dst.f1); - } finally { - TemplateRegistry.unregister(OptionalBaseClass2.class); - TemplateRegistry.unregister(OptionalMessagePackMessageClass2.class); - } - } - - @Test - public void testOptionalMessagePackMessageFieldClass02() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(OptionalBaseClass2.class); - TemplatePrecompiler.saveTemplateClass(OptionalMessagePackMessageClass2.class); - - OptionalBaseClass2 src = null; - - try { - byte[] raw = MessagePack.pack(src); - OptionalBaseClass2 dst = MessagePack.unpack(raw, OptionalBaseClass2.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(OptionalBaseClass2.class); - TemplateRegistry.unregister(OptionalMessagePackMessageClass2.class); - } - } - - @MessagePackMessage - public static class OptionalBaseClass2 { - @Optional - public int f0; - @Optional - public OptionalMessagePackMessageClass2 f1; - - public OptionalBaseClass2() { - } - } - - @MessagePackMessage - public static class OptionalMessagePackMessageClass2 { - @Optional - public int f2; - - public OptionalMessagePackMessageClass2() { - } - } - - @Test - public void testExtendedClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleSubClass.class); - - SampleSubClass src = new SampleSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - try { - byte[] raw = MessagePack.pack(src); - SampleSubClass dst = MessagePack.unpack(raw, SampleSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } finally { - TemplateRegistry.unregister(SampleSuperClass.class); - TemplateRegistry.unregister(SampleSubClass.class); - } - } - - @Test - public void testExtendedClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleSubClass.class); - - SampleSubClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleSubClass dst = MessagePack.unpack(raw, SampleSubClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleSuperClass.class); - TemplateRegistry.unregister(SampleSubClass.class); - } - } - - @MessagePackMessage - public static class SampleSubClass extends SampleSuperClass { - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleSubClass() { - } - } - - @MessagePackMessage - public static class SampleSuperClass { - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } - - @Test - public void testOptionalExtendedClass00() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSubClass.class); - - SampleOptionalSubClass src = new SampleOptionalSubClass(); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalSubClass dst = MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src.f1 == dst.f1); - assertTrue(src.f2 != dst.f2); - assertTrue(src.f3 != dst.f3); - assertTrue(src.f4 != dst.f4); - assertTrue(src.f5 == dst.f5); - assertTrue(src.f6 == dst.f6); - assertTrue(src.f8 != dst.f8); - assertTrue(src.f9 != dst.f9); - } finally { - TemplateRegistry.unregister(SampleOptionalSuperClass.class); - TemplateRegistry.unregister(SampleOptionalSubClass.class); - } - } - - @Test - public void testOptionalExtendedClass01() throws Exception { - System.getProperties().setProperty(TemplatePrecompiler.DEST, "./target/test-classes"); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSuperClass.class); - TemplatePrecompiler.saveTemplateClass(SampleOptionalSubClass.class); - - SampleOptionalSubClass src = null; - - try { - byte[] raw = MessagePack.pack(src); - SampleOptionalSubClass dst = MessagePack.unpack(raw, SampleOptionalSubClass.class); - assertEquals(src, dst); - } finally { - TemplateRegistry.unregister(SampleOptionalSuperClass.class); - TemplateRegistry.unregister(SampleOptionalSubClass.class); - } - } - - @MessagePackMessage - public static class SampleOptionalSubClass extends SampleOptionalSuperClass { - @Optional - public int f0; - public final int f1 = 1; - private int f2; - protected int f3; - int f4; - - public SampleOptionalSubClass() { - } - } - - @MessagePackMessage - public static class SampleOptionalSuperClass { - @Optional - public int f5; - public final int f6 = 2; - @SuppressWarnings("unused") - private int f7; - protected int f8; - int f9; - - public SampleOptionalSuperClass() { - } - } -} diff --git a/java/src/test/java/org/msgpack/util/test/AfterMethod.java b/java/src/test/java/org/msgpack/util/test/AfterMethod.java deleted file mode 100644 index 5873958d..00000000 --- a/java/src/test/java/org/msgpack/util/test/AfterMethod.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.msgpack.util.test; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.junit.Ignore; - -@Retention(value = RetentionPolicy.RUNTIME) -@Target(value = ElementType.METHOD) -@Ignore -public @interface AfterMethod { - String value(); -} diff --git a/java/src/test/java/org/msgpack/util/test/BeforeMethod.java b/java/src/test/java/org/msgpack/util/test/BeforeMethod.java deleted file mode 100644 index 06900009..00000000 --- a/java/src/test/java/org/msgpack/util/test/BeforeMethod.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.msgpack.util.test; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.junit.Ignore; - -@Retention(value = RetentionPolicy.RUNTIME) -@Target(value = ElementType.METHOD) -@Ignore -public @interface BeforeMethod { - String value(); -} diff --git a/java/src/test/java/org/msgpack/util/test/BlockContextJUnit4ClassRunner.java b/java/src/test/java/org/msgpack/util/test/BlockContextJUnit4ClassRunner.java deleted file mode 100644 index 6fa285b4..00000000 --- a/java/src/test/java/org/msgpack/util/test/BlockContextJUnit4ClassRunner.java +++ /dev/null @@ -1,234 +0,0 @@ -package org.msgpack.util.test; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.internal.AssumptionViolatedException; -import org.junit.internal.runners.model.EachTestNotifier; -import org.junit.internal.runners.model.ReflectiveCallable; -import org.junit.internal.runners.statements.Fail; -import org.junit.internal.runners.statements.RunAfters; -import org.junit.internal.runners.statements.RunBefores; -import org.junit.runner.notification.RunNotifier; -import org.junit.runner.notification.StoppedByUserException; -import org.junit.runners.BlockJUnit4ClassRunner; -import org.junit.runners.model.FrameworkMethod; -import org.junit.runners.model.InitializationError; -import org.junit.runners.model.Statement; -import org.junit.runners.model.TestClass; - -@Ignore -public final class BlockContextJUnit4ClassRunner extends BlockJUnit4ClassRunner { - - private TestClass tClass = null; - - public BlockContextJUnit4ClassRunner(Class c) throws InitializationError { - this(c, null); - } - - public BlockContextJUnit4ClassRunner(Class c, TestClass tClass) throws InitializationError { - super(c); - this.tClass = tClass; - } - - @Override - protected void collectInitializationErrors(List errors) { - super.collectInitializationErrors(errors); - } - - @Override - public void run(final RunNotifier notifier) { - EachTestNotifier testNotifier = new EachTestNotifier(notifier, getDescription()); - try { - Statement statement = classBlock(notifier); - statement.evaluate(); - } catch (AssumptionViolatedException e) { - testNotifier.fireTestIgnored(); - } catch (StoppedByUserException e) { - throw e; - } catch (Throwable e) { - testNotifier.addFailure(e); - } - } - - @Override - protected Statement classBlock(final RunNotifier notifier) { - return super.classBlock(notifier); - } - - @Override - protected Statement withBeforeClasses(Statement statement) { - Statement befores = super.withBeforeClasses(statement); - if (tClass == null) { - return befores; - } else { - List nestedBefores= tClass.getAnnotatedMethods(BeforeClass.class); - return nestedBefores.isEmpty() ? befores : new RunBefores(befores, nestedBefores, null); - } - } - - @Override - protected Statement withAfterClasses(Statement statement) { - Statement afters = super.withAfterClasses(statement); - if (tClass == null) { - return afters; - } else { - List nestedAfters= tClass.getAnnotatedMethods(AfterClass.class); - return nestedAfters.isEmpty() ? afters : new RunAfters(afters, nestedAfters, null); - - } - } - - @Override - protected void runChild(FrameworkMethod method, RunNotifier notifier) { - super.runChild(method, notifier); - } - - @Override - protected Object createTest() throws Exception { - return getTestClass().getOnlyConstructor().newInstance(); - } - - protected Object createNestedTest() { - try { - return tClass.getOnlyConstructor().newInstance(); - } catch (Throwable t) { - return new Fail(t); - } - } - - @Override - protected Statement methodBlock(FrameworkMethod method) { - Object test; - try { - test = new ReflectiveCallable() { - @Override - protected Object runReflectiveCall() throws Throwable { - return createTest(); - } - }.run(); - } catch (Throwable e) { - return new Fail(e); - } - - Statement statement = methodInvoker(method, test); - statement = possiblyExpectingExceptions(method, test, statement); - statement = withPotentialTimeout(method, test, statement); - statement = withBeforeMethods(method, test, statement); - statement = withBefores(method, test, statement); - statement = withAfterMethods(method, test, statement); - statement = withAfters(method, test, statement); - statement = withRules0(method, test, statement); - return statement; - } - - @Override - protected Statement methodInvoker(FrameworkMethod method, Object test) { - return super.methodInvoker(method, test); - } - - @Override @Deprecated - protected Statement possiblyExpectingExceptions(FrameworkMethod method, - Object test, Statement next) { - return super.possiblyExpectingExceptions(method, test, next); - } - - @Override @Deprecated - protected Statement withPotentialTimeout(FrameworkMethod method, - Object test, Statement next) { - return super.withPotentialTimeout(method, test, next); - } - - protected Statement withBeforeMethods(FrameworkMethod method, Object target, Statement statement) { - Statement beforeMethods = withBeforeMethods0(getTestClass(), method, target, statement); - if (tClass == null) { - return beforeMethods; - } else { - return withBeforeMethods0(tClass, method, target, beforeMethods); - } - } - - private Statement withBeforeMethods0(TestClass tc, FrameworkMethod method, Object target, Statement statement) { - List fms = new ArrayList(); - for (FrameworkMethod fm : tc.getAnnotatedMethods(BeforeMethod.class)) { - String targetMethodName = ((BeforeMethod) fm.getAnnotation(BeforeMethod.class)).value(); - if (method.getName().equals(targetMethodName)) { - fms.add(0, fm); - } - } - return fms.isEmpty() ? statement : new RunBefores(statement, fms, target); - } - - @Override @Deprecated - protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) { - Statement befores = super.withBefores(method, target, statement); - if (tClass == null) { - return befores; - } else { - List nestedBefores= tClass.getAnnotatedMethods(Before.class); - return nestedBefores.isEmpty() ? befores : new RunBefores(befores, nestedBefores, target); - - } - } - - protected Statement withAfterMethods(FrameworkMethod method, Object target, Statement statement) { - Statement afterMethods = withAfterMethods0(getTestClass(), method, target, statement); - if (tClass == null) { - return afterMethods; - } else { - return withAfterMethods0(tClass, method, target, afterMethods); - } - } - - private Statement withAfterMethods0(TestClass tc, FrameworkMethod method, Object target, Statement statement) { - List fms = new ArrayList(); - for (FrameworkMethod fm : tc.getAnnotatedMethods(AfterMethod.class)) { - String targetMethodName = ((AfterMethod) fm.getAnnotation(AfterMethod.class)).value(); - if (method.getName().equals(targetMethodName)) { - fms.add(fm); - } - } - return fms.isEmpty() ? statement : new RunAfters(statement, fms, target); - } - - @Override @Deprecated - protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) { - Statement afters = super.withAfters(method, target, statement); - if (tClass == null) { - return afters; - } else { - List nestedAfters= tClass.getAnnotatedMethods(After.class); - return nestedAfters.isEmpty() ? afters : new RunAfters(afters, nestedAfters, target); - } - } - - private Statement withRules0(FrameworkMethod method, Object test, Statement statement) { - Statement ret = null; - try { - Method m = BlockJUnit4ClassRunner.class.getDeclaredMethod( - "withRules", new Class[] { FrameworkMethod.class, - Object.class, Statement.class }); - m.setAccessible(true); - ret = (Statement) m.invoke(this, new Object[] { method, test, statement }); - m.setAccessible(false); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return ret; - } -} diff --git a/java/src/test/resources/cases.json b/java/src/test/resources/cases.json deleted file mode 100644 index fd390d48..00000000 --- a/java/src/test/resources/cases.json +++ /dev/null @@ -1 +0,0 @@ -[false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]] \ No newline at end of file diff --git a/java/src/test/resources/cases.mpac b/java/src/test/resources/cases.mpac deleted file mode 100644 index 5ec08c6a9af42d9568eb429a209a639616e94711..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 213 zcmXYp!4<+V3`3R4n8lOSY|v~#CV>aXw2-v7Qc6c)17ihrkbe}**V_dHM&J(DgGLop zU?R;l%8FI9$y_sy>V|HFdDW~{neAn-roN|Wd+OcH160;F91fo!