http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java new file mode 100755 index 0000000..c4d792f --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java @@ -0,0 +1,381 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.junit.Assert.*; + +import java.io.*; +import java.util.*; + +import javax.xml.datatype.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.annotation.Pojo; +import org.apache.juneau.json.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.transform.*; +import org.apache.juneau.transforms.*; +import org.junit.*; + +/** + * Tests designed to serialize and parse objects to make sure we end up + * with the same objects for all serializers and parsers. + */ +@SuppressWarnings({"unchecked","rawtypes","serial","javadoc"}) +public class RoundTripTransformBeansTest extends RoundTripTest { + + public RoundTripTransformBeansTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // testSwapBeans1 + //==================================================================================================== + @Test + public void testSwapBeans1() throws Exception { + Class<?>[] f = { + ByteArrayBase64Swap.class, + CalendarSwap.ISO8601DTZ.class, + DateSwap.ISO8601DTZ.class + }; + s.addPojoSwaps(f); + if (p != null) + p.addPojoSwaps(f); + A t = new A().init(); + t = roundTrip(t, A.class); + + // ByteArrayBase64Swap + assertEquals(3, t.fByte[3]); + assertNull(t.fnByte); + assertEquals(5, t.faByte[2][1]); + assertEquals(6, t.flByte.get(1)[2]); + assertNull(t.flByte.get(2)); + assertEquals(6, t.fmByte.get("bar")[2]); + assertNull(t.fmByte.get("baz")); + + // CalendarSwap + t.fCalendar.setTimeZone(TimeZone.getTimeZone("GMT")); + assertEquals(2001, t.fCalendar.get(Calendar.YEAR)); + assertEquals(01, t.fCalendar.get(Calendar.MONTH)); + assertEquals(02, t.fCalendar.get(Calendar.DATE)); + assertEquals(03, t.fCalendar.get(Calendar.HOUR)); + assertEquals(04, t.fCalendar.get(Calendar.MINUTE)); + assertEquals(05, t.fCalendar.get(Calendar.SECOND)); + + t.faCalendar[0].setTimeZone(TimeZone.getTimeZone("GMT")); + assertEquals(2001, t.faCalendar[0].get(Calendar.YEAR)); + assertEquals(01, t.faCalendar[0].get(Calendar.MONTH)); + assertEquals(02, t.faCalendar[0].get(Calendar.DATE)); + assertEquals(03, t.faCalendar[0].get(Calendar.HOUR)); + assertEquals(04, t.faCalendar[0].get(Calendar.MINUTE)); + assertEquals(05, t.faCalendar[0].get(Calendar.SECOND)); + assertNull(t.fnCalendar); + assertNull(t.fn2Calendar); + + // DateSwap + assertEquals(1000, t.fDate.getTime()); + assertNull(t.fnDate); + assertEquals(3000, t.faDate[2].getTime()); + assertEquals(4000, t.flDate.get(0).getTime()); + assertNull(t.flDate.get(1)); + assertEquals(5000, t.fmDate.get("foo").getTime()); + assertNull(t.fmDate.get("bar")); + } + + public static class A { + + // Test ByteArrayBase64Swap + public byte[] fByte; + public byte[] fnByte; + public byte[][] faByte; + public List<byte[]> flByte; + public Map<String,byte[]> fmByte; + + public GregorianCalendar fCalendar; + public GregorianCalendar fnCalendar; + public Calendar fn2Calendar; + public GregorianCalendar[] faCalendar; + + public Date fDate; + public Date fnDate; + public Date[] faDate; + public List<Date> flDate; + public Map<String,Date> fmDate; + + public A init() { + fByte = new byte[]{0,1,2,3}; + fnByte = null; + faByte = new byte[][]{{0,1},{2,3},{4,5}}; + flByte = new ArrayList<byte[]>() {{ + add(new byte[]{1,2,3}); + add(new byte[]{4,5,6}); + add(null); + }}; + fmByte = new LinkedHashMap<String,byte[]>() {{ + put("foo", new byte[]{1,2,3}); + put("bar", new byte[]{4,5,6}); + put("baz", null); + }}; + + fCalendar = new GregorianCalendar() {{ + set(2001, 01, 02, 03, 04, 05); + setTimeZone(TimeZone.getTimeZone("GMT")); + }}; + fnCalendar = null; + fn2Calendar = null; + faCalendar = new GregorianCalendar[]{ + new GregorianCalendar() {{ + set(2001, 01, 02, 03, 04, 05); + setTimeZone(TimeZone.getTimeZone("GMT")); + }} + }; + + fDate = new Date(1000); + fnDate = null; + faDate = new Date[]{ + new Date(1000), new Date(2000), new Date(3000) + }; + flDate = new ArrayList<Date>() {{ + add(new Date(4000)); + add(null); + }}; + fmDate = new LinkedHashMap<String,Date>() {{ + put("foo", new Date(5000)); + put("bar", null); + }}; + return this; + } + } + + + //==================================================================================================== + // testSwapBeans2 + //==================================================================================================== + @Test + public void testSwapBeans2() throws Exception { + Class<?>[] f = { + ByteArrayBase64Swap.class, + CalendarSwap.DateMedium.class, + DateSwap.RFC2822DT.class, + }; + s.addPojoSwaps(f); + if (p != null) + p.addPojoSwaps(f); + A t = new A().init(); + t = roundTrip(t, A.class); + + // ByteArrayBase64Swap + assertEquals(3, t.fByte[3]); + assertNull(t.fnByte); + assertEquals(5, t.faByte[2][1]); + assertEquals(6, t.flByte.get(1)[2]); + assertNull(t.flByte.get(2)); + assertEquals(6, t.fmByte.get("bar")[2]); + assertNull(t.fmByte.get("baz")); + + // CalendarSwap + t.fCalendar.setTimeZone(TimeZone.getTimeZone("GMT")); + assertEquals(2001, t.fCalendar.get(Calendar.YEAR)); + assertEquals(01, t.fCalendar.get(Calendar.MONTH)); + // Note: We lose precision on the following because of the transform type. + //assertEquals(02, b.fCalendar.get(Calendar.DATE)); + //assertEquals(03, b.fCalendar.get(Calendar.HOUR)); + //assertEquals(04, b.fCalendar.get(Calendar.MINUTE)); + //assertEquals(05, b.fCalendar.get(Calendar.SECOND)); + + t.faCalendar[0].setTimeZone(TimeZone.getTimeZone("GMT")); + assertEquals(2001, t.faCalendar[0].get(Calendar.YEAR)); + assertEquals(01, t.faCalendar[0].get(Calendar.MONTH)); + // Note: We lose precision on the following because of the transform type. + //assertEquals(02, b.faCalendar[0].get(Calendar.DATE)); + //assertEquals(03, b.faCalendar[0].get(Calendar.HOUR)); + //assertEquals(04, b.faCalendar[0].get(Calendar.MINUTE)); + //assertEquals(05, b.faCalendar[0].get(Calendar.SECOND)); + assertNull(t.fnCalendar); + assertNull(t.fn2Calendar); + + // DateSwap + assertEquals(1000, t.fDate.getTime() % 3600000); + assertNull(t.fnDate); + assertEquals(3000, t.faDate[2].getTime() % 3600000); + assertEquals(4000, t.flDate.get(0).getTime() % 3600000); + assertNull(t.flDate.get(1)); + assertEquals(5000, t.fmDate.get("foo").getTime() % 3600000); + assertNull(t.fmDate.get("bar")); + } + + //==================================================================================================== + // testSwaps - Bean.pojoSwaps annotation + //==================================================================================================== + @Test + public void testSwaps() throws Exception { + B t = new B(); + t.f1 = "bar"; + t = roundTrip(t, B.class); + + assertEquals("bar", t.f1); + } + + @Pojo(swap=BSwap.class) + public static class B { + public String f1; + } + + public static class BSwap extends StringSwap<B> { + @Override /* PojoSwap */ + public String swap(BeanSession session, B o) throws SerializeException { + return o.f1; + } + @Override /* PojoSwap */ + public B unswap(BeanSession session, String f, ClassMeta<?> hint) throws ParseException { + B b1 = new B(); + b1.f1 = f; + return b1; + } + } + + //==================================================================================================== + // testXMLGregorianCalendar - Test XMLGregorianCalendarSwap class. + //==================================================================================================== + @Test + public void testXMLGregorianCalendar() throws Exception { + + if (isValidationOnly()) + return; + + GregorianCalendar gc = new GregorianCalendar(); + XMLGregorianCalendar c = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); + + Serializer s = getSerializer().clone().addPojoSwaps(XMLGregorianCalendarSwap.class); + Parser p = getParser().clone().addPojoSwaps(XMLGregorianCalendarSwap.class); + + Object r = s.serialize(c); + XMLGregorianCalendar c2 = p.parse(r, XMLGregorianCalendar.class); + assertEquals(c, c2); + } + + //==================================================================================================== + // testSubTypeWithGenerics + //==================================================================================================== + @Test + public void testSubTypeWithGenerics() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT; + + C1 c1 = C3.create(); + String r = s.serialize(c1); + assertEquals("{\"type\":\"C3\",\"f1\":{\"f2\":\"f2\",\"f3\":3}}", r); + } + + + @Bean( + subTypeProperty="type", + subTypes={C3.class} + ) + public static interface C1<T> extends Serializable { + void setF1(T f1); + T getF1(); + } + + public abstract static class C2<T> implements C1<T> { + protected T f1; + + @Override /* C1 */ + public void setF1(T f1) { + this.f1 = f1; + } + + @Override /* C1 */ + public T getF1() { + return f1; + } + } + + @Bean(typeName="C3") + public static class C3<T> extends C2<T> { + + public static C3 create() { + C3 c3 = new C3<Object>(); + CDTO cdto = new CDTO(); + cdto.f2 = "f2"; + cdto.f3 = 3; + c3.f1 = cdto; + return c3; + } + + @Override /* C1 */ + public void setF1(T f1) { + this.f1 = f1; + } + + @Override /* C1 */ + public T getF1() { + return f1; + } + } + + public static class CDTO { + public String f2; + public int f3; + } + + + //==================================================================================================== + // Surrogate transforms + //==================================================================================================== + @Test + public void testSurrogates() throws Exception { + addPojoSwaps(D2.class); + + JsonSerializer s = new JsonSerializer.Simple().addPojoSwaps(D2.class); + JsonParser p = new JsonParser().addPojoSwaps(D2.class); + Object r; + D1 d1 = D1.create(); + + r = s.serialize(d1); + assertEquals("{f2:'f1'}", r); + + d1 = p.parse(r, D1.class); + assertEquals("f1", d1.f1); + + r = getSerializer().serialize(d1); + assertTrue(TestUtils.toString(r).contains("f2")); + + d1 = roundTrip(d1, D1.class); + } + + public static class D1 { + public String f1; + + public static D1 create() { + D1 d1 = new D1(); + d1.f1 = "f1"; + return d1; + } + } + + public static class D2 { + public String f2; + public D2(D1 d1) { + f2 = d1.f1; + } + public D2() { + } + public static D1 valueOf(D2 d2) { + D1 d1 = new D1(); + d1.f1 = d2.f2; + return d1; + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java new file mode 100755 index 0000000..1801f88 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/a/rttests/RoundTripTrimStringsTest.java @@ -0,0 +1,97 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.a.rttests; + +import static org.apache.juneau.TestUtils.*; + +import org.apache.juneau.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +/** + * Tests for the {@link SerializerContext#SERIALIZER_trimStrings} and {@link ParserContext#PARSER_trimStrings}. + */ +@SuppressWarnings("javadoc") +public class RoundTripTrimStringsTest extends RoundTripTest { + + public RoundTripTrimStringsTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + if (isValidationOnly()) + return; + Serializer s = getSerializer(); + Parser p = getParser(); + Object in, a, e; + + Serializer s2 = s.clone().setProperty(SerializerContext.SERIALIZER_trimStrings, true); + Parser p2 = p.clone().setProperty(ParserContext.PARSER_trimStrings, true); + + in = " foo bar "; + e = "foo bar"; + a = p.parse(s2.serialize(in), String.class); + assertEqualObjects(e, a); + a = p2.parse(s.serialize(in), String.class); + assertEqualObjects(e, a); + + in = new ObjectMap("{' foo ': ' bar '}"); + e = new ObjectMap("{foo:'bar'}"); + a = p.parse(s2.serialize(in), ObjectMap.class); + assertEqualObjects(e, a); + a = p2.parse(s.serialize(in), ObjectMap.class); + assertEqualObjects(e, a); + + in = new ObjectList("[' foo ', {' foo ': ' bar '}]"); + e = new ObjectList("['foo',{foo:'bar'}]"); + a = p.parse(s2.serialize(in), ObjectList.class); + assertEqualObjects(e, a); + a = p2.parse(s.serialize(in), ObjectList.class); + assertEqualObjects(e, a); + + in = new A().init1(); + e = new A().init2(); + a = p.parse(s2.serialize(in), A.class); + assertEqualObjects(e, a); + a = p2.parse(s.serialize(in), A.class); + assertEqualObjects(e, a); + } + + public static class A { + public String f1; + public String[] f2; + public ObjectList f3; + public ObjectMap f4; + + public A init1() throws Exception { + f1 = " f1 "; + f2 = new String[]{" f2a ", " f2b "}; + f3 = new ObjectList("[' f3a ',' f3b ']"); + f4 = new ObjectMap("{' foo ':' bar '}"); + return this; + } + + public A init2() throws Exception { + f1 = "f1"; + f2 = new String[]{"f2a", "f2b"}; + f3 = new ObjectList("['f3a','f3b']"); + f4 = new ObjectMap("{'foo':'bar'}"); + return this; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java b/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java new file mode 100755 index 0000000..1ca1fbd --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/csv/CsvTest.java @@ -0,0 +1,51 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.csv; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.serializer.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class CsvTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + List<A> l = new LinkedList<A>(); + l.add(new A("b1",1)); + l.add(new A("b2",2)); + + WriterSerializer s = new CsvSerializer(); + String r; + + r = s.serialize(l); + + assertEquals("b,c\nb1,1\nb2,2\n", r); + } + + public static class A { + public String b; + public int c; + + public A(String b, int c) { + this.b = b; + this.c = c; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java b/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java new file mode 100755 index 0000000..aadc082 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/dto/atom/AtomTest.java @@ -0,0 +1,201 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.dto.atom; + +import static org.apache.juneau.BeanContext.*; +import static org.apache.juneau.TestUtils.*; +import static org.apache.juneau.xml.XmlSerializerContext.*; +import static org.junit.Assert.*; +import static org.apache.juneau.dto.atom.AtomBuilder.*; + +import java.net.*; + +import org.apache.juneau.xml.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class AtomTest { + + public Feed createFeed() throws Exception { + return + feed("tag:foo.org", "Title", "2016-12-31T01:02:03-04:00") + .subtitle(text("html").text("Subtitle")) + .links( + link("alternate", "text/html", "http://foo.org/").hreflang("en"), + link("self", "application/atom+xml", "http://foo.org/feed.atom") + ) + .rights("Copyright (c) 2016, Apache Foundation") + .generator( + generator("Example Toolkit").uri("http://www.foo.org/").version("1.0") + ) + .entries( + entry("tag:foo.org", "Title", "2016-12-31T01:02:03-04:00") + .links( + link("alternate", "text/html", "http://foo.org/2005/04/02/atom"), + link("enclosure", "audio/mpeg", "http://foo.org/audio/foobar.mp3").length(1337) + ) + .published("2016-12-31T01:02:03-04:00") + .authors( + person("John Smith").uri(new URI("http://foo.org/")).email("[email protected]") + ) + .contributors( + person("John Smith"), + person("Jane Smith") + ) + .content( + content("xhtml") + .lang("en") + .base("http://foo.org/") + .text("<div><p><i>[Sample content]</i></p></div>") + ) + ); + } + + @Test + public void testNormal() throws Exception { + XmlSerializer s; + XmlParser p = XmlParser.DEFAULT; + String r; + Feed f = createFeed(), f2; + + String expected = + "<feed>\n" + +" <entry>\n" + +" <author>\n" + +" <email>[email protected]</email>\n" + +" <name>John Smith</name>\n" + +" <uri>http://foo.org/</uri>\n" + +" </author>\n" + +" <content base='http://foo.org/' lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></content>\n" + +" <contributor>\n" + +" <name>John Smith</name>\n" + +" </contributor>\n" + +" <contributor>\n" + +" <name>Jane Smith</name>\n" + +" </contributor>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>\n" + +" <published>2016-12-31T01:02:03-04:00</published>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +" </entry>\n" + +" <generator uri='http://www.foo.org/' version='1.0'>Example Toolkit</generator>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>\n" + +" <rights>Copyright (c) 2016, Apache Foundation</rights>\n" + +" <subtitle type='html'>Subtitle</subtitle>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +"</feed>\n"; + + s = new XmlSerializer.SqReadable().setProperty(XML_enableNamespaces, false).setProperty(BEAN_sortProperties, true); + r = s.serialize(f); + assertEquals(expected, r); + f2 = p.parse(r, Feed.class); + assertEqualObjects(f, f2); + } + + @Test + public void testWithNamespaces() throws Exception { + XmlSerializer s; + XmlParser p = XmlParser.DEFAULT; + String r; + Feed f = createFeed(), f2; + + String expected = + "<atom:feed xmlns='http://www.apache.org/2013/Juneau' xmlns:atom='http://www.w3.org/2005/Atom/' xmlns:xml='http://www.w3.org/XML/1998/namespace'>\n" + +" <atom:entry>\n" + +" <atom:author>\n" + +" <atom:email>[email protected]</atom:email>\n" + +" <atom:name>John Smith</atom:name>\n" + +" <atom:uri>http://foo.org/</atom:uri>\n" + +" </atom:author>\n" + +" <atom:content xml:base='http://foo.org/' xml:lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></atom:content>\n" + +" <atom:contributor>\n" + +" <atom:name>John Smith</atom:name>\n" + +" </atom:contributor>\n" + +" <atom:contributor>\n" + +" <atom:name>Jane Smith</atom:name>\n" + +" </atom:contributor>\n" + +" <atom:id>tag:foo.org</atom:id>\n" + +" <atom:link href='http://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>\n" + +" <atom:link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>\n" + +" <atom:published>2016-12-31T01:02:03-04:00</atom:published>\n" + +" <atom:title>Title</atom:title>\n" + +" <atom:updated>2016-12-31T01:02:03-04:00</atom:updated>\n" + +" </atom:entry>\n" + +" <atom:generator uri='http://www.foo.org/' version='1.0'>Example Toolkit</atom:generator>\n" + +" <atom:id>tag:foo.org</atom:id>\n" + +" <atom:link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>\n" + +" <atom:link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>\n" + +" <atom:rights>Copyright (c) 2016, Apache Foundation</atom:rights>\n" + +" <atom:subtitle type='html'>Subtitle</atom:subtitle>\n" + +" <atom:title>Title</atom:title>\n" + +" <atom:updated>2016-12-31T01:02:03-04:00</atom:updated>\n" + +"</atom:feed>\n"; + + s = new XmlSerializer.SqReadable().setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, true).setProperty(BEAN_sortProperties, true); + r = s.serialize(f); + assertEquals(expected, r); + f2 = p.parse(r, Feed.class); + assertEqualObjects(f, f2); + } + + @Test + public void testWithNamespacesWithAtomAsDefault() throws Exception { + XmlSerializer s; + XmlParser p = XmlParser.DEFAULT; + String r; + Feed f = createFeed(), f2; + + String expected = + "<feed xmlns='http://www.w3.org/2005/Atom/' xmlns:xml='http://www.w3.org/XML/1998/namespace'>\n" + +" <entry>\n" + +" <author>\n" + +" <email>[email protected]</email>\n" + +" <name>John Smith</name>\n" + +" <uri>http://foo.org/</uri>\n" + +" </author>\n" + +" <content xml:base='http://foo.org/' xml:lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></content>\n" + +" <contributor>\n" + +" <name>John Smith</name>\n" + +" </contributor>\n" + +" <contributor>\n" + +" <name>Jane Smith</name>\n" + +" </contributor>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>\n" + +" <published>2016-12-31T01:02:03-04:00</published>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +" </entry>\n" + +" <generator uri='http://www.foo.org/' version='1.0'>Example Toolkit</generator>\n" + +" <id>tag:foo.org</id>\n" + +" <link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>\n" + +" <link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>\n" + +" <rights>Copyright (c) 2016, Apache Foundation</rights>\n" + +" <subtitle type='html'>Subtitle</subtitle>\n" + +" <title>Title</title>\n" + +" <updated>2016-12-31T01:02:03-04:00</updated>\n" + +"</feed>\n"; + + s = new XmlSerializer.SqReadable().setProperty(XML_defaultNamespace, "atom").setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, true).setProperty(BEAN_sortProperties, true); + r = s.serialize(f); + assertEquals(expected, r); + f2 = p.parse(r, Feed.class); + assertEqualObjects(f, f2); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java b/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java new file mode 100755 index 0000000..90db605 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/dto/cognos/CognosXmlTest.java @@ -0,0 +1,112 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.dto.cognos; + +import static org.apache.juneau.TestUtils.*; +import static org.apache.juneau.serializer.SerializerContext.*; +import static org.apache.juneau.xml.XmlSerializerContext.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.xml.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class CognosXmlTest { + + //==================================================================================================== + // test + //==================================================================================================== + @Test + public void test() throws Exception { + String expected = "" + + "<dataset xmlns='http://developer.cognos.com/schemas/xmldata/1/'>\n" + + " <metadata>\n" + + " <item name='asOfDate' type='xs:string' length='12'/>\n" + + " <item name='rateOfReturn' type='xs:double'/>\n" + + " <item name='famAcctIndex' type='xs:string' length='3'/>\n" + + " <item name='rowID' type='xs:string' length='1'/>\n" + + " <item name='brM' type='xs:string' length='1'/>\n" + + " <item name='productLineCode' type='xs:int'/>\n" + + " </metadata>\n" + + " <data>\n" + + " <row>\n" + + " <value>Apr 26, 2002</value>\n" + + " <value>0.21006642</value>\n" + + " <value>JA1</value>\n" + + " <value>F</value>\n" + + " <value>B</value>\n" + + " <value>1</value>\n" + + " </row>\n" + + " <row>\n" + + " <value>Apr 27, 2002</value>\n" + + " <value>0.1111111</value>\n" + + " <value>BBB</value>\n" + + " <value>G</value>\n" + + " <value>B</value>\n" + + " <value>2</value>\n" + + " </row>\n" + + " </data>\n" + + "</dataset>\n"; + + List<Object> rows = new LinkedList<Object>(); + rows.add(new ObjectMap("{asOfDate:'Apr 26, 2002',rateOfReturn:0.21006642,famAcctIndex:'JA1',rowID:'F',brM:'B',productLineCode:1}")); + rows.add(new Item("Apr 27, 2002", 0.1111111, "BBB", "G", "B", 2)); + + Column[] c = { + new Column("asOfDate", "xs:string", 12), + new Column("rateOfReturn", "xs:double"), + new Column("famAcctIndex", "xs:string", 3), + new Column("rowID", "xs:string", 1), + new Column("brM", "xs:string", 1), + new Column("productLineCode", "xs:int") + }; + + XmlSerializer s = new XmlSerializer() + .setProperty(SERIALIZER_useIndentation, true) + .setProperty(SERIALIZER_quoteChar, '\'') + .setProperty(XML_defaultNamespace, "cognos") + .setProperty(XML_enableNamespaces, true) + .setProperty(XML_addNamespaceUrisToRoot, true); + + DataSet ds = new DataSet(c, rows, BeanContext.DEFAULT.createSession()); + + String out = s.serialize(ds); + + assertEquals(expected, out); + + // Make sure we can parse it back into a POJO. + DataSet ds2 = XmlParser.DEFAULT.parse(out, DataSet.class); + assertEqualObjects(ds, ds2); + } + + public static class Item { + public String asOfDate; + public double rateOfReturn; + public String famAcctIndex; + public String rowID; + public String brM; + public int productLineCode; + + public Item(String asOfDate, double rateOfReturn, String famAcctIndex, String rowID, String brM, int productLineCode) { + this.asOfDate = asOfDate; + this.rateOfReturn = rateOfReturn; + this.famAcctIndex = famAcctIndex; + this.rowID = rowID; + this.brM = brM; + this.productLineCode = productLineCode; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/dto/html/BasicHtmlSchemaTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/dto/html/BasicHtmlSchemaTest.java b/juneau-core-test/src/test/java/org/apache/juneau/dto/html/BasicHtmlSchemaTest.java new file mode 100755 index 0000000..b3ba825 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/dto/html/BasicHtmlSchemaTest.java @@ -0,0 +1,156 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.dto.html; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.dto.html5.*; +import org.apache.juneau.html.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.xml.*; +import org.junit.*; +import org.junit.runner.*; +import org.junit.runners.*; + +@RunWith(Parameterized.class) +@SuppressWarnings({"javadoc"}) +public class BasicHtmlSchemaTest { + + private static final WriterSerializer + sXmlSq = XmlSerializer.DEFAULT_SQ, + sXmlSqReadable = XmlSerializer.DEFAULT_SQ_READABLE, + sXmlNsSq = XmlSerializer.DEFAULT_NS_SQ, + sHtmlSq = HtmlSerializer.DEFAULT_SQ, + sHtmlSqReadable = HtmlSerializer.DEFAULT_SQ_READABLE; + + private static final ReaderParser + pXml = XmlParser.DEFAULT, + pHtml = HtmlParser.DEFAULT; + + private static final B btag = HtmlBuilder.b("bbb"); + + @Parameterized.Parameters + public static Collection<Object[]> getParameters() { + return Arrays.asList(new Object[][] { + { + "A-1", + HtmlBuilder.a("http://foo", "bar"), + "<a href='http://foo'>bar</a>", + "<a href='http://foo'>bar</a>\n", + "<a href='http://foo'>bar</a>", + "<a href='http://foo'>bar</a>", + "<a href='http://foo'>bar</a>\n", + }, + { + "A-2", + HtmlBuilder.a("http://foo", "bar", btag, "baz"), + "<a href='http://foo'>bar<b>bbb</b>baz</a>", + "<a href='http://foo'>bar<b>bbb</b>baz</a>\n", + "<a href='http://foo'>bar<b>bbb</b>baz</a>", + "<a href='http://foo'>bar<b>bbb</b>baz</a>", + "<a href='http://foo'>bar<b>bbb</b>baz</a>\n", + }, + }); + } + + + private String label, e1, e2, e3, e4, e5; + private Object in; + + public BasicHtmlSchemaTest(String label, Object in, String e1, String e2, String e3, String e4, String e5) throws Exception { + this.label = label; + this.in = in; + this.e1 = e1; + this.e2 = e2; + this.e3 = e3; + this.e4 = e4; + this.e5 = e5; + } + + private void testSerialize(WriterSerializer s, String expected) throws Exception { + try { + String r = s.serialize(in); + assertEquals(label + " serialize-normal failed", expected, r); + } catch (AssertionError e) { + throw e; + } catch (Exception e) { + throw new AssertionError(label + " test failed", e); + } + } + + private void testParse(WriterSerializer s, ReaderParser p, String expected) throws Exception { + try { + String r = s.serialize(in); + Object o = p.parse(r, in == null ? Object.class : in.getClass()); + r = s.serialize(o); + assertEquals(label + " parse-normal failed", expected, r); + } catch (AssertionError e) { + throw e; + } catch (Exception e) { + throw new AssertionError(label + " test failed", e); + } + } + + @Test + public void serializeXmlDefaultSq() throws Exception { + testSerialize(sXmlSq, e1); + } + + @Test + public void parseXmlDefaultSq() throws Exception { + testParse(sXmlSq, pXml, e1); + } + + @Test + public void serializeXmlDefaultSqReadable() throws Exception { + testSerialize(sXmlSqReadable, e2); + } + + @Test + public void parseXmlDefaultSqReadable() throws Exception { + testParse(sXmlSqReadable, pXml, e2); + } + + @Test + public void serializeXmlDefaultNsSq() throws Exception { + testSerialize(sXmlNsSq, e3); + } + + @Test + public void parseXmlDefaultNsSq() throws Exception { + testParse(sXmlNsSq, pXml, e3); + } + + @Test + public void serializeHtmlDefaultSq() throws Exception { + testSerialize(sHtmlSq, e4); + } + + @Test + public void parseHtmlDefaultSq() throws Exception { + testParse(sHtmlSq, pHtml, e4); + } + + @Test + public void serializeHtmlDefaultSqReadable() throws Exception { + testSerialize(sHtmlSqReadable, e5); + } + + @Test + public void parseHtmlDefaultSqReadable() throws Exception { + testParse(sHtmlSqReadable, pHtml, e5); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java b/juneau-core-test/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java new file mode 100755 index 0000000..e6883f5 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/dto/jsonschema/JsonSchemaTest.java @@ -0,0 +1,203 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.dto.jsonschema; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import java.net.*; + +import org.apache.juneau.json.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class JsonSchemaTest { + + @Test + public void testSchema1() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX_READABLE; + JsonParser p = JsonParser.DEFAULT; + String r; + Schema t, t2; + + String expected = + "{\n" + +" id: 'http://id', \n" + +" '$schema': 'http://schemaVersionUri', \n" + +" title: 'title', \n" + +" description: 'description', \n" + +" type: 'number', \n" + +" definitions: {\n" + +" definition: {\n" + +" '$ref': 'http://definition'\n" + +" }\n" + +" }, \n" + +" properties: {\n" + +" property: {\n" + +" type: 'number'\n" + +" }\n" + +" }, \n" + +" patternProperties: {\n" + +" '/pattern/': {\n" + +" type: 'number'\n" + +" }\n" + +" }, \n" + +" dependencies: {\n" + +" dependency: {\n" + +" '$ref': 'http://dependency'\n" + +" }\n" + +" }, \n" + +" items: [\n" + +" {\n" + +" type: 'number'\n" + +" }\n" + +" ], \n" + +" multipleOf: 1, \n" + +" maximum: 2, \n" + +" exclusiveMaximum: true, \n" + +" minimum: 3, \n" + +" exclusiveMinimum: true, \n" + +" maxLength: 4, \n" + +" minLength: 5, \n" + +" pattern: '/pattern/', \n" + +" additionalItems: [\n" + +" {\n" + +" type: 'number'\n" + +" }\n" + +" ], \n" + +" maxItems: 6, \n" + +" minItems: 7, \n" + +" uniqueItems: true, \n" + +" maxProperties: 8, \n" + +" minProperties: 9, \n" + +" required: [\n" + +" 'required'\n" + +" ], \n" + +" additionalProperties: {\n" + +" '$ref': 'http://additionalProperty'\n" + +" }, \n" + +" 'enum': [\n" + +" 'enum'\n" + +" ], \n" + +" allOf: [\n" + +" {\n" + +" '$ref': 'http://allOf'\n" + +" }\n" + +" ], \n" + +" anyOf: [\n" + +" {\n" + +" '$ref': 'http://anyOf'\n" + +" }\n" + +" ], \n" + +" oneOf: [\n" + +" {\n" + +" '$ref': 'http://oneOf'\n" + +" }\n" + +" ], \n" + +" not: {\n" + +" '$ref': 'http://not'\n" + +" }\n" + +"}"; + + t = getTest1(); + r = s.serialize(t); + assertEquals(expected, r); + t2 = p.parse(r, Schema.class); + assertEqualObjects(t, t2); + } + + @Test + public void testSchema2() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX_READABLE; + JsonParser p = JsonParser.DEFAULT; + String r; + Schema t, t2; + + String expected = + "{\n" + +" id: 'http://id', \n" + +" '$schema': 'http://schemaVersionUri', \n" + +" type: [\n" + +" 'string', \n" + +" 'number'\n" + +" ], \n" + +" definitions: {\n" + +" definition: {\n" + +" id: 'http://definition'\n" + +" }\n" + +" }, \n" + +" items: [\n" + +" {\n" + +" '$ref': 'http://items'\n" + +" }\n" + +" ], \n" + +" additionalItems: true, \n" + +" additionalProperties: true\n" + +"}"; + + t = getTest2(); + r = s.serialize(t); + assertEquals(expected, r); + t2 = p.parse(r, Schema.class); + assertEqualObjects(t, t2); + } + + /** Bean with simple values for each property */ + public static Schema getTest1() { + return new Schema() + .setId("http://id") + .setSchemaVersionUri("http://schemaVersionUri") + .setTitle("title") + .setDescription("description") + .setType(JsonType.NUMBER) + .addDefinition("definition", new SchemaRef("http://definition")) + .addProperties(new SchemaProperty("property", JsonType.NUMBER)) + .addPatternProperties(new SchemaProperty("/pattern/", JsonType.NUMBER)) + .addDependency("dependency", new SchemaRef("http://dependency")) + .addItems(new Schema().setType(JsonType.NUMBER)) + .setMultipleOf(1) + .setMaximum(2) + .setExclusiveMaximum(true) + .setMinimum(3) + .setExclusiveMinimum(true) + .setMaxLength(4) + .setMinLength(5) + .setPattern("/pattern/") + .addAdditionalItems(new SchemaProperty("additionalItem", JsonType.NUMBER)) + .setMaxItems(6) + .setMinItems(7) + .setUniqueItems(true) + .setMaxProperties(8) + .setMinProperties(9) + .addRequired("required") + .setAdditionalProperties(new SchemaRef("http://additionalProperty")) + .addEnum("enum") + .addAllOf(new SchemaRef("http://allOf")) + .addAnyOf(new SchemaRef("http://anyOf")) + .addOneOf(new SchemaRef("http://oneOf")) + .setNot(new SchemaRef("http://not")) + ; + } + + /** Bean with other possible property value types not covered in test1 */ + public static Schema getTest2() { + return new Schema() + .setId(URI.create("http://id")) + .setSchemaVersionUri(URI.create("http://schemaVersionUri")) + .setType(new JsonTypeArray(JsonType.STRING, JsonType.NUMBER)) + .addDefinition("definition", new Schema().setId("http://definition")) + .setItems(new SchemaArray(new SchemaRef("http://items"))) + .setAdditionalItems(Boolean.TRUE) + .setAdditionalProperties(Boolean.TRUE); + } +}
