http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripPrimitivesBeans.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripPrimitivesBeans.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripPrimitivesBeans.java deleted file mode 100755 index b8234fc..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripPrimitivesBeans.java +++ /dev/null @@ -1,367 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package 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"}) -public class CT_RoundTripPrimitivesBeans extends RoundTripTest { - - public CT_RoundTripPrimitivesBeans(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/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripReadOnlyBeans.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripReadOnlyBeans.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripReadOnlyBeans.java deleted file mode 100755 index cb4c40a..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripReadOnlyBeans.java +++ /dev/null @@ -1,100 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package 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. - */ -public class CT_RoundTripReadOnlyBeans extends RoundTripTest { - - public CT_RoundTripReadOnlyBeans(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/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripSimpleObjects.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripSimpleObjects.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripSimpleObjects.java deleted file mode 100755 index 305021a..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripSimpleObjects.java +++ /dev/null @@ -1,750 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package 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" }) -public class CT_RoundTripSimpleObjects extends RoundTripTest { - - public CT_RoundTripSimpleObjects(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/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripToObjectMaps.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripToObjectMaps.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripToObjectMaps.java deleted file mode 100755 index 384e06e..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripToObjectMaps.java +++ /dev/null @@ -1,78 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package 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. - */ -public class CT_RoundTripToObjectMaps extends RoundTripTest { - - public CT_RoundTripToObjectMaps(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); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTransformBeans.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTransformBeans.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTransformBeans.java deleted file mode 100755 index 3a118c8..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTransformBeans.java +++ /dev/null @@ -1,382 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.a.rttests; - -import static org.junit.Assert.*; - -import java.io.*; -import java.util.*; - -import javax.xml.datatype.*; - -import org.apache.juneau.*; -import org.apache.juneau.annotation.*; -import org.apache.juneau.annotation.Transform; -import org.apache.juneau.json.*; -import org.apache.juneau.parser.*; -import org.apache.juneau.serializer.*; -import org.apache.juneau.transform.*; -import org.apache.juneau.transforms.*; -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","hiding","serial"}) -public class CT_RoundTripTransformBeans extends RoundTripTest { - - public CT_RoundTripTransformBeans(String label, Serializer s, Parser p, int flags) throws Exception { - super(label, s, p, flags); - } - - //==================================================================================================== - // testTransformBeans1 - //==================================================================================================== - @Test - public void testTransformBeans1() throws Exception { - Class<?>[] f = { - ByteArrayBase64Transform.class, - CalendarTransform.ISO8601DTZ.class, - DateTransform.ISO8601DTZ.class - }; - s.addTransforms(f); - if (p != null) - p.addTransforms(f); - A t = new A().init(); - t = roundTrip(t, A.class); - - // ByteArrayBase64Transform - assertEquals(3, t.fByte[3]); - assertNull(t.fnByte); - assertEquals(5, t.faByte[2][1]); - assertEquals(6, t.flByte.get(1)[2]); - assertNull(t.flByte.get(2)); - assertEquals(6, t.fmByte.get("bar")[2]); - assertNull(t.fmByte.get("baz")); - - // CalendarTransform - t.fCalendar.setTimeZone(TimeZone.getTimeZone("GMT")); - assertEquals(2001, t.fCalendar.get(Calendar.YEAR)); - assertEquals(01, t.fCalendar.get(Calendar.MONTH)); - assertEquals(02, t.fCalendar.get(Calendar.DATE)); - assertEquals(03, t.fCalendar.get(Calendar.HOUR)); - assertEquals(04, t.fCalendar.get(Calendar.MINUTE)); - assertEquals(05, t.fCalendar.get(Calendar.SECOND)); - - t.faCalendar[0].setTimeZone(TimeZone.getTimeZone("GMT")); - assertEquals(2001, t.faCalendar[0].get(Calendar.YEAR)); - assertEquals(01, t.faCalendar[0].get(Calendar.MONTH)); - assertEquals(02, t.faCalendar[0].get(Calendar.DATE)); - assertEquals(03, t.faCalendar[0].get(Calendar.HOUR)); - assertEquals(04, t.faCalendar[0].get(Calendar.MINUTE)); - assertEquals(05, t.faCalendar[0].get(Calendar.SECOND)); - assertNull(t.fnCalendar); - assertNull(t.fn2Calendar); - - // DateTransform - assertEquals(1000, t.fDate.getTime()); - assertNull(t.fnDate); - assertEquals(3000, t.faDate[2].getTime()); - assertEquals(4000, t.flDate.get(0).getTime()); - assertNull(t.flDate.get(1)); - assertEquals(5000, t.fmDate.get("foo").getTime()); - assertNull(t.fmDate.get("bar")); - } - - public static class A { - - // Test ByteArrayBase64Transform - public byte[] fByte; - public byte[] fnByte; - public byte[][] faByte; - public List<byte[]> flByte; - public Map<String,byte[]> fmByte; - - public GregorianCalendar fCalendar; - public GregorianCalendar fnCalendar; - public Calendar fn2Calendar; - public GregorianCalendar[] faCalendar; - - public Date fDate; - public Date fnDate; - public Date[] faDate; - public List<Date> flDate; - public Map<String,Date> fmDate; - - public A init() { - fByte = new byte[]{0,1,2,3}; - fnByte = null; - faByte = new byte[][]{{0,1},{2,3},{4,5}}; - flByte = new ArrayList<byte[]>() {{ - add(new byte[]{1,2,3}); - add(new byte[]{4,5,6}); - add(null); - }}; - fmByte = new LinkedHashMap<String,byte[]>() {{ - put("foo", new byte[]{1,2,3}); - put("bar", new byte[]{4,5,6}); - put("baz", null); - }}; - - fCalendar = new GregorianCalendar() {{ - set(2001, 01, 02, 03, 04, 05); - setTimeZone(TimeZone.getTimeZone("GMT")); - }}; - fnCalendar = null; - fn2Calendar = null; - faCalendar = new GregorianCalendar[]{ - new GregorianCalendar() {{ - set(2001, 01, 02, 03, 04, 05); - setTimeZone(TimeZone.getTimeZone("GMT")); - }} - }; - - fDate = new Date(1000); - fnDate = null; - faDate = new Date[]{ - new Date(1000), new Date(2000), new Date(3000) - }; - flDate = new ArrayList<Date>() {{ - add(new Date(4000)); - add(null); - }}; - fmDate = new LinkedHashMap<String,Date>() {{ - put("foo", new Date(5000)); - put("bar", null); - }}; - return this; - } - } - - - //==================================================================================================== - // testTransformBeans2 - //==================================================================================================== - @Test - public void testTransformBeans2() throws Exception { - Class<?>[] f = { - ByteArrayBase64Transform.class, - CalendarTransform.Medium.class, - DateTransform.RFC2822DT.class, - }; - s.addTransforms(f); - if (p != null) - p.addTransforms(f); - A t = new A().init(); - t = roundTrip(t, A.class); - - // ByteArrayBase64Transform - assertEquals(3, t.fByte[3]); - assertNull(t.fnByte); - assertEquals(5, t.faByte[2][1]); - assertEquals(6, t.flByte.get(1)[2]); - assertNull(t.flByte.get(2)); - assertEquals(6, t.fmByte.get("bar")[2]); - assertNull(t.fmByte.get("baz")); - - // CalendarTransform - t.fCalendar.setTimeZone(TimeZone.getTimeZone("GMT")); - assertEquals(2001, t.fCalendar.get(Calendar.YEAR)); - assertEquals(01, t.fCalendar.get(Calendar.MONTH)); - // Note: We lose precision on the following because of the transform type. - //assertEquals(02, b.fCalendar.get(Calendar.DATE)); - //assertEquals(03, b.fCalendar.get(Calendar.HOUR)); - //assertEquals(04, b.fCalendar.get(Calendar.MINUTE)); - //assertEquals(05, b.fCalendar.get(Calendar.SECOND)); - - t.faCalendar[0].setTimeZone(TimeZone.getTimeZone("GMT")); - assertEquals(2001, t.faCalendar[0].get(Calendar.YEAR)); - assertEquals(01, t.faCalendar[0].get(Calendar.MONTH)); - // Note: We lose precision on the following because of the transform type. - //assertEquals(02, b.faCalendar[0].get(Calendar.DATE)); - //assertEquals(03, b.faCalendar[0].get(Calendar.HOUR)); - //assertEquals(04, b.faCalendar[0].get(Calendar.MINUTE)); - //assertEquals(05, b.faCalendar[0].get(Calendar.SECOND)); - assertNull(t.fnCalendar); - assertNull(t.fn2Calendar); - - // DateTransform - assertEquals(1000, t.fDate.getTime()); - assertNull(t.fnDate); - assertEquals(3000, t.faDate[2].getTime()); - assertEquals(4000, t.flDate.get(0).getTime()); - assertNull(t.flDate.get(1)); - assertEquals(5000, t.fmDate.get("foo").getTime()); - assertNull(t.fmDate.get("bar")); - } - - //==================================================================================================== - // testTransform - Bean.transform annotation - //==================================================================================================== - @Test - public void testTransform() throws Exception { - B t = new B(); - t.f1 = "bar"; - t = roundTrip(t, B.class); - - assertEquals("bar", t.f1); - } - - @Transform(BTransform.class) - public static class B { - public String f1; - } - - public static class BTransform extends PojoTransform<B,String> { - @Override /* PojoTransform */ - public String transform(B o) throws SerializeException { - return o.f1; - } - @Override /* PojoTransform */ - public B normalize(String f, ClassMeta<?> hint) throws ParseException { - B b1 = new B(); - b1.f1 = f; - return b1; - } - } - - //==================================================================================================== - // testXMLGregorianCalendar - Test XMLGregorianCalendarTransform class. - //==================================================================================================== - @Test - public void testXMLGregorianCalendar() throws Exception { - - if (isValidationOnly()) - return; - - GregorianCalendar gc = new GregorianCalendar(); - XMLGregorianCalendar c = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); - - Serializer s = getSerializer().clone().addTransforms(XMLGregorianCalendarTransform.class); - Parser p = getParser().clone().addTransforms(XMLGregorianCalendarTransform.class); - - Object r = s.serialize(c); - XMLGregorianCalendar c2 = p.parse(r, XMLGregorianCalendar.class); - assertEquals(c, c2); - } - - //==================================================================================================== - // testSubTypeWithGenerics - //==================================================================================================== - @Test - public void testSubTypeWithGenerics() throws Exception { - JsonSerializer s = JsonSerializer.DEFAULT; - - C1 c1 = C3.create(); - String r = s.serialize(c1); - assertEquals("{\"type\":\"C3\",\"f1\":{\"f2\":\"f2\",\"f3\":3}}", r); - } - - - @Bean( - subTypeProperty="type", - subTypes={ - @BeanSubType(id="C3", type=C3.class) - } - ) - public static interface C1<T> extends Serializable { - void setF1(T f1); - T getF1(); - } - - public abstract static class C2<T> implements C1<T> { - protected T f1; - - @Override /* C1 */ - public void setF1(T f1) { - this.f1 = f1; - } - - @Override /* C1 */ - public T getF1() { - return f1; - } - } - - public static class C3<T> extends C2<T> { - - public static C3 create() { - C3 c3 = new C3<Object>(); - CDTO cdto = new CDTO(); - cdto.f2 = "f2"; - cdto.f3 = 3; - c3.f1 = cdto; - return c3; - } - - @Override /* C1 */ - public void setF1(T f1) { - this.f1 = f1; - } - - @Override /* C1 */ - public T getF1() { - return f1; - } - } - - public static class CDTO { - public String f2; - public int f3; - } - - - //==================================================================================================== - // Surrogate transforms - //==================================================================================================== - @Test - public void testSurrogateTransform() throws Exception { - addTransforms(D2.class); - - JsonSerializer s = new JsonSerializer.Simple().addTransforms(D2.class); - JsonParser p = new JsonParser().addTransforms(D2.class); - Object r; - D1 d1 = D1.create(); - - r = s.serialize(d1); - assertEquals("{f2:'f1'}", r); - - d1 = p.parse(r, D1.class); - assertEquals("f1", d1.f1); - - r = getSerializer().serialize(d1); - assertTrue(TestUtils.toString(r).contains("f2")); - - d1 = roundTrip(d1, D1.class); - } - - public static class D1 { - public String f1; - - public static D1 create() { - D1 d1 = new D1(); - d1.f1 = "f1"; - return d1; - } - } - - public static class D2 { - public String f2; - public D2(D1 d1) { - f2 = d1.f1; - } - public D2() { - } - public static D1 valueOf(D2 d2) { - D1 d1 = new D1(); - d1.f1 = d2.f2; - return d1; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTrimStrings.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTrimStrings.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTrimStrings.java deleted file mode 100755 index cdd56c8..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/CT_RoundTripTrimStrings.java +++ /dev/null @@ -1,98 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.a.rttests; - -import static org.apache.juneau.TestUtils.*; - -import org.apache.juneau.*; -import org.apache.juneau.parser.*; -import org.apache.juneau.serializer.*; -import org.junit.*; - - -/** - * Tests for the {@link SerializerContext#SERIALIZER_trimStrings} and {@link ParserContext#PARSER_trimStrings}. - */ -public class CT_RoundTripTrimStrings extends RoundTripTest { - - public CT_RoundTripTrimStrings(String label, Serializer s, Parser p, int flags) throws Exception { - super(label, s, p, flags); - } - - //==================================================================================================== - // test - //==================================================================================================== - @SuppressWarnings("hiding") - @Test - public void test() throws Exception { - if (isValidationOnly()) - return; - Serializer s = getSerializer(); - Parser p = getParser(); - Object in, a, e; - - Serializer s2 = s.clone().setProperty(SerializerContext.SERIALIZER_trimStrings, true); - Parser p2 = p.clone().setProperty(ParserContext.PARSER_trimStrings, true); - - in = " foo bar "; - e = "foo bar"; - a = p.parse(s2.serialize(in), String.class); - assertEqualObjects(e, a); - a = p2.parse(s.serialize(in), String.class); - assertEqualObjects(e, a); - - in = new ObjectMap("{' foo ': ' bar '}"); - e = new ObjectMap("{foo:'bar'}"); - a = p.parse(s2.serialize(in), ObjectMap.class); - assertEqualObjects(e, a); - a = p2.parse(s.serialize(in), ObjectMap.class); - assertEqualObjects(e, a); - - in = new ObjectList("[' foo ', {' foo ': ' bar '}]"); - e = new ObjectList("['foo',{foo:'bar'}]"); - a = p.parse(s2.serialize(in), ObjectList.class); - assertEqualObjects(e, a); - a = p2.parse(s.serialize(in), ObjectList.class); - assertEqualObjects(e, a); - - in = new A().init1(); - e = new A().init2(); - a = p.parse(s2.serialize(in), A.class); - assertEqualObjects(e, a); - a = p2.parse(s.serialize(in), A.class); - assertEqualObjects(e, a); - } - - public static class A { - public String f1; - public String[] f2; - public ObjectList f3; - public ObjectMap f4; - - public A init1() throws Exception { - f1 = " f1 "; - f2 = new String[]{" f2a ", " f2b "}; - f3 = new ObjectList("[' f3a ',' f3b ']"); - f4 = new ObjectMap("{' foo ':' bar '}"); - return this; - } - - public A init2() throws Exception { - f1 = "f1"; - f2 = new String[]{"f2a", "f2b"}; - f3 = new ObjectList("['f3a','f3b']"); - f4 = new ObjectMap("{'foo':'bar'}"); - return this; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripAddClassAttrsTest.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripAddClassAttrsTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripAddClassAttrsTest.java new file mode 100755 index 0000000..7f5658f --- /dev/null +++ b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripAddClassAttrsTest.java @@ -0,0 +1,349 @@ +/*************************************************************************************************************************** + * 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.BeanContext.*; +import static org.apache.juneau.a.rttests.RoundTripTest.Flags.*; +import static org.apache.juneau.serializer.SerializerContext.*; +import static org.junit.Assert.*; + +import java.util.*; + +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.*; +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. + */ +public class RoundTripAddClassAttrsTest extends RoundTripTest { + + @Parameterized.Parameters + public static Collection<Object[]> getPairs() { + return Arrays.asList(new Object[][] { + { /* 0 */ + "JsonSerializer.DEFAULT/JsonParser.DEFAULT", + new JsonSerializer().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new JsonParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + }, + { /* 1 */ + "JsonSerializer.DEFAULT_LAX/JsonParser.DEFAULT", + new JsonSerializer.Simple().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new JsonParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + }, + { /* 2 */ + "JsonSerializer.DEFAULT_SQ/JsonParser.DEFAULT", + new JsonSerializer.Simple().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new JsonParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + }, + { /* 3 */ + "XmlSerializer.DEFAULT/XmlParser.DEFAULT", + new XmlSerializer.XmlJson().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new XmlParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + CHECK_XML_WHITESPACE | VALIDATE_XML + }, + { /* 4 */ + "HtmlSerializer.DEFAULT/HtmlParser.DEFAULT", + new HtmlSerializer().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new HtmlParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + CHECK_XML_WHITESPACE + }, + { /* 5 */ + "UonSerializer.DEFAULT_ENCODING/UonParser.DEFAULT_DECODING", + new UonSerializer.Encoding().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new UonParser.Decoding().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + }, + { /* 6 */ + "UonSerializer.DEFAULT/UonParser.DEFAULT", + new UonSerializer().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new UonParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + }, + { /* 7 */ + "UrlEncodingSerializer.DEFAULT/UrlEncodingParser.DEFAULT", + new UrlEncodingSerializer().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new UrlEncodingParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + }, + { /* 8 */ + "RdfSerializer.Xml/RdfParser.Xml", + new RdfSerializer.Xml().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new RdfParser.Xml().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + }, + { /* 9 */ + "MsgPackSerializer.DEFAULT/MsgPackParser.DEFAULT", + new MsgPackSerializer().setProperty(SERIALIZER_addClassAttrs, true).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + new MsgPackParser().setProperty(BEAN_useInterfaceProxies, false).setClassLoader(RoundTripAddClassAttrsTest.class.getClassLoader()), + 0 + } + }); + } + + public RoundTripAddClassAttrsTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // testBean + //==================================================================================================== + @Test + public void testBean() throws Exception { + A t = new A("foo"); + AA ta; + IA ti; + + t = roundTrip(t, A.class); + assertEquals("foo", t.getF1()); + + ta = roundTrip(t, AA.class); + assertEquals("foo", ta.getF1()); + + ti = roundTrip(t, IA.class); + assertEquals("foo", ti.getF1()); + + t = (A)roundTrip(t, Object.class); + assertEquals("foo", t.getF1()); + } + + public static interface IA { + public String getF1(); + public void setF1(String f1); + } + + public static abstract class AA implements IA { + } + + public static class A extends AA { + private String f1; + + @Override /* AA */ + public String getF1() { + return f1; + } + @Override /* AA */ + public void setF1(String f1) { + this.f1 = f1; + } + + public A() {} + public A(String f1) { + this.f1 = f1; + } + } + + //==================================================================================================== + // testBeanArray + //==================================================================================================== + @Test + public void testBeanArray() throws Exception { + A[] t = {new A("foo")}; + AA[] ta; + IA[] ti; + + t = roundTrip(t, A[].class); + assertEquals("foo", t[0].getF1()); + + ta = roundTrip(t, AA[].class); + assertEquals("foo", ta[0].getF1()); + + ti = roundTrip(t, IA[].class); + assertEquals("foo", ti[0].getF1()); + + t = (A[])roundTrip(t, Object.class); + assertEquals("foo", t[0].getF1()); + } + + //==================================================================================================== + // testBeanWithBeanProps + //==================================================================================================== + @Test + public void testBeanWithBeanProps() throws Exception { + B t = new B("foo"); + t = roundTrip(t, B.class); + assertEquals("foo", t.f2a.getF1()); + assertEquals("foo", t.f2b.getF1()); + assertEquals("foo", t.f2c.getF1()); + assertEquals("foo", ((A)t.f2d).getF1()); + + t = (B)roundTrip(t, Object.class); + assertEquals("foo", t.f2a.getF1()); + assertEquals("foo", t.f2b.getF1()); + assertEquals("foo", t.f2c.getF1()); + assertEquals("foo", ((A)t.f2d).getF1()); + } + + public static class B { + public A f2a; + public AA f2b; + public IA f2c; + public Object f2d; + public B() {} + public B(String f1) { + f2d = f2c = f2b = f2a = new A(f1); + } + } + + //==================================================================================================== + // testMapsWithTypeParams - Maps with type parameters should not have class attributes on entries. + //==================================================================================================== + @Test + public void testMapsWithTypeParams() throws Exception { + C t = new C("foo"); + t = roundTrip(t, C.class); + assertEquals("foo", t.f3a.get("foo").getF1()); + assertEquals("foo", t.f3b.get("foo").getF1()); + assertEquals("foo", t.f3c.get("foo").getF1()); + assertEquals("foo", t.f3d.get("foo").getF1()); + + t = (C)roundTrip(t, Object.class); + assertEquals("foo", t.f3a.get("foo").getF1()); + assertEquals("foo", t.f3b.get("foo").getF1()); + assertEquals("foo", t.f3c.get("foo").getF1()); + assertEquals("foo", t.f3d.get("foo").getF1()); + } + + public static class C { + public Map<String,A> f3a = new HashMap<String,A>(); + public Map<String,A> f3b = new HashMap<String,A>(); + public Map<String,A> f3c = new HashMap<String,A>(); + public Map<String,A> f3d = new HashMap<String,A>(); + + public C(){} + public C(String f1) { + A b = new A(f1); + f3a.put("foo", b); + f3b.put("foo", b); + f3c.put("foo", b); + f3d.put("foo", b); + } + } + + //==================================================================================================== + // testMapsWithoutTypeParams - Maps without type parameters should have class attributes on entries. + //==================================================================================================== + @Test + public void testMapsWithoutTypeParams() throws Exception { + D t = new D("foo"); + t = roundTrip(t, D.class); + assertEquals("foo", t.f4a[0].getF1()); + assertEquals("foo", t.f4b[0].getF1()); + assertEquals("foo", t.f4c[0].getF1()); + assertEquals("foo", ((A)t.f4d[0]).getF1()); + + t = (D)roundTrip(t, Object.class); + assertEquals("foo", t.f4a[0].getF1()); + assertEquals("foo", t.f4b[0].getF1()); + assertEquals("foo", t.f4c[0].getF1()); + assertEquals("foo", ((A)t.f4d[0]).getF1()); + } + + public static class D { + public A[] f4a; + public AA[] f4b; + public IA[] f4c; + public Object[] f4d; + + public D(){} + public D(String f1) { + A b = new A(f1); + f4a = new A[]{b}; + f4b = new AA[]{b}; + f4c = new IA[]{b}; + f4d = new Object[]{b}; + } + } + + //==================================================================================================== + // testBeanWithListProps + //==================================================================================================== + @Test + public void testBeanWithListProps() throws Exception { + E t = new E("foo"); + t = roundTrip(t, E.class); + assertEquals("foo", t.f5a.get(0).getF1()); + assertEquals("foo", t.f5b.get(0).getF1()); + assertEquals("foo", t.f5c.get(0).getF1()); + assertEquals("foo", ((A)t.f5d.get(0)).getF1()); + + t = (E)roundTrip(t, Object.class); + assertEquals("foo", t.f5a.get(0).getF1()); + assertEquals("foo", t.f5b.get(0).getF1()); + assertEquals("foo", t.f5c.get(0).getF1()); + assertEquals("foo", ((A)t.f5d.get(0)).getF1()); + } + + public static class E { + public List<A> f5a = new LinkedList<A>(); + public List<AA> f5b = new LinkedList<AA>(); + public List<IA> f5c = new LinkedList<IA>(); + public List<Object> f5d = new LinkedList<Object>(); + + public E(){} + public E(String f1) { + A b = new A(f1); + f5a.add(b); + f5b.add(b); + f5c.add(b); + f5d.add(b); + } + } + + //==================================================================================================== + // testBeanWithListOfArraysProps + //==================================================================================================== + @Test + public void testBeanWithListOfArraysProps() throws Exception { + F t = new F("foo"); + t = roundTrip(t, F.class); + assertEquals("foo", t.f6a.get(0)[0].getF1()); + assertEquals("foo", t.f6b.get(0)[0].getF1()); + assertEquals("foo", t.f6c.get(0)[0].getF1()); + assertEquals("foo", ((A)t.f6d.get(0)[0]).getF1()); + + t = (F)roundTrip(t, Object.class); + assertEquals("foo", t.f6a.get(0)[0].getF1()); + assertEquals("foo", t.f6b.get(0)[0].getF1()); + assertEquals("foo", t.f6c.get(0)[0].getF1()); + assertEquals("foo", ((A)t.f6d.get(0)[0]).getF1()); + } + + public static class F { + public List<A[]> f6a = new LinkedList<A[]>(); + public List<AA[]> f6b = new LinkedList<AA[]>(); + public List<IA[]> f6c = new LinkedList<IA[]>(); + public List<Object[]> f6d = new LinkedList<Object[]>(); + + public F(){} + public F(String f1) { + A[] b = {new A(f1)}; + f6a.add(b); + f6b.add(b); + f6c.add(b); + f6d.add(b); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripBeanInheritanceTest.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripBeanInheritanceTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripBeanInheritanceTest.java new file mode 100755 index 0000000..6495a10 --- /dev/null +++ b/org.apache.juneau/src/test/java/org/apache/juneau/a/rttests/RoundTripBeanInheritanceTest.java @@ -0,0 +1,220 @@ +/*************************************************************************************************************************** + * 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 org.apache.juneau.*; +import org.apache.juneau.json.*; +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. + */ +public class RoundTripBeanInheritanceTest extends RoundTripTest { + + public RoundTripBeanInheritanceTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // testBeanInheritance + //==================================================================================================== + @Test + public void testBeanInheritance() throws Exception { + + // Skip tests that just return the same object. + if (returnOriginalObject) + return; + + A2 t1 = new A2(), t2; + t1.init(); + t2 = roundTrip(t1, A2.class); + assertEqualObjects(t1, t2); + + A3 t3 = new A3(); + t3.init(); + try { + ClassMeta<?> cm = BeanContext.DEFAULT.getClassMeta(A3.class); + assertEquals("No properties detected on bean class", cm.getNotABeanReason()); + roundTrip(t3, A3.class); + fail("Exception expected"); + } catch (ParseException e) { + } catch (SerializeException e) { + } catch (InvalidDataConversionException e) {} + } + + + public static abstract class A1 { + protected String x = null; + protected String y = null; + protected String z = null; + + public A1() { + this.x = null; + this.y = null; + this.z = null; + } + + public A1(String x, String y, String z) { + this.x = x; + this.y = y; + this.z = z; + } + + public void setX(String x) { + this.x = x; + } + + public void setY(String y) { + this.y = y; + } + + public void setZ(String z) { + this.z = z; + } + + @Override /* Object */ + public String toString() { + return ("A1(x: " + this.x + ", y: " + this.y + ", z: " + this.z + ")"); + } + + public A1 init() { + x = null; + y = ""; + z = "z"; + return this; + } + } + + public static class A2 extends A1 { + public A2() { + super(); + } + + public A2(String x, String y, String z) { + super(x, y, z); + } + + public String getX() { + return this.x; + } + + public String getY() { + return this.y; + } + + public String getZ() { + return this.z; + } + } + + // This is not supposed to be a valid bean since it has no getters defined. + public static class A3 extends A1 { + public A3() { + super(); + } + + public A3(String x, String y, String z) { + super(x, y, z); + } + + public String isX() { + throw new RuntimeException("Should not be called!"); + } + + public String isY() { + throw new RuntimeException("Should not be called!"); + } + + public String isZ() { + throw new RuntimeException("Should not be called!"); + } + } + + //==================================================================================================== + // testBeanInheritance2 + //==================================================================================================== + @Test + public void testBeanInheritance2() throws Exception { + B1 t1 = new B1().init(), t2; + t2 = roundTrip(t1, B1.class); + assertEqualObjects(t1, t2); + } + + public static class B1 extends B2 { + private A2 f4; + + public A2 getF4() { + return this.f4; + } + + public void setF4(A2 f4) { + this.f4 = f4; + } + + @Override /* Object */ + public String toString() { + return super.toString() + " / " + this.f4; + } + + public B1 init() { + setF1("A1"); + setF2(101); + setF3(false); + setF4((A2)new A2().init()); + return this; + } + } + + public static class B2 { + private String f1 = null; + private int f2 = -1; + private boolean f3 = false; + + public String getF1() { + return this.f1; + } + + public void setF1(String f1) { + this.f1 = f1; + } + + public int getF2() { + return this.f2; + } + + public void setF2(int f2) { + this.f2 = f2; + } + + public boolean isF3() { + return this.f3; + } + + public void setF3(boolean f3) { + this.f3 = f3; + } + + @Override /* Object */ + public String toString() { + return ("B2(f1: " + this.getF1() + ", f2: " + this.getF2() + ")"); + } + } +}
