http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ab15d45b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java deleted file mode 100755 index f424533..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java +++ /dev/null @@ -1,205 +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.jena; - -import static org.apache.juneau.jena.RdfCommonContext.*; -import static org.junit.Assert.*; - -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.annotation.*; -import org.apache.juneau.parser.*; -import org.apache.juneau.serializer.*; -import org.junit.*; - -@SuppressWarnings({"rawtypes","serial","javadoc"}) -public class CommonParserTest { - - private String wrap(String in) { - return "" - + "<rdf:RDF" - + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'" - + " xmlns:j='http://www.apache.org/juneau/'" - + " xmlns:jp='http://www.apache.org/juneaubp/'>" - + in - + "</rdf:RDF>"; - } - - private String strip(String s) { - return s.replaceFirst("<rdf:RDF[^>]+>\\s*", "").replaceAll("</rdf:RDF>$", "").trim().replaceAll("[\\r\\n]", ""); - } - - private RdfSerializerBuilder getBasicSerializer() { - return new RdfSerializerBuilder() - .sq() - .addLiteralTypes(true) - .useWhitespace(false) - .property(RDF_rdfxml_allowBadUris, true) - .property(RDF_rdfxml_showDoctypeDeclaration, false) - .property(RDF_rdfxml_showXmlDeclaration, false); - } - - //==================================================================================================== - // testBasicFromSerializer - //==================================================================================================== - @Test - public void testFromSerializer() throws Exception { - WriterSerializer s = getBasicSerializer().build(); - ReaderParser p = new RdfParserBuilder().xml().trimWhitespace(true).build(); - Map m = null; - String in; - Integer one = Integer.valueOf(1); - - in = wrap("<rdf:Description><jp:a rdf:datatype='http://www.w3.org/2001/XMLSchema#int'>1</jp:a></rdf:Description>"); - m = (Map)p.parse(in, Object.class); - assertEquals(one, m.get("a")); - - in = wrap("<rdf:Description><jp:a rdf:datatype='http://www.w3.org/2001/XMLSchema#int'>1</jp:a><jp:b>foo bar</jp:b><jp:c rdf:datatype='http://www.w3.org/2001/XMLSchema#boolean'>false</jp:c></rdf:Description>"); - m = (Map)p.parse(in, Object.class); - assertEquals(one, m.get("a")); - assertEquals("foo bar", m.get("b")); - in = wrap("<rdf:Description><jp:a rdf:datatype='http://www.w3.org/2001/XMLSchema#int'> 1 </jp:a><jp:b> foo bar </jp:b><jp:c rdf:datatype='http://www.w3.org/2001/XMLSchema#boolean'> false </jp:c></rdf:Description>"); - m = (Map)p.parse(in, Object.class); - assertEquals(one, m.get("a")); - assertEquals("foo bar", m.get("b")); - assertEquals(false, m.get("c")); - - in = wrap("<rdf:Description><jp:x>org.apache.juneau.test.Person</jp:x><jp:addresses><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:x>org.apache.juneau.test.Address</jp:x><jp:city>city A</jp:city><jp:state>state A</jp:state><jp:street>street A</jp:street><jp:zip rdf:datatype='http://www.w3.org/2001/XMLSchema#int'>12345</jp:zip></rdf:li></rdf:Seq></jp:addresses></rdf:Description>"); - m = (Map)p.parse(in, Object.class); - assertEquals("org.apache.juneau.test.Person", m.get("x")); - List l = (List)m.get("addresses"); - assertNotNull(l); - m = (Map)l.get(0); - assertNotNull(m); - assertEquals("org.apache.juneau.test.Address", m.get("x")); - assertEquals("city A", m.get("city")); - assertEquals("state A", m.get("state")); - assertEquals("street A", m.get("street")); - assertEquals(12345, m.get("zip")); - - in = wrap("<rdf:Seq><rdf:li rdf:parseType='Resource'><jp:attribute>value</jp:attribute></rdf:li><rdf:li rdf:parseType='Resource'><jp:attribute>value</jp:attribute></rdf:li></rdf:Seq>"); - ObjectList jl = (ObjectList)p.parse(in, Object.class); - assertEquals("value", jl.getObjectMap(0).getString("attribute")); - assertEquals("value", jl.getObjectMap(1).getString("attribute")); - - // Verify that all the following return null. - assertNull(p.parse((CharSequence)null, Object.class)); - assertNull(p.parse(wrap(""), Object.class)); - assertNull(p.parse(wrap(" "), Object.class)); - assertNull(p.parse(wrap(" \t"), Object.class)); - assertNull(p.parse(wrap(" <!--foo-->"), Object.class)); - assertNull(p.parse(wrap(" <!--foo--> "), Object.class)); - assertNull(p.parse(wrap(" //foo "), Object.class)); - - - A1 t1 = new A1(); - A2 t2 = new A2(); - t2.add(new A3("name0","value0")); - t2.add(new A3("name1","value1")); - t1.list = t2; - - s = getBasicSerializer().addBeanTypeProperties(true).build(); - in = strip(s.serialize(t1)); - assertEquals("<rdf:Description><jp:_type>A1</jp:_type><jp:list><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:name>name0</jp:name><jp:value>value0</jp:value></rdf:li><rdf:li rdf:parseType='Resource'><jp:name>name1</jp:name><jp:value>value1</jp:value></rdf:li></rdf:Seq></jp:list></rdf:Description>", in); - in = wrap(in); - t1 = p.parse(in, A1.class); - assertEquals("value1", t1.list.get(1).value); - } - - @Bean(typeName="A1") - 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 RdfParserBuilder().xml().ignoreUnknownBeanProperties(true).build(); - B t; - - String in = wrap("<rdf:Description><jp:a rdf:datatype='http://www.w3.org/2001/XMLSchema#int'>1</jp:a><jp:unknownProperty>foo</jp:unknownProperty><jp:b rdf:datatype='http://www.w3.org/2001/XMLSchema#int'>2</jp:b></rdf:Description>"); - t = p.parse(in, B.class); - assertEquals(t.a, 1); - assertEquals(t.b, 2); - - try { - p = new RdfParserBuilder().xml().build(); - 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 { - RdfParser p = new RdfParserBuilder().xml().build(); - String in = wrap("<rdf:Description><jp:ints><rdf:Seq><rdf:li>1</rdf:li><rdf:li>2</rdf:li></rdf:Seq></jp:ints><jp:beans><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:a>1</jp:a><jp:b>2</jp:b></rdf:li></rdf:Seq></jp:beans></rdf:Description>"); - C t = p.parse(in, C.class); - assertEquals(t.getInts().size(), 2); - 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 { - RdfParser p = new RdfParserBuilder().xml().ignoreUnknownBeanProperties(true).listener(MyParserListener.class).build(); - - String in = wrap("<rdf:Description><jp:a rdf:datatype='http://www.w3.org/2001/XMLSchema#int'>1</jp:a><jp:unknownProperty>foo</jp:unknownProperty><jp:b rdf:datatype='http://www.w3.org/2001/XMLSchema#int'>2</jp:b></rdf:Description>"); - p.parse(in, B.class); - assertEquals(1, MyParserListener.events.size()); - assertEquals("unknownProperty,-1,-1", MyParserListener.events.get(0)); - } - - public static class MyParserListener extends ParserListener { - final static List<String> events = new LinkedList<String>(); - - @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { - events.add(propertyName + "," + line + "," + col); - } - } -}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ab15d45b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonTest.java b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonTest.java deleted file mode 100755 index adbf317..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonTest.java +++ /dev/null @@ -1,347 +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.jena; - -import static org.apache.juneau.TestUtils.*; -import static org.apache.juneau.jena.RdfCommonContext.*; -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.jena.annotation.*; -import org.apache.juneau.utils.*; -import org.junit.*; - -@SuppressWarnings({"serial","javadoc"}) -public class CommonTest { - - private RdfSerializerBuilder getBasicSerializer() { - return new RdfSerializerBuilder() - .sq() - .useWhitespace(false) - .property(RDF_rdfxml_allowBadUris, true) - .property(RDF_rdfxml_showDoctypeDeclaration, false) - .property(RDF_rdfxml_showXmlDeclaration, false); - } - - private String strip(String s) { - return s.replaceFirst("<rdf:RDF[^>]+>\\s*", "").replaceAll("</rdf:RDF>$", "").trim().replaceAll("[\\r\\n]", ""); - } - - //==================================================================================================== - // Trim nulls from beans - //==================================================================================================== - @Test - public void testTrimNullsFromBeans() throws Exception { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - A t1 = A.create(), t2; - - s.trimNullProperties(false); - String r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:s1 rdf:resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/><jp:s2>s2</jp:s2></rdf:Description>", strip(r)); - t2 = p.parse(r, A.class); - assertEqualObjects(t1, t2); - - s.trimNullProperties(true); - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:s2>s2</jp:s2></rdf:Description>", strip(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 { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - B t1 = B.create(), t2; - String r; - - s.trimEmptyMaps(false); - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:f1 rdf:parseType='Resource'></jp:f1><jp:f2 rdf:parseType='Resource'><jp:f2a rdf:resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/><jp:f2b rdf:parseType='Resource'><jp:s2>s2</jp:s2></jp:f2b></jp:f2></rdf:Description>", strip(r)); - t2 = p.parse(r, B.class); - assertEqualObjects(t1, t2); - - s.trimEmptyMaps(true); - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:f2 rdf:parseType='Resource'><jp:f2a rdf:resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/><jp:f2b rdf:parseType='Resource'><jp:s2>s2</jp:s2></jp:f2b></jp:f2></rdf:Description>", strip(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 { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - C t1 = C.create(), t2; - String r; - - s.trimEmptyCollections(false); - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:f1><rdf:Seq/></jp:f1><jp:f2><rdf:Seq><rdf:li rdf:resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/><rdf:li rdf:parseType='Resource'><jp:s2>s2</jp:s2></rdf:li></rdf:Seq></jp:f2></rdf:Description>", strip(r)); - t2 = p.parse(r, C.class); - assertEqualObjects(t1, t2); - - s.trimEmptyCollections(true); - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:f2><rdf:Seq><rdf:li rdf:resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/><rdf:li rdf:parseType='Resource'><jp:s2>s2</jp:s2></rdf:li></rdf:Seq></jp:f2></rdf:Description>", strip(r)); - t2 = p.parse(r, C.class); - assertNull(t2.f1); - t2 = p.parse(r, C.class); - } - - public static class C { - public List<A> f1, f2; - - public static C create() { - C t = new C(); - t.f1 = new AList<A>(); - t.f2 = new AList<A>().append(null).append(A.create()); - return t; - } - } - - //==================================================================================================== - // Trim empty arrays - //==================================================================================================== - @Test - public void testTrimEmptyArrays() throws Exception { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - D t1 = D.create(), t2; - String r; - - s.trimEmptyCollections(false); - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:f1><rdf:Seq/></jp:f1><jp:f2><rdf:Seq><rdf:li rdf:resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/><rdf:li rdf:parseType='Resource'><jp:s2>s2</jp:s2></rdf:li></rdf:Seq></jp:f2></rdf:Description>", strip(r)); - t2 = p.parse(r, D.class); - assertEqualObjects(t1, t2); - - s.trimEmptyCollections(true); - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:f2><rdf:Seq><rdf:li rdf:resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/><rdf:li rdf:parseType='Resource'><jp:s2>s2</jp:s2></rdf:li></rdf:Seq></jp:f2></rdf:Description>", strip(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 testBeanPropertyProperties() throws Exception { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - E1 t1 = E1.create(), t2; - String r; - - r = s.build().serialize(t1); - assertEquals("<rdf:Description><jp:x1 rdf:parseType='Resource'><jp:f1>1</jp:f1></jp:x1><jp:x2 rdf:parseType='Resource'><jp:f1>1</jp:f1></jp:x2><jp:x3><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:f1>1</jp:f1></rdf:li></rdf:Seq></jp:x3><jp:x4><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:f1>1</jp:f1></rdf:li></rdf:Seq></jp:x4><jp:x5><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:f1>1</jp:f1></rdf:li></rdf:Seq></jp:x5><jp:x6><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:f1>1</jp:f1></rdf:li></rdf:Seq></jp:x6></rdf:Description>", strip(r)); - t2 = p.parse(r, E1.class); - assertEqualObjects(t1, t2); - } - - public static class E1 { - @BeanProperty(properties="f1") public E2 x1; - @BeanProperty(properties="f1") public Map<String,Integer> x2; - @BeanProperty(properties="f1") public E2[] x3; - @BeanProperty(properties="f1") public List<E2> x4; - @BeanProperty(properties="f1") public ObjectMap[] x5; - @BeanProperty(properties="f1") public List<ObjectMap> x6; - - public static E1 create() { - E1 t = new E1(); - t.x1 = new E2(); - t.x2 = new AMap<String,Integer>().append("f1",1).append("f2",2); - t.x3 = new E2[] {new E2()}; - t.x4 = new AList<E2>().append(new E2()); - t.x5 = new ObjectMap[] {new ObjectMap().append("f1","1").append("f2","2")}; - t.x6 = new AList<ObjectMap>().append(new ObjectMap().append("f1","1").append("f2","2")); - return t; - } - } - - public static class E2 { - public int f1 = 1; - public int f2 = 2; - } - - //==================================================================================================== - // @BeanProperty.properties annotation on list of beans. - //==================================================================================================== - @Test - public void testBeanPropertyProperiesOnListOfBeans() throws Exception { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - List<F> l1 = new LinkedList<F>(), l2; - F t = F.create(); - t.x1.add(F.create()); - l1.add(t); - - String r = s.build().serialize(l1); - assertEquals("<rdf:Seq><rdf:li rdf:parseType='Resource'><jp:x1><rdf:Seq><rdf:li rdf:parseType='Resource'><jp:x2>2</jp:x2></rdf:li></rdf:Seq></jp:x1><jp:x2>2</jp:x2></rdf:li></rdf:Seq>", strip(r)); - l2 = p.parse(r, LinkedList.class, F.class); - assertEqualObjects(l1, l2); - } - - public static class F { - @BeanProperty(properties="x2") public List<F> x1; - public int x2; - - public static F create() { - F t = new F(); - t.x1 = new LinkedList<F>(); - t.x2 = 2; - return t; - } - } - - //==================================================================================================== - // Test URIAttr - Test that URLs and URIs are serialized and parsed correctly. - //==================================================================================================== - @Test - public void testURIAttr() throws Exception { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - - G t = new G(); - t.uri = new URI("http://uri"); - t.f1 = new URI("http://f1"); - t.f2 = new URL("http://f2"); - - String xml = s.build().serialize(t); - t = p.parse(xml, G.class); - assertEquals("http://uri", t.uri.toString()); - assertEquals("http://f1", t.f1.toString()); - assertEquals("http://f2", t.f2.toString()); - } - - public static class G { - @Rdf(beanUri=true) public URI uri; - public URI f1; - public URL f2; - } - - - //==================================================================================================== - // Recursion - //==================================================================================================== - @Test - public void testRecursion() throws Exception { - RdfSerializerBuilder s = new RdfSerializerBuilder().xmlabbrev().sq(); - - 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.build().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.detectRecursions(true); - try { - s.build().serialize(r1); - fail("Exception expected!"); - } catch (Exception e) { - String msg = e.getLocalizedMessage(); - assertTrue(msg.contains("[0]root:org.apache.juneau.jena.CommonTest$R1")); - assertTrue(msg.contains("->[1]r2:org.apache.juneau.jena.CommonTest$R2")); - assertTrue(msg.contains("->[2]r3:org.apache.juneau.jena.CommonTest$R3")); - assertTrue(msg.contains("->[3]r1:org.apache.juneau.jena.CommonTest$R1")); - } - - s.ignoreRecursions(true); - String r = s.build().serialize(r1).replace("\r", ""); - // Note...the order of the namespaces is not always the same depending on the JVM. - // The Jena libraries appear to use a hashmap for these. - assertTrue(r.contains( - "<rdf:Description>\n"+ - "<jp:name>foo</jp:name>\n"+ - "<jp:r2 rdf:parseType='Resource'>\n"+ - "<jp:name>bar</jp:name>\n"+ - "<jp:r3 rdf:parseType='Resource'>\n"+ - "<jp:name>baz</jp:name>\n"+ - "</jp:r3>\n"+ - "</jp:r2>\n"+ - "</rdf:Description>\n"+ - "</rdf:RDF>\n" - )); - assertTrue(r.contains("xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#")); - assertTrue(r.contains("xmlns:j='http://www.apache.org/juneau/")); - assertTrue(r.contains("xmlns:jp='http://www.apache.org/juneaubp/")); - } - - 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/ab15d45b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonXmlTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonXmlTest.java b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonXmlTest.java deleted file mode 100755 index 00ccbb7..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonXmlTest.java +++ /dev/null @@ -1,96 +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.jena; - -import static org.apache.juneau.TestUtils.*; -import static org.apache.juneau.jena.RdfCommonContext.*; -import static org.junit.Assert.*; - -import java.net.*; - -import org.apache.juneau.jena.annotation.*; -import org.apache.juneau.xml.annotation.*; -import org.junit.*; - -@SuppressWarnings("javadoc") -public class CommonXmlTest { - - private RdfSerializerBuilder getBasicSerializer() { - return new RdfSerializerBuilder() - .sq() - .useWhitespace(false) - .property(RDF_rdfxml_allowBadUris, true) - .property(RDF_rdfxml_showDoctypeDeclaration, false) - .property(RDF_rdfxml_showXmlDeclaration, false); - } - - private String strip(String s) { - return s.replaceFirst("<rdf:RDF[^>]+>\\s*", "").replaceAll("</rdf:RDF>$", "").trim().replaceAll("[\\r\\n]", ""); - } - - //==================================================================================================== - // Bean.uri annotation - //==================================================================================================== - @Test - public void testBeanUriAnnotation() throws Exception { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - A t1 = A.create(), t2; - String r; - - r = s.build().serialize(t1); - assertEquals("<rdf:Description rdf:about='http://foo'><jp:name>bar</jp:name></rdf:Description>", strip(r)); - t2 = p.parse(r, A.class); - assertEqualObjects(t1, t2); - } - - public static class A { - @Rdf(beanUri=true) @Xml(format=XmlFormat.ATTR) public URL url; - public String name; - - public static A create() throws Exception { - A t = new A(); - t.url = new URL("http://foo"); - t.name = "bar"; - return t; - } - } - - //==================================================================================================== - // Bean.uri annotation, only uri property - //==================================================================================================== - @Test - public void testBeanUriAnnotationOnlyUriProperty() throws Exception { - RdfSerializerBuilder s = getBasicSerializer(); - RdfParser p = RdfParser.DEFAULT_XML; - B t1 = B.create(), t2; - String r; - - r = s.build().serialize(t1); - assertEquals("<rdf:Description rdf:about='http://foo'><jp:url2 rdf:resource='http://foo/2'/></rdf:Description>", strip(r)); - t2 = p.parse(r, B.class); - assertEqualObjects(t1, t2); - } - - public static class B { - @Rdf(beanUri=true) @Xml(format=XmlFormat.ATTR) public URL url; - public URL url2; - - public static B create() throws Exception { - B t = new B(); - t.url = new URL("http://foo"); - t.url2 = new URL("http://foo/2"); - return t; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ab15d45b/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfParserTest.java b/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfParserTest.java deleted file mode 100755 index 20bb71b..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfParserTest.java +++ /dev/null @@ -1,159 +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.jena; - -import static org.apache.juneau.TestUtils.*; -import static org.apache.juneau.jena.RdfCommonContext.*; -import static org.junit.Assert.*; - -import java.net.URI; -import java.text.*; -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.annotation.*; -import org.apache.juneau.jena.annotation.*; -import org.apache.juneau.json.*; -import org.apache.juneau.transforms.*; -import org.junit.*; - -@SuppressWarnings("javadoc") -public class RdfParserTest { - - @Before - public void beforeTest() { - TestUtils.setLocale(Locale.US); - } - - @After - public void afterTest() { - TestUtils.unsetLocale(); - } - - @Test - public void testParseIntoGenericPojos() throws Exception { - A a = new A().init(); - - // Create a new serializer with readable output. - RdfSerializer s = new RdfSerializerBuilder().xmlabbrev() - .property(RDF_rdfxml_tab, 3) - .sq() - .addRootProperty(true) - .build(); - - String expected = - "<rdf:RDF a='http://ns/' a1='http://ns2/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/a'>" - + "\n <a:f1>1</a:f1>" - + "\n <a:f2>f2</a:f2>" - + "\n <a:f4a rdf:resource='http://test/a'/>" - + "\n <a:f4b rdf:resource='http://test/external'/>" - + "\n <a:f5>1999-01-01T00:00:00Z</a:f5>" - + "\n <a:f6>" - + "\n <rdf:Seq>" - + "\n <rdf:li>" - + "\n <rdf:Description about='http://test/a/a1'>" - + "\n <a1:f1>1</a1:f1>" - + "\n <a1:f2>f2</a1:f2>" - + "\n <a1:f4a rdf:resource='http://test/a'/>" - + "\n <a1:f4b rdf:resource='http://test/external'/>" - + "\n <a1:f5>1999-01-01T00:00:00Z</a1:f5>" - + "\n </rdf:Description>" - + "\n </rdf:li>" - + "\n </rdf:Seq>" - + "\n </a:f6>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - - String rdfXml = s.serialize(a); - assertXmlEquals(expected, rdfXml); - - A a2 = RdfParser.DEFAULT_XML.parse(rdfXml, A.class); - - assertEqualObjects(a, a2); - - ObjectMap m = RdfParser.DEFAULT_XML.parse(rdfXml, ObjectMap.class); - String json = JsonSerializer.DEFAULT_LAX_READABLE.serialize(m); - - String e = "" - + "{\n" - + " uri: 'http://test/a',\n" - + " f6: [\n" - + " {\n" - + " uri: 'http://test/a/a1',\n" - + " f5: '1999-01-01T00:00:00Z',\n" - + " f4b: 'http://test/external',\n" - + " f4a: 'http://test/a',\n" - + " f2: 'f2',\n" - + " f1: '1'\n" - + " }\n" - + " ],\n" - + " f5: '1999-01-01T00:00:00Z',\n" - + " f4b: 'http://test/external',\n" - + " f4a: 'http://test/a',\n" - + " f2: 'f2',\n" - + " f1: '1',\n" - + " root: 'true'\n" - + "}"; - assertEquals(e, json.replace("\r", "")); - - } - - @Rdf(prefix="a", namespace="http://ns/") - public static class A { - public int f1; - public String f2; - @Rdf(beanUri=true) public URI f3; - public URI f4a, f4b; - @BeanProperty(swap=CalendarSwap.ISO8601DTZ.class) public Calendar f5; - public LinkedList<A1> f6 = new LinkedList<A1>(); - - public A init() throws Exception { - f1 = 1; - f2 = "f2"; - f3 = new URI("http://test/a"); // Bean URI. - f4a = new URI("http://test/a"); // Points to itself. - f4b = new URI("http://test/external"); - f5 = new GregorianCalendar(); - DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); - df.setTimeZone(TimeZone.getTimeZone("GMT")); - f5.setTime(df.parse("Jan 1, 1999")); - f6 = new LinkedList<A1>(); - f6.add(new A1().init()); - return this; - } - } - - @Rdf(prefix="a1", namespace="http://ns2/") - public static class A1 { - public int f1; - public String f2; - @Rdf(beanUri=true) public URI f3; - public URI f4a, f4b; - @BeanProperty(swap=CalendarSwap.ISO8601DTZ.class) public Calendar f5; - - public A1 init() throws Exception { - f1 = 1; - f2 = "f2"; - f3 = new URI("http://test/a/a1"); - f4a = new URI("http://test/a"); - f4b = new URI("http://test/external"); - f5 = new GregorianCalendar(); - DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); - df.setTimeZone(TimeZone.getTimeZone("GMT")); - f5.setTime(df.parse("Jan 1, 1999")); - return this; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ab15d45b/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfTest.java b/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfTest.java deleted file mode 100755 index 840e272..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/jena/RdfTest.java +++ /dev/null @@ -1,593 +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.jena; - -import static org.apache.juneau.TestUtils.*; -import static org.apache.juneau.jena.RdfCommonContext.*; - -import java.net.*; -import java.util.*; - -import org.apache.juneau.jena.annotation.*; -import org.apache.juneau.parser.*; -import org.apache.juneau.serializer.*; -import org.junit.*; - -@SuppressWarnings({"serial","javadoc"}) -public class RdfTest { - - @Test - public void testCollectionFormatProperties() throws Exception { - A a = new A().init(), a2; - String rdfXml; - String expected; - - RdfSerializerBuilder s = new RdfSerializerBuilder().xmlabbrev() - .property(RDF_rdfxml_tab, 3) - .sq() - .addRootProperty(true); - RdfParser p = new RdfParserBuilder().xml().build(); - - //-------------------------------------------------------------------------------- - // Normal format - Sequence - //-------------------------------------------------------------------------------- - expected = - "<rdf:RDF a='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/a'>" - + "\n <a:f2>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f2a</rdf:li>" - + "\n <rdf:li>f2b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </a:f2>" - + "\n <a:f3>" - + "\n <rdf:Seq>" - + "\n <rdf:li>1</rdf:li>" - + "\n <rdf:li>2</rdf:li>" - + "\n </rdf:Seq>" - + "\n </a:f3>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - rdfXml = s.build().serialize(a); - assertXmlEquals(expected, rdfXml); - - a2 = p.parse(rdfXml, A.class); - assertEqualObjects(a, a2); - - //-------------------------------------------------------------------------------- - // Explicit sequence - //-------------------------------------------------------------------------------- - s.collectionFormat(RdfCollectionFormat.SEQ); - expected = - "<rdf:RDF a='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/a'>" - + "\n <a:f2>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f2a</rdf:li>" - + "\n <rdf:li>f2b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </a:f2>" - + "\n <a:f3>" - + "\n <rdf:Seq>" - + "\n <rdf:li>1</rdf:li>" - + "\n <rdf:li>2</rdf:li>" - + "\n </rdf:Seq>" - + "\n </a:f3>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - rdfXml = s.build().serialize(a); - assertXmlEquals(expected, rdfXml); - - a2 = p.parse(rdfXml, A.class); - assertEqualObjects(a, a2); - - //-------------------------------------------------------------------------------- - // Bag - //-------------------------------------------------------------------------------- - s.collectionFormat(RdfCollectionFormat.BAG); - expected = - "<rdf:RDF a='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/a'>" - + "\n <a:f2>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f2a</rdf:li>" - + "\n <rdf:li>f2b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </a:f2>" - + "\n <a:f3>" - + "\n <rdf:Bag>" - + "\n <rdf:li>1</rdf:li>" - + "\n <rdf:li>2</rdf:li>" - + "\n </rdf:Bag>" - + "\n </a:f3>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - rdfXml = s.build().serialize(a); - assertXmlEquals(expected, rdfXml); - - a2 = p.parse(rdfXml, A.class); - assertEqualObjects(a, a2); - - //-------------------------------------------------------------------------------- - // List - //-------------------------------------------------------------------------------- - s.collectionFormat(RdfCollectionFormat.LIST); - expected = - "<rdf:RDF a='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/a'>" - + "\n <a:f2 parseType='Resource'>" - + "\n <rdf:first>f2a</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>f2b</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </a:f2>" - + "\n <a:f3 parseType='Resource'>" - + "\n <rdf:first>1</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>2</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </a:f3>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - rdfXml = s.build().serialize(a); - assertXmlEquals(expected, rdfXml); - - a2 = p.parse(rdfXml, A.class); - assertEqualObjects(a, a2); - - //-------------------------------------------------------------------------------- - // Multi-properties - //-------------------------------------------------------------------------------- - s.collectionFormat(RdfCollectionFormat.MULTI_VALUED); - expected = - "<rdf:RDF a='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/a'>" - + "\n <a:f2>f2a</a:f2>" - + "\n <a:f2>f2b</a:f2>" - + "\n <a:f3>1</a:f3>" - + "\n <a:f3>2</a:f3>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - rdfXml = s.build().serialize(a); - assertXmlEquals(expected, rdfXml); - - // Note - Must specify collection format on parser for it to be able to understand this layout. - p = new RdfParserBuilder().xml().collectionFormat(RdfCollectionFormat.MULTI_VALUED).build(); - a2 = p.parse(rdfXml, A.class); - assertEqualObjects(a, a2); - } - - @Rdf(prefix="a", namespace="http://ns/") - public static class A { - @Rdf(beanUri=true) public URI f1; - public String[] f2; - public List<Integer> f3; - - public A init() throws Exception { - f1 = new URI("http://test/a"); - f2 = new String[]{"f2a","f2b"}; - f3 = Arrays.asList(new Integer[]{1,2}); - return this; - } - } - - @Test - public void testCollectionFormatAnnotations() throws Exception { - B b = new B().init(), b2; - String rdfXml, expected; - RdfSerializerBuilder s = new RdfSerializerBuilder().xmlabbrev() - .property(RDF_rdfxml_tab, 3) - .sq() - .addRootProperty(true); - RdfParser p = RdfParser.DEFAULT_XML; - - //-------------------------------------------------------------------------------- - // Normal format - Sequence - //-------------------------------------------------------------------------------- - - expected = - "<rdf:RDF b='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/b'>" - + "\n <b:f2>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f2a</rdf:li>" - + "\n <rdf:li>f2b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:f2>" - + "\n <b:f3>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f3a</rdf:li>" - + "\n <rdf:li>f3b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f3>" - + "\n <b:f4 parseType='Resource'>" - + "\n <rdf:first>f4a</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>f4b</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </b:f4>" - + "\n <b:f5>f5a</b:f5>" - + "\n <b:f5>f5b</b:f5>" - + "\n <b:f6>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f6a</rdf:li>" - + "\n <rdf:li>f6b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:f6>" - + "\n <b:f7>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f7a</rdf:li>" - + "\n <rdf:li>f7b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:f7>" - + "\n <b:f8>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f8a</rdf:li>" - + "\n <rdf:li>f8b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f8>" - + "\n <b:f9 parseType='Resource'>" - + "\n <rdf:first>f9a</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>f9b</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </b:f9>" - + "\n <b:fa>faa</b:fa>" - + "\n <b:fa>fab</b:fa>" - + "\n <b:fb>" - + "\n <rdf:Seq>" - + "\n <rdf:li>fba</rdf:li>" - + "\n <rdf:li>fbb</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:fb>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - rdfXml = s.build().serialize(b); - assertXmlEquals(expected, rdfXml); - - b2 = p.parse(rdfXml, B.class); - assertEqualObjects(b, b2, true); - - //-------------------------------------------------------------------------------- - // Default is Bag - Should only affect DEFAULT properties. - //-------------------------------------------------------------------------------- - s.collectionFormat(RdfCollectionFormat.BAG); - expected = - "<rdf:RDF b='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/b'>" - + "\n <b:f2>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f2a</rdf:li>" - + "\n <rdf:li>f2b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:f2>" - + "\n <b:f3>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f3a</rdf:li>" - + "\n <rdf:li>f3b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f3>" - + "\n <b:f4 parseType='Resource'>" - + "\n <rdf:first>f4a</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>f4b</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </b:f4>" - + "\n <b:f5>f5a</b:f5>" - + "\n <b:f5>f5b</b:f5>" - + "\n <b:f6>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f6a</rdf:li>" - + "\n <rdf:li>f6b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f6>" - + "\n <b:f7>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f7a</rdf:li>" - + "\n <rdf:li>f7b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:f7>" - + "\n <b:f8>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f8a</rdf:li>" - + "\n <rdf:li>f8b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f8>" - + "\n <b:f9 parseType='Resource'>" - + "\n <rdf:first>f9a</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>f9b</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </b:f9>" - + "\n <b:fa>faa</b:fa>" - + "\n <b:fa>fab</b:fa>" - + "\n <b:fb>" - + "\n <rdf:Bag>" - + "\n <rdf:li>fba</rdf:li>" - + "\n <rdf:li>fbb</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:fb>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - - rdfXml = s.build().serialize(b); - assertXmlEquals(expected, rdfXml); - - b2 = p.parse(rdfXml, B.class); - assertEqualObjects(b, b2, true); - } - - @Rdf(prefix="b", namespace="http://ns/") - public static class B { - @Rdf(beanUri=true) public URI f1; - - @Rdf(collectionFormat=RdfCollectionFormat.SEQ) - public String[] f2; - - @Rdf(collectionFormat=RdfCollectionFormat.BAG) - public String[] f3; - - @Rdf(collectionFormat=RdfCollectionFormat.LIST) - public String[] f4; - - @Rdf(collectionFormat=RdfCollectionFormat.MULTI_VALUED) - public String[] f5; - - @Rdf(collectionFormat=RdfCollectionFormat.DEFAULT) - public String[] f6; - - public BA f7; - public BB f8; - public BC f9; - public BD fa; - public BE fb; - - public B init() throws Exception { - f1 = new URI("http://test/b"); - f2 = new String[]{"f2a","f2b"}; - f3 = new String[]{"f3a","f3b"}; - f4 = new String[]{"f4a","f4b"}; - f5 = new String[]{"f5a","f5b"}; - f6 = new String[]{"f6a","f6b"}; - f7 = new BA().append("f7a","f7b"); - f8 = new BB().append("f8a","f8b"); - f9 = new BC().append("f9a","f9b"); - fa = new BD().append("faa","fab"); - fb = new BE().append("fba","fbb"); - return this; - } - } - - @Rdf(prefix="ba", namespace="http://ns/", collectionFormat=RdfCollectionFormat.SEQ) - public static class BA extends ArrayList<String> { - public BA append(String...s) { - this.addAll(Arrays.asList(s)); - return this; - } - } - - @Rdf(prefix="bb", namespace="http://ns/", collectionFormat=RdfCollectionFormat.BAG) - public static class BB extends ArrayList<String> { - public BB append(String...s) { - this.addAll(Arrays.asList(s)); - return this; - } - } - - @Rdf(prefix="bc", namespace="http://ns/", collectionFormat=RdfCollectionFormat.LIST) - public static class BC extends ArrayList<String> { - public BC append(String...s) { - this.addAll(Arrays.asList(s)); - return this; - } - } - - @Rdf(prefix="bd", namespace="http://ns/", collectionFormat=RdfCollectionFormat.MULTI_VALUED) - public static class BD extends ArrayList<String> { - public BD append(String...s) { - this.addAll(Arrays.asList(s)); - return this; - } - } - - @Rdf(prefix="bd", namespace="http://ns/", collectionFormat=RdfCollectionFormat.DEFAULT) - public static class BE extends ArrayList<String> { - public BE append(String...s) { - this.addAll(Arrays.asList(s)); - return this; - } - } - - @Test - public void testCollectionFormatAnnotationOnClass() throws Exception { - C c = new C().init(), c2; - String rdfXml, expected; - RdfSerializerBuilder s = new RdfSerializerBuilder().xmlabbrev() - .property(RDF_rdfxml_tab, 3) - .sq() - .addRootProperty(true); - RdfParser p = RdfParser.DEFAULT_XML; - - //-------------------------------------------------------------------------------- - // Default on class is Bag - Should only affect DEFAULT properties. - //-------------------------------------------------------------------------------- - s.collectionFormat(RdfCollectionFormat.BAG); - expected = - "<rdf:RDF b='http://ns/' j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://test/b'>" - + "\n <b:f2>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f2a</rdf:li>" - + "\n <rdf:li>f2b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:f2>" - + "\n <b:f3>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f3a</rdf:li>" - + "\n <rdf:li>f3b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f3>" - + "\n <b:f4 parseType='Resource'>" - + "\n <rdf:first>f4a</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>f4b</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </b:f4>" - + "\n <b:f5>f5a</b:f5>" - + "\n <b:f5>f5b</b:f5>" - + "\n <b:f6>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f6a</rdf:li>" - + "\n <rdf:li>f6b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f6>" - + "\n <b:f7>" - + "\n <rdf:Seq>" - + "\n <rdf:li>f7a</rdf:li>" - + "\n <rdf:li>f7b</rdf:li>" - + "\n </rdf:Seq>" - + "\n </b:f7>" - + "\n <b:f8>" - + "\n <rdf:Bag>" - + "\n <rdf:li>f8a</rdf:li>" - + "\n <rdf:li>f8b</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:f8>" - + "\n <b:f9 parseType='Resource'>" - + "\n <rdf:first>f9a</rdf:first>" - + "\n <rdf:rest parseType='Resource'>" - + "\n <rdf:first>f9b</rdf:first>" - + "\n <rdf:rest resource='http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'/>" - + "\n </rdf:rest>" - + "\n </b:f9>" - + "\n <b:fa>faa</b:fa>" - + "\n <b:fa>fab</b:fa>" - + "\n <b:fb>" - + "\n <rdf:Bag>" - + "\n <rdf:li>fba</rdf:li>" - + "\n <rdf:li>fbb</rdf:li>" - + "\n </rdf:Bag>" - + "\n </b:fb>" - + "\n <j:root>true</j:root>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - - rdfXml = s.build().serialize(c); - assertXmlEquals(expected, rdfXml); - - c2 = p.parse(rdfXml, C.class); - assertEqualObjects(c, c2, true); - } - - @Rdf(collectionFormat=RdfCollectionFormat.BAG) - public static class C extends B { - @Override /* B */ - public C init() throws Exception { - f1 = new URI("http://test/b"); - f2 = new String[]{"f2a","f2b"}; - f3 = new String[]{"f3a","f3b"}; - f4 = new String[]{"f4a","f4b"}; - f5 = new String[]{"f5a","f5b"}; - f6 = new String[]{"f6a","f6b"}; - f7 = new BA().append("f7a","f7b"); - f8 = new BB().append("f8a","f8b"); - f9 = new BC().append("f9a","f9b"); - fa = new BD().append("faa","fab"); - fb = new BE().append("fba","fbb"); - return this; - } - } - - @Test - public void testLooseCollectionsOfBeans() throws Exception { - WriterSerializer s = new RdfSerializerBuilder().xmlabbrev().looseCollections(true).build(); - ReaderParser p = new RdfParserBuilder().xml().looseCollections(true).build(); - String rdfXml, expected; - - List<D> l = new LinkedList<D>(); - l.add(new D().init(1)); - l.add(new D().init(2)); - - rdfXml = s.serialize(l); - expected = - "<rdf:RDF j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://localhost/f1/2'>" - + "\n <jp:f2>f2</jp:f2>" - + "\n <jp:f3 resource='http://localhost/f3/2'/>" - + "\n </rdf:Description>" - + "\n <rdf:Description about='http://localhost/f1/1'>" - + "\n <jp:f2>f2</jp:f2>" - + "\n <jp:f3 resource='http://localhost/f3/1'/>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - assertXmlEquals(expected, rdfXml); - - l = p.parse(rdfXml, LinkedList.class, D.class); - D[] da = l.toArray(new D[l.size()]); - rdfXml = s.serialize(da); - expected = - "<rdf:RDF j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://localhost/f1/2'>" - + "\n <jp:f2>f2</jp:f2>" - + "\n <jp:f3 resource='http://localhost/f3/2'/>" - + "\n </rdf:Description>" - + "\n <rdf:Description about='http://localhost/f1/1'>" - + "\n <jp:f2>f2</jp:f2>" - + "\n <jp:f3 resource='http://localhost/f3/1'/>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - assertXmlEquals(expected, rdfXml); - - da = p.parse(rdfXml, D[].class); - rdfXml = s.serialize(da); - expected = - "<rdf:RDF j='http://www.apache.org/juneau/' jp='http://www.apache.org/juneaubp/' rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" - + "\n <rdf:Description about='http://localhost/f1/2'>" - + "\n <jp:f2>f2</jp:f2>" - + "\n <jp:f3 resource='http://localhost/f3/2'/>" - + "\n </rdf:Description>" - + "\n <rdf:Description about='http://localhost/f1/1'>" - + "\n <jp:f2>f2</jp:f2>" - + "\n <jp:f3 resource='http://localhost/f3/1'/>" - + "\n </rdf:Description>" - + "\n</rdf:RDF>"; - assertXmlEquals(expected, rdfXml); - } - - public static class D { - @Rdf(beanUri=true) public URI f1; - public String f2; - public URI f3; - - public D init(int num) throws Exception { - f1 = new URI("http://localhost/f1/" + num); - f2 = "f2"; - f3 = new URI("http://localhost/f3/" + num); - return this; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ab15d45b/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java b/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java deleted file mode 100755 index 24c013b..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java +++ /dev/null @@ -1,180 +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.json; - -import static org.junit.Assert.*; - -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.annotation.*; -import org.apache.juneau.parser.*; -import org.junit.*; - -@SuppressWarnings({"rawtypes","serial","javadoc"}) -public class CommonParserTest { - - //==================================================================================================== - // testFromSerializer - //==================================================================================================== - @Test - public void testFromSerializer() throws Exception { - ReaderParser p = new JsonParserBuilder().beanDictionary(A1.class).build(); - - Map m = null; - m = (Map)p.parse("{a:1}", Object.class); - assertEquals(1, m.get("a")); - m = (Map)p.parse("{a:1,b:\"foo bar\"}", Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo bar", m.get("b")); - m = (Map)p.parse("{a:1,b:\"foo bar\",c:false}", Object.class); - assertEquals(1, m.get("a")); - assertEquals(false, m.get("c")); - m = (Map)p.parse(" { a : 1 , b : 'foo' , c : false } ", Object.class); - assertEquals(1, m.get("a")); - assertEquals("foo", m.get("b")); - assertEquals(false, m.get("c")); - - m = (Map)p.parse("{x:\"org.apache.juneau.test.Person\",addresses:[{x:\"org.apache.juneau.test.Address\",city:\"city A\",state:\"state A\",street:\"street A\",zip:12345}]}", Object.class); - assertEquals("org.apache.juneau.test.Person", m.get("x")); - List l = (List)m.get("addresses"); - assertNotNull(l); - m = (Map)l.get(0); - assertNotNull(m); - assertEquals("org.apache.juneau.test.Address", m.get("x")); - assertEquals("city A", m.get("city")); - assertEquals("state A", m.get("state")); - assertEquals("street A", m.get("street")); - assertEquals(12345, m.get("zip")); - - ObjectList jl = (ObjectList)p.parse("[{attribute:'value'},{attribute:'value'}]", Object.class); - assertEquals("value", jl.getObjectMap(0).getString("attribute")); - assertEquals("value", jl.getObjectMap(1).getString("attribute")); - - // Verify that all the following return null. - assertNull(p.parse((CharSequence)null, Object.class)); - assertNull(p.parse("", Object.class)); - assertNull(p.parse(" ", Object.class)); - assertNull(p.parse(" \t", Object.class)); - assertNull(p.parse(" /*foo*/", Object.class)); - assertNull(p.parse(" /*foo*/ ", Object.class)); - assertNull(p.parse(" //foo ", Object.class)); - - try { - jl = (ObjectList)p.parse("[{attribute:'value'},{attribute:'value'}]", Object.class); - assertEquals("value", jl.getObjectMap(0).getString("attribute")); - assertEquals("value", jl.getObjectMap(1).getString("attribute")); - } catch (Exception e) { - fail(e.getLocalizedMessage()); - } - - A1 b = new A1(); - A2 tl = new A2(); - tl.add(new A3("name0","value0")); - tl.add(new A3("name1","value1")); - b.list = tl; - String json = new JsonSerializerBuilder().addBeanTypeProperties(true).beanDictionary(A1.class).build().serialize(b); - b = (A1)p.parse(json, Object.class); - assertEquals("value1", b.list.get(1).value); - - json = JsonSerializer.DEFAULT.serialize(b); - b = p.parse(json, A1.class); - assertEquals("value1", b.list.get(1).value); - } - - @Bean(typeName="A1") - 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 JsonParserBuilder().ignoreUnknownBeanProperties(true).build(); - B b; - - String in = "{a:1,unknown:3,b:2}"; - b = p.parse(in, B.class); - assertEquals(b.a, 1); - assertEquals(b.b, 2); - - try { - p = JsonParser.DEFAULT; - 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 { - JsonParser p = JsonParser.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 { - JsonParser p = new JsonParserBuilder().ignoreUnknownBeanProperties(true).listener(MyParserListener.class).build(); - - String json = "{a:1,unknownProperty:\"/foo\",b:2}"; - p.parse(json, B.class); - assertEquals(1, MyParserListener.events.size()); - assertEquals("unknownProperty,1,5", MyParserListener.events.get(0)); - } - - public static class MyParserListener extends ParserListener { - final static List<String> events = new LinkedList<String>(); - - @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { - events.add(propertyName + "," + line + "," + col); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ab15d45b/juneau-core-test/src/test/java/org/apache/juneau/json/CommonTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/json/CommonTest.java b/juneau-core-test/src/test/java/org/apache/juneau/json/CommonTest.java deleted file mode 100755 index cd2cb22..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/json/CommonTest.java +++ /dev/null @@ -1,343 +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.json; - -import static org.apache.juneau.TestUtils.*; -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.utils.*; -import org.junit.*; - -@SuppressWarnings({"serial","javadoc"}) -public class CommonTest { - - //==================================================================================================== - // Trim nulls from beans - //==================================================================================================== - @Test - public void testTrimNullsFromBeans() throws Exception { - JsonSerializerBuilder s = new JsonSerializerBuilder().simple(); - JsonParser p = JsonParser.DEFAULT; - A t1 = A.create(), t2; - - s.trimNullProperties(false); - String r = s.build().serialize(t1); - assertEquals("{s1:null,s2:'s2'}", r); - t2 = p.parse(r, A.class); - assertEqualObjects(t1, t2); - - s.trimNullProperties(true); - r = s.build().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 { - JsonSerializerBuilder s = new JsonSerializerBuilder().simple(); - JsonParser p = JsonParser.DEFAULT; - B t1 = B.create(), t2; - String r; - - s.trimEmptyMaps(false); - r = s.build().serialize(t1); - assertEquals("{f1:{},f2:{f2a:null,f2b:{s2:'s2'}}}", r); - t2 = p.parse(r, B.class); - assertEqualObjects(t1, t2); - - s.trimEmptyMaps(true); - r = s.build().serialize(t1); - assertEquals("{f2:{f2a:null,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 { - JsonSerializerBuilder s = new JsonSerializerBuilder().simple(); - JsonParser p = JsonParser.DEFAULT; - C t1 = C.create(), t2; - String r; - - s.trimEmptyCollections(false); - r = s.build().serialize(t1); - assertEquals("{f1:[],f2:[null,{s2:'s2'}]}", r); - t2 = p.parse(r, C.class); - assertEqualObjects(t1, t2); - - s.trimEmptyCollections(true); - r = s.build().serialize(t1); - assertEquals("{f2:[null,{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 AList<A>(); - t.f2 = new AList<A>().append(null).append(A.create()); - return t; - } - } - - //==================================================================================================== - // Trim empty arrays - //==================================================================================================== - @Test - public void testTrimEmptyArrays() throws Exception { - JsonSerializerBuilder s = new JsonSerializerBuilder().simple(); - JsonParser p = JsonParser.DEFAULT; - D t1 = D.create(), t2; - String r; - - s.trimEmptyCollections(false); - r = s.build().serialize(t1); - assertEquals("{f1:[],f2:[null,{s2:'s2'}]}", r); - t2 = p.parse(r, D.class); - assertEqualObjects(t1, t2); - - s.trimEmptyCollections(true); - r = s.build().serialize(t1); - assertEquals("{f2:[null,{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 { - JsonSerializer s = JsonSerializer.DEFAULT_LAX; - E1 t = new E1(); - String r; - - r = s.serialize(t); - assertEquals("{x1:{f1:1},x2:{f1:1},x3:[{f1:1}],x4:[{f1:1}],x5:[{f1:1}],x6:[{f1:1}]}", r); - r = s.getSchemaSerializer().serialize(t); - assertTrue(r.indexOf("f2") == -1); - } - - public static class E1 { - @BeanProperty(properties="f1") public E2 x1 = new E2(); - @BeanProperty(properties="f1") public Map<String,Integer> x2 = new AMap<String,Integer>().append("f1",1).append("f2",2); - @BeanProperty(properties="f1") public E2[] x3 = {new E2()}; - @BeanProperty(properties="f1") public List<E2> x4 = new AList<E2>().append(new E2()); - @BeanProperty(properties="f1") public ObjectMap[] x5 = {new ObjectMap().append("f1",1).append("f2",2)}; - @BeanProperty(properties="f1") public List<ObjectMap> x6 = new AList<ObjectMap>().append(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 testBeanPropertyProperiesOnListOfBeans() throws Exception { - JsonSerializer s = JsonSerializer.DEFAULT_LAX; - List<F> l = new LinkedList<F>(); - F t = new F(); - t.x1.add(new F()); - l.add(t); - String json = s.serialize(l); - assertEquals("[{x1:[{x2:2}],x2:2}]", json); - } - - public static class F { - @BeanProperty(properties="x2") public List<F> x1 = new LinkedList<F>(); - public int x2 = 2; - } - - //==================================================================================================== - // Test that URLs and URIs are serialized and parsed correctly. - //==================================================================================================== - @Test - public void testURIAttr() throws Exception { - JsonSerializer s = JsonSerializer.DEFAULT_LAX; - JsonParser p = JsonParser.DEFAULT; - - G t = new G(); - t.uri = new URI("http://uri"); - t.f1 = new URI("http://f1"); - t.f2 = new URL("http://f2"); - - String json = s.serialize(t); - t = p.parse(json, G.class); - assertEquals("http://uri", t.uri.toString()); - assertEquals("http://f1", t.f1.toString()); - assertEquals("http://f2", t.f2.toString()); - } - - public static class G { - public URI uri; - public URI f1; - public URL f2; - } - - - //==================================================================================================== - // Recursion - //==================================================================================================== - @Test - public void testRecursion() throws Exception { - JsonSerializerBuilder s = new JsonSerializerBuilder().simple(); - - 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.build().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.detectRecursions(true); - try { - s.build().serialize(r1); - fail("Exception expected!"); - } catch (Exception e) { - String msg = e.getLocalizedMessage(); - assertTrue(msg.contains("[0]root:org.apache.juneau.json.CommonTest$R1")); - assertTrue(msg.contains("->[1]r2:org.apache.juneau.json.CommonTest$R2")); - assertTrue(msg.contains("->[2]r3:org.apache.juneau.json.CommonTest$R3")); - assertTrue(msg.contains("->[3]r1:org.apache.juneau.json.CommonTest$R1")); - } - - s.ignoreRecursions(true); - assertEquals("{name:'foo',r2:{name:'bar',r3:{name:'baz'}}}", s.build().serialize(r1)); - - // Make sure this doesn't blow up. - s.build().getSchemaSerializer().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; - } - - //==================================================================================================== - // Basic bean - //==================================================================================================== - @Test - public void testBasicBean() throws Exception { - JsonSerializer s = new JsonSerializerBuilder().simple().trimNullProperties(false).sortProperties(true).build(); - - J a = new J(); - a.setF1("J"); - a.setF2(100); - a.setF3(true); - assertEquals("C1", "{f1:'J',f2:100,f3:true}", s.serialize(a)); - } - - public static class J { - private String f1 = null; - private int f2 = -1; - private boolean f3 = false; - - public String getF1() { - return this.f1; - } - - public void setF1(String f1) { - this.f1 = f1; - } - - public int getF2() { - return this.f2; - } - - public void setF2(int f2) { - this.f2 = f2; - } - - public boolean isF3() { - return this.f3; - } - - public void setF3(boolean f3) { - this.f3 = f3; - } - - @Override /* Object */ - public String toString() { - return ("J(f1: " + this.getF1() + ", f2: " + this.getF2() + ")"); - } - } -}
