http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripObjectsWithSpecialMethodsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripObjectsWithSpecialMethodsTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripObjectsWithSpecialMethodsTest.java new file mode 100755 index 0000000..d40ebf3 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripObjectsWithSpecialMethodsTest.java @@ -0,0 +1,114 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.annotation.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@SuppressWarnings("javadoc") +public class RoundTripObjectsWithSpecialMethodsTest extends RoundTripTest { + + public RoundTripObjectsWithSpecialMethodsTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // @NameProperty method. + //==================================================================================================== + @Test + public void testNameProperty() throws Exception { + A t = new A().init(); + t = roundTrip(t); + assertObjectEquals("{a2:{f2:2},m:{k1:{f2:2}}}", t); + if (isValidationOnly()) + return; + assertEquals("a2", t.a2.name); + assertEquals("k1", t.m.get("k1").name); + } + + public static class A { + public A2 a2; + public Map<String,A2> m; + + A init() { + a2 = new A2().init(); + m = new LinkedHashMap<String,A2>(); + m.put("k1", new A2().init()); + return this; + } + + } + public static class A2 { + String name; + public int f2; + + @NameProperty + protected void setName(String name) { + this.name = name; + } + + A2 init() { + f2 = 2; + return this; + } + } + + //==================================================================================================== + // @ParentProperty method. + //==================================================================================================== + @Test + public void testParentProperty() throws Exception { + B t = new B().init(); + t = roundTrip(t); + if (isValidationOnly()) + return; + assertEquals(t.f1, t.b2.parent.f1); + } + + public static class B { + public int f1; + public B2 b2; + + B init() { + f1 = 1; + b2 = new B2().init(); + return this; + } + + } + public static class B2 { + B parent; + public int f2; + + @ParentProperty + protected void setParent(B parent) { + this.parent = parent; + } + + B2 init() { + f2 = 2; + return this; + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitiveObjectBeansTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitiveObjectBeansTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitiveObjectBeansTest.java new file mode 100755 index 0000000..a8eefcb --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitiveObjectBeansTest.java @@ -0,0 +1,198 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.junit.Assert.*; + +import org.apache.juneau.jena.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.testbeans.*; +import org.junit.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@SuppressWarnings("javadoc") +public class RoundTripPrimitiveObjectBeansTest extends RoundTripTest { + + public RoundTripPrimitiveObjectBeansTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // testPrimitiveObjectsBean + //==================================================================================================== + @Test + public void testPrimitiveObjectsBean() throws Exception { + PrimitiveObjectsBean t = new PrimitiveObjectsBean().init(); + t = roundTrip(t, PrimitiveObjectsBean.class); + t = roundTrip(t, PrimitiveObjectsBean.class); + + // primitives + assertEquals(Boolean.valueOf(true), t.poBoolean); + assertEquals(Byte.valueOf((byte)1), t.poByte); + assertEquals(Character.valueOf('a'), t.poChar); + assertEquals(Short.valueOf("2"), t.poShort); + assertEquals(Integer.valueOf(3), t.poInt); + assertEquals(Long.valueOf(4), t.poLong); + assertEquals(Float.valueOf(5), t.poFloat); + assertEquals(Double.valueOf(6), t.poDouble); + assertEquals(Integer.valueOf(7), t.poNumber); + assertEquals(8, t.poBigInteger.intValue()); + assertTrue(t.poBigDecimal.floatValue() == 9f); + + // uninitialized primitives + assertNull(t.pouBoolean); + assertNull(t.pouByte); + assertNull(t.pouChar); + assertNull(t.pouShort); + assertNull(t.pouInt); + assertNull(t.pouLong); + assertNull(t.pouFloat); + assertNull(t.pouDouble); + assertNull(t.pouNumber); + assertNull(t.pouBigInteger); + assertNull(t.pouBigDecimal); + + // primitive arrays + assertEquals(Boolean.valueOf(false), t.poaBoolean[1][0]); + assertEquals(Byte.valueOf((byte)2), t.poaByte[1][0]); + assertEquals(Character.valueOf('b'), t.poaChar[1][0]); + assertEquals(Short.valueOf("2"), t.poaShort[1][0]); + assertEquals(Integer.valueOf(2), t.poaInt[1][0]); + assertEquals(Long.valueOf(2), t.poaLong[1][0]); + assertEquals(Float.valueOf(2), t.poaFloat[1][0]); + assertEquals(Double.valueOf(2), t.poaDouble[1][0]); + assertEquals(Integer.valueOf(2), t.poaNumber[1][0]); + assertEquals(2, t.poaBigInteger[1][0].intValue()); + assertEquals(2, t.poaBigDecimal[1][0].intValue()); + assertNull(t.poaBoolean[2]); + assertNull(t.poaByte[2]); + assertNull(t.poaChar[2]); + assertNull(t.poaShort[2]); + assertNull(t.poaInt[2]); + assertNull(t.poaLong[2]); + assertNull(t.poaFloat[2]); + assertNull(t.poaDouble[2]); + assertNull(t.poaNumber[2]); + assertNull(t.poaBigInteger[2]); + assertNull(t.poaBigDecimal[2]); + + // uninitialized primitive arrays + assertNull(t.poauBoolean); + assertNull(t.poauByte); + assertNull(t.poauChar); + assertNull(t.poauShort); + assertNull(t.poauInt); + assertNull(t.poauLong); + assertNull(t.poauFloat); + assertNull(t.poauDouble); + assertNull(t.poauNumber); + assertNull(t.poauBigInteger); + assertNull(t.poauBigDecimal); + + // anonymous list of object primitive arrays + assertEquals(Boolean.valueOf(true), t.poalBoolean.get(0)[0]); + assertEquals(Byte.valueOf((byte)1), t.poalByte.get(0)[0]); + assertEquals(Character.valueOf('a'), t.poalChar.get(0)[0]); + assertEquals(Short.valueOf((short)1), t.poalShort.get(0)[0]); + assertEquals(Integer.valueOf(1), t.poalInt.get(0)[0]); + assertEquals(Long.valueOf(1l), t.poalLong.get(0)[0]); + assertEquals(Float.valueOf(1f), t.poalFloat.get(0)[0]); + assertEquals(Double.valueOf(1d), t.poalDouble.get(0)[0]); + assertEquals(1, t.poalBigInteger.get(0)[0].intValue()); + assertEquals(1, t.poalBigDecimal.get(0)[0].intValue()); + assertNull(t.poalBoolean.get(1)); + assertNull(t.poalByte.get(1)); + assertNull(t.poalChar.get(1)); + assertNull(t.poalShort.get(1)); + assertNull(t.poalInt.get(1)); + assertNull(t.poalLong.get(1)); + assertNull(t.poalFloat.get(1)); + assertNull(t.poalDouble.get(1)); + assertNull(t.poalNumber.get(1)); + assertNull(t.poalBigInteger.get(1)); + assertNull(t.poalBigDecimal.get(1)); + + // regular list of object primitive arrays + assertEquals(Boolean.valueOf(true), t.polBoolean.get(0)[0]); + assertEquals(Byte.valueOf((byte)1), t.polByte.get(0)[0]); + assertEquals(Character.valueOf('a'), t.polChar.get(0)[0]); + assertEquals(Short.valueOf((short)1), t.polShort.get(0)[0]); + assertEquals(Integer.valueOf(1), t.polInt.get(0)[0]); + assertEquals(Long.valueOf(1l), t.polLong.get(0)[0]); + assertEquals(Float.valueOf(1f), t.polFloat.get(0)[0]); + assertEquals(Double.valueOf(1d), t.polDouble.get(0)[0]); + assertEquals(1, t.polBigInteger.get(0)[0].intValue()); + assertEquals(1, t.polBigDecimal.get(0)[0].intValue()); + assertNull(t.polBoolean.get(1)); + assertNull(t.polByte.get(1)); + assertNull(t.polChar.get(1)); + assertNull(t.polShort.get(1)); + assertNull(t.polInt.get(1)); + assertNull(t.polLong.get(1)); + assertNull(t.polFloat.get(1)); + assertNull(t.polDouble.get(1)); + assertNull(t.polNumber.get(1)); + assertNull(t.polBigInteger.get(1)); + assertNull(t.polBigDecimal.get(1)); + } + + //==================================================================================================== + // testPrimitiveAtomicObjectsBean + //==================================================================================================== + @Test + public void testPrimitiveAtomicObjectsBean() throws Exception { + + // Jena does not support parsing into AtomicIntegers and AtomicLongs. + if (getSerializer() instanceof RdfSerializer) + return; + + PrimitiveAtomicObjectsBean t = new PrimitiveAtomicObjectsBean().init(); + t = roundTrip(t, PrimitiveAtomicObjectsBean.class); + t = roundTrip(t, PrimitiveAtomicObjectsBean.class); + + // primitives + assertEquals(1, t.poAtomicInteger.intValue()); + assertEquals(2, t.poAtomicLong.intValue()); + + // uninitialized primitives + assertNull(t.pouAtomicInteger); + assertNull(t.pouAtomicLong); + + // primitive arrays + assertEquals(2, t.poaAtomicInteger[1][0].intValue()); + assertEquals(2, t.poaAtomicLong[1][0].intValue()); + assertNull(t.poaAtomicInteger[2]); + assertNull(t.poaAtomicLong[2]); + + // uninitialized primitive arrays + assertNull(t.poauAtomicInteger); + assertNull(t.poauAtomicLong); + + // anonymous list of object primitive arrays + assertEquals(1, t.poalAtomicInteger.get(0)[0].intValue()); + assertEquals(1, t.poalAtomicLong.get(0)[0].intValue()); + assertNull(t.poalAtomicInteger.get(1)); + assertNull(t.poalAtomicLong.get(1)); + + // regular list of object primitive arrays + assertEquals(1, t.polAtomicInteger.get(0)[0].intValue()); + assertEquals(1, t.polAtomicLong.get(0)[0].intValue()); + assertNull(t.polAtomicInteger.get(1)); + assertNull(t.polAtomicLong.get(1)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitivesBeansTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitivesBeansTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitivesBeansTest.java new file mode 100755 index 0000000..cd83e0f --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripPrimitivesBeansTest.java @@ -0,0 +1,367 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@SuppressWarnings({"unchecked","serial","javadoc"}) +public class RoundTripPrimitivesBeansTest extends RoundTripTest { + + public RoundTripPrimitivesBeansTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // testPrimitivesBean + //==================================================================================================== + @Test + public void testPrimitivesBean() throws Exception { + PrimitivesBean t = new PrimitivesBean().init(); + t = roundTrip(t, PrimitivesBean.class); + + // primitives + assertEquals(true, t.pBoolean); + assertEquals(1, t.pByte); + assertEquals('a', t.pChar); + assertEquals(2, t.pShort); + assertEquals(3, t.pInt); + assertEquals(4l, t.pLong); + assertEquals(5f, t.pFloat, 0.1f); + assertEquals(6d, t.pDouble, 0.1f); + + // uninitialized primitives + assertEquals(false, t.puBoolean); + assertEquals(0, t.puByte); + assertEquals((char)0, t.puChar); + assertEquals(0, t.puShort); + assertEquals(0, t.puInt); + assertEquals(0l, t.puLong); + assertEquals(0f, t.puFloat, 0.1f); + assertEquals(0d, t.puDouble, 0.1f); + + // primitive arrays + assertEquals(false, t.paBoolean[1][0]); + assertEquals(2, t.paByte[1][0]); + assertEquals('b', t.paChar[1][0]); + assertEquals(2, t.paShort[1][0]); + assertEquals(2, t.paInt[1][0]); + assertEquals(2l, t.paLong[1][0]); + assertEquals(2f, t.paFloat[1][0], 0.1f); + assertEquals(2d, t.paDouble[1][0], 0.1f); + assertNull(t.paBoolean[2]); + assertNull(t.paByte[2]); + assertNull(t.paChar[2]); + assertNull(t.paShort[2]); + assertNull(t.paInt[2]); + assertNull(t.paLong[2]); + assertNull(t.paFloat[2]); + assertNull(t.paDouble[2]); + + // uninitialized primitive arrays + assertNull(t.pauBoolean); + assertNull(t.pauByte); + assertNull(t.pauChar); + assertNull(t.pauShort); + assertNull(t.pauInt); + assertNull(t.pauLong); + assertNull(t.pauFloat); + assertNull(t.pauDouble); + + // anonymous list of primitive arrays + assertEquals(true, t.palBoolean.get(0)[0]); + assertEquals(1, t.palByte.get(0)[0]); + assertEquals('a', t.palChar.get(0)[0]); + assertEquals(1, t.palShort.get(0)[0]); + assertEquals(1, t.palInt.get(0)[0]); + assertEquals(1l, t.palLong.get(0)[0]); + assertEquals(1f, t.palFloat.get(0)[0], 0.1f); + assertEquals(1d, t.palDouble.get(0)[0], 0.1f); + assertNull(t.palBoolean.get(1)); + assertNull(t.palByte.get(1)); + assertNull(t.palChar.get(1)); + assertNull(t.palShort.get(1)); + assertNull(t.palInt.get(1)); + assertNull(t.palLong.get(1)); + assertNull(t.palFloat.get(1)); + assertNull(t.palDouble.get(1)); + + // regular list of primitive arrays + assertEquals(true, t.plBoolean.get(0)[0]); + assertEquals(1, t.plByte.get(0)[0]); + assertEquals('a', t.plChar.get(0)[0]); + assertEquals(1, t.plShort.get(0)[0]); + assertEquals(1, t.plInt.get(0)[0]); + assertEquals(1l, t.plLong.get(0)[0]); + assertEquals(1f, t.plFloat.get(0)[0], 0.1f); + assertEquals(1d, t.plDouble.get(0)[0], 0.1f); + assertNull(t.plBoolean.get(1)); + assertNull(t.plByte.get(1)); + assertNull(t.plChar.get(1)); + assertNull(t.plShort.get(1)); + assertNull(t.plInt.get(1)); + assertNull(t.plLong.get(1)); + assertNull(t.plFloat.get(1)); + assertNull(t.plDouble.get(1)); + } + + public static class PrimitivesBean { + + // primitives + public boolean pBoolean; + public byte pByte; + public char pChar; + public short pShort; + public int pInt; + public long pLong; + public float pFloat; + public double pDouble; + + // uninitialized primitives + public boolean puBoolean; + public byte puByte; + public char puChar; + public short puShort; + public int puInt; + public long puLong; + public float puFloat; + public double puDouble; + + // primitive arrays + public boolean[][] paBoolean; + public byte[][] paByte; + public char[][] paChar; + public short[][] paShort; + public int[][] paInt; + public long[][] paLong; + public float[][] paFloat; + public double[][] paDouble; + + // uninitialized primitive arrays + public boolean[][] pauBoolean; + public byte[][] pauByte; + public char[][] pauChar; + public short[][] pauShort; + public int[][] pauInt; + public long[][] pauLong; + public float[][] pauFloat; + public double[][] pauDouble; + + // Regular lists of primitives + public List<boolean[]> plBoolean; + public List<byte[]> plByte; + public List<char[]> plChar; + public List<short[]> plShort; + public List<int[]> plInt; + public List<long[]> plLong; + public List<float[]> plFloat; + public List<double[]> plDouble; + + // Anonymous list of primitives + public List<boolean[]> palBoolean; + public List<byte[]> palByte; + public List<char[]> palChar; + public List<short[]> palShort; + public List<int[]> palInt; + public List<long[]> palLong; + public List<float[]> palFloat; + public List<double[]> palDouble; + + public PrimitivesBean init() { + // primitives + pBoolean = true; + pByte = 1; + pChar = 'a'; + pShort = 2; + pInt = 3; + pLong = 4l; + pFloat = 5f; + pDouble = 6d; + + // primitive arrays + paBoolean = new boolean[][]{{true},{false},null}; + paByte = new byte[][]{{1},{2},null}; + paChar = new char[][]{{'a'},{'b'},null}; + paShort = new short[][]{{1},{2},null}; + paInt = new int[][]{{1},{2},null}; + paLong = new long[][]{{1},{2},null}; + paFloat = new float[][]{{1},{2},null}; + paDouble = new double[][]{{1},{2},null}; + + // Regular lists of primitives + plBoolean = new ArrayList<boolean[]>() {{ + add(new boolean[]{true}); add(null); + }}; + plByte = new ArrayList<byte[]>() {{ + add(new byte[]{1}); add(null); + }}; + plChar = new ArrayList<char[]>() {{ + add(new char[]{'a'}); add(null); + }}; + plShort = new ArrayList<short[]>() {{ + add(new short[]{1}); add(null); + }}; + plInt = new ArrayList<int[]>() {{ + add(new int[]{1}); add(null); + }}; + plLong = new ArrayList<long[]>() {{ + add(new long[]{1}); add(null); + }}; + plFloat = new ArrayList<float[]>() {{ + add(new float[]{1}); add(null); + }}; + plDouble = new ArrayList<double[]>() {{ + add(new double[]{1}); add(null); + }}; + + // Anonymous list of primitives + palBoolean = new ArrayList<boolean[]>(); + palBoolean.add(new boolean[]{true}); + palBoolean.add(null); + palByte = new ArrayList<byte[]>(); + palByte.add(new byte[]{1}); + palByte.add(null); + palChar = new ArrayList<char[]>(); + palChar.add(new char[]{'a'}); + palChar.add(null); + palShort = new ArrayList<short[]>(); + palShort.add(new short[]{1}); + palShort.add(null); + palInt = new ArrayList<int[]>(); + palInt.add(new int[]{1}); + palInt.add(null); + palLong = new ArrayList<long[]>(); + palLong.add(new long[]{1}); + palLong.add(null); + palFloat = new ArrayList<float[]>(); + palFloat.add(new float[]{1}); + palFloat.add(null); + palDouble = new ArrayList<double[]>(); + palDouble.add(new double[]{1}); + palDouble.add(null); + return this; + } + } + + //==================================================================================================== + // List of PrimitivesBean + //==================================================================================================== + @Test + public void testPrimitivesBeanList() throws Exception { + List<PrimitivesBean> t = new ArrayList<PrimitivesBean>() {{ + add(new PrimitivesBean().init()); + add(null); + add(new PrimitivesBean().init()); + }}; + if (p == null) + return; + t = roundTrip(t, p.getBeanContext().getCollectionClassMeta(List.class, PrimitivesBean.class)); + + PrimitivesBean t2 = t.get(2); + + // primitives + assertEquals(true, t2.pBoolean); + assertEquals(1, t2.pByte); + assertEquals('a', t2.pChar); + assertEquals(2, t2.pShort); + assertEquals(3, t2.pInt); + assertEquals(4l, t2.pLong); + assertEquals(5f, t2.pFloat, 0.1f); + assertEquals(6d, t2.pDouble, 0.1f); + + // uninitialized primitives + assertEquals(false, t2.puBoolean); + assertEquals(0, t2.puByte); + assertEquals((char)0, t2.puChar); + assertEquals(0, t2.puShort); + assertEquals(0, t2.puInt); + assertEquals(0l, t2.puLong); + assertEquals(0f, t2.puFloat, 0.1f); + assertEquals(0d, t2.puDouble, 0.1f); + + // primitive arrays + assertEquals(false, t2.paBoolean[1][0]); + assertEquals(2, t2.paByte[1][0]); + assertEquals('b', t2.paChar[1][0]); + assertEquals(2, t2.paShort[1][0]); + assertEquals(2, t2.paInt[1][0]); + assertEquals(2l, t2.paLong[1][0]); + assertEquals(2f, t2.paFloat[1][0], 0.1f); + assertEquals(2d, t2.paDouble[1][0], 0.1f); + assertNull(t2.paBoolean[2]); + assertNull(t2.paByte[2]); + assertNull(t2.paChar[2]); + assertNull(t2.paShort[2]); + assertNull(t2.paInt[2]); + assertNull(t2.paLong[2]); + assertNull(t2.paFloat[2]); + assertNull(t2.paDouble[2]); + + // uninitialized primitive arrays + assertNull(t2.pauBoolean); + assertNull(t2.pauByte); + assertNull(t2.pauChar); + assertNull(t2.pauShort); + assertNull(t2.pauInt); + assertNull(t2.pauLong); + assertNull(t2.pauFloat); + assertNull(t2.pauDouble); + + // anonymous list of primitive arrays + assertEquals(true, t2.palBoolean.get(0)[0]); + assertEquals(1, t2.palByte.get(0)[0]); + assertEquals('a', t2.palChar.get(0)[0]); + assertEquals(1, t2.palShort.get(0)[0]); + assertEquals(1, t2.palInt.get(0)[0]); + assertEquals(1l, t2.palLong.get(0)[0]); + assertEquals(1f, t2.palFloat.get(0)[0], 0.1f); + assertEquals(1d, t2.palDouble.get(0)[0], 0.1f); + assertNull(t2.palBoolean.get(1)); + assertNull(t2.palByte.get(1)); + assertNull(t2.palChar.get(1)); + assertNull(t2.palShort.get(1)); + assertNull(t2.palInt.get(1)); + assertNull(t2.palLong.get(1)); + assertNull(t2.palFloat.get(1)); + assertNull(t2.palDouble.get(1)); + + // regular list of primitive arrays + assertEquals(true, t2.plBoolean.get(0)[0]); + assertEquals(1, t2.plByte.get(0)[0]); + assertEquals('a', t2.plChar.get(0)[0]); + assertEquals(1, t2.plShort.get(0)[0]); + assertEquals(1, t2.plInt.get(0)[0]); + assertEquals(1l, t2.plLong.get(0)[0]); + assertEquals(1f, t2.plFloat.get(0)[0], 0.1f); + assertEquals(1d, t2.plDouble.get(0)[0], 0.1f); + assertNull(t2.plBoolean.get(1)); + assertNull(t2.plByte.get(1)); + assertNull(t2.plChar.get(1)); + assertNull(t2.plShort.get(1)); + assertNull(t2.plInt.get(1)); + assertNull(t2.plLong.get(1)); + assertNull(t2.plFloat.get(1)); + assertNull(t2.plDouble.get(1)); + + assertNull(t.get(1)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripReadOnlyBeansTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripReadOnlyBeansTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripReadOnlyBeansTest.java new file mode 100755 index 0000000..1f8f8cf --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripReadOnlyBeansTest.java @@ -0,0 +1,101 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.junit.Assert.*; + +import org.apache.juneau.annotation.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@SuppressWarnings("javadoc") +public class RoundTripReadOnlyBeansTest extends RoundTripTest { + + public RoundTripReadOnlyBeansTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + B t1 = new B(1, "a"), t2 = new B(2, "b"); + A t3 = new A(t1, t2); + + t3 = roundTrip(t3, A.class); + assertEquals(1, t3.getF1().getF1()); + assertEquals("a", t3.getF1().getF2()); + assertEquals(2, t3.getF2().getF1()); + assertEquals("b", t3.getF2().getF2()); + } + + public static class A { + private B f1; + private final B f2; + + @BeanConstructor(properties="f2") + public A(B f2) { + this.f2 = f2; + } + + public A(B f1, B f2) { + this.f1 = f1; + this.f2 = f2; + } + + public B getF1() { + return f1; + } + + public void setF1(B f1) { + this.f1 = f1; + } + + public B getF2() { + return f2; + } + } + + public static class B { + private int f1; + private final String f2; + + @BeanConstructor(properties="f2") + public B(String sField) { + this.f2 = sField; + } + + public B(int iField, String sField) { + this.f1 = iField; + this.f2 = sField; + } + + public int getF1() { + return f1; + } + + public void setF1(int f1) { + this.f1 = f1; + } + + public String getF2() { + return f2; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripSimpleObjectsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripSimpleObjectsTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripSimpleObjectsTest.java new file mode 100755 index 0000000..217209a --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripSimpleObjectsTest.java @@ -0,0 +1,750 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@SuppressWarnings({"unchecked","rawtypes","javadoc"}) +public class RoundTripSimpleObjectsTest extends RoundTripTest { + + public RoundTripSimpleObjectsTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // testNull + //==================================================================================================== + @Test + public void testNull() throws Exception { + String t = null; + t = roundTrip(t); + assertNull(t); + } + + //==================================================================================================== + // testString + //==================================================================================================== + @Test + public void testString() throws Exception { + String t = "foobar"; + t = roundTrip(t); + assertEquals("foobar", t); + t = ""; + t = roundTrip(t); + assertEquals("", t); + } + + //==================================================================================================== + // testStringArray + //==================================================================================================== + @Test + public void testStringArray() throws Exception { + String[] t = {"foo", null, "null", ""}; + t = roundTrip(t, String[].class); + assertEquals("foo", t[0]); + assertNull(t[1]); + assertEquals("null", t[2]); + assertEquals("", t[3]); + } + + //==================================================================================================== + // testString2dArray + //==================================================================================================== + @Test + public void testString2dArray() throws Exception { + String[][] t = {{"foo", null, "null", ""},null}; + t = roundTrip(t, String[][].class); + assertEquals("foo", t[0][0]); + assertNull(t[0][1]); + assertEquals("null", t[0][2]); + assertEquals("", t[0][3]); + assertNull(t[1]); + } + + //==================================================================================================== + // testInt + //==================================================================================================== + @Test + public void testInt() throws Exception { + int t = 123; + t = roundTrip(t); + assertEquals(123, t); + } + + //==================================================================================================== + // testIntArray + //==================================================================================================== + @Test + public void testIntArray() throws Exception { + int[] t = roundTrip(new int[]{1,2,3}, int[].class); + assertEquals(1, t[0]); + assertEquals(2, t[1]); + assertEquals(3, t[2]); + } + + //==================================================================================================== + // testInt2dArray + //==================================================================================================== + @Test + public void testInt2dArray() throws Exception { + int[][] t = {{1,2,3},null}; + t = roundTrip(t, int[][].class); + assertEquals(1, t[0][0]); + assertEquals(2, t[0][1]); + assertEquals(3, t[0][2]); + assertNull(t[1]); + } + + //==================================================================================================== + // testInt3dArray + //==================================================================================================== + @Test + public void testInt3dArray() throws Exception { + int[][][] t = {{{1,2,3},{4,5,6},null},null}; + t = roundTrip(t, int[][][].class); + assertEquals(1, t[0][0][0]); + assertEquals(2, t[0][0][1]); + assertEquals(3, t[0][0][2]); + assertEquals(4, t[0][1][0]); + assertEquals(5, t[0][1][1]); + assertEquals(6, t[0][1][2]); + assertNull(t[0][2]); + assertNull(t[1]); + } + + //==================================================================================================== + // testBoolean + //==================================================================================================== + @Test + public void testBoolean() throws Exception { + boolean t = true; + t = roundTrip(t); + assertTrue(t); + t = false; + t = roundTrip(t); + assertFalse(t); + } + + //==================================================================================================== + // testBooleanArray + //==================================================================================================== + @Test + public void testBooleanArray() throws Exception { + boolean[] t = {true,false}; + t = roundTrip(t, boolean[].class); + assertTrue(t[0]); + assertFalse(t[1]); + } + + //==================================================================================================== + // testBoolean2dArray + //==================================================================================================== + @Test + public void testBoolean2dArray() throws Exception { + boolean[][] t = {{true,false},null}; + t = roundTrip(t, boolean[][].class); + assertTrue(t[0][0]); + assertFalse(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testChar + //==================================================================================================== + @Test + public void testChar() throws Exception { + char t = 'a'; + t = roundTrip(t, char.class); + assertEquals('a', t); + } + + //==================================================================================================== + // testCharArray + //==================================================================================================== + @Test + public void testCharArray() throws Exception { + char[] t = {'a',0}; + t = roundTrip(t, char[].class); + assertEquals('a', t[0]); + assertEquals(0, t[1]); + } + + //==================================================================================================== + // testChar2dArray + //==================================================================================================== + @Test + public void testChar2dArray() throws Exception { + char[][] t = {{'a',0},null}; + t = roundTrip(t, char[][].class); + assertEquals('a', t[0][0]); + assertEquals(0, t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testFloat + //==================================================================================================== + @Test + public void testFloat() throws Exception { + float t = 1f; + t = roundTrip(t, float.class); + assertEquals(1f, t, 0.1f); + } + + //==================================================================================================== + // testFloatArray + //==================================================================================================== + @Test + public void testFloatArray() throws Exception { + float[] t = {1f}; + t = roundTrip(t, float[].class); + assertEquals(1f, t[0], 0.1f); + } + + //==================================================================================================== + // testFloat2dArray + //==================================================================================================== + @Test + public void testFloat2dArray() throws Exception { + float[][] t = {{1f},null}; + t = roundTrip(t, float[][].class); + assertEquals(1f, t[0][0], 0.1f); + assertNull(t[1]); + } + + //==================================================================================================== + // testDouble + //==================================================================================================== + @Test + public void testDouble() throws Exception { + double t = 1d; + t = roundTrip(t, double.class); + assertEquals(1d, t, 0.1f); + } + + //==================================================================================================== + // testDoubleArray + //==================================================================================================== + @Test + public void testDoubleArray() throws Exception { + double[] t = {1d}; + t = roundTrip(t, double[].class); + assertEquals(1d, t[0], 0.1f); + } + + //==================================================================================================== + // testDouble2dArray + //==================================================================================================== + @Test + public void testDouble2dArray() throws Exception { + double[][] t = {{1d},null}; + t = roundTrip(t, double[][].class); + assertEquals(1d, t[0][0], 0.1f); + assertNull(t[1]); + } + + //==================================================================================================== + // testLong + //==================================================================================================== + @Test + public void testLong() throws Exception { + long t = 1l; + t = roundTrip(t, long.class); + assertEquals(1l, t); + } + + //==================================================================================================== + // testLongArray + //==================================================================================================== + @Test + public void testLongArray() throws Exception { + long[] t = {1l}; + t = roundTrip(t, long[].class); + assertEquals(1l, t[0]); + } + + //==================================================================================================== + // testLong2dArray + //==================================================================================================== + @Test + public void testLong2dArray() throws Exception { + long[][] t = {{1l},null}; + t = roundTrip(t, long[][].class); + assertEquals(1l, t[0][0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testShort + //==================================================================================================== + @Test + public void testShort() throws Exception { + short t = (short)1; + t = roundTrip(t, short.class); + assertEquals(1l, t); + } + + //==================================================================================================== + // testShortArray + //==================================================================================================== + @Test + public void testShortArray() throws Exception { + short[] t = {(short)1}; + t = roundTrip(t, short[].class); + assertEquals(1l, t[0]); + } + + //==================================================================================================== + // testShort2dArray + //==================================================================================================== + @Test + public void testShort2dArray() throws Exception { + short[][] t = {{(short)1},null}; + t = roundTrip(t, short[][].class); + assertEquals((short)1, t[0][0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testInteger + //==================================================================================================== + @Test + public void testInteger() throws Exception { + Integer t = 123; + t = roundTrip(t, Integer.class); + assertEquals(new Integer(123), t); + } + + //==================================================================================================== + // testIntegerArray + //==================================================================================================== + @Test + public void testIntegerArray() throws Exception { + Integer[] t = {123, null}; + t = roundTrip(t, Integer[].class); + assertEquals(new Integer(123), t[0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testInteger2dArray + //==================================================================================================== + @Test + public void testInteger2dArray() throws Exception { + Integer[][] t = {{123,null},null}; + t = roundTrip(t, Integer[][].class); + assertEquals(new Integer(123), t[0][0]); + assertNull(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testInteger3dArray + //==================================================================================================== + @Test + public void testInteger3dArray() throws Exception { + Integer[][][] t = {{{123,null},null},null}; + t = roundTrip(t, Integer[][][].class); + assertEquals(new Integer(123), t[0][0][0]); + assertNull(t[0][0][1]); + assertNull(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testBooleanObject + //==================================================================================================== + @Test + public void testBooleanObject() throws Exception { + Boolean t = Boolean.TRUE; + t = roundTrip(t, Boolean.class); + assertTrue(t); + t = Boolean.FALSE; + t = roundTrip(t, Boolean.class); + assertFalse(t); + } + + //==================================================================================================== + // testBooleanObjectArray + //==================================================================================================== + @Test + public void testBooleanObjectArray() throws Exception { + Boolean[] t = {true,false,null}; + t = roundTrip(t, Boolean[].class); + assertTrue(t[0]); + assertFalse(t[1]); + assertNull(t[2]); + } + + //==================================================================================================== + // testBooleanObject2dArray + //==================================================================================================== + @Test + public void testBooleanObject2dArray() throws Exception { + Boolean[][] t = {{true,false,null},null}; + t = roundTrip(t, Boolean[][].class); + assertTrue(t[0][0]); + assertFalse(t[0][1]); + assertNull(t[0][2]); + assertNull(t[1]); + } + + //==================================================================================================== + // testCharacter + //==================================================================================================== + @Test + public void testCharacter() throws Exception { + Character t = 'a'; + t = roundTrip(t, Character.class); + assertEquals(new Character('a'), t); + } + + //==================================================================================================== + // testCharacterArray + //==================================================================================================== + @Test + public void testCharacterArray() throws Exception { + Character[] t = {'a',null}; + t = roundTrip(t, Character[].class); + assertEquals(new Character('a'), t[0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testCharacter2dArray + //==================================================================================================== + @Test + public void testCharacter2dArray() throws Exception { + Character[][] t = {{'a',null},null}; + t = roundTrip(t, Character[][].class); + assertEquals(new Character('a'), t[0][0]); + assertNull(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testFloatObject + //==================================================================================================== + @Test + public void testFloatObject() throws Exception { + Float t = 1f; + t = roundTrip(t, Float.class); + assertEquals(new Float(1f), t); + } + + //==================================================================================================== + // testFloatObjectArray + //==================================================================================================== + @Test + public void testFloatObjectArray() throws Exception { + Float[] t = {1f, null}; + t = roundTrip(t, Float[].class); + assertEquals(new Float(1f), t[0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testFloatObject2dArray + //==================================================================================================== + @Test + public void testFloatObject2dArray() throws Exception { + Float[][] t = {{1f,null},null}; + t = roundTrip(t, Float[][].class); + assertEquals(new Float(1f), t[0][0]); + assertNull(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testDoubleObject + //==================================================================================================== + @Test + public void testDoubleObject() throws Exception { + Double t = 1d; + t = roundTrip(t, Double.class); + assertEquals(new Double(1d), t); + } + + //==================================================================================================== + // testDoubleObjectArray + //==================================================================================================== + @Test + public void testDoubleObjectArray() throws Exception { + Double[] t = {1d,null}; + t = roundTrip(t, Double[].class); + assertEquals(new Double(1d), t[0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testDoubleObject2dArray + //==================================================================================================== + @Test + public void testDoubleObject2dArray() throws Exception { + Double[][] t = {{1d,null},null}; + t = roundTrip(t, Double[][].class); + assertEquals(new Double(1d), t[0][0]); + assertNull(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testLongObject + //==================================================================================================== + @Test + public void testLongObject() throws Exception { + Long t = 1l; + t = roundTrip(t, Long.class); + assertEquals(new Long(1l), t); + } + + //==================================================================================================== + // testLongObjectArray + //==================================================================================================== + @Test + public void testLongObjectArray() throws Exception { + Long[] t = {1l, null}; + t = roundTrip(t, Long[].class); + assertEquals(new Long(1l), t[0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testLongObject2dArray + //==================================================================================================== + @Test + public void testLongObject2dArray() throws Exception { + Long[][] t = {{1l,null},null}; + t = roundTrip(t, Long[][].class); + assertEquals(new Long(1l), t[0][0]); + assertNull(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testShortObject + //==================================================================================================== + @Test + public void testShortObject() throws Exception { + Short t = (short)1; + t = roundTrip(t, Short.class); + assertEquals(new Short((short)1), t); + } + + //==================================================================================================== + // testShortObjectArray + //==================================================================================================== + @Test + public void testShortObjectArray() throws Exception { + Short[] t = {(short)1,null}; + t = roundTrip(t, Short[].class); + assertEquals(new Short((short)1), t[0]); + assertNull(t[1]); + } + + //==================================================================================================== + // testShortObject2dArray + //==================================================================================================== + @Test + public void testShortObject2dArray() throws Exception { + Short[][] t = {{(short)1,null},null}; + t = roundTrip(t, Short[][].class); + assertEquals(new Short((short)1), t[0][0]); + assertNull(t[0][1]); + assertNull(t[1]); + } + + //==================================================================================================== + // testObjectMap + //==================================================================================================== + @Test + public void testObjectMap() throws Exception { + ObjectMap t = new ObjectMap("{a:'b',c:123,d:false,e:null,f:[123,'abc',true,false,null]}"); + t = roundTrip(t); + assertEquals("b", t.get("a")); + assertEquals(123, t.get("c")); + assertEquals(false, t.get("d")); + assertNull(t.get("e")); + List l = (List)t.get("f"); + assertEquals(123, l.get(0)); + assertEquals("abc", l.get(1)); + assertEquals(true, l.get(2)); + assertEquals(false, l.get(3)); + assertNull(l.get(4)); + } + + //==================================================================================================== + // testObjectList + //==================================================================================================== + @Test + public void testObjectList() throws Exception { + ObjectList t = new ObjectList("['abc',123,true,false,null,{a:'b'}]"); + t = roundTrip(t); + assertEquals("abc", t.get(0)); + assertEquals(123, t.get(1)); + assertEquals(true, t.get(2)); + assertEquals(false, t.get(3)); + assertNull(t.get(4)); + Map m = (Map)t.get(5); + assertEquals("b", m.get("a")); + } + + //==================================================================================================== + // testTreeMap + //==================================================================================================== + @Test + public void testTreeMap() throws Exception { + TreeMap t = new TreeMap(); + t.put("a", 1); + t.put("b", 2); + t.put("c", 3); + t = roundTrip(t, TreeMap.class); + assertEquals(1, t.get("a")); + assertEquals(2, t.get("b")); + assertEquals(3, t.get("c")); + + t = new TreeMap(); + t.put("a", true); + t.put("b", false); + t.put("c", null); + t.put("d", "foo"); + t.put("null", "baz"); + t.put("a\"a", "a\"a"); + t.put("b'b", "b'b"); + t.put("\"cc\"", "\"cc\""); + t.put("'dd'", "'dd'"); + t = roundTrip(t, TreeMap.class); + assertEquals(true, t.get("a")); + assertEquals(false, t.get("b")); + assertNull(t.get("c")); + assertEquals("foo", t.get("d")); + assertEquals("baz", t.get("null")); + assertEquals("a\"a", t.get("a\"a")); + assertEquals("b'b", t.get("b'b")); + assertEquals("\"cc\"", t.get("\"cc\"")); + assertEquals("'dd'", t.get("'dd'")); + } + + //==================================================================================================== + // testLinkedHashMap + //==================================================================================================== + @Test + public void testLinkedHashMap() throws Exception { + LinkedHashMap t = new LinkedHashMap(); + t.put("a", true); + t.put("b", false); + t.put("c", null); + t.put("d", "foo"); + t.put(null, "bar"); + t.put("null", "null"); + t.put("true", "true"); + t.put("false", "false"); + t.put("a\"a", "a\"a"); + t.put("b'b", "b'b"); + t.put("\"cc\"", "\"cc\""); + t.put("'dd'", "'dd'"); + t.put("<ee>", "<ee>"); + t.put("<ff/>", "<ff/>"); + t.put("</gg>", "</gg>"); + t.put("<>", "<>"); + t.put("{}", "{}"); + t.put("[]", "[]"); + t.put("&", "&"); + t.put("?", "?"); + t.put("/", "/"); + t.put("\b", "\b"); + t.put("\\b", "\\b"); + t.put("\n", "\n"); + t.put("\\n", "\\n"); + t.put("\t", "\t"); + t.put("\\t", "\\t"); + t.put("\f", "\f"); + t.put("\\f", "\\f"); + t.put("\\", "\\"); + t.put("\\\\", "\\\\"); + t.put("\u2345", "\u2345"); + t.put("\\u2345", "\\u2345"); + t.put("\\\u2345", "\\\u2345"); + t.put("<>{}[]&?/\b\n\t\f\\\\\u2345", "<>{}[]&?/\b\n\t\f\\\\\u2345"); + t = roundTrip(t, LinkedHashMap.class); + assertEquals(true, t.get("a")); + assertEquals(false, t.get("b")); + assertNull(t.get("c")); + assertEquals("foo", t.get("d")); + assertEquals("bar", t.get(null)); + assertEquals("null", t.get("null")); + assertEquals("true", t.get("true")); + assertEquals("false", t.get("false")); + assertEquals("a\"a", t.get("a\"a")); + assertEquals("b'b", t.get("b'b")); + assertEquals("\"cc\"", t.get("\"cc\"")); + assertEquals("'dd'", t.get("'dd'")); + assertEquals("<ee>", t.get("<ee>")); + assertEquals("<ff/>", t.get("<ff/>")); + assertEquals("</gg>", t.get("</gg>")); + assertEquals("<>", t.get("<>")); + assertEquals("{}", t.get("{}")); + assertEquals("[]", t.get("[]")); + assertEquals("&", t.get("&")); + assertEquals("?", t.get("?")); + assertEquals("/", t.get("/")); + assertEquals("\b", t.get("\b")); + assertEquals("\\b", t.get("\\b")); + assertEquals("\n", t.get("\n")); + assertEquals("\\n", t.get("\\n")); + assertEquals("\t", t.get("\t")); + assertEquals("\\t", t.get("\\t")); + assertEquals("\f", t.get("\f")); + assertEquals("\\f", t.get("\\f")); + assertEquals("\\", t.get("\\")); + assertEquals("\\\\", t.get("\\\\")); + assertEquals("\u2345", t.get("\u2345")); + assertEquals("\\u2345", t.get("\\u2345")); + assertEquals("\\\u2345", t.get("\\\u2345")); + assertEquals("<>{}[]&?/\b\n\t\f\\\\\u2345", t.get("<>{}[]&?/\b\n\t\f\\\\\u2345")); + } + + //==================================================================================================== + // testVector + //==================================================================================================== + @Test + public void testVector() throws Exception { + Vector<Integer> t = new Vector<Integer>(); + t.add(1); + t.add(2); + t.add(3); + t = roundTripCollection(t, Vector.class, Integer.class); + } + + //==================================================================================================== + // testNull + //==================================================================================================== + @Test + public void testExtendedUnicode() throws Exception { + // Test 4-byte UTF-8 character + String t = "ð¤¢ð¤¢"; + t = roundTrip(t); + assertEquals("ð¤¢ð¤¢", t); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java new file mode 100755 index 0000000..3c9b63a --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java @@ -0,0 +1,326 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.apache.juneau.a.rttests.RoundTripTest.Flags.*; +import static org.apache.juneau.jena.RdfSerializerContext.*; +import static org.apache.juneau.urlencoding.UonSerializerContext.*; +import static org.apache.juneau.urlencoding.UrlEncodingContext.*; +import static org.apache.juneau.xml.XmlSerializerContext.*; +import static org.apache.juneau.html.HtmlSerializerContext.*; + +import java.util.*; +import java.util.Map.*; + +import org.apache.juneau.*; +import org.apache.juneau.html.*; +import org.apache.juneau.jena.*; +import org.apache.juneau.json.*; +import org.apache.juneau.msgpack.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.urlencoding.*; +import org.apache.juneau.xml.*; +import org.junit.runner.*; +import org.junit.runners.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@RunWith(Parameterized.class) +@SuppressWarnings({"unchecked","javadoc"}) +public abstract class RoundTripTest { + + public static class Flags { + public static int CHECK_XML_WHITESPACE = 1, VALIDATE_XML = 2, SERIALIZE_SCHEMA = 4, RETURN_ORIGINAL_OBJECT = 8; + } + + @Parameterized.Parameters + public static Collection<Object[]> getPairs() { + return Arrays.asList(new Object[][] { + // Full round-trip testing + { /* 0 */ + "Json - default", + new JsonSerializer().setProperty(SERIALIZER_trimNullProperties, false), + JsonParser.DEFAULT, + 0 + }, + { /* 1 */ + "Json - lax", + new JsonSerializer.Simple().setProperty(SERIALIZER_trimNullProperties, false), + JsonParser.DEFAULT, + 0 + }, + { /* 2 */ + "Json - lax, readable", + new JsonSerializer.SimpleReadable().setProperty(SERIALIZER_trimNullProperties, false), + JsonParser.DEFAULT, + 0 + }, + { /* 3 */ + "Xml - namespaces, validation, readable", + new XmlSerializer.NsSq().setProperty(SERIALIZER_trimNullProperties, false).setProperty(XML_addNamespaceUrisToRoot, true).setProperty(SERIALIZER_useIndentation, true), + XmlParser.DEFAULT, + CHECK_XML_WHITESPACE | VALIDATE_XML + }, + { /* 4 */ + "Xml - no namespaces, validation", + new XmlSerializer.Sq().setProperty(SERIALIZER_trimNullProperties, false), + XmlParser.DEFAULT, + CHECK_XML_WHITESPACE + }, + { /* 5 */ + "Html - default", + new HtmlSerializer().setProperty(SERIALIZER_trimNullProperties, false), + HtmlParser.DEFAULT, + CHECK_XML_WHITESPACE + }, + { /* 6 */ + "Html - readable", + new HtmlSerializer.SqReadable().setProperty(SERIALIZER_trimNullProperties, false), + HtmlParser.DEFAULT, + CHECK_XML_WHITESPACE + }, + { /* 7 */ + "Html - with key/value headers", + new HtmlSerializer().setProperty(HTML_addKeyValueTableHeaders, true), + HtmlParser.DEFAULT, + CHECK_XML_WHITESPACE + }, + { /* 8 */ + "Uon - default", + new UonSerializer().setProperty(SERIALIZER_trimNullProperties, false).setProperty(UON_simpleMode, false), + UonParser.DEFAULT, + 0 + }, + { /* 9 */ + "Uon - readable", + new UonSerializer.Readable().setProperty(SERIALIZER_trimNullProperties, false).setProperty(UON_simpleMode, false), + UonParser.DEFAULT_WS_AWARE, + 0 + }, + { /* 10 */ + "Uon - encoded", + new UonSerializer.Encoding().setProperty(SERIALIZER_trimNullProperties, false).setProperty(UON_simpleMode, false), + UonParser.DEFAULT_DECODING, + 0 + }, + { /* 11 */ + "UrlEncoding - default", + new UrlEncodingSerializer().setProperty(SERIALIZER_trimNullProperties, false).setProperty(UON_simpleMode, false), + UrlEncodingParser.DEFAULT, + 0 + }, + { /* 12 */ + "UrlEncoding - readable", + new UrlEncodingSerializer.Readable().setProperty(SERIALIZER_trimNullProperties, false).setProperty(UON_simpleMode, false), + UrlEncodingParser.DEFAULT_WS_AWARE, + 0 + }, + { /* 13 */ + "UrlEncoding - expanded params", + new UrlEncodingSerializer().setProperty(URLENC_expandedParams, true).setProperty(UON_simpleMode, false), + new UrlEncodingParser().setProperty(URLENC_expandedParams, true), + 0 + }, + { /* 14 */ + "Rdf.Xml", + new RdfSerializer.Xml().setProperty(SERIALIZER_trimNullProperties, false).setProperty(RDF_addLiteralTypes, true), + RdfParser.DEFAULT_XML, + 0 + }, + { /* 15 */ + "Rdf.XmlAbbrev", + new RdfSerializer.XmlAbbrev().setProperty(SERIALIZER_trimNullProperties, false).setProperty(RDF_addLiteralTypes, true), + RdfParser.DEFAULT_XML, + 0 + }, + { /* 16 */ + "Rdf.Turtle", + new RdfSerializer.Turtle().setProperty(SERIALIZER_trimNullProperties, false).setProperty(RDF_addLiteralTypes, true), + RdfParser.DEFAULT_TURTLE, + 0 + }, + { /* 17 */ + "Rdf.NTriple", + new RdfSerializer.NTriple().setProperty(SERIALIZER_trimNullProperties, false).setProperty(RDF_addLiteralTypes, true), + RdfParser.DEFAULT_NTRIPLE, + 0 + }, + { /* 18 */ + "Rdf.N3", + new RdfSerializer.N3().setProperty(SERIALIZER_trimNullProperties, false).setProperty(RDF_addLiteralTypes, true), + RdfParser.DEFAULT_N3, + 0 + }, + { /* 19 */ + "MsgPack", + new MsgPackSerializer().setProperty(SERIALIZER_trimNullProperties, false).setProperty(RDF_addLiteralTypes, true), + MsgPackParser.DEFAULT, + 0 + }, + + // Validation testing only + { /* 20 */ + "Json schema", + new JsonSchemaSerializer().setProperty(SERIALIZER_trimNullProperties, false), + null, + RETURN_ORIGINAL_OBJECT + }, + { /* 21 */ + "Xml schema", + new XmlSchemaSerializer().setProperty(SERIALIZER_trimNullProperties, false), + new XmlValidatorParser(), + RETURN_ORIGINAL_OBJECT | CHECK_XML_WHITESPACE + }, + }); + } + + protected Serializer s; + protected Parser p; + private boolean validateXmlWhitespace; + protected boolean returnOriginalObject; + private boolean validateXml; + protected String label; + public boolean debug = false; + + public RoundTripTest(String label, Serializer s, Parser p, int flags) throws Exception { + this.s = s.clone().addBeanFilters(getBeanFilters()).addPojoSwaps(getPojoSwaps()).addToDictionary(getDictionary()); + this.p = p == null ? null : p.clone().addBeanFilters(getBeanFilters()).addPojoSwaps(getPojoSwaps()).addToDictionary(getDictionary()); + this.label = label; + + Map<Class<Object>, Class<? extends Object>> m = getImplClasses(); + if (m != null) { + for (Entry<Class<Object>, Class<? extends Object>> e : m.entrySet()) { + this.s.addImplClass(e.getKey(), e.getValue()); + if (this.p != null) + this.p.addImplClass(e.getKey(), e.getValue()); + } + } + this.validateXmlWhitespace = (flags & CHECK_XML_WHITESPACE) > 0; + this.validateXml = (flags & VALIDATE_XML) > 0; + this.returnOriginalObject = (flags & RETURN_ORIGINAL_OBJECT) > 0; + } + + + public Class<?>[] getBeanFilters() { + return new Class<?>[0]; + } + + public Class<?>[] getPojoSwaps() { + return new Class<?>[0]; + } + + public Class<?>[] getDictionary() { + return new Class<?>[0]; + } + + public <T> Map<Class<T>,Class<? extends T>> getImplClasses() { + return null; + } + + public <T> T roundTrip(T object, ClassMeta<? extends T> t) throws Exception { + Object out = serialize(object, this.s); + if (p == null) + return object; + T o = this.p.parse(out, t); + return (returnOriginalObject ? object : o); + } + public <T> T roundTrip(T object, Class<? extends T> c) throws Exception { + Object out = serialize(object, this.s); + if (p == null) + return object; + T o = this.p.parse(out, p.getBeanContext().getClassMeta(c)); + return (returnOriginalObject ? object : o); + } + public <K,V,T extends Map<K,V>> T roundTripMap(T object, Class<? extends T> c, Class<K> k, Class<V> v) throws Exception { + Object out = serialize(object, this.s); + if (p == null) + return object; + ClassMeta<? extends T> cm = p.getBeanContext().getMapClassMeta(c, k, v); + T o = this.p.parse(out, cm); + return (returnOriginalObject ? object : o); + } + public <E,T extends Collection<E>> T roundTripCollection(T object, Class<? extends T> c, Class<E> e) throws Exception { + Object out = serialize(object, this.s); + if (p == null) + return object; + ClassMeta<? extends T> cm = p.getBeanContext().getCollectionClassMeta(c, e); + T o = this.p.parse(out, cm); + return (returnOriginalObject ? object : o); + } + + public <T> T roundTrip(T object) throws Exception { + return roundTrip(object, s, p); + } + + public <T> T roundTrip(T object, Serializer serializer, Parser parser) throws Exception { + Object out = serialize(object, serializer); + if (parser == null) + return object; + T o = (T)parser.parse(out, object == null ? Object.class : object.getClass()); + return (returnOriginalObject ? object : o); + } + + public Serializer getSerializer() { + return s; + } + + public Parser getParser() { + return p; + } + + protected void addBeanFilters(Class<?>...c) { + s.addBeanFilters(c); + if (p != null) + p.addBeanFilters(c); + } + + protected void addPojoSwaps(Class<?>...c) { + s.addPojoSwaps(c); + if (p != null) + p.addPojoSwaps(c); + } + + protected void addToDictionary(Class<?>...c) { + s.addToDictionary(c); + if (p != null) + p.addToDictionary(c); + } + + public boolean isValidationOnly() { + return returnOriginalObject; + } + + public <T> Object serialize(T object, Serializer s) throws Exception { + + Object out = null; + if (s.isWriterSerializer()) + out = ((WriterSerializer)s).serialize(object); + else { + out = ((OutputStreamSerializer)s).serialize(object); + } + + if (debug) + System.err.println("Serialized contents from ["+label+"]...\n---START---\n" + (out instanceof byte[] ? TestUtils.toReadableBytes((byte[])out) : out) + "\n---END---\n"); + + if (validateXmlWhitespace) + TestUtils.checkXmlWhitespace(out.toString()); + + if (validateXml) + TestUtils.validateXml(object, (XmlSerializer)s); + + return out; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripToObjectMapsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripToObjectMapsTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripToObjectMapsTest.java new file mode 100755 index 0000000..9e48e24 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripToObjectMapsTest.java @@ -0,0 +1,77 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@SuppressWarnings("javadoc") +public class RoundTripToObjectMapsTest extends RoundTripTest { + + public RoundTripToObjectMapsTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // Class with X(ObjectMap) constructor and toObjectMap() method. + //==================================================================================================== + @SuppressWarnings({ "serial", "unchecked" }) + @Test + public void test() throws Exception { + A a = new A(new ObjectMap("{f1:'a',f2:2}")); + a = roundTrip(a, A.class); + assertEquals("a", a.f1); + assertEquals(2, a.f2); + + A[] aa = new A[]{a}; + aa = roundTrip(aa, A[].class); + assertEquals(1, aa.length); + assertEquals("a", aa[0].f1); + assertEquals(2, aa[0].f2); + + List<A> a2 = new ArrayList<A>(){{add(new A(new ObjectMap("{f1:'a',f2:2}")));}}; + a2 = roundTrip(a2, BeanContext.DEFAULT.getCollectionClassMeta(List.class, A.class)); + assertEquals(1, a2.size()); + assertEquals("a", a2.get(0).f1); + assertEquals(2, a2.get(0).f2); + + Map<String,A> a3 = new LinkedHashMap<String,A>(){{put("a", new A(new ObjectMap("{f1:'a',f2:2}")));}}; + a3 = roundTrip(a3, BeanContext.DEFAULT.getMapClassMeta(Map.class, String.class, A.class)); + assertEquals(1, a3.size()); + assertEquals("a", a3.get("a").f1); + assertEquals(2, a3.get("a").f2); + } + + public static class A { + private String f1; + private int f2; + public A(ObjectMap m) { + this.f1 = m.getString("f1"); + this.f2 = m.getInt("f2"); + } + public ObjectMap toObjectMap() { + return new ObjectMap().append("f1",f1).append("f2",f2); + } + } + +}
