From dfb97e7961e7947d09240f18234a5aaced6e2d6f Mon Sep 17 00:00:00 2001 From: Muga Nishizawa Date: Mon, 27 Sep 2010 10:00:47 +0900 Subject: java: adds several annotations in an org.msgpack.annotation package and edits Packer.java and its test program --- .../org/msgpack/TestReflectionPackerTemplate.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java b/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java index f361eb4..27edde1 100644 --- a/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java +++ b/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java @@ -42,5 +42,33 @@ public class TestReflectionPackerTemplate { assertEquals(src.s1, dst.s1); assertEquals(src.s2, dst.s2); } + + @Test + public void testPackConvert02() throws Exception { + tString(); // FIXME link StringTemplate + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + CustomPacker.register(StringFieldClass.class, ReflectionPacker.create(StringFieldClass.class)); + + + StringFieldClass src = new StringFieldClass(); + + src.s1 = "kumofs"; + src.s2 = "frsyuki"; + + new Packer(out).pack(src); + + Template tmpl = ReflectionTemplate.create(StringFieldClass.class); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + + Object obj = tmpl.unpack(new Unpacker(in)); + assertEquals(obj.getClass(), StringFieldClass.class); + + StringFieldClass dst = (StringFieldClass)obj; + assertEquals(src.s1, dst.s1); + assertEquals(src.s2, dst.s2); + } } -- cgit v1.2.1 From 1c0afbc5c5abba1bddff6743581d4e1e48b895f8 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Mon, 27 Sep 2010 17:42:00 +0900 Subject: java: loads template classes when Unpacker, MessagePackObject or ClassTemplate is loaded --- java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java | 4 ---- 1 file changed, 4 deletions(-) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java b/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java index 27edde1..1f0016d 100644 --- a/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java +++ b/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java @@ -18,8 +18,6 @@ public class TestReflectionPackerTemplate { @Test public void testPackConvert() throws Exception { - tString(); // FIXME link StringTemplate - ByteArrayOutputStream out = new ByteArrayOutputStream(); MessagePacker packer = ReflectionPacker.create(StringFieldClass.class); @@ -45,8 +43,6 @@ public class TestReflectionPackerTemplate { @Test public void testPackConvert02() throws Exception { - tString(); // FIXME link StringTemplate - ByteArrayOutputStream out = new ByteArrayOutputStream(); CustomPacker.register(StringFieldClass.class, ReflectionPacker.create(StringFieldClass.class)); -- cgit v1.2.1 From 2736b88dd569cc0e11c9ea18a917ba4dfc1a095e Mon Sep 17 00:00:00 2001 From: Muga Nishizawa Date: Mon, 27 Sep 2010 17:55:48 +0900 Subject: edit Packer and Unpacker classes, and move org.msgpack.util.annotation.*.java to org.msgpack.util.codegen.*.java --- .../util/annotation/TestMessagePackUnpackable.java | 700 -------------------- .../util/codegen/TestDynamicCodeGenPacker.java | 51 ++ .../util/codegen/TestMessagePackUnpackable.java | 703 +++++++++++++++++++++ 3 files changed, 754 insertions(+), 700 deletions(-) delete mode 100644 java/src/test/java/org/msgpack/util/annotation/TestMessagePackUnpackable.java create mode 100644 java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java create mode 100644 java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/util/annotation/TestMessagePackUnpackable.java b/java/src/test/java/org/msgpack/util/annotation/TestMessagePackUnpackable.java deleted file mode 100644 index 08c4fa8..0000000 --- a/java/src/test/java/org/msgpack/util/annotation/TestMessagePackUnpackable.java +++ /dev/null @@ -1,700 +0,0 @@ -package org.msgpack.util.annotation; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.math.BigInteger; -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.MessagePackObject; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Unpacker; - -public class TestMessagePackUnpackable extends TestCase { - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = (PrimitiveTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(PrimitiveTypeFieldsClass.class); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(PrimitiveTypeFieldsClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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 = (PrimitiveTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(PrimitiveTypeFieldsClass.class); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil - .initEnhancedInstance(mpo, 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); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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 testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); - 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 }; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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]); - } - - @Test - public void testGeneralReferenceTypeFieldsClass02() throws Exception { - GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); - 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 }; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .initEnhancedInstance(mpo, - 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]); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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 GeneralReferenceTypeFieldsClass() { - } - } - - public void testListTypes01() throws Exception { - SampleListTypes src = (SampleListTypes) PackUnpackUtil - .newEnhancedInstance(SampleListTypes.class); - 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"); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - SampleListTypes dst = (SampleListTypes) PackUnpackUtil - .newEnhancedInstance(SampleListTypes.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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)); - } - } - - public void testListTypes02() throws Exception { - SampleListTypes src = (SampleListTypes) PackUnpackUtil - .newEnhancedInstance(SampleListTypes.class); - 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"); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - SampleListTypes dst = (SampleListTypes) PackUnpackUtil - .initEnhancedInstance(mpo, SampleListTypes.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)); - } - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - - public SampleListTypes() { - } - } - - public void testMapTypes01() throws Exception { - SampleMapTypes src = (SampleMapTypes) PackUnpackUtil - .newEnhancedInstance(SampleMapTypes.class); - 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); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil - .newEnhancedInstance(SampleMapTypes.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - assertEquals(src.f0.size(), 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(); - Integer d1 = dstf1.next(); - assertEquals(s1, d1); - assertEquals(src.f1.get(s1), dst.f1.get(d1)); - } - 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(); - String d2 = dstf2.next(); - assertEquals(s2, d2); - assertEquals(src.f2.get(s2), dst.f2.get(d2)); - } - } - - public void testMapTypes02() throws Exception { - SampleMapTypes src = (SampleMapTypes) PackUnpackUtil - .newEnhancedInstance(SampleMapTypes.class); - 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); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil - .initEnhancedInstance(mpo, SampleMapTypes.class); - assertEquals(src.f0.size(), 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(); - Integer d1 = dstf1.next(); - assertEquals(s1, d1); - assertEquals(src.f1.get(s1), dst.f1.get(d1)); - } - 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(); - String d2 = dstf2.next(); - assertEquals(s2, d2); - assertEquals(src.f2.get(s2), dst.f2.get(d2)); - } - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testDefaultConstructorModifiers() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(NoDefaultConstructorClass.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil - .newEnhancedInstance(PrivateDefaultConstructorClass.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil - .newEnhancedInstance(ProtectedDefaultConstructorClass.class); - assertTrue(true); - } catch (PackUnpackUtilException e) { - fail(); - } - assertTrue(true); - try { - PackUnpackUtil - .newEnhancedInstance(PackageDefaultConstructorClass.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - public static class NoDefaultConstructorClass { - public NoDefaultConstructorClass(int i) { - } - } - - @MessagePackUnpackable - public static class PrivateDefaultConstructorClass { - private PrivateDefaultConstructorClass() { - } - } - - @MessagePackUnpackable - public static class ProtectedDefaultConstructorClass { - protected ProtectedDefaultConstructorClass() { - } - } - - @MessagePackUnpackable - public static class PackageDefaultConstructorClass { - PackageDefaultConstructorClass() { - } - } - - @Test - public void testClassModifiers() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(PrivateModifierClass.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(ProtectedModifierClass.class); - assertTrue(true); - } catch (PackUnpackUtilException e) { - fail(); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(PackageModifierClass.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - private static class PrivateModifierClass { - } - - @MessagePackUnpackable - protected static class ProtectedModifierClass { - protected ProtectedModifierClass() { - } - } - - @MessagePackUnpackable - static class PackageModifierClass { - } - - @Test - public void testFinalClassAndAbstractClass() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(FinalModifierClass.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(AbstractModifierClass.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - public final static class FinalModifierClass { - } - - @MessagePackUnpackable - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceAndEnumType() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(SampleInterface.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(SampleEnum.class); - fail(); - } catch (PackUnpackUtilException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - public interface SampleInterface { - } - - @MessagePackUnpackable - public enum SampleEnum { - } - - @Test - public void testFieldModifiers01() throws Exception { - FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil - .newEnhancedInstance(FieldModifiersClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil - .newEnhancedInstance(FieldModifiersClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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); - } - - @Test - public void testFieldModifiers02() throws Exception { - FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil - .newEnhancedInstance(FieldModifiersClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil - .initEnhancedInstance(mpo, FieldModifiersClass.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); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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 testNestedAnnotatedFieldClass01() throws Exception { - NestedClass src2 = (NestedClass) PackUnpackUtil - .newEnhancedInstance(NestedClass.class); - BaseClass src = (BaseClass) PackUnpackUtil - .newEnhancedInstance(BaseClass.class); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - BaseClass dst = (BaseClass) PackUnpackUtil - .newEnhancedInstance(BaseClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - assertTrue(src.f0 == dst.f0); - assertTrue(src2.f2 == dst.f1.f2); - } - - @Test - public void testNestedAnnotatedFieldClass02() throws Exception { - NestedClass src2 = (NestedClass) PackUnpackUtil - .newEnhancedInstance(NestedClass.class); - BaseClass src = (BaseClass) PackUnpackUtil - .newEnhancedInstance(BaseClass.class); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - BaseClass dst = (BaseClass) PackUnpackUtil.initEnhancedInstance(mpo, - BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src2.f2 == dst.f1.f2); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - @MessagePackUnpackable - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = (SampleSubClass) PackUnpackUtil - .newEnhancedInstance(SampleSubClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - SampleSubClass dst = (SampleSubClass) PackUnpackUtil - .newEnhancedInstance(SampleSubClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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 testExtendedClass02() throws Exception { - SampleSubClass src = (SampleSubClass) PackUnpackUtil - .newEnhancedInstance(SampleSubClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - SampleSubClass dst = (SampleSubClass) PackUnpackUtil - .initEnhancedInstance(mpo, 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); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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; - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } -} diff --git a/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java b/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java new file mode 100644 index 0000000..d4e9c16 --- /dev/null +++ b/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java @@ -0,0 +1,51 @@ +package org.msgpack.util.codegen; + +import static org.msgpack.Templates.tString; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import org.junit.Test; +import org.msgpack.MessagePacker; +import org.msgpack.Packer; +import org.msgpack.ReflectionPacker; +import org.msgpack.ReflectionTemplate; +import org.msgpack.Template; +import org.msgpack.Unpacker; + +import junit.framework.TestCase; + + +public class TestDynamicCodeGenPacker extends TestCase { + public static class StringFieldClass { + public String s1; + public String s2; + public StringFieldClass() { } + } + + @Test + public void testPackConvert() throws Exception { + tString(); // FIXME link StringTemplate + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + MessagePacker packer = DynamicCodeGenPacker.create(StringFieldClass.class); + + StringFieldClass src = new StringFieldClass(); + + src.s1 = "kumofs"; + src.s2 = "frsyuki"; + + packer.pack(new Packer(out), src); + + Template tmpl = ReflectionTemplate.create(StringFieldClass.class); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + + Object obj = tmpl.unpack(new Unpacker(in)); + assertEquals(obj.getClass(), StringFieldClass.class); + + StringFieldClass dst = (StringFieldClass)obj; + assertEquals(src.s1, dst.s1); + assertEquals(src.s2, dst.s2); + } +} diff --git a/java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java b/java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java new file mode 100644 index 0000000..7e0bacc --- /dev/null +++ b/java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java @@ -0,0 +1,703 @@ +package org.msgpack.util.codegen; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.math.BigInteger; +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.MessagePackObject; +import org.msgpack.MessageUnpackable; +import org.msgpack.Packer; +import org.msgpack.Unpacker; +import org.msgpack.util.codegen.MessagePackUnpackable; +import org.msgpack.util.codegen.PackUnpackUtil; +import org.msgpack.util.codegen.DynamicCodeGenException; + +public class TestMessagePackUnpackable extends TestCase { + + @Test + public void testPrimitiveTypeFields01() throws Exception { + PrimitiveTypeFieldsClass src = (PrimitiveTypeFieldsClass) PackUnpackUtil + .newEnhancedInstance(PrimitiveTypeFieldsClass.class); + src.f0 = (byte) 0; + src.f1 = 1; + src.f2 = 2; + src.f3 = 3; + src.f4 = 4; + src.f5 = 5; + src.f6 = false; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil + .newEnhancedInstance(PrimitiveTypeFieldsClass.class); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + pac.unpack((MessageUnpackable) dst); + 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 = (PrimitiveTypeFieldsClass) PackUnpackUtil + .newEnhancedInstance(PrimitiveTypeFieldsClass.class); + src.f0 = (byte) 0; + src.f1 = 1; + src.f2 = 2; + src.f3 = 3; + src.f4 = 4; + src.f5 = 5; + src.f6 = false; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertTrue(it.hasNext()); + MessagePackObject mpo = it.next(); + PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil + .initEnhancedInstance(mpo, 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); + assertFalse(it.hasNext()); + } + + @MessagePackUnpackable + 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 testGeneralReferenceTypeFieldsClass01() throws Exception { + GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil + .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); + 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 }; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil + .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + pac.unpack((MessageUnpackable) dst); + 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]); + } + + @Test + public void testGeneralReferenceTypeFieldsClass02() throws Exception { + GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil + .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); + 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 }; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertTrue(it.hasNext()); + MessagePackObject mpo = it.next(); + GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil + .initEnhancedInstance(mpo, + 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]); + assertFalse(it.hasNext()); + } + + @MessagePackUnpackable + 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 GeneralReferenceTypeFieldsClass() { + } + } + + public void testListTypes01() throws Exception { + SampleListTypes src = (SampleListTypes) PackUnpackUtil + .newEnhancedInstance(SampleListTypes.class); + 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"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + SampleListTypes dst = (SampleListTypes) PackUnpackUtil + .newEnhancedInstance(SampleListTypes.class); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + pac.unpack((MessageUnpackable) dst); + 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)); + } + } + + public void testListTypes02() throws Exception { + SampleListTypes src = (SampleListTypes) PackUnpackUtil + .newEnhancedInstance(SampleListTypes.class); + 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"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertTrue(it.hasNext()); + MessagePackObject mpo = it.next(); + SampleListTypes dst = (SampleListTypes) PackUnpackUtil + .initEnhancedInstance(mpo, SampleListTypes.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)); + } + assertFalse(it.hasNext()); + } + + @MessagePackUnpackable + public static class SampleListTypes { + public List f0; + public List f1; + public List f2; + + public SampleListTypes() { + } + } + + public void testMapTypes01() throws Exception { + SampleMapTypes src = (SampleMapTypes) PackUnpackUtil + .newEnhancedInstance(SampleMapTypes.class); + 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); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil + .newEnhancedInstance(SampleMapTypes.class); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + pac.unpack((MessageUnpackable) dst); + assertEquals(src.f0.size(), 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(); + Integer d1 = dstf1.next(); + assertEquals(s1, d1); + assertEquals(src.f1.get(s1), dst.f1.get(d1)); + } + 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(); + String d2 = dstf2.next(); + assertEquals(s2, d2); + assertEquals(src.f2.get(s2), dst.f2.get(d2)); + } + } + + public void testMapTypes02() throws Exception { + SampleMapTypes src = (SampleMapTypes) PackUnpackUtil + .newEnhancedInstance(SampleMapTypes.class); + 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); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertTrue(it.hasNext()); + MessagePackObject mpo = it.next(); + SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil + .initEnhancedInstance(mpo, SampleMapTypes.class); + assertEquals(src.f0.size(), 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(); + Integer d1 = dstf1.next(); + assertEquals(s1, d1); + assertEquals(src.f1.get(s1), dst.f1.get(d1)); + } + 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(); + String d2 = dstf2.next(); + assertEquals(s2, d2); + assertEquals(src.f2.get(s2), dst.f2.get(d2)); + } + assertFalse(it.hasNext()); + } + + @MessagePackUnpackable + public static class SampleMapTypes { + public Map f0; + public Map f1; + public Map f2; + + public SampleMapTypes() { + } + } + + @Test + public void testDefaultConstructorModifiers() throws Exception { + try { + PackUnpackUtil.newEnhancedInstance(NoDefaultConstructorClass.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + try { + PackUnpackUtil + .newEnhancedInstance(PrivateDefaultConstructorClass.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + try { + PackUnpackUtil + .newEnhancedInstance(ProtectedDefaultConstructorClass.class); + assertTrue(true); + } catch (DynamicCodeGenException e) { + fail(); + } + assertTrue(true); + try { + PackUnpackUtil + .newEnhancedInstance(PackageDefaultConstructorClass.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + } + + @MessagePackUnpackable + public static class NoDefaultConstructorClass { + public NoDefaultConstructorClass(int i) { + } + } + + @MessagePackUnpackable + public static class PrivateDefaultConstructorClass { + private PrivateDefaultConstructorClass() { + } + } + + @MessagePackUnpackable + public static class ProtectedDefaultConstructorClass { + protected ProtectedDefaultConstructorClass() { + } + } + + @MessagePackUnpackable + public static class PackageDefaultConstructorClass { + PackageDefaultConstructorClass() { + } + } + + @Test + public void testClassModifiers() throws Exception { + try { + PackUnpackUtil.newEnhancedInstance(PrivateModifierClass.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + try { + PackUnpackUtil.newEnhancedInstance(ProtectedModifierClass.class); + assertTrue(true); + } catch (DynamicCodeGenException e) { + fail(); + } + assertTrue(true); + try { + PackUnpackUtil.newEnhancedInstance(PackageModifierClass.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + } + + @MessagePackUnpackable + private static class PrivateModifierClass { + } + + @MessagePackUnpackable + protected static class ProtectedModifierClass { + protected ProtectedModifierClass() { + } + } + + @MessagePackUnpackable + static class PackageModifierClass { + } + + @Test + public void testFinalClassAndAbstractClass() throws Exception { + try { + PackUnpackUtil.newEnhancedInstance(FinalModifierClass.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + try { + PackUnpackUtil.newEnhancedInstance(AbstractModifierClass.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + } + + @MessagePackUnpackable + public final static class FinalModifierClass { + } + + @MessagePackUnpackable + public abstract static class AbstractModifierClass { + } + + @Test + public void testInterfaceAndEnumType() throws Exception { + try { + PackUnpackUtil.newEnhancedInstance(SampleInterface.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + try { + PackUnpackUtil.newEnhancedInstance(SampleEnum.class); + fail(); + } catch (DynamicCodeGenException e) { + assertTrue(true); + } + assertTrue(true); + } + + @MessagePackUnpackable + public interface SampleInterface { + } + + @MessagePackUnpackable + public enum SampleEnum { + } + + @Test + public void testFieldModifiers01() throws Exception { + FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil + .newEnhancedInstance(FieldModifiersClass.class); + src.f0 = 0; + src.f2 = 2; + src.f3 = 3; + src.f4 = 4; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil + .newEnhancedInstance(FieldModifiersClass.class); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + pac.unpack((MessageUnpackable) dst); + 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); + } + + @Test + public void testFieldModifiers02() throws Exception { + FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil + .newEnhancedInstance(FieldModifiersClass.class); + src.f0 = 0; + src.f2 = 2; + src.f3 = 3; + src.f4 = 4; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertTrue(it.hasNext()); + MessagePackObject mpo = it.next(); + FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil + .initEnhancedInstance(mpo, FieldModifiersClass.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); + assertFalse(it.hasNext()); + } + + @MessagePackUnpackable + 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 testNestedAnnotatedFieldClass01() throws Exception { + NestedClass src2 = (NestedClass) PackUnpackUtil + .newEnhancedInstance(NestedClass.class); + BaseClass src = (BaseClass) PackUnpackUtil + .newEnhancedInstance(BaseClass.class); + src.f0 = 0; + src2.f2 = 2; + src.f1 = src2; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + BaseClass dst = (BaseClass) PackUnpackUtil + .newEnhancedInstance(BaseClass.class); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + pac.unpack((MessageUnpackable) dst); + assertTrue(src.f0 == dst.f0); + assertTrue(src2.f2 == dst.f1.f2); + } + + @Test + public void testNestedAnnotatedFieldClass02() throws Exception { + NestedClass src2 = (NestedClass) PackUnpackUtil + .newEnhancedInstance(NestedClass.class); + BaseClass src = (BaseClass) PackUnpackUtil + .newEnhancedInstance(BaseClass.class); + src.f0 = 0; + src2.f2 = 2; + src.f1 = src2; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertTrue(it.hasNext()); + MessagePackObject mpo = it.next(); + BaseClass dst = (BaseClass) PackUnpackUtil.initEnhancedInstance(mpo, + BaseClass.class); + assertTrue(src.f0 == dst.f0); + assertTrue(src2.f2 == dst.f1.f2); + assertFalse(it.hasNext()); + } + + @MessagePackUnpackable + public static class BaseClass { + public int f0; + public NestedClass f1; + + public BaseClass() { + } + } + + @MessagePackUnpackable + public static class NestedClass { + public int f2; + + public NestedClass() { + } + } + + @Test + public void testExtendedClass01() throws Exception { + SampleSubClass src = (SampleSubClass) PackUnpackUtil + .newEnhancedInstance(SampleSubClass.class); + src.f0 = 0; + src.f2 = 2; + src.f3 = 3; + src.f4 = 4; + src.f5 = 5; + src.f8 = 8; + src.f9 = 9; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + SampleSubClass dst = (SampleSubClass) PackUnpackUtil + .newEnhancedInstance(SampleSubClass.class); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + pac.unpack((MessageUnpackable) dst); + 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 testExtendedClass02() throws Exception { + SampleSubClass src = (SampleSubClass) PackUnpackUtil + .newEnhancedInstance(SampleSubClass.class); + src.f0 = 0; + src.f2 = 2; + src.f3 = 3; + src.f4 = 4; + src.f5 = 5; + src.f8 = 8; + src.f9 = 9; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(src); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertTrue(it.hasNext()); + MessagePackObject mpo = it.next(); + SampleSubClass dst = (SampleSubClass) PackUnpackUtil + .initEnhancedInstance(mpo, 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); + assertFalse(it.hasNext()); + } + + @MessagePackUnpackable + 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; + private int f7; + protected int f8; + int f9; + + public SampleSuperClass() { + } + } +} -- cgit v1.2.1 From bffe0443f9e20fb9bf55e58ab95510ed19e74ab7 Mon Sep 17 00:00:00 2001 From: Muga Nishizawa Date: Tue, 28 Sep 2010 12:17:32 +0900 Subject: edit DynamicCodeGenPacker and DynamicCodeGenUnpacker class --- .../util/codegen/TestDynamicCodeGenPacker.java | 48 +++++++++++++--------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java b/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java index d4e9c16..8851537 100644 --- a/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java +++ b/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java @@ -1,50 +1,60 @@ package org.msgpack.util.codegen; -import static org.msgpack.Templates.tString; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import org.junit.Test; import org.msgpack.MessagePacker; +import org.msgpack.MessageUnpacker; import org.msgpack.Packer; -import org.msgpack.ReflectionPacker; -import org.msgpack.ReflectionTemplate; import org.msgpack.Template; import org.msgpack.Unpacker; import junit.framework.TestCase; - public class TestDynamicCodeGenPacker extends TestCase { public static class StringFieldClass { public String s1; public String s2; - public StringFieldClass() { } + + public StringFieldClass() { + } } @Test - public void testPackConvert() throws Exception { - tString(); // FIXME link StringTemplate - + public void testPackConvert01() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); - MessagePacker packer = DynamicCodeGenPacker.create(StringFieldClass.class); + MessagePacker packer = DynamicCodeGenPacker + .create(StringFieldClass.class); + MessageUnpacker unpacker = DynamicCodeGenUnpacker + .create(StringFieldClass.class); StringFieldClass src = new StringFieldClass(); - - src.s1 = "kumofs"; - src.s2 = "frsyuki"; - + src.s1 = "muga"; + src.s2 = "nishizawa"; packer.pack(new Packer(out), src); - Template tmpl = ReflectionTemplate.create(StringFieldClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + StringFieldClass dst = (StringFieldClass) new Unpacker(in) + .unpack(unpacker); + assertEquals(src.s1, dst.s1); + assertEquals(src.s2, dst.s2); + } + + @Test + public void testPackConvert02() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + MessagePacker packer = DynamicCodeGenPacker + .create(StringFieldClass.class); + Template tmpl = DynamicCodeGenTemplate.create(StringFieldClass.class); - Object obj = tmpl.unpack(new Unpacker(in)); - assertEquals(obj.getClass(), StringFieldClass.class); + StringFieldClass src = new StringFieldClass(); + src.s1 = "muga"; + src.s2 = "nishizawa"; + packer.pack(new Packer(out), src); - StringFieldClass dst = (StringFieldClass)obj; + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + StringFieldClass dst = (StringFieldClass) tmpl.unpack(new Unpacker(in)); assertEquals(src.s1, dst.s1); assertEquals(src.s2, dst.s2); } -- cgit v1.2.1 From 923580d2cd2356e40b1d542df3625381b139f8cc Mon Sep 17 00:00:00 2001 From: Muga Nishizawa Date: Tue, 28 Sep 2010 15:31:55 +0900 Subject: write a simple test program for a DynamicCodeGenPacker class --- .../util/TestDynamicCodeGenPackerTemplate.java | 77 +++ .../util/codegen/TestDynamicCodeGenPacker.java | 61 -- .../util/codegen/TestMessagePackUnpackable.java | 703 --------------------- 3 files changed, 77 insertions(+), 764 deletions(-) create mode 100644 java/src/test/java/org/msgpack/util/TestDynamicCodeGenPackerTemplate.java delete mode 100644 java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java delete mode 100644 java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/util/TestDynamicCodeGenPackerTemplate.java b/java/src/test/java/org/msgpack/util/TestDynamicCodeGenPackerTemplate.java new file mode 100644 index 0000000..5b52c8a --- /dev/null +++ b/java/src/test/java/org/msgpack/util/TestDynamicCodeGenPackerTemplate.java @@ -0,0 +1,77 @@ +package org.msgpack.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import org.junit.Test; +import org.msgpack.CustomPacker; +import org.msgpack.MessagePacker; +import org.msgpack.Packer; +import org.msgpack.ReflectionTemplate; +import org.msgpack.Template; +import org.msgpack.Unpacker; +import org.msgpack.util.codegen.DynamicCodeGenPacker; +import org.msgpack.util.codegen.DynamicCodeGenTemplate; + +import static org.junit.Assert.*; + +public class TestDynamicCodeGenPackerTemplate { + + public static class StringFieldClass { + public String s1; + public String s2; + public StringFieldClass() { } + } + + @Test + public void testPackConvert() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + MessagePacker packer = DynamicCodeGenPacker.create(StringFieldClass.class); + + StringFieldClass src = new StringFieldClass(); + + src.s1 = "kumofs"; + src.s2 = "frsyuki"; + + packer.pack(new Packer(out), src); + + Template tmpl = DynamicCodeGenTemplate.create(StringFieldClass.class); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + + Object obj = tmpl.unpack(new Unpacker(in)); + assertEquals(obj.getClass(), StringFieldClass.class); + + StringFieldClass dst = (StringFieldClass)obj; + assertEquals(src.s1, dst.s1); + assertEquals(src.s2, dst.s2); + } + + @Test + public void testPackConvert02() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + CustomPacker.register(StringFieldClass.class, DynamicCodeGenPacker.create(StringFieldClass.class)); + + + StringFieldClass src = new StringFieldClass(); + + src.s1 = "kumofs"; + src.s2 = "frsyuki"; + + new Packer(out).pack(src); + + Template tmpl = ReflectionTemplate.create(StringFieldClass.class); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + + Object obj = tmpl.unpack(new Unpacker(in)); + assertEquals(obj.getClass(), StringFieldClass.class); + + StringFieldClass dst = (StringFieldClass)obj; + assertEquals(src.s1, dst.s1); + assertEquals(src.s2, dst.s2); + } +} + diff --git a/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java b/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java deleted file mode 100644 index 8851537..0000000 --- a/java/src/test/java/org/msgpack/util/codegen/TestDynamicCodeGenPacker.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.msgpack.util.codegen; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -import org.junit.Test; -import org.msgpack.MessagePacker; -import org.msgpack.MessageUnpacker; -import org.msgpack.Packer; -import org.msgpack.Template; -import org.msgpack.Unpacker; - -import junit.framework.TestCase; - -public class TestDynamicCodeGenPacker extends TestCase { - public static class StringFieldClass { - public String s1; - public String s2; - - public StringFieldClass() { - } - } - - @Test - public void testPackConvert01() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - MessagePacker packer = DynamicCodeGenPacker - .create(StringFieldClass.class); - MessageUnpacker unpacker = DynamicCodeGenUnpacker - .create(StringFieldClass.class); - - StringFieldClass src = new StringFieldClass(); - src.s1 = "muga"; - src.s2 = "nishizawa"; - packer.pack(new Packer(out), src); - - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - StringFieldClass dst = (StringFieldClass) new Unpacker(in) - .unpack(unpacker); - assertEquals(src.s1, dst.s1); - assertEquals(src.s2, dst.s2); - } - - @Test - public void testPackConvert02() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - MessagePacker packer = DynamicCodeGenPacker - .create(StringFieldClass.class); - Template tmpl = DynamicCodeGenTemplate.create(StringFieldClass.class); - - StringFieldClass src = new StringFieldClass(); - src.s1 = "muga"; - src.s2 = "nishizawa"; - packer.pack(new Packer(out), src); - - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - StringFieldClass dst = (StringFieldClass) tmpl.unpack(new Unpacker(in)); - assertEquals(src.s1, dst.s1); - assertEquals(src.s2, dst.s2); - } -} diff --git a/java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java b/java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java deleted file mode 100644 index 7e0bacc..0000000 --- a/java/src/test/java/org/msgpack/util/codegen/TestMessagePackUnpackable.java +++ /dev/null @@ -1,703 +0,0 @@ -package org.msgpack.util.codegen; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.math.BigInteger; -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.MessagePackObject; -import org.msgpack.MessageUnpackable; -import org.msgpack.Packer; -import org.msgpack.Unpacker; -import org.msgpack.util.codegen.MessagePackUnpackable; -import org.msgpack.util.codegen.PackUnpackUtil; -import org.msgpack.util.codegen.DynamicCodeGenException; - -public class TestMessagePackUnpackable extends TestCase { - - @Test - public void testPrimitiveTypeFields01() throws Exception { - PrimitiveTypeFieldsClass src = (PrimitiveTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(PrimitiveTypeFieldsClass.class); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(PrimitiveTypeFieldsClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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 = (PrimitiveTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(PrimitiveTypeFieldsClass.class); - src.f0 = (byte) 0; - src.f1 = 1; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f6 = false; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil - .initEnhancedInstance(mpo, 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); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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 testGeneralReferenceTypeFieldsClass01() throws Exception { - GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); - 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 }; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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]); - } - - @Test - public void testGeneralReferenceTypeFieldsClass02() throws Exception { - GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); - 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 }; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil - .initEnhancedInstance(mpo, - 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]); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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 GeneralReferenceTypeFieldsClass() { - } - } - - public void testListTypes01() throws Exception { - SampleListTypes src = (SampleListTypes) PackUnpackUtil - .newEnhancedInstance(SampleListTypes.class); - 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"); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - SampleListTypes dst = (SampleListTypes) PackUnpackUtil - .newEnhancedInstance(SampleListTypes.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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)); - } - } - - public void testListTypes02() throws Exception { - SampleListTypes src = (SampleListTypes) PackUnpackUtil - .newEnhancedInstance(SampleListTypes.class); - 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"); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - SampleListTypes dst = (SampleListTypes) PackUnpackUtil - .initEnhancedInstance(mpo, SampleListTypes.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)); - } - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - public static class SampleListTypes { - public List f0; - public List f1; - public List f2; - - public SampleListTypes() { - } - } - - public void testMapTypes01() throws Exception { - SampleMapTypes src = (SampleMapTypes) PackUnpackUtil - .newEnhancedInstance(SampleMapTypes.class); - 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); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil - .newEnhancedInstance(SampleMapTypes.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - assertEquals(src.f0.size(), 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(); - Integer d1 = dstf1.next(); - assertEquals(s1, d1); - assertEquals(src.f1.get(s1), dst.f1.get(d1)); - } - 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(); - String d2 = dstf2.next(); - assertEquals(s2, d2); - assertEquals(src.f2.get(s2), dst.f2.get(d2)); - } - } - - public void testMapTypes02() throws Exception { - SampleMapTypes src = (SampleMapTypes) PackUnpackUtil - .newEnhancedInstance(SampleMapTypes.class); - 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); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil - .initEnhancedInstance(mpo, SampleMapTypes.class); - assertEquals(src.f0.size(), 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(); - Integer d1 = dstf1.next(); - assertEquals(s1, d1); - assertEquals(src.f1.get(s1), dst.f1.get(d1)); - } - 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(); - String d2 = dstf2.next(); - assertEquals(s2, d2); - assertEquals(src.f2.get(s2), dst.f2.get(d2)); - } - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - public static class SampleMapTypes { - public Map f0; - public Map f1; - public Map f2; - - public SampleMapTypes() { - } - } - - @Test - public void testDefaultConstructorModifiers() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(NoDefaultConstructorClass.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil - .newEnhancedInstance(PrivateDefaultConstructorClass.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil - .newEnhancedInstance(ProtectedDefaultConstructorClass.class); - assertTrue(true); - } catch (DynamicCodeGenException e) { - fail(); - } - assertTrue(true); - try { - PackUnpackUtil - .newEnhancedInstance(PackageDefaultConstructorClass.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - public static class NoDefaultConstructorClass { - public NoDefaultConstructorClass(int i) { - } - } - - @MessagePackUnpackable - public static class PrivateDefaultConstructorClass { - private PrivateDefaultConstructorClass() { - } - } - - @MessagePackUnpackable - public static class ProtectedDefaultConstructorClass { - protected ProtectedDefaultConstructorClass() { - } - } - - @MessagePackUnpackable - public static class PackageDefaultConstructorClass { - PackageDefaultConstructorClass() { - } - } - - @Test - public void testClassModifiers() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(PrivateModifierClass.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(ProtectedModifierClass.class); - assertTrue(true); - } catch (DynamicCodeGenException e) { - fail(); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(PackageModifierClass.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - private static class PrivateModifierClass { - } - - @MessagePackUnpackable - protected static class ProtectedModifierClass { - protected ProtectedModifierClass() { - } - } - - @MessagePackUnpackable - static class PackageModifierClass { - } - - @Test - public void testFinalClassAndAbstractClass() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(FinalModifierClass.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(AbstractModifierClass.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - public final static class FinalModifierClass { - } - - @MessagePackUnpackable - public abstract static class AbstractModifierClass { - } - - @Test - public void testInterfaceAndEnumType() throws Exception { - try { - PackUnpackUtil.newEnhancedInstance(SampleInterface.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - try { - PackUnpackUtil.newEnhancedInstance(SampleEnum.class); - fail(); - } catch (DynamicCodeGenException e) { - assertTrue(true); - } - assertTrue(true); - } - - @MessagePackUnpackable - public interface SampleInterface { - } - - @MessagePackUnpackable - public enum SampleEnum { - } - - @Test - public void testFieldModifiers01() throws Exception { - FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil - .newEnhancedInstance(FieldModifiersClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil - .newEnhancedInstance(FieldModifiersClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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); - } - - @Test - public void testFieldModifiers02() throws Exception { - FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil - .newEnhancedInstance(FieldModifiersClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil - .initEnhancedInstance(mpo, FieldModifiersClass.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); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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 testNestedAnnotatedFieldClass01() throws Exception { - NestedClass src2 = (NestedClass) PackUnpackUtil - .newEnhancedInstance(NestedClass.class); - BaseClass src = (BaseClass) PackUnpackUtil - .newEnhancedInstance(BaseClass.class); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - BaseClass dst = (BaseClass) PackUnpackUtil - .newEnhancedInstance(BaseClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - assertTrue(src.f0 == dst.f0); - assertTrue(src2.f2 == dst.f1.f2); - } - - @Test - public void testNestedAnnotatedFieldClass02() throws Exception { - NestedClass src2 = (NestedClass) PackUnpackUtil - .newEnhancedInstance(NestedClass.class); - BaseClass src = (BaseClass) PackUnpackUtil - .newEnhancedInstance(BaseClass.class); - src.f0 = 0; - src2.f2 = 2; - src.f1 = src2; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - BaseClass dst = (BaseClass) PackUnpackUtil.initEnhancedInstance(mpo, - BaseClass.class); - assertTrue(src.f0 == dst.f0); - assertTrue(src2.f2 == dst.f1.f2); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - public static class BaseClass { - public int f0; - public NestedClass f1; - - public BaseClass() { - } - } - - @MessagePackUnpackable - public static class NestedClass { - public int f2; - - public NestedClass() { - } - } - - @Test - public void testExtendedClass01() throws Exception { - SampleSubClass src = (SampleSubClass) PackUnpackUtil - .newEnhancedInstance(SampleSubClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - SampleSubClass dst = (SampleSubClass) PackUnpackUtil - .newEnhancedInstance(SampleSubClass.class); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - pac.unpack((MessageUnpackable) dst); - 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 testExtendedClass02() throws Exception { - SampleSubClass src = (SampleSubClass) PackUnpackUtil - .newEnhancedInstance(SampleSubClass.class); - src.f0 = 0; - src.f2 = 2; - src.f3 = 3; - src.f4 = 4; - src.f5 = 5; - src.f8 = 8; - src.f9 = 9; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(src); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - Iterator it = pac.iterator(); - assertTrue(it.hasNext()); - MessagePackObject mpo = it.next(); - SampleSubClass dst = (SampleSubClass) PackUnpackUtil - .initEnhancedInstance(mpo, 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); - assertFalse(it.hasNext()); - } - - @MessagePackUnpackable - 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; - private int f7; - protected int f8; - int f9; - - public SampleSuperClass() { - } - } -} -- cgit v1.2.1