http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripSimpleObjectsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripSimpleObjectsTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripSimpleObjectsTest.java new file mode 100755 index 0000000..d3339a8 --- /dev/null +++ b/juneau-core/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, SerializerBuilder s, ParserBuilder 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 = roundTrip(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/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java new file mode 100755 index 0000000..b09a8bd --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTest.java @@ -0,0 +1,304 @@ +// *************************************************************************************************************************** +// * 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 java.lang.reflect.*; +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.uon.*; +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 JsonSerializerBuilder().trimNullProperties(false), + new JsonParserBuilder(), + 0 + }, + { /* 1 */ + "Json - lax", + new JsonSerializerBuilder().simple().trimNullProperties(false), + new JsonParserBuilder(), + 0 + }, + { /* 2 */ + "Json - lax, readable", + new JsonSerializerBuilder().simple().ws().trimNullProperties(false), + new JsonParserBuilder(), + 0 + }, + { /* 3 */ + "Xml - namespaces, validation, readable", + new XmlSerializerBuilder().ns().sq().trimNullProperties(false).addNamespaceUrisToRoot(true).useWhitespace(true), + new XmlParserBuilder(), + CHECK_XML_WHITESPACE | VALIDATE_XML + }, + { /* 4 */ + "Xml - no namespaces, validation", + new XmlSerializerBuilder().sq().trimNullProperties(false), + new XmlParserBuilder(), + CHECK_XML_WHITESPACE + }, + { /* 5 */ + "Html - default", + new HtmlSerializerBuilder().trimNullProperties(false), + new HtmlParserBuilder(), + CHECK_XML_WHITESPACE + }, + { /* 6 */ + "Html - readable", + new HtmlSerializerBuilder().sq().ws().trimNullProperties(false), + new HtmlParserBuilder(), + CHECK_XML_WHITESPACE + }, + { /* 7 */ + "Html - with key/value headers", + new HtmlSerializerBuilder().addKeyValueTableHeaders(true), + new HtmlParserBuilder(), + CHECK_XML_WHITESPACE + }, + { /* 8 */ + "Uon - default", + new UonSerializerBuilder().trimNullProperties(false), + new UonParserBuilder(), + 0 + }, + { /* 9 */ + "Uon - readable", + new UonSerializerBuilder().ws().trimNullProperties(false), + new UonParserBuilder(), + 0 + }, + { /* 10 */ + "Uon - encoded", + new UonSerializerBuilder().encoding().trimNullProperties(false), + new UonParserBuilder().decoding(), + 0 + }, + { /* 11 */ + "UrlEncoding - default", + new UrlEncodingSerializerBuilder().trimNullProperties(false), + new UrlEncodingParserBuilder(), + 0 + }, + { /* 12 */ + "UrlEncoding - readable", + new UrlEncodingSerializerBuilder().ws().trimNullProperties(false), + new UrlEncodingParserBuilder(), + 0 + }, + { /* 13 */ + "UrlEncoding - expanded params", + new UrlEncodingSerializerBuilder().expandedParams(true), + new UrlEncodingParserBuilder().expandedParams(true), + 0 + }, + { /* 14 */ + "Rdf.Xml", + new RdfSerializerBuilder().trimNullProperties(false).addLiteralTypes(true), + new RdfParserBuilder().xml(), + 0 + }, + { /* 15 */ + "Rdf.XmlAbbrev", + new RdfSerializerBuilder().xmlabbrev().trimNullProperties(false).addLiteralTypes(true), + new RdfParserBuilder().xml(), + 0 + }, + { /* 16 */ + "Rdf.Turtle", + new RdfSerializerBuilder().turtle().trimNullProperties(false).addLiteralTypes(true), + new RdfParserBuilder().turtle(), + 0 + }, + { /* 17 */ + "Rdf.NTriple", + new RdfSerializerBuilder().ntriple().trimNullProperties(false).addLiteralTypes(true), + new RdfParserBuilder().ntriple(), + 0 + }, + { /* 18 */ + "Rdf.N3", + new RdfSerializerBuilder().n3().trimNullProperties(false).addLiteralTypes(true), + new RdfParserBuilder().n3(), + 0 + }, + { /* 19 */ + "MsgPack", + new MsgPackSerializerBuilder().trimNullProperties(false), + new MsgPackParserBuilder(), + 0 + }, + + // Validation testing only + { /* 20 */ + "Json schema", + new JsonSchemaSerializerBuilder().trimNullProperties(false), + null, + RETURN_ORIGINAL_OBJECT + }, + { /* 21 */ + "Xml schema", + new XmlSchemaSerializerBuilder().trimNullProperties(false), + new XmlValidatorParserBuilder(), + 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, SerializerBuilder s, ParserBuilder p, int flags) throws Exception { + this.label = label; + + Map<Class<Object>, Class<? extends Object>> m = getImplClasses(); + if (m != null) { + for (Entry<Class<Object>, Class<? extends Object>> e : m.entrySet()) { + s.implClass(e.getKey(), e.getValue()); + if (p != null) + p.implClass(e.getKey(), e.getValue()); + } + } + this.s = s.beanFilters(getBeanFilters()).pojoSwaps(getPojoSwaps()).beanDictionary(getDictionary()).properties(getProperties()).build(); + this.p = p == null ? null : p.beanFilters(getBeanFilters()).pojoSwaps(getPojoSwaps()).beanDictionary(getDictionary()).properties(getProperties()).build(); + 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 ObjectMap getProperties() { + return ObjectMap.EMPTY_MAP; + } + + public <T> Map<Class<T>,Class<? extends T>> getImplClasses() { + return null; + } + + public <T> T roundTrip(T object, Type c, Type...args) throws Exception { + Object out = serialize(object, this.s); + if (p == null) + return object; + T o = (T)this.p.parse(out, c, args); + 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 beanFilters(Class<?>...c) { + s = s.builder().beanFilters(c).build(); + if (p != null) + p = p.builder().beanFilters(c).build(); + } + + protected void pojoSwaps(Class<?>...c) { + s = s.builder().pojoSwaps(c).build(); + if (p != null) + p = p.builder().pojoSwaps(c).build(); + } + + protected void beanDictionary(Class<?>...c) { + s = s.builder().beanDictionary(c).build(); + if (p != null) + p = p.builder().beanDictionary(c).build(); + } + + 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"); // NOT DEBUG + + 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/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripToObjectMapsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripToObjectMapsTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripToObjectMapsTest.java new file mode 100755 index 0000000..c74f65f --- /dev/null +++ b/juneau-core/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.apache.juneau.utils.*; +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, SerializerBuilder s, ParserBuilder p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // Class with X(ObjectMap) constructor and toObjectMap() method. + //==================================================================================================== + @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 AList<A>().append(new A(new ObjectMap("{f1:'a',f2:2}"))); + a2 = roundTrip(a2, 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 AMap<String,A>().append("a", new A(new ObjectMap("{f1:'a',f2:2}"))); + a3 = roundTrip(a3, 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 swap(BeanSession session) { + return new ObjectMap().append("f1",f1).append("f2",f2); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java new file mode 100755 index 0000000..7ea011c --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java @@ -0,0 +1,374 @@ +// *************************************************************************************************************************** +// * 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.json.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.transform.*; +import org.apache.juneau.transforms.*; +import org.apache.juneau.utils.*; +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","serial","javadoc"}) +public class RoundTripTransformBeansTest extends RoundTripTest { + + public RoundTripTransformBeansTest(String label, SerializerBuilder s, ParserBuilder p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // testSwapBeans1 + //==================================================================================================== + @Test + public void testSwapBeans1() throws Exception { + Class<?>[] f = { + ByteArrayBase64Swap.class, + CalendarSwap.ISO8601DTZ.class, + DateSwap.ISO8601DTZ.class + }; + pojoSwaps(f); + A t = new A().init(); + t = roundTrip(t, A.class); + + // ByteArrayBase64Swap + 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")); + + // CalendarSwap + 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); + + // DateSwap + 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 ByteArrayBase64Swap + 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 AList<byte[]>() + .append(new byte[]{1,2,3}) + .append(new byte[]{4,5,6}) + .append(null) + ; + fmByte = new AMap<String,byte[]>() + .append("foo", new byte[]{1,2,3}) + .append("bar", new byte[]{4,5,6}) + .append("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 AList<Date>() + .append(new Date(4000)) + .append(null) + ; + fmDate = new AMap<String,Date>() + .append("foo", new Date(5000)) + .append("bar", null) + ; + return this; + } + } + + + //==================================================================================================== + // testSwapBeans2 + //==================================================================================================== + @Test + public void testSwapBeans2() throws Exception { + Class<?>[] f = { + ByteArrayBase64Swap.class, + CalendarSwap.DateMedium.class, + DateSwap.RFC2822DT.class, + }; + pojoSwaps(f); + A t = new A().init(); + t = roundTrip(t, A.class); + + // ByteArrayBase64Swap + 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")); + + // CalendarSwap + 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); + + // DateSwap + assertEquals(1000, t.fDate.getTime() % 3600000); + assertNull(t.fnDate); + assertEquals(3000, t.faDate[2].getTime() % 3600000); + assertEquals(4000, t.flDate.get(0).getTime() % 3600000); + assertNull(t.flDate.get(1)); + assertEquals(5000, t.fmDate.get("foo").getTime() % 3600000); + assertNull(t.fmDate.get("bar")); + } + + //==================================================================================================== + // testSwaps - Bean.pojoSwaps annotation + //==================================================================================================== + @Test + public void testSwaps() throws Exception { + B t = new B(); + t.f1 = "bar"; + t = roundTrip(t, B.class); + + assertEquals("bar", t.f1); + } + + @Pojo(swap=BSwap.class) + public static class B { + public String f1; + } + + public static class BSwap extends StringSwap<B> { + @Override /* PojoSwap */ + public String swap(BeanSession session, B o) throws SerializeException { + return o.f1; + } + @Override /* PojoSwap */ + public B unswap(BeanSession session, String f, ClassMeta<?> hint) throws ParseException { + B b1 = new B(); + b1.f1 = f; + return b1; + } + } + + //==================================================================================================== + // testXMLGregorianCalendar - Test XMLGregorianCalendarSwap class. + //==================================================================================================== + @Test + public void testXMLGregorianCalendar() throws Exception { + + if (isValidationOnly()) + return; + + GregorianCalendar gc = new GregorianCalendar(); + XMLGregorianCalendar c = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); + + Serializer s = getSerializer().builder().pojoSwaps(XMLGregorianCalendarSwap.class).build(); + Parser p = getParser().builder().pojoSwaps(XMLGregorianCalendarSwap.class).build(); + + 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(beanDictionary={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; + } + } + + @Bean(typeName="C3") + 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 testSurrogates() throws Exception { + pojoSwaps(D2.class); + + JsonSerializer s = new JsonSerializerBuilder().simple().pojoSwaps(D2.class).build(); + JsonParser p = new JsonParserBuilder().pojoSwaps(D2.class).build(); + 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/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java new file mode 100755 index 0000000..dfac4fe --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java @@ -0,0 +1,97 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package 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}. + */ +@SuppressWarnings("javadoc") +public class RoundTripTrimStringsTest extends RoundTripTest { + + public RoundTripTrimStringsTest(String label, SerializerBuilder s, ParserBuilder p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + if (isValidationOnly()) + return; + Serializer s = getSerializer(); + Parser p = getParser(); + Object in, a, e; + + Serializer s2 = s.builder().trimStrings(true).build(); + Parser p2 = p.builder().trimStrings(true).build(); + + 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/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java new file mode 100755 index 0000000..84f9abf --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java @@ -0,0 +1,51 @@ +// *************************************************************************************************************************** +// * 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.csv; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.serializer.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class CsvTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + List<A> l = new LinkedList<A>(); + l.add(new A("b1",1)); + l.add(new A("b2",2)); + + WriterSerializer s = CsvSerializer.DEFAULT; + String r; + + r = s.serialize(l); + + assertEquals("b,c\nb1,1\nb2,2\n", r); + } + + public static class A { + public String b; + public int c; + + public A(String b, int c) { + this.b = b; + this.c = c; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java new file mode 100755 index 0000000..91ba021 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java @@ -0,0 +1,195 @@ +// *************************************************************************************************************************** +// * 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.dto.atom; + +import static org.apache.juneau.TestUtils.*; +import static org.apache.juneau.dto.atom.AtomBuilder.*; +import static org.junit.Assert.*; + +import java.net.*; + +import org.apache.juneau.xml.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class AtomTest { + + public Feed createFeed() throws Exception { + return + feed("tag:foo.org", "Title", "2016-12-31T01:02:03-04:00") + .subtitle(text("html").text("Subtitle")) + .links( + link("alternate", "text/html", "http://foo.org/").hreflang("en"), + link("self", "application/atom+xml", "http://foo.org/feed.atom") + ) + .generator( + generator("Example Toolkit").uri("http://www.foo.org/").version("1.0") + ) + .entries( + entry("tag:foo.org", "Title", "2016-12-31T01:02:03-04:00") + .links( + link("alternate", "text/html", "http://foo.org/2005/04/02/atom"), + link("enclosure", "audio/mpeg", "http://foo.org/audio/foobar.mp3").length(1337) + ) + .published("2016-12-31T01:02:03-04:00") + .authors( + person("John Smith").uri(new URI("http://foo.org/")).email("[email protected]") + ) + .contributors( + person("John Smith"), + person("Jane Smith") + ) + .content( + content("xhtml") + .lang("en") + .base("http://foo.org/") + .text("<div><p><i>[Sample content]</i></p></div>") + ) + ); + } + + @Test + public void testNormal() throws Exception { + XmlSerializer s; + XmlParser p = XmlParser.DEFAULT; + String r; + Feed f = createFeed(), f2; + + String expected = + "<feed>\n" + +" <entry>\n" + +" <author>\n" + +" <email>[email protected]</email>\n" + +" <name>John Smith</name>\n" + +" <uri>http://foo.org/</uri>\n" + +" </author>\n" + +" <content base='http://foo.org/' lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></content>\n" + +" <contributor>\n" + +" <name>John Smith</name>\n" + +" </contributor>\n" + +" <contributor>\n" + +" <name>Jane Smith</name>\n" + +" </contributor>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>\n" + +" <published>2016-12-31T01:02:03-04:00</published>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +" </entry>\n" + +" <generator uri='http://www.foo.org/' version='1.0'>Example Toolkit</generator>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>\n" + +" <subtitle type='html'>Subtitle</subtitle>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +"</feed>\n"; + + s = new XmlSerializerBuilder().sq().ws().enableNamespaces(false).sortProperties(true).build(); + r = s.serialize(f); + assertEquals(expected, r); + f2 = p.parse(r, Feed.class); + assertEqualObjects(f, f2); + } + + @Test + public void testWithNamespaces() throws Exception { + XmlSerializer s; + XmlParser p = XmlParser.DEFAULT; + String r; + Feed f = createFeed(), f2; + + String expected = + "<atom:feed xmlns='http://www.apache.org/2013/Juneau' xmlns:atom='http://www.w3.org/2005/Atom/' xmlns:xml='http://www.w3.org/XML/1998/namespace'>\n" + +" <atom:entry>\n" + +" <atom:author>\n" + +" <atom:email>[email protected]</atom:email>\n" + +" <atom:name>John Smith</atom:name>\n" + +" <atom:uri>http://foo.org/</atom:uri>\n" + +" </atom:author>\n" + +" <atom:content xml:base='http://foo.org/' xml:lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></atom:content>\n" + +" <atom:contributor>\n" + +" <atom:name>John Smith</atom:name>\n" + +" </atom:contributor>\n" + +" <atom:contributor>\n" + +" <atom:name>Jane Smith</atom:name>\n" + +" </atom:contributor>\n" + +" <atom:id>tag:foo.org</atom:id>\n" + +" <atom:link href='http://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>\n" + +" <atom:link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>\n" + +" <atom:published>2016-12-31T01:02:03-04:00</atom:published>\n" + +" <atom:title>Title</atom:title>\n" + +" <atom:updated>2016-12-31T01:02:03-04:00</atom:updated>\n" + +" </atom:entry>\n" + +" <atom:generator uri='http://www.foo.org/' version='1.0'>Example Toolkit</atom:generator>\n" + +" <atom:id>tag:foo.org</atom:id>\n" + +" <atom:link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>\n" + +" <atom:link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>\n" + +" <atom:subtitle type='html'>Subtitle</atom:subtitle>\n" + +" <atom:title>Title</atom:title>\n" + +" <atom:updated>2016-12-31T01:02:03-04:00</atom:updated>\n" + +"</atom:feed>\n"; + + s = new XmlSerializerBuilder().sq().ws().enableNamespaces(true).addNamespaceUrisToRoot(true).sortProperties(true).build(); + r = s.serialize(f); + assertEquals(expected, r); + f2 = p.parse(r, Feed.class); + assertEqualObjects(f, f2); + } + + @Test + public void testWithNamespacesWithAtomAsDefault() throws Exception { + XmlSerializer s; + XmlParser p = XmlParser.DEFAULT; + String r; + Feed f = createFeed(), f2; + + String expected = + "<feed xmlns='http://www.w3.org/2005/Atom/' xmlns:xml='http://www.w3.org/XML/1998/namespace'>\n" + +" <entry>\n" + +" <author>\n" + +" <email>[email protected]</email>\n" + +" <name>John Smith</name>\n" + +" <uri>http://foo.org/</uri>\n" + +" </author>\n" + +" <content xml:base='http://foo.org/' xml:lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></content>\n" + +" <contributor>\n" + +" <name>John Smith</name>\n" + +" </contributor>\n" + +" <contributor>\n" + +" <name>Jane Smith</name>\n" + +" </contributor>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>\n" + +" <published>2016-12-31T01:02:03-04:00</published>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +" </entry>\n" + +" <generator uri='http://www.foo.org/' version='1.0'>Example Toolkit</generator>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>\n" + +" <subtitle type='html'>Subtitle</subtitle>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +"</feed>\n"; + + s = new XmlSerializerBuilder().sq().ws().defaultNamespace("atom").enableNamespaces(true).addNamespaceUrisToRoot(true).sortProperties(true).build(); + r = s.serialize(f); + assertEquals(expected, r); + f2 = p.parse(r, Feed.class); + assertEqualObjects(f, f2); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java new file mode 100755 index 0000000..0f0bdd4 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java @@ -0,0 +1,111 @@ +// *************************************************************************************************************************** +// * 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.dto.cognos; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.xml.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class CognosXmlTest { + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + String expected = "" + + "<dataset xmlns='http://developer.cognos.com/schemas/xmldata/1/'>\n" + + " <metadata>\n" + + " <item name='asOfDate' type='xs:string' length='12'/>\n" + + " <item name='rateOfReturn' type='xs:double'/>\n" + + " <item name='famAcctIndex' type='xs:string' length='3'/>\n" + + " <item name='rowID' type='xs:string' length='1'/>\n" + + " <item name='brM' type='xs:string' length='1'/>\n" + + " <item name='productLineCode' type='xs:int'/>\n" + + " </metadata>\n" + + " <data>\n" + + " <row>\n" + + " <value>Apr 26, 2002</value>\n" + + " <value>0.21006642</value>\n" + + " <value>JA1</value>\n" + + " <value>F</value>\n" + + " <value>B</value>\n" + + " <value>1</value>\n" + + " </row>\n" + + " <row>\n" + + " <value>Apr 27, 2002</value>\n" + + " <value>0.1111111</value>\n" + + " <value>BBB</value>\n" + + " <value>G</value>\n" + + " <value>B</value>\n" + + " <value>2</value>\n" + + " </row>\n" + + " </data>\n" + + "</dataset>\n"; + + List<Object> rows = new LinkedList<Object>(); + rows.add(new ObjectMap("{asOfDate:'Apr 26, 2002',rateOfReturn:0.21006642,famAcctIndex:'JA1',rowID:'F',brM:'B',productLineCode:1}")); + rows.add(new Item("Apr 27, 2002", 0.1111111, "BBB", "G", "B", 2)); + + Column[] c = { + new Column("asOfDate", "xs:string", 12), + new Column("rateOfReturn", "xs:double"), + new Column("famAcctIndex", "xs:string", 3), + new Column("rowID", "xs:string", 1), + new Column("brM", "xs:string", 1), + new Column("productLineCode", "xs:int") + }; + + XmlSerializer s = new XmlSerializerBuilder() + .ws() + .sq() + .defaultNamespace("cognos") + .ns() + .addNamespaceUrisToRoot(true) + .build(); + + DataSet ds = new DataSet(c, rows, BeanContext.DEFAULT.createSession()); + + String out = s.serialize(ds); + + assertEquals(expected, out); + + // Make sure we can parse it back into a POJO. + DataSet ds2 = XmlParser.DEFAULT.parse(out, DataSet.class); + assertEqualObjects(ds, ds2); + } + + public static class Item { + public String asOfDate; + public double rateOfReturn; + public String famAcctIndex; + public String rowID; + public String brM; + public int productLineCode; + + public Item(String asOfDate, double rateOfReturn, String famAcctIndex, String rowID, String brM, int productLineCode) { + this.asOfDate = asOfDate; + this.rateOfReturn = rateOfReturn; + this.famAcctIndex = famAcctIndex; + this.rowID = rowID; + this.brM = brM; + this.productLineCode = productLineCode; + } + } +} \ No newline at end of file
