http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ObjectListTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ObjectListTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ObjectListTest.java new file mode 100755 index 0000000..0c60499 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ObjectListTest.java @@ -0,0 +1,141 @@ +// *************************************************************************************************************************** +// * 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; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import java.io.*; +import java.util.*; + +import org.junit.*; + +@SuppressWarnings("javadoc") +public class ObjectListTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + + assertEquals( + "['A','B','C']", + new ObjectList((Object[])new String[]{"A","B","C"}).toString() + ); + + assertEquals( + "['A','B','C']", + new ObjectList("A","B","C").toString() + ); + + assertEquals( + "['A','B','C']", + new ObjectList(Arrays.asList(new String[]{"A","B","C"})).toString() + ); + } + + //==================================================================================================== + // testIterateAs + //==================================================================================================== + @Test + public void testIterateAs() throws Exception { + + // Iterate over a list of ObjectMaps. + ObjectList l = new ObjectList("[{foo:'bar'},{baz:123}]"); + Iterator<ObjectMap> i1 = l.elements(ObjectMap.class).iterator(); + assertEquals("bar", i1.next().getString("foo")); + assertEquals(123, (int)i1.next().getInt("baz")); + + // Iterate over a list of ints. + l = new ObjectList("[1,2,3]"); + Iterator<Integer> i2 = l.elements(Integer.class).iterator(); + assertEquals(1, (int)i2.next()); + assertEquals(2, (int)i2.next()); + assertEquals(3, (int)i2.next()); + + // Iterate over a list of beans. + // Automatically converts to beans. + l = new ObjectList("[{name:'John Smith',age:45}]"); + Iterator<Person> i3 = l.elements(Person.class).iterator(); + assertEquals("John Smith", i3.next().name); + } + + public static class Person { + public String name; + public int age; + } + + //==================================================================================================== + // testAtMethods + //==================================================================================================== + @Test + public void testAtMethods() throws Exception { + ObjectList l = new ObjectList("[{foo:'bar'},{baz:123}]"); + String r; + + r = l.getAt("0/foo", String.class); + assertEquals("bar", r); + + l.putAt("0/foo", "bing"); + r = l.getAt("0/foo", String.class); + assertEquals("bing", r); + + l.postAt("", new ObjectMap("{a:'b'}")); + r = l.getAt("2/a", String.class); + assertEquals("b", r); + + l.deleteAt("2"); + assertEquals("[{foo:'bing'},{baz:123}]", l.toString()); + } + + //==================================================================================================== + // ObjectList(Reader) + //==================================================================================================== + @Test + public void testFromReader() throws Exception { + assertObjectEquals("[1,2,3]", new ObjectList(new StringReader("[1,2,3]"))); + } + + //==================================================================================================== + // testGetMap + //==================================================================================================== + @Test + public void testGetMap() throws Exception { + ObjectList l = new ObjectList("[{1:'true',2:'false'}]"); + Map<Integer,Boolean> m2 = l.getMap(0, Integer.class, Boolean.class); + assertObjectEquals("{'1':true,'2':false}", m2); + assertEquals(Integer.class, m2.keySet().iterator().next().getClass()); + assertEquals(Boolean.class, m2.values().iterator().next().getClass()); + + m2 = l.get(0, Map.class, Integer.class, Boolean.class); + assertObjectEquals("{'1':true,'2':false}", m2); + assertEquals(Integer.class, m2.keySet().iterator().next().getClass()); + assertEquals(Boolean.class, m2.values().iterator().next().getClass()); + } + + //==================================================================================================== + // testGetList + //==================================================================================================== + @Test + public void testGetList() throws Exception { + ObjectList l = new ObjectList("[['123','456']]"); + List<Integer> l2 = l.getList(0, Integer.class); + assertObjectEquals("[123,456]", l2); + assertEquals(Integer.class, l2.iterator().next().getClass()); + + l2 = l.get(0, List.class, Integer.class); + assertObjectEquals("[123,456]", l2); + assertEquals(Integer.class, l2.iterator().next().getClass()); + } +} \ 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/ObjectMapTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ObjectMapTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ObjectMapTest.java new file mode 100755 index 0000000..efce0a0 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ObjectMapTest.java @@ -0,0 +1,369 @@ +// *************************************************************************************************************************** +// * 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; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import java.io.*; +import java.util.*; + +import org.apache.juneau.utils.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class ObjectMapTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + String in; + + in = "{A:'asdf'}"; + checkStep(1, in, new ObjectMap(in).getString("A"), "asdf"); + + in = "{A:{B:'asdf'}}"; + checkStep(2, in, getDeepString(new ObjectMap(in), "A/B"), "asdf"); + checkStep(3, in, new ObjectMap(in).getString("A"), "{B:'asdf'}"); + + in = "{A:{B:'asdf'+\"asdf\"}}"; + checkStep(4, in, getDeepString(new ObjectMap(in), "A/B"), "asdfasdf"); + checkStep(5, in, new ObjectMap(in).getString("A"), "{B:'asdfasdf'}"); + + in = "{A:{B:'asdf' + \n\t \"asdf\"}}"; + checkStep(6, in, getDeepString(new ObjectMap(in), "A/B"), "asdfasdf"); + checkStep(7, in, new ObjectMap(in).getString("A"), "{B:'asdfasdf'}"); + + in = "{A:{B:'asdf\"asdf', C:\"asdf'asdf\", D : \"asdf\\\"asdf\", E: 'asdf\\\'asdf', F:\"asdf\\\'asdf\", G:'asdf\\\"asdf'}}"; + checkStep(8, in, getDeepString(new ObjectMap(in), "A/B"), "asdf\"asdf"); + checkStep(9, in, getDeepString(new ObjectMap(in), "A/C"), "asdf'asdf"); + checkStep(10, in, getDeepString(new ObjectMap(in), "A/D"), "asdf\"asdf"); + checkStep(11, in, getDeepString(new ObjectMap(in), "A/E"), "asdf'asdf"); + checkStep(12, in, getDeepString(new ObjectMap(in), "A/F"), "asdf'asdf"); + checkStep(13, in, getDeepString(new ObjectMap(in), "A/G"), "asdf\"asdf"); + + in = "{A:123, B: 123}"; + checkStep(16, in, new Integer(new ObjectMap(in).getInt("A")).toString(), "123"); + checkStep(17, in, new Integer(new ObjectMap(in).getInt("B")).toString(), "123"); + + in = "{A:true, B: true, C:false, D: false}"; + checkStep(18, in, new Boolean(new ObjectMap(in).getBoolean("A")).toString(), "true"); + checkStep(19, in, new Boolean(new ObjectMap(in).getBoolean("B")).toString(), "true"); + checkStep(20, in, new Boolean(new ObjectMap(in).getBoolean("C")).toString(), "false"); + checkStep(21, in, new Boolean(new ObjectMap(in).getBoolean("D")).toString(), "false"); + + in = "{'AAA':{\"BBB\":\"CCC\",'DDD':false}}"; + checkStep(31, in, getDeepString(new ObjectMap(in), "AAA/BBB"), "CCC"); + checkStep(32, in, getDeepBoolean(new ObjectMap(in), "AAA/DDD").toString(), "false"); + + in = " \n\n\t { 'AAA' : { \"BBB\" : \"CCC\" , 'DDD' : false } } \n\t"; + checkStep(33, in, getDeepString(new ObjectMap(in), "AAA/BBB"), "CCC"); + checkStep(34, in, getDeepBoolean(new ObjectMap(in), "AAA/DDD").toString(), "false"); + + in = "/*x*/{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(100, in, new ObjectMap(in).getString("A"), "B"); + in = "{/*x*/A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(101, in, new ObjectMap(in).getString("A"), "B"); + in = "{A/*x*/:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(102, in, new ObjectMap(in).getString("A"), "B"); + in = "{A:/*x*/'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(103, in, new ObjectMap(in).getString("A"), "B"); + in = "{A:'/*x*/B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(104, in, new ObjectMap(in).getString("A"), "/*x*/B"); + in = "{A:'B/*x*/','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(105, in, new ObjectMap(in).getString("A"), "B/*x*/"); + in = "{A:'B'/*x*/,'C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(106, in, new ObjectMap(in).getString("A"), "B"); + in = "{A:'B',/*x*/'C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(107, in, new ObjectMap(in).getString("C"), "1"); + in = "{A:'B','C':/*x*/1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(108, in, new ObjectMap(in).getString("C"), "1"); + in = "{A:'B','C':1/*x*/,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(109, in, new ObjectMap(in).getString("C"), "1"); + in = "{A:'B','C':1,/*x*/\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(110, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"/*x*/E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(111, in, new ObjectMap(in).getObjectList("/*x*/E").getString(0), "1"); + in = "{A:'B','C':1,\"E/*x*/\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(112, in, new ObjectMap(in).getObjectList("E/*x*/").getString(0), "1"); + in = "{A:'B','C':1,\"E\"/*x*/:[1,2,3],G:['g1','g2','g3']}"; + checkStep(113, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":/*x*/[1,2,3],G:['g1','g2','g3']}"; + checkStep(114, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":[/*x*/1,2,3],G:['g1','g2','g3']}"; + checkStep(115, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":[1/*x*/,2,3],G:['g1','g2','g3']}"; + checkStep(116, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":[1,/*x*/2,3],G:['g1','g2','g3']}"; + checkStep(117, in, new ObjectMap(in).getObjectList("E").getString(1), "2"); + in = "{A:'B','C':1,\"E\":[1,2/*x*/,3],G:['g1','g2','g3']}"; + checkStep(118, in, new ObjectMap(in).getObjectList("E").getString(1), "2"); + in = "{A:'B','C':1,\"E\":[1,2,/*x*/3],G:['g1','g2','g3']}"; + checkStep(119, in, new ObjectMap(in).getObjectList("E").getString(2), "3"); + in = "{A:'B','C':1,\"E\":[1,2,3]/*x*/,G:['g1','g2','g3']}"; + checkStep(120, in, new ObjectMap(in).getObjectList("E").getString(2), "3"); + in = "{A:'B','C':1,\"E\":[1,2,3],/*x*/G:['g1','g2','g3']}"; + checkStep(121, in, new ObjectMap(in).getObjectList("G").getString(0), "g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:[/*x*/'g1','g2','g3']}"; + checkStep(122, in, new ObjectMap(in).getObjectList("G").getString(0), "g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['/*x*/g1','g2','g3']}"; + checkStep(123, in, new ObjectMap(in).getObjectList("G").getString(0), "/*x*/g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1'/*x*/,'g2','g3']}"; + checkStep(124, in, new ObjectMap(in).getObjectList("G").getString(0), "g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1',/*x*/'g2','g3']}"; + checkStep(125, in, new ObjectMap(in).getObjectList("G").getString(1), "g2"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2'/*x*/,'g3']}"; + checkStep(126, in, new ObjectMap(in).getObjectList("G").getString(1), "g2"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2',/*x*/'g3']}"; + checkStep(127, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3'/*x*/]}"; + checkStep(128, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']/*x*/}"; + checkStep(129, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}/*x*/"; + checkStep(130, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + + in = "/*\tx\t*///\tx\t\n\t/*\tx\t*/{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(201, in, new ObjectMap(in).getString("A"), "B"); + in = "{/*\tx\t*///\tx\t\n\t/*\tx\t*/A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(202, in, new ObjectMap(in).getString("A"), "B"); + in = "{A/*\tx\t*///\tx\t\n\t/*\tx\t*/:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(203, in, new ObjectMap(in).getString("A"), "B"); + in = "{A:/*\tx\t*///\tx\t\n\t/*\tx\t*/'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(204, in, new ObjectMap(in).getString("A"), "B"); + in = "{A:'/*\tx\t*///\tx\t\n\t/*\tx\t*/B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(205, in, new ObjectMap(in).getString("A"), "/*\tx\t*///\tx\t\n\t/*\tx\t*/B"); + in = "{A:'B/*\tx\t*///\tx\t\n\t/*\tx\t*/','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(206, in, new ObjectMap(in).getString("A"), "B/*\tx\t*///\tx\t\n\t/*\tx\t*/"); + in = "{A:'B'/*\tx\t*///\tx\t\n\t/*\tx\t*/,'C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(207, in, new ObjectMap(in).getString("A"), "B"); + in = "{A:'B',/*\tx\t*///\tx\t\n\t/*\tx\t*/'C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(208, in, new ObjectMap(in).getString("C"), "1"); + in = "{A:'B','C':/*\tx\t*///\tx\t\n\t/*\tx\t*/1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(209, in, new ObjectMap(in).getString("C"), "1"); + in = "{A:'B','C':1/*\tx\t*///\tx\t\n\t/*\tx\t*/,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(210, in, new ObjectMap(in).getString("C"), "1"); + in = "{A:'B','C':1,/*\tx\t*///\tx\t\n\t/*\tx\t*/\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(211, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"/*\tx\t*///\tx\t\n\t/*\tx\t*/E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(212, in, new ObjectMap(in).getObjectList("/*\tx\t*///\tx\t\n\t/*\tx\t*/E").getString(0), "1"); + in = "{A:'B','C':1,\"E/*\tx\t*///\tx\t\n\t/*\tx\t*/\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(213, in, new ObjectMap(in).getObjectList("E/*\tx\t*///\tx\t\n\t/*\tx\t*/").getString(0), "1"); + in = "{A:'B','C':1,\"E\"/*\tx\t*///\tx\t\n\t/*\tx\t*/:[1,2,3],G:['g1','g2','g3']}"; + checkStep(214, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":/*\tx\t*///\tx\t\n\t/*\tx\t*/[1,2,3],G:['g1','g2','g3']}"; + checkStep(215, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":[/*\tx\t*///\tx\t\n\t/*\tx\t*/1,2,3],G:['g1','g2','g3']}"; + checkStep(216, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":[1/*\tx\t*///\tx\t\n\t/*\tx\t*/,2,3],G:['g1','g2','g3']}"; + checkStep(217, in, new ObjectMap(in).getObjectList("E").getString(0), "1"); + in = "{A:'B','C':1,\"E\":[1,/*\tx\t*///\tx\t\n\t/*\tx\t*/2,3],G:['g1','g2','g3']}"; + checkStep(218, in, new ObjectMap(in).getObjectList("E").getString(1), "2"); + in = "{A:'B','C':1,\"E\":[1,2/*\tx\t*///\tx\t\n\t/*\tx\t*/,3],G:['g1','g2','g3']}"; + checkStep(219, in, new ObjectMap(in).getObjectList("E").getString(1), "2"); + in = "{A:'B','C':1,\"E\":[1,2,/*\tx\t*///\tx\t\n\t/*\tx\t*/3],G:['g1','g2','g3']}"; + checkStep(220, in, new ObjectMap(in).getObjectList("E").getString(2), "3"); + in = "{A:'B','C':1,\"E\":[1,2,3]/*\tx\t*///\tx\t\n\t/*\tx\t*/,G:['g1','g2','g3']}"; + checkStep(221, in, new ObjectMap(in).getObjectList("E").getString(2), "3"); + in = "{A:'B','C':1,\"E\":[1,2,3],/*\tx\t*///\tx\t\n\t/*\tx\t*/G:['g1','g2','g3']}"; + checkStep(222, in, new ObjectMap(in).getObjectList("G").getString(0), "g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:[/*\tx\t*///\tx\t\n\t/*\tx\t*/'g1','g2','g3']}"; + checkStep(223, in, new ObjectMap(in).getObjectList("G").getString(0), "g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['/*\tx\t*///\tx\t\n\t/*\tx\t*/g1','g2','g3']}"; + checkStep(224, in, new ObjectMap(in).getObjectList("G").getString(0), "/*\tx\t*///\tx\t\n\t/*\tx\t*/g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1'/*\tx\t*///\tx\t\n\t/*\tx\t*/,'g2','g3']}"; + checkStep(225, in, new ObjectMap(in).getObjectList("G").getString(0), "g1"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1',/*\tx\t*///\tx\t\n\t/*\tx\t*/'g2','g3']}"; + checkStep(226, in, new ObjectMap(in).getObjectList("G").getString(1), "g2"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2'/*\tx\t*///\tx\t\n\t/*\tx\t*/,'g3']}"; + checkStep(227, in, new ObjectMap(in).getObjectList("G").getString(1), "g2"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2',/*\tx\t*///\tx\t\n\t/*\tx\t*/'g3']}"; + checkStep(228, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3'/*\tx\t*///\tx\t\n\t/*\tx\t*/]}"; + checkStep(229, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']/*\tx\t*///\tx\t\n\t/*\tx\t*/}"; + checkStep(230, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}/*\tx\t*///\tx\t\n\t/*\tx\t*/"; + checkStep(231, in, new ObjectMap(in).getObjectList("G").getString(2), "g3"); + + in = "{ /* x */ // x \n /* x */ A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(240, in, new ObjectMap(in).getString("A"), "B"); + + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(301, in, new ObjectMap(in).getString("A", "default"), "B"); + in = "{/*A:'B',*/'C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(302, in, new ObjectMap(in).getString("A", "default"), "default"); + in = "{A:'B',/*'C':1,*/\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(303, in, new ObjectMap(in).getString("C", "default"), "default"); + in = "{A:'B','C':1,/*\"E\":[1,2,3],*/G:['g1','g2','g3']}"; + checkStep(304, in, new ObjectMap(in).getString("E", "default"), "default"); + in = "{A:'B','C':1,\"E\":[/*1,*/2,3],G:['g1','g2','g3']}"; + checkStep(305, in, new ObjectMap(in).getObjectList("E").getString(0), "2"); + in = "{A:'B','C':1,\"E\":[1,/*2,*/3],G:['g1','g2','g3']}"; + checkStep(306, in, new ObjectMap(in).getObjectList("E").getString(1), "3"); + in = "{A:'B','C':1,\"E\":[1,2/*,3*/],G:['g1','g2','g3']}"; + checkStep(307, in, new ObjectMap(in).getObjectList("E").getString(1), "2"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:[/*'g1',*/'g2','g3']}"; + checkStep(308, in, new ObjectMap(in).getObjectList("G").getString(0), "g2"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1'/*,'g2'*/,'g3']}"; + checkStep(309, in, new ObjectMap(in).getObjectList("G").getString(1), "g3"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2'/*,'g3'*/]}"; + checkStep(310, in, new ObjectMap(in).getObjectList("G").getString(1), "g2"); + in = "{A:'B','C':1,\"E\":[1,2,3],G:['g1','g2','g3']}"; + checkStep(310, in, new ObjectMap(in).getObjectList("G").getString(1), "g2"); + + // Check keys that contain array indexes + in = "{A:{B:[{C:'c0'},{C:'c1'},{C:'c2'}]}}"; + checkStep(401, in, getDeepString(new ObjectMap(in), "A/B/0/C"), "c0"); + checkStep(402, in, getDeepString(new ObjectMap(in), "A/B/1/C"), "c1"); + checkStep(403, in, getDeepString(new ObjectMap(in), "A/B/2/C"), "c2"); + + // Check extended unicode characters. + in = "{'ð¤¢ð¤¢':'ð¤¢ð¤¢'}"; + checkStep(1, in, new ObjectMap(in).getString("ð¤¢ð¤¢"), "ð¤¢ð¤¢"); + } + + private String getDeepString(ObjectMap m, String url) { + PojoRest r = new PojoRest(m); + return (String)r.get(url); + } + + private Boolean getDeepBoolean(ObjectMap m, String url) { + PojoRest r = new PojoRest(m); + return (Boolean)r.get(url); + } + + private void checkStep(int step, String input, String output, String expectedValue) { + if (!output.equals(expectedValue)) { + String msg = "Step #" + step + " failed: [" + input + "]->[" + output + "]...Expected value=[" + expectedValue + "]"; + fail(msg); + } + } + + //==================================================================================================== + // testComparison + //==================================================================================================== + @Test + public void testComparison() throws Exception { + ObjectMap m1 = new ObjectMap("{ firstName:'John', lastName:'Smith', age:123, isDeceased:false }"); + ObjectMap m2 = new ObjectMap("{ age:123, isDeceased:false, lastName:'Smith', firstName:'John' }"); + + assertTrue(m1.equals(m2)); + } + + //==================================================================================================== + // testParent + //==================================================================================================== + @Test + public void testParent() throws Exception { + ObjectMap m1 = new ObjectMap("{a:1}"); + ObjectMap m2 = new ObjectMap("{b:2}").setInner(m1); + + assertEquals(new Integer(1), m2.getInt("a")); + } + + //==================================================================================================== + // testUpdatability + //==================================================================================================== + @Test + public void testUpdatability() throws Exception { + ObjectMap m = new ObjectMap("{a:[{b:'c'}]}"); + ObjectList l = m.getObjectList("a"); + ObjectMap m2 = l.getObjectMap(0); + m2.put("b", "x"); + assertObjectEquals("{a:[{b:'x'}]}", m); + + m = new ObjectMap("{a:[{b:'c'}]}"); + for (ObjectMap m3 : m.getObjectList("a").elements(ObjectMap.class)) + m3.put("b", "y"); + + assertObjectEquals("{a:[{b:'y'}]}", m); + } + + //==================================================================================================== + // testAtMethods + //==================================================================================================== + @Test + public void testAtMethods() throws Exception { + ObjectMap m = new ObjectMap("{a:[{b:'c'}]}"); + String r; + + r = m.getAt("a/0/b", String.class); + assertEquals("c", r); + + m.putAt("a/0/b", "d"); + r = m.getAt("a/0/b", String.class); + assertEquals("d", r); + + m.postAt("a", "e"); + r = m.getAt("a/1", String.class); + assertEquals("e", r); + + m.deleteAt("a/1"); + assertEquals("{a:[{b:'d'}]}", m.toString()); + } + + //==================================================================================================== + // ObjectMap(Reader) + //==================================================================================================== + @Test + public void testFromReader() throws Exception { + assertObjectEquals("{foo:'bar'}", new ObjectMap(new StringReader("{foo:'bar'}"))); + } + + //==================================================================================================== + // testGetMap + //==================================================================================================== + @Test + public void testGetMap() throws Exception { + ObjectMap m = new ObjectMap("{a:{1:'true',2:'false'}}"); + Map<Integer,Boolean> m2 = m.getMap("a", Integer.class, Boolean.class, null); + assertObjectEquals("{'1':true,'2':false}", m2); + assertEquals(Integer.class, m2.keySet().iterator().next().getClass()); + assertEquals(Boolean.class, m2.values().iterator().next().getClass()); + + m2 = m.getMap("b", Integer.class, Boolean.class, null); + assertNull(m2); + + m2 = m.get("a", Map.class, Integer.class, Boolean.class); + assertObjectEquals("{'1':true,'2':false}", m2); + assertEquals(Integer.class, m2.keySet().iterator().next().getClass()); + assertEquals(Boolean.class, m2.values().iterator().next().getClass()); + + m2 = m.get("b", Map.class, Integer.class, Boolean.class); + assertNull(m2); + } + + //==================================================================================================== + // testGetList + //==================================================================================================== + @Test + public void testGetList() throws Exception { + ObjectMap m = new ObjectMap("{a:['123','456']}"); + List<Integer> l2 = m.getList("a", Integer.class, null); + assertObjectEquals("[123,456]", l2); + assertEquals(Integer.class, l2.iterator().next().getClass()); + + l2 = m.getList("b", Integer.class, null); + assertNull(l2); + + l2 = m.get("a", List.class, Integer.class); + assertObjectEquals("[123,456]", l2); + assertEquals(Integer.class, l2.iterator().next().getClass()); + + l2 = m.get("b", List.class, Integer.class); + assertNull(l2); + } +} \ 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/ParserGenericsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ParserGenericsTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ParserGenericsTest.java new file mode 100755 index 0000000..d1b9491 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ParserGenericsTest.java @@ -0,0 +1,71 @@ +// *************************************************************************************************************************** +// * 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; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.json.*; +import org.apache.juneau.parser.*; +import org.junit.*; + +@SuppressWarnings({"serial","javadoc"}) +public class ParserGenericsTest { + + //==================================================================================================== + // Test generic maps + //==================================================================================================== + @Test + public void testMap() throws Exception { + ReaderParser p = JsonParser.DEFAULT; + + String t = "{foo:{bar:'baz'}}"; + Map<String,TreeMap<String,String>> r1 = p.parse(t, TestMap1.class); + assertEquals(TestMap1.class, r1.getClass()); + assertEquals(TreeMap.class, r1.get("foo").getClass()); + + t = "{foo:[1,2,3]}"; + Map<String,LinkedList<Integer>> r2 = p.parse(t, TestMap2.class); + assertEquals(TestMap2.class, r2.getClass()); + assertEquals(LinkedList.class, r2.get("foo").getClass()); + assertEquals(Integer.class, r2.get("foo").get(0).getClass()); + } + + public static class TestMap1 extends LinkedHashMap<String,TreeMap<String,String>> {} + public static class TestMap2 extends LinkedHashMap<String,LinkedList<Integer>> {} + + //==================================================================================================== + // Test generic maps + //==================================================================================================== + @Test + public void testCollection() throws Exception { + ReaderParser p = JsonParser.DEFAULT; + + String t = "[{foo:{bar:'baz'}}]"; + List<TestMap1> r1 = p.parse(t, TestCollection1.class); + assertEquals(TestCollection1.class, r1.getClass()); + assertEquals(TestMap1.class, r1.get(0).getClass()); + assertEquals(TreeMap.class, r1.get(0).get("foo").getClass()); + + t = "[{foo:[1,2,3]}]"; + List<TestMap2> r2 = p.parse(t, TestCollection2.class); + assertEquals(TestCollection2.class, r2.getClass()); + assertEquals(TestMap2.class, r2.get(0).getClass()); + assertEquals(LinkedList.class, r2.get(0).get("foo").getClass()); + assertEquals(Integer.class, r2.get(0).get("foo").get(0).getClass()); + } + + public static class TestCollection1 extends LinkedList<TestMap1> {} + public static class TestCollection2 extends LinkedList<TestMap2> {} +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ParserReaderTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ParserReaderTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ParserReaderTest.java new file mode 100755 index 0000000..49a8918 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/ParserReaderTest.java @@ -0,0 +1,186 @@ +// *************************************************************************************************************************** +// * 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; + +import static org.junit.Assert.*; + +import java.io.*; + +import org.apache.juneau.parser.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class ParserReaderTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + String t = "01234567890123456789012345678901234567890123456789"; + + // Min buff size is 20. + ParserReader pr = createParserReader(t); + String r = read(pr); + assertEquals(t, r); + pr.close(); + + pr = createParserReader(t); + pr.read(); + pr.unread(); + r = read(pr); + assertEquals(t, r); + pr.close(); + + pr = createParserReader(t); + assertEquals('0', (char)pr.peek()); + assertEquals('0', (char)pr.peek()); + r = read(pr); + assertEquals(t, r); + + pr = createParserReader(t); + pr.read(); + pr.unread(); + try { + pr.unread(); + fail("Exception expected"); + } catch (IOException e) { + // Good + } + } + + //==================================================================================================== + // testMarking + //==================================================================================================== + @Test + public void testMarking() throws Exception { + String t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789"; + String r = null; + + // Min buff size is 20. + ParserReader pr = createParserReader(t); + read(pr, 5); + pr.mark(); + read(pr, 10); + r = pr.getMarked(); + assertEquals("56789b1234", r); + r = read(pr); + assertEquals("56789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789", r); + + // Force doubling of buffer size + pr =createParserReader(t); + read(pr, 5); + pr.mark(); + read(pr, 20); + r = pr.getMarked(); + assertEquals("56789b123456789c1234", r); + r = read(pr); + assertEquals("56789d123456789e123456789f123456789g123456789h123456789i123456789j123456789", r); + } + + //==================================================================================================== + // testReadStrings + //==================================================================================================== + @Test + public void testReadStrings() throws Exception { + String t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789"; + + // Min buff size is 20. + ParserReader pr = createParserReader(t); + assertEquals("a123456789", pr.read(10)); + pr.mark(); + assertEquals("b123456789c123456789", pr.read(20)); + assertEquals("d123456789e123456789f123456789", pr.read(30)); + assertEquals("123456789c123456789d123456789e123456789f12345678", pr.getMarked(1, -1)); + assertEquals("g123456789h123456789i123456789j123456789", pr.read(100)); + assertEquals("", pr.read(100)); + pr.close(); + } + + //==================================================================================================== + // testReplace + //==================================================================================================== + @Test + public void testReplace() throws Exception { + String t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789"; + + // Min buff size is 20. + ParserReader pr = createParserReader(t); + assertEquals("a123456789", pr.read(10)); + pr.mark(); + assertEquals("b123456789", pr.read(10)); + pr.replace('x'); + assertEquals("c123456789", pr.read(10)); + assertEquals("b12345678xc123456789", pr.getMarked()); + pr.close(); + + pr = createParserReader(t); + assertEquals("a123456789", pr.read(10)); + pr.mark(); + assertEquals("b123456789", pr.read(10)); + pr.replace('x', 5); + assertEquals("c123456789", pr.read(10)); + assertEquals("b1234xc123456789", pr.getMarked()); + pr.close(); + } + + //==================================================================================================== + // testDelete + //==================================================================================================== + @Test + public void testDelete() throws Exception { + String t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789"; + + // Min buff size is 20. + ParserReader pr = createParserReader(t); + assertEquals("a123456789", pr.read(10)); + pr.mark(); + assertEquals("b123456789", pr.read(10)); + pr.delete(); + assertEquals("c123456789", pr.read(10)); + assertEquals("b12345678c123456789", pr.getMarked()); + pr.close(); + + pr = createParserReader(t); + assertEquals("a123456789", pr.read(10)); + pr.mark(); + assertEquals("b123456789", pr.read(10)); + pr.delete(5); + assertEquals("c123456789", pr.read(10)); + assertEquals("b1234c123456789", pr.getMarked()); + pr.close(); + } + + //==================================================================================================== + // Utility methods + //==================================================================================================== + + private String read(ParserReader r) throws IOException { + return read(r, Integer.MAX_VALUE); + } + + private String read(ParserReader r, int length) throws IOException { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) { + int c = r.read(); + if (c == -1) + return sb.toString(); + sb.append((char)c); + } + return sb.toString(); + } + + private ParserReader createParserReader(Object in) throws Exception { + return new ParserReader(new ParserPipe(in, false, false, null, null)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoSwapTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoSwapTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoSwapTest.java new file mode 100755 index 0000000..5fa73b2 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoSwapTest.java @@ -0,0 +1,57 @@ +// *************************************************************************************************************************** +// * 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; + +import static org.junit.Assert.*; + +import org.apache.juneau.json.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.transform.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class PojoSwapTest { + + //==================================================================================================== + // Test same type + // If you define a PojoSwap<String,String> filter, then it should be invoked on all strings. + //==================================================================================================== + @Test + public void testSameType() throws Exception { + JsonSerializer s = new JsonSerializerBuilder().simple().pojoSwaps(ASwap.class).build(); + JsonParser p = new JsonParserBuilder().pojoSwaps(ASwap.class).build(); + String r; + + r = s.serialize("foobar"); + assertEquals("'xfoobarx'", r); + r = p.parse(r, String.class); + assertEquals("foobar", r); + + ObjectMap m = new ObjectMap("{foo:'bar'}"); + r = s.serialize(m); + assertEquals("{xfoox:'xbarx'}", r); + } + + public static class ASwap extends StringSwap<String> { + @Override + public String swap(BeanSession session, String o) throws SerializeException { + return "x" + o + "x"; + } + + @Override + public String unswap(BeanSession session, String f, ClassMeta<?> hint) throws ParseException { + return f.substring(1, f.length()-1); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java new file mode 100755 index 0000000..48a23d1 --- /dev/null +++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java @@ -0,0 +1,57 @@ +// *************************************************************************************************************************** +// * 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; + +import static org.junit.Assert.*; + +import org.junit.*; + +@SuppressWarnings("javadoc") +public class PropertyNamerTest { + + //==================================================================================================== + // test dashed-lower-case + //==================================================================================================== + @Test + public void testDLC() throws Exception { + PropertyNamer n = new PropertyNamerDLC(); + + assertEquals("abc", n.getPropertyName("ABC")); + assertEquals("abc", n.getPropertyName("abc")); + assertEquals("foo-bar-baz", n.getPropertyName("FooBarBaz")); + assertEquals("foo-bar-baz", n.getPropertyName("FooBarBAZ")); + assertEquals("foo-bar-baz", n.getPropertyName("fooBarBAZ")); + assertEquals("", n.getPropertyName("")); + assertNull(n.getPropertyName(null)); + assertEquals("a", n.getPropertyName("A")); + assertEquals("a", n.getPropertyName("A")); + } + + //==================================================================================================== + // test underscore-lower-case + //==================================================================================================== + @Test + public void testULC() throws Exception { + PropertyNamer n = new PropertyNamerULC(); + + assertEquals("abc", n.getPropertyName("ABC")); + assertEquals("abc", n.getPropertyName("abc")); + assertEquals("foo_bar_baz", n.getPropertyName("FooBarBaz")); + assertEquals("foo_bar_baz", n.getPropertyName("FooBarBAZ")); + assertEquals("foo_bar_baz", n.getPropertyName("fooBarBAZ")); + assertEquals("", n.getPropertyName("")); + assertNull(n.getPropertyName(null)); + assertEquals("a", n.getPropertyName("A")); + assertEquals("a", n.getPropertyName("A")); + } +} \ No newline at end of file
