http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/transforms/DateFilterTest.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/transforms/DateFilterTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/DateFilterTest.java new file mode 100755 index 0000000..41d4329 --- /dev/null +++ b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/DateFilterTest.java @@ -0,0 +1,170 @@ +/*************************************************************************************************************************** + * 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.transforms; + +import static org.junit.Assert.*; + +import java.text.*; +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.json.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +@SuppressWarnings("deprecation") +public class DateFilterTest { + + private static TimeZone tz; + + @BeforeClass + public static void beforeClass() { + tz = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("GMT-5")); + } + + @AfterClass + public static void afterClass() { + TimeZone.setDefault(tz); + } + + private Date testDate = new Date(1, 2, 3, 4, 5, 6); + private String tz1 = new SimpleDateFormat("zzz").format(testDate); + + //==================================================================================================== + // testString - DEFAULT_STRING + //==================================================================================================== + @Test + public void testString() throws Exception { + Class<?> f = DateTransform.ToString.class; + WriterSerializer s = new JsonSerializer.Simple().addTransforms(f); + ReaderParser p = new JsonParser().addTransforms(f); + doTest(s, p, "'Sun Mar 03 04:05:06 "+tz1+" 1901'"); + } + + //==================================================================================================== + // testISO8601DTZ - DEFAULT_ISO8601DTZ + //==================================================================================================== + @Test + public void testISO8601DTZ() throws Exception { + Class<?> f = DateTransform.ISO8601DTZ.class; + WriterSerializer s = new JsonSerializer.Simple().addTransforms(f); + ReaderParser p = new JsonParser().addTransforms(f); + doTest(s, p, "'1901-03-03T09:05:06Z'"); + } + + //==================================================================================================== + // testRFC2822DT - DEFAULT_RFC2822DT + //==================================================================================================== + @Test + public void testRFC2822DT() throws Exception { + Class<?> f = DateTransform.RFC2822DT.class; + WriterSerializer s = new JsonSerializer.Simple().addTransforms(f); + ReaderParser p = new JsonParser().addTransforms(f); + doTest(s, p, "'Sun, 03 Mar 1901 04:05:06 "+tz1+"'"); + } + + //==================================================================================================== + // testLong - DEFAULT_LONG + //==================================================================================================== + @Test + public void testLong() throws Exception { + Class<?> f = DateLongTransform.class; + WriterSerializer s = new JsonSerializer.Simple().addTransforms(f); + ReaderParser p = new JsonParser().addTransforms(f); + doTest(s, p, "-2172149694000"); + } + + //==================================================================================================== + // testMap - DEFAULT_MAP + //==================================================================================================== + @Test + public void testMap() throws Exception { + Class<?> f = DateMapTransform.class; + WriterSerializer s = new JsonSerializer.Simple().addTransforms(f); + ReaderParser p = new JsonParser().addTransforms(f); + doTest(s, p, "{time:-2172149694000}"); + } + + public void doTest(WriterSerializer s, ReaderParser p, String expected) throws Exception { + Date d; + String actual; + + d = testDate; + actual = s.serialize(d); + assertEquals(expected, actual); + d = p.parse(actual, Date.class); + assertEquals(1, d.getYear()); + assertEquals(2, d.getMonth()); + assertEquals(3, d.getDate()); + assertEquals(4, d.getHours()); + assertEquals(5, d.getMinutes()); + assertEquals(6, d.getSeconds()); + + d = new java.sql.Date(testDate.getTime()); + actual = s.serialize(d); + assertEquals(expected, actual); + d = p.parse(actual, java.sql.Date.class); + assertEquals(1, d.getYear()); + assertEquals(2, d.getMonth()); + assertEquals(3, d.getDate()); + + d = new java.sql.Time(testDate.getTime()); + actual = s.serialize(d); + assertEquals(expected, actual); + d = p.parse(actual, java.sql.Time.class); + assertEquals(4, d.getHours()); + assertEquals(5, d.getMinutes()); + assertEquals(6, d.getSeconds()); + + d = new java.sql.Timestamp(testDate.getTime()); + actual = s.serialize(d); + assertEquals(expected, actual); + d = p.parse(actual, java.sql.Timestamp.class); + assertEquals(1, d.getYear()); + assertEquals(2, d.getMonth()); + assertEquals(3, d.getDate()); + assertEquals(4, d.getHours()); + assertEquals(5, d.getMinutes()); + assertEquals(6, d.getSeconds()); + } + + //==================================================================================================== + //==================================================================================================== + @Test + public void testBeanWithDate() throws Exception { + A testBeanA = new A().init(); + + final String jsonData = new JsonSerializer().addTransforms( + DateTransform.ISO8601DT.class).serialize(testBeanA); + final ObjectMap data = new JsonParser().addTransforms( + DateTransform.ISO8601DT.class).parse(jsonData, ObjectMap.class); + + final DateTransform.ISO8601DT dateTransform = new DateTransform.ISO8601DT(); + // this works + final String sValue = data.getString("birthday"); //$NON-NLS-1$ + dateTransform.normalize(sValue, data.getBeanContext().getClassMeta(Date.class)); + // this does not work + data.get(dateTransform, "birthday"); //$NON-NLS-1$ + } + + public static class A { + public Date birthday; + + public A init() { + birthday = new Date(); + return this; + } + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/transforms/EnumerationTransformTest.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/transforms/EnumerationTransformTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/EnumerationTransformTest.java new file mode 100755 index 0000000..064249a --- /dev/null +++ b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/EnumerationTransformTest.java @@ -0,0 +1,35 @@ +/*************************************************************************************************************************** + * 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.transforms; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.json.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +public class EnumerationTransformTest { + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + WriterSerializer s = new JsonSerializer.Simple().addTransforms(EnumerationTransform.class); + Vector<String> v = new Vector<String>(Arrays.asList(new String[]{"foo","bar","baz"})); + Enumeration<String> e = v.elements(); + assertEquals("['foo','bar','baz']", s.serialize(e)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/transforms/IteratorTransformTest.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/transforms/IteratorTransformTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/IteratorTransformTest.java new file mode 100755 index 0000000..be1b76a --- /dev/null +++ b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/IteratorTransformTest.java @@ -0,0 +1,37 @@ +/*************************************************************************************************************************** + * 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.transforms; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.json.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +public class IteratorTransformTest { + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + WriterSerializer s = new JsonSerializer.Simple().addTransforms(IteratorTransform.class); + + // Iterators + List<String> l = new ArrayList<String>(Arrays.asList(new String[]{"foo","bar","baz"})); + Iterator<String> i = l.iterator(); + assertEquals("['foo','bar','baz']", s.serialize(i)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/transforms/ReaderFilterTest.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/transforms/ReaderFilterTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/ReaderFilterTest.java new file mode 100755 index 0000000..6157895 --- /dev/null +++ b/org.apache.juneau/src/test/java/org/apache/juneau/transforms/ReaderFilterTest.java @@ -0,0 +1,47 @@ +/*************************************************************************************************************************** + * 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.transforms; + +import static org.junit.Assert.*; + +import java.io.*; +import java.util.*; + +import org.apache.juneau.json.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +public class ReaderFilterTest { + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + WriterSerializer s = new JsonSerializer.Simple().addTransforms(ReaderTransform.Json.class); + + Reader r; + Map<String,Object> m; + + r = new StringReader("{foo:'bar',baz:'quz'}"); + m = new HashMap<String,Object>(); + m.put("X", r); + assertEquals("{X:{foo:'bar',baz:'quz'}}", s.serialize(m)); + + s.addTransforms(ReaderTransform.Xml.class); + r = new StringReader("<object><foo type='string'>bar</foo><baz type='string'>quz</baz></object>"); + m = new HashMap<String,Object>(); + m.put("X", r); + assertEquals("{X:{foo:'bar',baz:'quz'}}", s.serialize(m)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_Uon.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_Uon.java b/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_Uon.java deleted file mode 100755 index 6ef0429..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_Uon.java +++ /dev/null @@ -1,168 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.urlencoding; - -import static org.apache.juneau.BeanContext.*; -import static org.apache.juneau.serializer.SerializerContext.*; -import static org.junit.Assert.*; - -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.parser.*; -import org.junit.*; - -@SuppressWarnings({"rawtypes","hiding","serial"}) -public class CT_CommonParser_Uon { - - ReaderParser p = UonParser.DEFAULT.clone().setClassLoader(getClass().getClassLoader()); - ReaderParser pe = UonParser.DEFAULT_DECODING.clone().setClassLoader(getClass().getClassLoader()); - - //==================================================================================================== - // testFromSerializer - //==================================================================================================== - @Test - public void testFromSerializer() throws Exception { - Map m = null; - String in; - - in = "$o(a=$n(1))"; - m = (Map)p.parse(in, Object.class); - assertEquals(1, m.get("a")); - - in = "$o(a=$n(1),b=foo+bar)"; - m = (Map)p.parse(in, Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo+bar", m.get("b")); - m = (Map)pe.parse(in, Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo bar", m.get("b")); - - in = "$o(a=$n(1),b=foo+bar,c=$b(false))"; - m = (Map)pe.parse(in, Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo bar", m.get("b")); - assertEquals(false, m.get("c")); - - in = "$o(a=$n(1),b=foo%20bar,c=$b(false))"; - m = (Map)pe.parse(in, Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo bar", m.get("b")); - assertEquals(false, m.get("c")); - - ObjectList jl = (ObjectList)p.parse("$a($o(attribute=value),$o(attribute='value'))", Object.class); - assertEquals("value", jl.getObjectMap(0).getString("attribute")); - assertEquals("'value'", jl.getObjectMap(1).getString("attribute")); - - A1 b = new A1(); - A2 tl = new A2(); - tl.add(new A3("name0","value0")); - tl.add(new A3("name1","value1")); - b.list = tl; - - in = new UonSerializer().setProperty(SERIALIZER_addClassAttrs, true).serialize(b); - b = (A1)p.parse(in, Object.class); - assertEquals("value1", b.list.get(1).value); - - in = UonSerializer.DEFAULT.serialize(b); - b = p.parse(in, A1.class); - assertEquals("value1", b.list.get(1).value); - } - - public static class A1 { - public A2 list; - } - - public static class A2 extends LinkedList<A3> { - } - - public static class A3 { - public String name, value; - public A3(){} - public A3(String name, String value) { - this.name = name; - this.value = value; - } - } - - //==================================================================================================== - // Correct handling of unknown properties. - //==================================================================================================== - @Test - public void testCorrectHandlingOfUnknownProperties() throws Exception { - ReaderParser p = new UonParser().setProperty(BEAN_ignoreUnknownBeanProperties, true); - B t; - - String in = "(a=1,unknown=3,b=2)"; - t = p.parse(in, B.class); - assertEquals(t.a, 1); - assertEquals(t.b, 2); - - try { - p = new UonParser(); - p.parse(in, B.class); - fail("Exception expected"); - } catch (ParseException e) {} - } - - public static class B { - public int a, b; - } - - //==================================================================================================== - // Writing to Collection properties with no setters. - //==================================================================================================== - @Test - public void testCollectionPropertiesWithNoSetters() throws Exception { - - ReaderParser p = UonParser.DEFAULT; - - String json = "(ints=(1,2,3),beans=((a=1,b=2)))"; - C t = p.parse(json, C.class); - assertEquals(t.getInts().size(), 3); - assertEquals(t.getBeans().get(0).b, 2); - } - - public static class C { - private Collection<Integer> ints = new LinkedList<Integer>(); - private List<B> beans = new LinkedList<B>(); - public Collection<Integer> getInts() { - return ints; - } - public List<B> getBeans() { - return beans; - } - } - - //==================================================================================================== - // Parser listeners. - //==================================================================================================== - @Test - public void testParserListeners() throws Exception { - final List<String> events = new LinkedList<String>(); - UonParser p = new UonParser().setProperty(BEAN_ignoreUnknownBeanProperties, true); - p.addListener( - new ParserListener() { - @Override /* ParserListener */ - public <T> void onUnknownProperty(String propertyName, Class<T> beanClass, T bean, int line, int col) { - events.add(propertyName + "," + line + "," + col); - } - } - ); - - String in = "(a=1,unknownProperty=foo,b=2)"; - p.parse(in, B.class); - assertEquals(1, events.size()); - assertEquals("unknownProperty,1,5", events.get(0)); - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_UrlEncoding.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_UrlEncoding.java b/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_UrlEncoding.java deleted file mode 100755 index 4256402..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_CommonParser_UrlEncoding.java +++ /dev/null @@ -1,185 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.urlencoding; - -import static org.apache.juneau.BeanContext.*; -import static org.apache.juneau.TestUtils.*; -import static org.apache.juneau.serializer.SerializerContext.*; -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.*; - -@SuppressWarnings({"rawtypes","hiding","serial"}) -public class CT_CommonParser_UrlEncoding { - - ReaderParser p = UrlEncodingParser.DEFAULT.clone().setClassLoader(getClass().getClassLoader()); - - //==================================================================================================== - // testFromSerializer - //==================================================================================================== - @Test - public void testFromSerializer() throws Exception { - Map m = null; - String in; - - in = "a=$n(1)"; - m = (Map)p.parse(in, Object.class); - assertEquals(1, m.get("a")); - - in = "a=$n(1)&b=foo+bar"; - m = (Map)p.parse(in, Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo bar", m.get("b")); - - in = "a=$n(1)&b=foo+bar&c=$b(false)"; - m = (Map)p.parse(in, Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo bar", m.get("b")); - assertEquals(false, m.get("c")); - - in = "a=$n(1)&b=foo%20bar&c=$b(false)"; - m = (Map)p.parse(in, Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo bar", m.get("b")); - assertEquals(false, m.get("c")); - - ObjectMap jm = (ObjectMap)p.parse("x=$a($o(attribute=value),$o(attribute='value'))", Object.class); - assertEquals("value", jm.getObjectList("x").getObjectMap(0).getString("attribute")); - assertEquals("'value'", jm.getObjectList("x").getObjectMap(1).getString("attribute")); - - ObjectList jl = (ObjectList)p.parse("_value=$a($o(attribute=value),$o(attribute='value'))", Object.class); - assertEquals("value", jl.getObjectMap(0).getString("attribute")); - assertEquals("'value'", jl.getObjectMap(1).getString("attribute")); - - A1 b = new A1(); - A2 tl = new A2(); - tl.add(new A3("name0","value0")); - tl.add(new A3("name1","value1")); - b.list = tl; - - in = new UrlEncodingSerializer().setProperty(SERIALIZER_addClassAttrs, true).serialize(b); - b = (A1)p.parse(in, Object.class); - assertEquals("value1", b.list.get(1).value); - - in = UrlEncodingSerializer.DEFAULT.serialize(b); - b = p.parse(in, A1.class); - assertEquals("value1", b.list.get(1).value); - } - - public static class A1 { - public A2 list; - } - - public static class A2 extends LinkedList<A3> { - } - - public static class A3 { - public String name, value; - public A3(){} - public A3(String name, String value) { - this.name = name; - this.value = value; - } - } - - //==================================================================================================== - // Correct handling of unknown properties. - //==================================================================================================== - @Test - public void testCorrectHandlingOfUnknownProperties() throws Exception { - ReaderParser p = new UrlEncodingParser().setProperty(BEAN_ignoreUnknownBeanProperties, true); - B t; - - String in = "a=1&unknown=3&b=2"; - t = p.parse(in, B.class); - assertEquals(t.a, 1); - assertEquals(t.b, 2); - - try { - p = new UrlEncodingParser(); - p.parse(in, B.class); - fail("Exception expected"); - } catch (ParseException e) {} - } - - public static class B { - public int a, b; - } - - //==================================================================================================== - // Writing to Collection properties with no setters. - //==================================================================================================== - @Test - public void testCollectionPropertiesWithNoSetters() throws Exception { - - ReaderParser p = UrlEncodingParser.DEFAULT; - - String json = "ints=(1,2,3)&beans=((a=1,b=2))"; - C t = p.parse(json, C.class); - assertEquals(t.getInts().size(), 3); - assertEquals(t.getBeans().get(0).b, 2); - } - - public static class C { - private Collection<Integer> ints = new LinkedList<Integer>(); - private List<B> beans = new LinkedList<B>(); - public Collection<Integer> getInts() { - return ints; - } - public List<B> getBeans() { - return beans; - } - } - - //==================================================================================================== - // Parser listeners. - //==================================================================================================== - @Test - public void testParserListeners() throws Exception { - final List<String> events = new LinkedList<String>(); - UonParser p = new UrlEncodingParser().setProperty(BEAN_ignoreUnknownBeanProperties, true); - p.addListener( - new ParserListener() { - @Override /* ParserListener */ - public <T> void onUnknownProperty(String propertyName, Class<T> beanClass, T bean, int line, int col) { - events.add(propertyName + "," + line + "," + col); - } - } - ); - - String in = "a=1&unknownProperty=foo&b=2"; - p.parse(in, B.class); - assertEquals(1, events.size()); - assertEquals("unknownProperty,1,4", events.get(0)); - } - - @SuppressWarnings("unchecked") - @Test - public void testCollections() throws Exception { - WriterSerializer s = new UrlEncodingSerializer().setProperty(UonSerializerContext.UON_simpleMode, true); - ReaderParser p = new UrlEncodingParser(); - - List l = new ObjectList("foo","bar"); - assertEquals("0=foo&1=bar", s.serialize(l)); - - String in = "0=foo&1=bar"; - ClassMeta<LinkedList<String>> cm = p.getBeanContext().getCollectionClassMeta(LinkedList.class, String.class); - l = p.parse(in, cm); - assertObjectEquals("['foo','bar']",l); - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_Uon.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_Uon.java b/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_Uon.java deleted file mode 100755 index a1c0c0d..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_Uon.java +++ /dev/null @@ -1,450 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.urlencoding; - -import static org.apache.juneau.TestUtils.*; -import static org.apache.juneau.serializer.SerializerContext.*; -import static org.junit.Assert.*; - -import java.net.*; -import java.net.URI; -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.annotation.*; -import org.apache.juneau.json.*; -import org.apache.juneau.serializer.*; -import org.apache.juneau.testbeans.*; -import org.junit.*; - -@SuppressWarnings({"hiding","serial"}) -public class CT_Common_Uon { - UonParser p = UonParser.DEFAULT; - UonParser pe = UonParser.DEFAULT_DECODING; - - //==================================================================================================== - // Trim nulls from beans - //==================================================================================================== - @Test - public void testTrimNullsFromBeans() throws Exception { - UonSerializer s = new UonSerializer.Encoding(); - A t1 = A.create(), t2; - - s.setProperty(SERIALIZER_trimNullProperties, false); - String r = s.serialize(t1); - assertEquals("$o(s1=%00,s2=s2)", r); - t2 = pe.parse(r, A.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimNullProperties, true); - r = s.serialize(t1); - assertEquals("$o(s2=s2)", r); - t2 = p.parse(r, A.class); - assertEqualObjects(t1, t2); - } - - public static class A { - public String s1, s2; - - public static A create() { - A t = new A(); - t.s2 = "s2"; - return t; - } - } - - //==================================================================================================== - // Trim empty maps - //==================================================================================================== - @Test - public void testTrimEmptyMaps() throws Exception { - UonSerializer s = UonSerializer.DEFAULT_SIMPLE_ENCODING.clone(); - B t1 = B.create(), t2; - String r; - - s.setProperty(SERIALIZER_trimEmptyMaps, false); - r = s.serialize(t1); - assertEquals("(f1=(),f2=(f2a=%00,f2b=(s2=s2)))", r); - t2 = pe.parse(r, B.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimEmptyMaps, true); - r = s.serialize(t1); - assertEquals("(f2=(f2a=%00,f2b=(s2=s2)))", r); - t2 = pe.parse(r, B.class); - assertNull(t2.f1); - } - - public static class B { - public TreeMap<String,A> f1, f2; - - public static B create() { - B t = new B(); - t.f1 = new TreeMap<String,A>(); - t.f2 = new TreeMap<String,A>(){{put("f2a",null);put("f2b",A.create());}}; - return t; - } - } - - //==================================================================================================== - // Trim empty lists - //==================================================================================================== - @Test - public void testTrimEmptyLists() throws Exception { - UonSerializer s = new UonSerializer.Encoding(); - C t1 = C.create(), t2; - String r; - - s.setProperty(SERIALIZER_trimEmptyLists, false); - r = s.serialize(t1); - assertEquals("$o(f1=$a(),f2=$a(%00,$o(s2=s2)))", r); - t2 = pe.parse(r, C.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimEmptyLists, true); - r = s.serialize(t1); - assertEquals("$o(f2=$a(%00,$o(s2=s2)))", r); - t2 = pe.parse(r, C.class); - assertNull(t2.f1); - } - - public static class C { - public List<A> f1, f2; - - public static C create() { - C t = new C(); - t.f1 = new LinkedList<A>(); - t.f2 = new LinkedList<A>(){{add(null);add(A.create());}}; - return t; - } - } - - //==================================================================================================== - // Trim empty arrays - //==================================================================================================== - @Test - public void testTrimEmptyArrays() throws Exception { - UonSerializer s = new UonSerializer.Encoding(); - D t1 = D.create(), t2; - String r; - - s.setProperty(SERIALIZER_trimEmptyLists, false); - r = s.serialize(t1); - assertEquals("$o(f1=$a(),f2=$a(%00,$o(s2=s2)))", r); - t2 = pe.parse(r, D.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimEmptyLists, true); - r = s.serialize(t1); - assertEquals("$o(f2=$a(%00,$o(s2=s2)))", r); - t2 = pe.parse(r, D.class); - assertNull(t2.f1); - } - - public static class D { - public A[] f1, f2; - - public static D create() { - D t = new D(); - t.f1 = new A[]{}; - t.f2 = new A[]{null, A.create()}; - return t; - } - } - - //==================================================================================================== - // @BeanProperty.properties annotation. - //==================================================================================================== - @Test - public void testBeanPropertyProperies() throws Exception { - UonSerializer s = UonSerializer.DEFAULT; - UonSerializer ss = UonSerializer.DEFAULT_SIMPLE; - String ue = ss.serialize(new E1()); - assertEquals("(x1=(f1=1),x2=(f1=1),x3=((f1=1)),x4=((f1=1)),x5=((f1=1)),x6=((f1=1)))", ue); - ue = s.serialize(new E1()); - assertEquals("$o(x1=$o(f1=$n(1)),x2=$o(f1=$n(1)),x3=$a($o(f1=$n(1))),x4=$a($o(f1=$n(1))),x5=$a($o(f1=$n(1))),x6=$a($o(f1=$n(1))))", ue); - } - - public static class E1 { - @BeanProperty(properties={"f1"}) public E2 x1 = new E2(); - @BeanProperty(properties={"f1"}) public Map<String,Integer> x2 = new LinkedHashMap<String,Integer>() {{ - put("f1",1); put("f2",2); - }}; - @BeanProperty(properties={"f1"}) public E2[] x3 = {new E2()}; - @BeanProperty(properties={"f1"}) public List<E2> x4 = new LinkedList<E2>() {{ - add(new E2()); - }}; - @BeanProperty(properties={"f1"}) public ObjectMap[] x5 = {new ObjectMap().append("f1",1).append("f2",2)}; - @BeanProperty(properties={"f1"}) public List<ObjectMap> x6 = new LinkedList<ObjectMap>() {{ - add(new ObjectMap().append("f1",1).append("f2",2)); - }}; - } - - public static class E2 { - public int f1 = 1; - public int f2 = 2; - } - - //==================================================================================================== - // @BeanProperty.properties annotation on list of beans. - //==================================================================================================== - @Test - public void testBeanPropertyPropertiesOnListOfBeans() throws Exception { - UonSerializer s = UonSerializer.DEFAULT; - List<F> l = new LinkedList<F>(); - F t = new F(); - t.x1.add(new F()); - l.add(t); - String xml = s.serialize(l); - assertEquals("$a($o(x1=$a($o(x2=$n(2))),x2=$n(2)))", xml); - } - - public static class F { - @BeanProperty(properties={"x2"}) public List<F> x1 = new LinkedList<F>(); - public int x2 = 2; - } - - //==================================================================================================== - // Test URIAttr - Test that URLs and URIs are serialized and parsed correctly. - //==================================================================================================== - @Test - public void testURIAttr() throws Exception { - UonSerializer s = UonSerializer.DEFAULT; - UonParser p = UonParser.DEFAULT; - - G t = new G(); - t.uri = new URI("http://uri"); - t.f1 = new URI("http://f1"); - t.f2 = new URL("http://f2"); - - String r = s.serialize(t); - t = p.parse(r, G.class); - assertEquals("http://uri", t.uri.toString()); - assertEquals("http://f1", t.f1.toString()); - assertEquals("http://f2", t.f2.toString()); - } - - public static class G { - @BeanProperty(beanUri=true) public URI uri; - public URI f1; - public URL f2; - } - - //==================================================================================================== - // Test URIs with URI_CONTEXT and URI_AUTHORITY - //==================================================================================================== - @Test - public void testUris() throws Exception { - WriterSerializer s = new UonSerializer(); - TestURI t = new TestURI(); - String r; - String expected; - - s.setProperty(SERIALIZER_relativeUriBase, null); - r = s.serialize(t); - expected = "" - +"$o(" - +"f0=f0/x0," - +"f1=f1/x1," - +"f2=/f2/x2," - +"f3=http://www.ibm.com/f3/x3," - +"f4=f4/x4," - +"f5=/f5/x5," - +"f6=http://www.ibm.com/f6/x6," - +"f7=http://www.ibm.com/f7/x7," - +"f8=f8/x8," - +"f9=f9/x9," - +"fa=http://www.ibm.com/fa/xa#MY_LABEL," - +"fb=http://www.ibm.com/fb/xb?label~=MY_LABEL&foo~=bar," - +"fc=http://www.ibm.com/fc/xc?foo~=bar&label~=MY_LABEL," - +"fd=http://www.ibm.com/fd/xd?label2~=MY_LABEL&foo~=bar," - +"fe=http://www.ibm.com/fe/xe?foo~=bar&label2~=MY_LABEL" - +")"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, ""); // Same as null. - r = s.serialize(t); - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, "/cr"); - r = s.serialize(t); - expected = "" - +"$o(" - +"f0=/cr/f0/x0," - +"f1=/cr/f1/x1," - +"f2=/f2/x2," - +"f3=http://www.ibm.com/f3/x3," - +"f4=/cr/f4/x4," - +"f5=/f5/x5," - +"f6=http://www.ibm.com/f6/x6," - +"f7=http://www.ibm.com/f7/x7," - +"f8=/cr/f8/x8," - +"f9=/cr/f9/x9," - +"fa=http://www.ibm.com/fa/xa#MY_LABEL," - +"fb=http://www.ibm.com/fb/xb?label~=MY_LABEL&foo~=bar," - +"fc=http://www.ibm.com/fc/xc?foo~=bar&label~=MY_LABEL," - +"fd=http://www.ibm.com/fd/xd?label2~=MY_LABEL&foo~=bar," - +"fe=http://www.ibm.com/fe/xe?foo~=bar&label2~=MY_LABEL" - +")"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, "/cr/"); // Same as above - r = s.serialize(t); - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, "/"); - r = s.serialize(t); - expected = "" - +"$o(" - +"f0=/f0/x0," - +"f1=/f1/x1," - +"f2=/f2/x2," - +"f3=http://www.ibm.com/f3/x3," - +"f4=/f4/x4," - +"f5=/f5/x5," - +"f6=http://www.ibm.com/f6/x6," - +"f7=http://www.ibm.com/f7/x7," - +"f8=/f8/x8," - +"f9=/f9/x9," - +"fa=http://www.ibm.com/fa/xa#MY_LABEL," - +"fb=http://www.ibm.com/fb/xb?label~=MY_LABEL&foo~=bar," - +"fc=http://www.ibm.com/fc/xc?foo~=bar&label~=MY_LABEL," - +"fd=http://www.ibm.com/fd/xd?label2~=MY_LABEL&foo~=bar," - +"fe=http://www.ibm.com/fe/xe?foo~=bar&label2~=MY_LABEL" - +")"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, null); - - s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo"); - r = s.serialize(t); - expected = "" - +"$o(" - +"f0=f0/x0," - +"f1=f1/x1," - +"f2=http://foo/f2/x2," - +"f3=http://www.ibm.com/f3/x3," - +"f4=f4/x4," - +"f5=http://foo/f5/x5," - +"f6=http://www.ibm.com/f6/x6," - +"f7=http://www.ibm.com/f7/x7," - +"f8=f8/x8," - +"f9=f9/x9," - +"fa=http://www.ibm.com/fa/xa#MY_LABEL," - +"fb=http://www.ibm.com/fb/xb?label~=MY_LABEL&foo~=bar," - +"fc=http://www.ibm.com/fc/xc?foo~=bar&label~=MY_LABEL," - +"fd=http://www.ibm.com/fd/xd?label2~=MY_LABEL&foo~=bar," - +"fe=http://www.ibm.com/fe/xe?foo~=bar&label2~=MY_LABEL" - +")"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo/"); - r = s.serialize(t); - assertEquals(expected, r); - - s.setProperty(SERIALIZER_absolutePathUriBase, ""); // Same as null. - r = s.serialize(t); - expected = "" - +"$o(" - +"f0=f0/x0," - +"f1=f1/x1," - +"f2=/f2/x2," - +"f3=http://www.ibm.com/f3/x3," - +"f4=f4/x4," - +"f5=/f5/x5," - +"f6=http://www.ibm.com/f6/x6," - +"f7=http://www.ibm.com/f7/x7," - +"f8=f8/x8," - +"f9=f9/x9," - +"fa=http://www.ibm.com/fa/xa#MY_LABEL," - +"fb=http://www.ibm.com/fb/xb?label~=MY_LABEL&foo~=bar," - +"fc=http://www.ibm.com/fc/xc?foo~=bar&label~=MY_LABEL," - +"fd=http://www.ibm.com/fd/xd?label2~=MY_LABEL&foo~=bar," - +"fe=http://www.ibm.com/fe/xe?foo~=bar&label2~=MY_LABEL" - +")"; - assertEquals(expected, r); - } - - //==================================================================================================== - // Validate that you cannot update properties on locked serializer. - //==================================================================================================== - @Test - public void testLockedSerializer() throws Exception { - UonSerializer s = new UonSerializer().lock(); - try { - s.setProperty(JsonSerializerContext.JSON_simpleMode, true); - fail("Locked exception not thrown"); - } catch (LockedException e) {} - try { - s.setProperty(SerializerContext.SERIALIZER_addClassAttrs, true); - fail("Locked exception not thrown"); - } catch (LockedException e) {} - try { - s.setProperty(BeanContext.BEAN_beanMapPutReturnsOldValue, true); - fail("Locked exception not thrown"); - } catch (LockedException e) {} - } - - //==================================================================================================== - // Recursion - //==================================================================================================== - @Test - public void testRecursion() throws Exception { - WriterSerializer s = new UonSerializer(); - - R1 r1 = new R1(); - R2 r2 = new R2(); - R3 r3 = new R3(); - r1.r2 = r2; - r2.r3 = r3; - r3.r1 = r1; - - // No recursion detection - try { - s.serialize(r1); - fail("Exception expected!"); - } catch (Exception e) { - String msg = e.getLocalizedMessage(); - assertTrue(msg.contains("It's recommended you use the SerializerContext.SERIALIZER_detectRecursions setting to help locate the loop.")); - } - - // Recursion detection, no ignore - s.setProperty(SERIALIZER_detectRecursions, true); - try { - s.serialize(r1); - fail("Exception expected!"); - } catch (Exception e) { - String msg = e.getLocalizedMessage(); - assertTrue(msg.contains("[0]root:org.apache.juneau.urlencoding.CT_Common_Uon$R1")); - assertTrue(msg.contains("->[1]r2:org.apache.juneau.urlencoding.CT_Common_Uon$R2")); - assertTrue(msg.contains("->[2]r3:org.apache.juneau.urlencoding.CT_Common_Uon$R3")); - assertTrue(msg.contains("->[3]r1:org.apache.juneau.urlencoding.CT_Common_Uon$R1")); - } - - s.setProperty(SERIALIZER_ignoreRecursions, true); - assertEquals("$o(name=foo,r2=$o(name=bar,r3=$o(name=baz)))", s.serialize(r1)); - } - - public static class R1 { - public String name = "foo"; - public R2 r2; - } - public static class R2 { - public String name = "bar"; - public R3 r3; - } - public static class R3 { - public String name = "baz"; - public R1 r1; - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_UrlEncoding.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_UrlEncoding.java b/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_UrlEncoding.java deleted file mode 100755 index fe499fc..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_Common_UrlEncoding.java +++ /dev/null @@ -1,442 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.urlencoding; - -import static org.apache.juneau.TestUtils.*; -import static org.apache.juneau.serializer.SerializerContext.*; -import static org.junit.Assert.*; - -import java.net.*; -import java.net.URI; -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.annotation.*; -import org.apache.juneau.json.*; -import org.apache.juneau.serializer.*; -import org.apache.juneau.testbeans.*; -import org.junit.*; - -@SuppressWarnings({"hiding","serial"}) -public class CT_Common_UrlEncoding { - UrlEncodingParser p = UrlEncodingParser.DEFAULT; - - //==================================================================================================== - // Trim nulls from beans - //==================================================================================================== - @Test - public void testTrimNullsFromBeans() throws Exception { - UrlEncodingSerializer s = new UrlEncodingSerializer(); - A t1 = A.create(), t2; - - s.setProperty(SERIALIZER_trimNullProperties, false); - String r = s.serialize(t1); - assertEquals("s1=%00&s2=s2", r); - t2 = p.parse(r, A.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimNullProperties, true); - r = s.serialize(t1); - assertEquals("s2=s2", r); - t2 = p.parse(r, A.class); - assertEqualObjects(t1, t2); - } - - public static class A { - public String s1, s2; - - public static A create() { - A t = new A(); - t.s2 = "s2"; - return t; - } - } - - //==================================================================================================== - // Trim empty maps - //==================================================================================================== - @Test - public void testTrimEmptyMaps() throws Exception { - UrlEncodingSerializer s = UrlEncodingSerializer.DEFAULT_SIMPLE.clone(); - B t1 = B.create(), t2; - String r; - - s.setProperty(SERIALIZER_trimEmptyMaps, false); - r = s.serialize(t1); - assertEquals("f1=()&f2=(f2a=%00,f2b=(s2=s2))", r); - t2 = p.parse(r, B.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimEmptyMaps, true); - r = s.serialize(t1); - assertEquals("f2=(f2a=%00,f2b=(s2=s2))", r); - t2 = p.parse(r, B.class); - assertNull(t2.f1); - } - - public static class B { - public TreeMap<String,A> f1, f2; - - public static B create() { - B t = new B(); - t.f1 = new TreeMap<String,A>(); - t.f2 = new TreeMap<String,A>(){{put("f2a",null);put("f2b",A.create());}}; - return t; - } - } - - //==================================================================================================== - // Trim empty lists - //==================================================================================================== - @Test - public void testTrimEmptyLists() throws Exception { - UrlEncodingSerializer s = new UrlEncodingSerializer(); - C t1 = C.create(), t2; - String r; - - s.setProperty(SERIALIZER_trimEmptyLists, false); - r = s.serialize(t1); - assertEquals("f1=$a()&f2=$a(%00,$o(s2=s2))", r); - t2 = p.parse(r, C.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimEmptyLists, true); - r = s.serialize(t1); - assertEquals("f2=$a(%00,$o(s2=s2))", r); - t2 = p.parse(r, C.class); - assertNull(t2.f1); - } - - public static class C { - public List<A> f1, f2; - - public static C create() { - C t = new C(); - t.f1 = new LinkedList<A>(); - t.f2 = new LinkedList<A>(){{add(null);add(A.create());}}; - return t; - } - } - - //==================================================================================================== - // Trim empty arrays - //==================================================================================================== - @Test - public void testTrimEmptyArrays() throws Exception { - UrlEncodingSerializer s = new UrlEncodingSerializer(); - D t1 = D.create(), t2; - String r; - - s.setProperty(SERIALIZER_trimEmptyLists, false); - r = s.serialize(t1); - assertEquals("f1=$a()&f2=$a(%00,$o(s2=s2))", r); - t2 = p.parse(r, D.class); - assertEqualObjects(t1, t2); - - s.setProperty(SERIALIZER_trimEmptyLists, true); - r = s.serialize(t1); - assertEquals("f2=$a(%00,$o(s2=s2))", r); - t2 = p.parse(r, D.class); - assertNull(t2.f1); - } - - public static class D { - public A[] f1, f2; - - public static D create() { - D t = new D(); - t.f1 = new A[]{}; - t.f2 = new A[]{null, A.create()}; - return t; - } - } - - //==================================================================================================== - // @BeanProperty.properties annotation. - //==================================================================================================== - @Test - public void testBeanPropertyProperies() throws Exception { - UrlEncodingSerializer s = UrlEncodingSerializer.DEFAULT; - UrlEncodingSerializer ss = UrlEncodingSerializer.DEFAULT_SIMPLE; - String ue = ss.serialize(new E1()); - assertEquals("x1=(f1=1)&x2=(f1=1)&x3=((f1=1))&x4=((f1=1))&x5=((f1=1))&x6=((f1=1))", ue); - ue = s.serialize(new E1()); - assertEquals("x1=$o(f1=$n(1))&x2=$o(f1=$n(1))&x3=$a($o(f1=$n(1)))&x4=$a($o(f1=$n(1)))&x5=$a($o(f1=$n(1)))&x6=$a($o(f1=$n(1)))", ue); - } - - public static class E1 { - @BeanProperty(properties={"f1"}) public E2 x1 = new E2(); - @BeanProperty(properties={"f1"}) public Map<String,Integer> x2 = new LinkedHashMap<String,Integer>() {{ - put("f1",1); put("f2",2); - }}; - @BeanProperty(properties={"f1"}) public E2[] x3 = {new E2()}; - @BeanProperty(properties={"f1"}) public List<E2> x4 = new LinkedList<E2>() {{ - add(new E2()); - }}; - @BeanProperty(properties={"f1"}) public ObjectMap[] x5 = {new ObjectMap().append("f1",1).append("f2",2)}; - @BeanProperty(properties={"f1"}) public List<ObjectMap> x6 = new LinkedList<ObjectMap>() {{ - add(new ObjectMap().append("f1",1).append("f2",2)); - }}; - } - - public static class E2 { - public int f1 = 1; - public int f2 = 2; - } - - //==================================================================================================== - // @BeanProperty.properties annotation on list of beans. - //==================================================================================================== - @Test - public void testBeanPropertyPropertiesOnListOfBeans() throws Exception { - UrlEncodingSerializer s = UrlEncodingSerializer.DEFAULT; - List<F> l = new LinkedList<F>(); - F t = new F(); - t.x1.add(new F()); - l.add(t); - ObjectMap m = new ObjectMap().append("t", l); - String xml = s.serialize(m); - assertEquals("t=$a($o(x1=$a($o(x2=$n(2))),x2=$n(2)))", xml); - xml = s.serialize(l); - assertEquals("$n(0)=$o(x1=$a($o(x2=$n(2))),x2=$n(2))", xml); - } - - public static class F { - @BeanProperty(properties={"x2"}) public List<F> x1 = new LinkedList<F>(); - public int x2 = 2; - } - - //==================================================================================================== - // Test URIAttr - Test that URLs and URIs are serialized and parsed correctly. - //==================================================================================================== - @Test - public void testURIAttr() throws Exception { - UrlEncodingSerializer s = UrlEncodingSerializer.DEFAULT; - UrlEncodingParser p = UrlEncodingParser.DEFAULT; - - G t = new G(); - t.uri = new URI("http://uri"); - t.f1 = new URI("http://f1"); - t.f2 = new URL("http://f2"); - - String r = s.serialize(t); - t = p.parse(r, G.class); - assertEquals("http://uri", t.uri.toString()); - assertEquals("http://f1", t.f1.toString()); - assertEquals("http://f2", t.f2.toString()); - } - - public static class G { - @BeanProperty(beanUri=true) public URI uri; - public URI f1; - public URL f2; - } - - //==================================================================================================== - // Test URIs with URI_CONTEXT and URI_AUTHORITY - //==================================================================================================== - @Test - public void testUris() throws Exception { - WriterSerializer s = new UrlEncodingSerializer(); - TestURI t = new TestURI(); - String r; - String expected = ""; - - s.setProperty(SERIALIZER_relativeUriBase, null); - r = s.serialize(t); - expected = "" - +"f0=f0/x0" - +"&f1=f1/x1" - +"&f2=/f2/x2" - +"&f3=http://www.ibm.com/f3/x3" - +"&f4=f4/x4" - +"&f5=/f5/x5" - +"&f6=http://www.ibm.com/f6/x6" - +"&f7=http://www.ibm.com/f7/x7" - +"&f8=f8/x8" - +"&f9=f9/x9" - +"&fa=http://www.ibm.com/fa/xa%23MY_LABEL" - +"&fb=http://www.ibm.com/fb/xb?label=MY_LABEL%26foo=bar" - +"&fc=http://www.ibm.com/fc/xc?foo=bar%26label=MY_LABEL" - +"&fd=http://www.ibm.com/fd/xd?label2=MY_LABEL%26foo=bar" - +"&fe=http://www.ibm.com/fe/xe?foo=bar%26label2=MY_LABEL"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, ""); // Same as null. - r = s.serialize(t); - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, "/cr"); - r = s.serialize(t); - expected = "" - +"f0=/cr/f0/x0" - +"&f1=/cr/f1/x1" - +"&f2=/f2/x2" - +"&f3=http://www.ibm.com/f3/x3" - +"&f4=/cr/f4/x4" - +"&f5=/f5/x5" - +"&f6=http://www.ibm.com/f6/x6" - +"&f7=http://www.ibm.com/f7/x7" - +"&f8=/cr/f8/x8" - +"&f9=/cr/f9/x9" - +"&fa=http://www.ibm.com/fa/xa%23MY_LABEL" - +"&fb=http://www.ibm.com/fb/xb?label=MY_LABEL%26foo=bar" - +"&fc=http://www.ibm.com/fc/xc?foo=bar%26label=MY_LABEL" - +"&fd=http://www.ibm.com/fd/xd?label2=MY_LABEL%26foo=bar" - +"&fe=http://www.ibm.com/fe/xe?foo=bar%26label2=MY_LABEL"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, "/cr/"); // Same as above - r = s.serialize(t); - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, "/"); - r = s.serialize(t); - expected = "" - +"f0=/f0/x0" - +"&f1=/f1/x1" - +"&f2=/f2/x2" - +"&f3=http://www.ibm.com/f3/x3" - +"&f4=/f4/x4" - +"&f5=/f5/x5" - +"&f6=http://www.ibm.com/f6/x6" - +"&f7=http://www.ibm.com/f7/x7" - +"&f8=/f8/x8" - +"&f9=/f9/x9" - +"&fa=http://www.ibm.com/fa/xa%23MY_LABEL" - +"&fb=http://www.ibm.com/fb/xb?label=MY_LABEL%26foo=bar" - +"&fc=http://www.ibm.com/fc/xc?foo=bar%26label=MY_LABEL" - +"&fd=http://www.ibm.com/fd/xd?label2=MY_LABEL%26foo=bar" - +"&fe=http://www.ibm.com/fe/xe?foo=bar%26label2=MY_LABEL"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_relativeUriBase, null); - - s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo"); - r = s.serialize(t); - expected = "" - +"f0=f0/x0" - +"&f1=f1/x1" - +"&f2=http://foo/f2/x2" - +"&f3=http://www.ibm.com/f3/x3" - +"&f4=f4/x4" - +"&f5=http://foo/f5/x5" - +"&f6=http://www.ibm.com/f6/x6" - +"&f7=http://www.ibm.com/f7/x7" - +"&f8=f8/x8" - +"&f9=f9/x9" - +"&fa=http://www.ibm.com/fa/xa%23MY_LABEL" - +"&fb=http://www.ibm.com/fb/xb?label=MY_LABEL%26foo=bar" - +"&fc=http://www.ibm.com/fc/xc?foo=bar%26label=MY_LABEL" - +"&fd=http://www.ibm.com/fd/xd?label2=MY_LABEL%26foo=bar" - +"&fe=http://www.ibm.com/fe/xe?foo=bar%26label2=MY_LABEL"; - assertEquals(expected, r); - - s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo/"); - r = s.serialize(t); - assertEquals(expected, r); - - s.setProperty(SERIALIZER_absolutePathUriBase, ""); // Same as null. - r = s.serialize(t); - expected = "" - +"f0=f0/x0" - +"&f1=f1/x1" - +"&f2=/f2/x2" - +"&f3=http://www.ibm.com/f3/x3" - +"&f4=f4/x4" - +"&f5=/f5/x5" - +"&f6=http://www.ibm.com/f6/x6" - +"&f7=http://www.ibm.com/f7/x7" - +"&f8=f8/x8" - +"&f9=f9/x9" - +"&fa=http://www.ibm.com/fa/xa%23MY_LABEL" - +"&fb=http://www.ibm.com/fb/xb?label=MY_LABEL%26foo=bar" - +"&fc=http://www.ibm.com/fc/xc?foo=bar%26label=MY_LABEL" - +"&fd=http://www.ibm.com/fd/xd?label2=MY_LABEL%26foo=bar" - +"&fe=http://www.ibm.com/fe/xe?foo=bar%26label2=MY_LABEL"; - assertEquals(expected, r); - } - - //==================================================================================================== - // Validate that you cannot update properties on locked serializer. - //==================================================================================================== - @Test - public void testLockedSerializer() throws Exception { - UrlEncodingSerializer s = new UrlEncodingSerializer().lock(); - try { - s.setProperty(JsonSerializerContext.JSON_simpleMode, true); - fail("Locked exception not thrown"); - } catch (LockedException e) {} - try { - s.setProperty(SerializerContext.SERIALIZER_addClassAttrs, true); - fail("Locked exception not thrown"); - } catch (LockedException e) {} - try { - s.setProperty(BeanContext.BEAN_beanMapPutReturnsOldValue, true); - fail("Locked exception not thrown"); - } catch (LockedException e) {} - } - - //==================================================================================================== - // Recursion - //==================================================================================================== - @Test - public void testRecursion() throws Exception { - WriterSerializer s = new UrlEncodingSerializer(); - - R1 r1 = new R1(); - R2 r2 = new R2(); - R3 r3 = new R3(); - r1.r2 = r2; - r2.r3 = r3; - r3.r1 = r1; - - // No recursion detection - try { - s.serialize(r1); - fail("Exception expected!"); - } catch (Exception e) { - String msg = e.getLocalizedMessage(); - assertTrue(msg.contains("It's recommended you use the SerializerContext.SERIALIZER_detectRecursions setting to help locate the loop.")); - } - - // Recursion detection, no ignore - s.setProperty(SERIALIZER_detectRecursions, true); - try { - s.serialize(r1); - fail("Exception expected!"); - } catch (Exception e) { - String msg = e.getLocalizedMessage(); - assertTrue(msg.contains("[0]root:org.apache.juneau.urlencoding.CT_Common_UrlEncoding$R1")); - assertTrue(msg.contains("->[1]r2:org.apache.juneau.urlencoding.CT_Common_UrlEncoding$R2")); - assertTrue(msg.contains("->[2]r3:org.apache.juneau.urlencoding.CT_Common_UrlEncoding$R3")); - assertTrue(msg.contains("->[3]r1:org.apache.juneau.urlencoding.CT_Common_UrlEncoding$R1")); - } - - s.setProperty(SERIALIZER_ignoreRecursions, true); - assertEquals("name=foo&r2=$o(name=bar,r3=$o(name=baz))", s.serialize(r1)); - } - - public static class R1 { - public String name = "foo"; - public R2 r2; - } - public static class R2 { - public String name = "bar"; - public R3 r3; - } - public static class R3 { - public String name = "baz"; - public R1 r1; - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParser.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParser.java b/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParser.java deleted file mode 100755 index 49d37de..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParser.java +++ /dev/null @@ -1,542 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.urlencoding; - -import static org.junit.Assert.*; - -import java.util.*; - -import org.apache.juneau.parser.*; -import org.junit.*; - -@SuppressWarnings({"rawtypes","unchecked","hiding"}) -public class CT_UonParser { - - static UonParser p = UonParser.DEFAULT; - static UonParser pe = UonParser.DEFAULT_DECODING; - - //==================================================================================================== - // Basic test - //==================================================================================================== - @Test - public void testBasic() throws Exception { - - String t; - Map m; - - // Simple string - // Top level - t = "a"; - assertEquals("a", p.parse(t, String.class)); - assertEquals("a", p.parse(t, Object.class)); - assertEquals("a", pe.parse(t, String.class)); - t = "(a)"; - assertEquals("a", p.parse(t, String.class)); - assertEquals("a", p.parse(t, Object.class)); - t = "$s(a)"; - assertEquals("a", p.parse(t, String.class)); - - // 2nd level - t = "$o(a=a)"; - assertEquals("a", p.parse(t, Map.class).get("a")); - assertEquals("a", pe.parse(t, Map.class).get("a")); - - t = "(a=a)"; - assertEquals("a", p.parse(t, Map.class).get("a")); - assertEquals("a", pe.parse(t, Map.class).get("a")); - - // Simple map - // Top level - t = "$o(a=b,c=$n(123),d=$b(false),e=$b(true),f=%00)"; - m = p.parse(t, Map.class); - assertEquals("b", m.get("a")); - assertTrue(m.get("c") instanceof Number); - assertEquals(123, m.get("c")); - assertTrue(m.get("d") instanceof Boolean); - assertEquals(Boolean.FALSE, m.get("d")); - assertTrue(m.get("e") instanceof Boolean); - assertEquals(Boolean.TRUE, m.get("e")); - m = pe.parse(t, Map.class); - assertNull(m.get("f")); - - t = "(a=true)"; - m = p.parseMap(t, HashMap.class, String.class, Boolean.class); - assertTrue(m.get("a") instanceof Boolean); - assertEquals("true", m.get("a").toString()); - - // null - // Top level - t = "%00"; - assertEquals("%00", p.parse(t, Object.class)); - assertNull(pe.parse(t, Object.class)); - - // 2nd level - t = "$o(%00=%00)"; - m = p.parse(t, Map.class); - assertEquals("%00", m.get("%00")); - m = pe.parse(t, Map.class); - assertTrue(m.containsKey(null)); - assertNull(m.get(null)); - - t = "(%00=%00)"; - m = p.parse(t, Map.class); - assertEquals("%00", m.get("%00")); - m = pe.parse(t, Map.class); - assertTrue(m.containsKey(null)); - assertNull(m.get(null)); - - t = "(\u0000=\u0000)"; - m = p.parse(t, Map.class); - assertTrue(m.containsKey(null)); - assertNull(m.get(null)); - m = pe.parse(t, Map.class); - assertTrue(m.containsKey(null)); - assertNull(m.get(null)); - - // 3rd level - t = "$o(%00=$o(%00=%00))"; - m = p.parse(t, Map.class); - assertEquals("%00", ((Map)m.get("%00")).get("%00")); - m = pe.parse(t, Map.class); - assertTrue(((Map)m.get(null)).containsKey(null)); - assertNull(((Map)m.get(null)).get(null)); - - // Empty array - // Top level - t = "$a()"; - List l = (List)p.parse(t, Object.class); - assertTrue(l.isEmpty()); - t = "()"; - l = p.parse(t, List.class); - assertTrue(l.isEmpty()); - - // 2nd level in map - t = "$o(x=$a())"; - m = p.parseMap(t, HashMap.class, String.class, List.class); - assertTrue(m.containsKey("x")); - assertTrue(((List)m.get("x")).isEmpty()); - m = (Map)p.parse(t, Object.class); - assertTrue(m.containsKey("x")); - assertTrue(((List)m.get("x")).isEmpty()); - t = "(x=())"; - m = p.parseMap(t, HashMap.class, String.class, List.class); - assertTrue(m.containsKey("x")); - assertTrue(((List)m.get("x")).isEmpty()); - - // Empty 2 dimensional array - t = "$a($a())"; - l = (List)p.parse(t, Object.class); - assertTrue(l.size() == 1); - l = (List)l.get(0); - assertTrue(l.isEmpty()); - t = "(())"; - l = p.parseCollection(t, LinkedList.class, List.class); - assertTrue(l.size() == 1); - l = (List)l.get(0); - assertTrue(l.isEmpty()); - - // Array containing empty string - // Top level - t = "$a(())"; - l = (List)p.parse(t, Object.class); - assertTrue(l.size() == 1); - assertEquals("", l.get(0)); - t = "(())"; - l = p.parseCollection(t, List.class, String.class); - assertTrue(l.size() == 1); - assertEquals("", l.get(0)); - - // 2nd level - t = "$o(()=$a(()))"; - m = (Map)p.parse(t, Object.class); - assertEquals("", ((List)m.get("")).get(0)); - t = "(=(()))"; - m = p.parseMap(t, HashMap.class, String.class, List.class); - assertEquals("", ((List)m.get("")).get(0)); - - // Array containing 3 empty strings - t = "$a(,,)"; - l = (List)p.parse(t, Object.class); - assertTrue(l.size() == 3); - assertEquals("", l.get(0)); - assertEquals("", l.get(1)); - assertEquals("", l.get(2)); - t = "(,,)"; - l = p.parseCollection(t, List.class, Object.class); - assertTrue(l.size() == 3); - assertEquals("", l.get(0)); - assertEquals("", l.get(1)); - assertEquals("", l.get(2)); - - // String containing \u0000 - // Top level - t = "$s(\u0000)"; - assertEquals("\u0000", p.parse(t, Object.class)); - t = "(\u0000)"; - assertEquals("\u0000", p.parse(t, String.class)); - assertEquals("\u0000", p.parse(t, Object.class)); - - // 2nd level - t = "$o((\u0000)=(\u0000))"; - m = (Map)p.parse(t, Object.class); - assertTrue(m.size() == 1); - assertEquals("\u0000", m.get("\u0000")); - t = "((\u0000)=(\u0000))"; - m = p.parseMap(t, HashMap.class, String.class, String.class); - assertTrue(m.size() == 1); - assertEquals("\u0000", m.get("\u0000")); - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertTrue(m.size() == 1); - assertEquals("\u0000", m.get("\u0000")); - - // Boolean - // Top level - t = "$b(false)"; - Boolean b = (Boolean)p.parse(t, Object.class); - assertEquals(Boolean.FALSE, b); - b = p.parse(t, Boolean.class); - assertEquals(Boolean.FALSE, b); - t = "false"; - b = p.parse(t, Boolean.class); - assertEquals(Boolean.FALSE, b); - - // 2nd level - t = "$o(x=$b(false))"; - m = (Map)p.parse(t, Object.class); - assertEquals(Boolean.FALSE, m.get("x")); - t = "(x=$b(false))"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals(Boolean.FALSE, m.get("x")); - t = "(x=false)"; - m = p.parseMap(t, HashMap.class, String.class, Boolean.class); - assertEquals(Boolean.FALSE, m.get("x")); - - // Number - // Top level - t = "$n(123)"; - Integer i = (Integer)p.parse(t, Object.class); - assertEquals(123, i.intValue()); - i = p.parse(t, Integer.class); - assertEquals(123, i.intValue()); - Double d = p.parse(t, Double.class); - assertEquals(123, d.intValue()); - Float f = p.parse(t, Float.class); - assertEquals(123, f.intValue()); - t = "123"; - i = p.parse(t, Integer.class); - assertEquals(123, i.intValue()); - - // 2nd level - t = "$o(x=$n(123))"; - m = (Map)p.parse(t, Object.class); - assertEquals(123, ((Integer)m.get("x")).intValue()); - t = "(x=123)"; - m = p.parseMap(t, HashMap.class, String.class, Number.class); - assertEquals(123, ((Integer)m.get("x")).intValue()); - m = p.parseMap(t, HashMap.class, String.class, Double.class); - assertEquals(123, ((Double)m.get("x")).intValue()); - - // Unencoded chars - // Top level - t = "x;/?:@-_.!*'"; - assertEquals("x;/?:@-_.!*'", p.parse(t, Object.class)); - assertEquals("x;/?:@-_.!*'", pe.parse(t, Object.class)); - - // 2nd level - t = "$o(x;/?:@-_.!*'=x;/?:@-_.!*')"; - m = (Map)p.parse(t, Object.class); - assertEquals("x;/?:@-_.!*'", m.get("x;/?:@-_.!*'")); - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("x;/?:@-_.!*'", m.get("x;/?:@-_.!*'")); - m = p.parseMap(t, HashMap.class, String.class, String.class); - assertEquals("x;/?:@-_.!*'", m.get("x;/?:@-_.!*'")); - - // Encoded chars - // Top level - t = "x{}|\\^[]`<>#%\"&+"; - assertEquals("x{}|\\^[]`<>#%\"&+", p.parse(t, Object.class)); - assertEquals("x{}|\\^[]`<>#%\"&+", p.parse(t, String.class)); - try { - assertEquals("x{}|\\^[]`<>#%\"&+", pe.parse(t, Object.class)); - fail("Expected parse exception from invalid hex sequence."); - } catch (ParseException e) { - // Good. - } - t = "x%7B%7D%7C%5C%5E%5B%5D%60%3C%3E%23%25%22%26%2B"; - assertEquals("x{}|\\^[]`<>#%\"&+", pe.parse(t, Object.class)); - assertEquals("x{}|\\^[]`<>#%\"&+", pe.parse(t, String.class)); - - // 2nd level - t = "$o(x{}|\\^[]`<>#%\"&+=x{}|\\^[]`<>#%\"&+)"; - m = (Map)p.parse(t, Object.class); - assertEquals("x{}|\\^[]`<>#%\"&+", m.get("x{}|\\^[]`<>#%\"&+")); - try { - m = (Map)pe.parse(t, Object.class); - fail("Expected parse exception from invalid hex sequence."); - } catch (ParseException e) { - // Good. - } - t = "$o(x%7B%7D%7C%5C%5E%5B%5D%60%3C%3E%23%25%22%26%2B=x%7B%7D%7C%5C%5E%5B%5D%60%3C%3E%23%25%22%26%2B)"; - m = (Map)pe.parse(t, Object.class); - assertEquals("x{}|\\^[]`<>#%\"&+", m.get("x{}|\\^[]`<>#%\"&+")); - - // Special chars - // Top level - t = "x~$~,~(~)"; - assertEquals("x$,()", p.parse(t, Object.class)); - t = "(x~$~,~(~))"; - assertEquals("x$,()", p.parse(t, Object.class)); - t = "$s(x~$~,~(~))"; - assertEquals("x$,()", p.parse(t, Object.class)); - - // 2nd level - // Note behavior on serializeParams() is different since 2nd-level is top level. - t = "$o(x~$~,~(~)=x~$~,~(~))"; - m = (Map)p.parse(t, Object.class); - assertEquals("x$,()", m.get("x$,()")); - t = "$o((x~$~,~(~))=(x~$~,~(~)))"; - m = (Map)p.parse(t, Object.class); - assertEquals("x$,()", m.get("x$,()")); - t = "$o($s(x~$~,~(~))=$s(x~$~,~(~)))"; - m = (Map)p.parse(t, Object.class); - assertEquals("x$,()", m.get("x$,()")); - - // Equals sign - // Gets encoded at top level, and encoded+escaped at 2nd level. - // Top level - t = "x="; - assertEquals("x=", p.parse(t, Object.class)); - t = "x%3D"; - assertEquals("x=", pe.parse(t, Object.class)); - - // 2nd level - t = "$o(x~==x~=)"; - m = (Map)p.parse(t, Object.class); - assertEquals("x=", m.get("x=")); - t = "$o((x~=)=(x~=))"; - m = (Map)p.parse(t, Object.class); - assertEquals("x=", m.get("x=")); - t = "$o($s(x~=)=$s(x~=))"; - m = (Map)p.parse(t, Object.class); - assertEquals("x=", m.get("x=")); - t = "(x~==x~=)"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("x=", m.get("x=")); - t = "((x~=)=(x~=))"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("x=", m.get("x=")); - t = "($s(x~=)=$s(x~=))"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("x=", m.get("x=")); - t = "$o(x~%3D=x~%3D)"; - m = (Map)pe.parse(t, Object.class); - assertEquals("x=", m.get("x=")); - t = "$o((x~%3D)=(x~%3D))"; - m = (Map)pe.parse(t, Object.class); - assertEquals("x=", m.get("x=")); - t = "$o($s(x~%3D)=$s(x~%3D))"; - m = (Map)pe.parse(t, Object.class); - assertEquals("x=", m.get("x=")); - t = "(x~%3D=x~%3D)"; - m = pe.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("x=", m.get("x=")); - t = "((x~%3D)=(x~%3D))"; - m = pe.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("x=", m.get("x=")); - t = "($s(x~%3D)=$s(x~%3D))"; - m = pe.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("x=", m.get("x=")); - - // String starting with parenthesis - // Top level - t = "~(~)"; - assertEquals("()", p.parse(t, Object.class)); - assertEquals("()", p.parse(t, String.class)); - - t = "(~(~))"; - assertEquals("()", p.parse(t, Object.class)); - assertEquals("()", p.parse(t, String.class)); - t = "$s(~(~))"; - assertEquals("()", p.parse(t, Object.class)); - assertEquals("()", p.parse(t, String.class)); - - // 2nd level - t = "$o((~(~))=(~(~)))"; - m = (Map)p.parse(t, Object.class); - assertEquals("()", m.get("()")); - t = "((~(~))=(~(~)))"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("()", m.get("()")); - t = "($s(~(~))=$s(~(~)))"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("()", m.get("()")); - - // String starting with $ - // Top level - t = "($a)"; - assertEquals("$a", p.parse(t, Object.class)); - t = "($a)"; - assertEquals("$a", p.parse(t, Object.class)); - - // 2nd level - t = "$o(($a)=($a))"; - m = (Map)p.parse(t, Object.class); - assertEquals("$a", m.get("$a")); - t = "(($a)=($a))"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("$a", m.get("$a")); - - // Blank string - // Top level - t = ""; - assertEquals("", p.parse(t, Object.class)); - assertEquals("", pe.parse(t, Object.class)); - - // 2nd level - t = "$o(=)"; - m = (Map)p.parse(t, Object.class); - assertEquals("", m.get("")); - t = "(=)"; - m = p.parseMap(t, HashMap.class, String.class, Object.class); - assertEquals("", m.get("")); - - // 3rd level - t = "$o(=$o(=))"; - m = (Map)p.parse(t, Object.class); - assertEquals("", ((Map)m.get("")).get("")); - t = "(=(=))"; - m = p.parseMap(t, HashMap.class, String.class, HashMap.class); - assertEquals("", ((Map)m.get("")).get("")); - - // Newline character - // Top level - t = "(%0A)"; - assertEquals("\n", pe.parse(t, Object.class)); - assertEquals("%0A", p.parse(t, Object.class)); - - // 2nd level - t = "$o((%0A)=(%0A))"; - m = (Map)pe.parse(t, Object.class); - assertEquals("\n", m.get("\n")); - m = (Map)p.parse(t, Object.class); - assertEquals("%0A", m.get("%0A")); - - // 3rd level - t = "$o((%0A)=$o((%0A)=(%0A)))"; - m = (Map)pe.parse(t, Object.class); - assertEquals("\n", ((Map)m.get("\n")).get("\n")); - } - - //==================================================================================================== - // Unicode character test - //==================================================================================================== - @Test - public void testUnicodeChars() throws Exception { - String t; - Map m; - - // 2-byte UTF-8 character - // Top level - t = "¢"; - assertEquals("¢", p.parse(t, Object.class)); - assertEquals("¢", p.parse(t, String.class)); - t = "%C2%A2"; - assertEquals("¢", pe.parse(t, Object.class)); - assertEquals("¢", pe.parse(t, String.class)); - - // 2nd level - t = "$o(¢=¢)"; - m = (Map)p.parse(t, Object.class); - assertEquals("¢", m.get("¢")); - t = "$o(%C2%A2=%C2%A2)"; - m = (Map)pe.parse(t, Object.class); - assertEquals("¢", m.get("¢")); - - // 3rd level - t = "$o(¢=$o(¢=¢))"; - m = (Map)p.parse(t, Object.class); - assertEquals("¢", ((Map)m.get("¢")).get("¢")); - t = "$o(%C2%A2=$o(%C2%A2=%C2%A2))"; - m = (Map)pe.parse(t, Object.class); - assertEquals("¢", ((Map)m.get("¢")).get("¢")); - - // 3-byte UTF-8 character - // Top level - t = "â¬"; - assertEquals("â¬", p.parse(t, Object.class)); - assertEquals("â¬", p.parse(t, String.class)); - t = "%E2%82%AC"; - assertEquals("â¬", pe.parse(t, Object.class)); - assertEquals("â¬", pe.parse(t, String.class)); - - // 2nd level - t = "$o(â¬=â¬)"; - m = (Map)p.parse(t, Object.class); - assertEquals("â¬", m.get("â¬")); - t = "$o(%E2%82%AC=%E2%82%AC)"; - m = (Map)pe.parse(t, Object.class); - assertEquals("â¬", m.get("â¬")); - - // 3rd level - t = "$o(â¬=$o(â¬=â¬))"; - m = (Map)p.parse(t, Object.class); - assertEquals("â¬", ((Map)m.get("â¬")).get("â¬")); - t = "$o(%E2%82%AC=$o(%E2%82%AC=%E2%82%AC))"; - m = (Map)pe.parse(t, Object.class); - assertEquals("â¬", ((Map)m.get("â¬")).get("â¬")); - - // 4-byte UTF-8 character - // Top level - t = "ð¤¢"; - assertEquals("ð¤¢", p.parse(t, Object.class)); - assertEquals("ð¤¢", p.parse(t, String.class)); - t = "%F0%A4%AD%A2"; - assertEquals("ð¤¢", pe.parse(t, Object.class)); - assertEquals("ð¤¢", pe.parse(t, String.class)); - - // 2nd level - t = "$o(ð¤¢=ð¤¢)"; - m = (Map)p.parse(t, Object.class); - assertEquals("ð¤¢", m.get("ð¤¢")); - t = "$o(%F0%A4%AD%A2=%F0%A4%AD%A2)"; - m = (Map)pe.parse(t, Object.class); - assertEquals("ð¤¢", m.get("ð¤¢")); - - // 3rd level - t = "$o(ð¤¢=$o(ð¤¢=ð¤¢))"; - m = (Map)p.parse(t, Object.class); - assertEquals("ð¤¢", ((Map)m.get("ð¤¢")).get("ð¤¢")); - t = "$o(%F0%A4%AD%A2=$o(%F0%A4%AD%A2=%F0%A4%AD%A2))"; - m = (Map)pe.parse(t, Object.class); - assertEquals("ð¤¢", ((Map)m.get("ð¤¢")).get("ð¤¢")); - } - - //==================================================================================================== - // Test simple bean - //==================================================================================================== - @Test - public void testSimpleBean() throws Exception { - UonParser p = UonParser.DEFAULT; - A t; - - String s = "(f1=foo,f2=123)"; - t = p.parse(s, A.class); - assertEquals("foo", t.f1); - assertEquals(123, t.f2); - } - - public static class A { - public String f1; - public int f2; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParserReader.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParserReader.java b/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParserReader.java deleted file mode 100755 index 8ae5cc7..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/urlencoding/CT_UonParserReader.java +++ /dev/null @@ -1,217 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.urlencoding; - -import static org.junit.Assert.*; - -import java.io.*; - -import org.apache.juneau.*; -import org.junit.*; - -public class CT_UonParserReader { - - //==================================================================================================== - // Basic tests - //==================================================================================================== - @Test - public void testBasic() throws Exception { - - UonReader r; - String s, in; - r = r("f", true); - assertEquals('f', r.read()); - assertEquals(-1, r.read()); - - r = r("%66", true); - assertEquals('f', r.read()); - assertEquals(-1, r.read()); - - r = r("%7D", true); - assertEquals('}', r.read()); - assertEquals(-1, r.read()); - - r = r("%7D%7D", true); - assertEquals('}', r.read()); - assertEquals('}', r.read()); - assertEquals(-1, r.read()); - - r = r("%00%00", true); - r.mark(); - assertEquals(0, r.read()); - assertEquals(0, r.read()); - assertEquals("\u0000\u0000", r.getMarked()); - assertEquals(-1, r.read()); - - in = escape("\u0080"); - r = r(in, true); - assertEquals('\u0080', r.read()); - assertEquals(-1, r.read()); - - in = escape("\u0800"); - r = r(in, true); - assertEquals('\u0800', r.read()); - assertEquals(-1, r.read()); - - in = escape("\uffff"); - r = r(in, true); - assertEquals('\uffff', r.read()); - assertEquals(-1, r.read()); - - // 2-byte codepoint - s = "¢"; - r = r(escape(s), true); - assertEquals(s.codePointAt(0), r.read()); - assertEquals(-1, r.read()); - - // 3-byte codepoint - s = "â¬"; - r = r(escape(s), true); - assertEquals(s.codePointAt(0), r.readCodePoint()); - assertEquals(-1, r.read()); - - // 4-byte codepoint - s = "ð¤¢"; - r = r(escape(s), true); - assertEquals(s.codePointAt(0), r.readCodePoint()); - assertEquals(-1, r.read()); - - s = "ð¤¢ð¤¢"; - r = r(escape(s), true); - assertEquals(s.codePointAt(0), r.readCodePoint()); - assertEquals(s.codePointAt(2), r.readCodePoint()); - assertEquals(-1, r.read()); - - // Multiple codepoints - s = "¢â¬ð¤¢Â¢â¬ð¤¢"; - in = escape(s); - r = r(in, true); - assertEquals(s.codePointAt(0), r.readCodePoint()); - assertEquals(s.codePointAt(1), r.readCodePoint()); - assertEquals(s.codePointAt(2), r.readCodePoint()); - assertEquals(s.codePointAt(4), r.readCodePoint()); - assertEquals(s.codePointAt(5), r.readCodePoint()); - assertEquals(s.codePointAt(6), r.readCodePoint()); - assertEquals(-1, r.read()); - - // Multiple codepoints read in small chunks. - s = "¢â¬ð¤¢Â¢â¬ð¤¢"; - String s2; - int i; - in = escape(s); - r = r(in, true); - char[] buff = new char[2]; - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("¢", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("â¬", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("ð¤¢", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("¢", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("â¬", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("ð¤¢", s2); - i = r.read(buff, 0, buff.length); - assertEquals(-1, i); - - // Multiple codepoints read in slightly larger chunks. - s = "¢â¬ð¤¢Â¢â¬ð¤¢"; - in = escape(s); - r = r(in, true); - buff = new char[3]; - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("¢â¬", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("ð¤¢", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("¢â¬", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("ð¤¢", s2); - i = r.read(buff, 0, buff.length); - assertEquals(-1, i); - - s = "¢â¬ð¤¢Â¢â¬ð¤¢"; - in = escape(s); - r = r(in, true); - buff = new char[4]; - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("¢â¬ð¤¢", s2); - i = r.read(buff, 0, buff.length); - s2 = new String(buff, 0, i); - assertEquals("¢â¬ð¤¢", s2); - i = r.read(buff, 0, buff.length); - assertEquals(-1, i); - - // Reader that only returns 1 character at a time; - s = "x¢â¬ð¤¢x¢â¬ð¤¢"; - in = "x" + escape("¢â¬ð¤¢") + "x" + escape("¢â¬ð¤¢"); - r = new UonReader(new SlowStringReader(in), true); - assertEquals(s.codePointAt(0), r.readCodePoint()); - assertEquals(s.codePointAt(1), r.readCodePoint()); - assertEquals(s.codePointAt(2), r.readCodePoint()); - assertEquals(s.codePointAt(3), r.readCodePoint()); - assertEquals(s.codePointAt(5), r.readCodePoint()); - assertEquals(s.codePointAt(6), r.readCodePoint()); - assertEquals(s.codePointAt(7), r.readCodePoint()); - assertEquals(s.codePointAt(8), r.readCodePoint()); - assertEquals(-1, r.readCodePoint()); - } - - private String escape(String s) throws UnsupportedEncodingException { - StringBuilder sb = new StringBuilder(); - byte[] b = s.getBytes("UTF-8"); - for (int i = 0; i < b.length; i++) - sb.append('%').append(TestUtils.toHex(b[i])); - return sb.toString(); - } - - private UonReader r(String in, boolean decodeChars) { - return new UonReader(in, decodeChars); - } - - private static class SlowStringReader extends Reader { - - String s; - int i = 0; - - SlowStringReader(String s) { - this.s = s; - } - - @Override /* Reader */ - public int read(char[] cbuf, int off, int len) throws IOException { - if (i >= s.length()) - return -1; - cbuf[off] = s.charAt(i++); - return 1; - } - - @Override /* Reader */ - public void close() throws IOException { - } - - } -} \ No newline at end of file
