You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2017/09/02 14:10:42 UTC
[20/51] [partial] incubator-juneau git commit: Add project
hierarchies, part 1
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java
new file mode 100755
index 0000000..36b619f
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java
@@ -0,0 +1,435 @@
+// ***************************************************************************************************************************
+// * 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.xml;
+
+import static org.apache.juneau.TestUtils.*;
+import static org.apache.juneau.xml.annotation.XmlFormat.*;
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.utils.*;
+import org.apache.juneau.xml.annotation.*;
+import org.junit.*;
+
+@SuppressWarnings({"serial","javadoc"})
+public class XmlCollapsedTest {
+
+ //====================================================================================================
+ // testBasic - @Xml.format=COLLAPSED
+ //====================================================================================================
+ @Test
+ public void testBasic() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ A t = new A();
+
+ t.f1 = new AList<String>().append("f1a").append("f1b");
+ t.f2 = new String[]{"f2a","f2b"};
+ t.f3 = new AList<String>().append("f3a").append("f3b");
+ t.f4 = new String[]{"f4a","f4b"};
+
+ String xml = s.serialize(t);
+ assertEquals("<object><f1>f1a</f1><f1>f1b</f1><f2>f2a</f2><f2>f2b</f2><xf3>f3a</xf3><xf3>f3b</xf3><xf4>f4a</xf4><xf4>f4b</xf4></object>", xml);
+ t = p.parse(xml, A.class);
+ assertEquals("f1a", t.f1.get(0));
+ assertEquals("f2a", t.f2[0]);
+ assertEquals("f3a", t.f3.get(0));
+ assertEquals("f4a", t.f4[0]);
+
+ validateXml(t, s);
+ }
+
+ public static class A {
+
+ @Xml(format=COLLAPSED)
+ public List<String> f1 = new LinkedList<String>();
+
+ @Xml(format=COLLAPSED)
+ public String[] f2 = new String[0];
+
+ @Xml(format=COLLAPSED,childName="xf3")
+ public List<String> f3 = new LinkedList<String>();
+
+ @Xml(format=COLLAPSED,childName="xf4")
+ public String[] f4 = new String[0];
+ }
+
+ //====================================================================================================
+ // testUninitializedFields - @Xml.format=COLLAPSED, uninitialized fields.
+ //====================================================================================================
+ @Test
+ public void testUninitializedFields() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ B t = new B();
+
+ t.f1 = new AList<String>().append("f1a").append("f1b");
+ t.f2 = new String[]{"f2a","f2b"};
+ t.f3 = new AList<String>().append("f3a").append("f3b");
+ t.f4 = new String[]{"f4a","f4b"};
+
+ String xml = s.serialize(t);
+ assertEquals("<object><f1>f1a</f1><f1>f1b</f1><f2>f2a</f2><f2>f2b</f2><xf3>f3a</xf3><xf3>f3b</xf3><xf4>f4a</xf4><xf4>f4b</xf4></object>", xml);
+ t = p.parse(xml, B.class);
+ assertEquals("f1a", t.f1.get(0));
+ assertEquals("f2a", t.f2[0]);
+ assertEquals("f3a", t.f3.get(0));
+ assertEquals("f4a", t.f4[0]);
+
+ validateXml(t, s);
+ }
+
+ public static class B {
+
+ @Xml(format=COLLAPSED)
+ public List<String> f1;
+
+ @Xml(format=COLLAPSED)
+ public String[] f2;
+
+ @Xml(format=COLLAPSED,childName="xf3")
+ public List<String> f3;
+
+ @Xml(format=COLLAPSED,childName="xf4")
+ public String[] f4;
+ }
+
+ //====================================================================================================
+ // testInitializedFields - @Xml.format=COLLAPSED, initialized fields.
+ //====================================================================================================
+ @Test
+ public void testInitializedFields() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ C t = new C();
+
+ t.f1 = new AList<String>().append("f1b");
+ t.f2 = new String[]{"f2b"};
+ t.f3 = new AList<String>().append("f3b");
+ t.f4 = new String[]{"f4b"};
+
+ String xml = s.serialize(t);
+ assertEquals("<object><f1>f1b</f1><f2>f2b</f2><xf3>f3b</xf3><xf4>f4b</xf4></object>", xml);
+
+ // Note that existing fields should be reused and appended to.
+ t = p.parse(xml, C.class);
+ assertEquals("f1a", t.f1.get(0));
+ assertEquals("f1b", t.f1.get(1));
+ assertEquals("f2a", t.f2[0]);
+ assertEquals("f2b", t.f2[1]);
+ assertEquals("f3a", t.f3.get(0));
+ assertEquals("f3b", t.f3.get(1));
+ assertEquals("f4a", t.f4[0]);
+ assertEquals("f4b", t.f4[1]);
+
+ validateXml(t, s);
+ }
+
+ public static class C {
+
+ @Xml(format=COLLAPSED)
+ public List<String> f1 = new AList<String>().append("f1a");
+
+ @Xml(format=COLLAPSED)
+ public String[] f2 = {"f2a"};
+
+ @Xml(format=COLLAPSED,childName="xf3")
+ public List<String> f3 = new AList<String>().append("f3a");
+
+ @Xml(format=COLLAPSED,childName="xf4")
+ public String[] f4 = {"f4a"};
+ }
+
+ //====================================================================================================
+ // testGetters - @Xml.format=COLLAPSED, getters.
+ //====================================================================================================
+ @Test
+ @SuppressWarnings("synthetic-access")
+ public void testGetters() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ D t = new D();
+
+ t.f1 = new AList<String>().append("f1a");
+ t.f2 = new String[]{"f2a"};
+ t.f3 = new AList<String>().append("f3a");
+ t.f4 = new String[]{"f4a"};
+
+ String xml = s.serialize(t);
+ assertEquals("<object><f1>f1a</f1><f2>f2a</f2><xf3>f3a</xf3><xf4>f4a</xf4></object>", xml);
+
+ // Note that existing fields should be reused and appended to.
+ t = p.parse(xml, D.class);
+ assertEquals("f1a", t.f1.get(0));
+ assertEquals("f2a", t.f2[0]);
+ assertEquals("f3a", t.f3.get(0));
+ assertEquals("f4a", t.f4[0]);
+
+ validateXml(t, s);
+ }
+
+ @Bean(properties="f1,f2,f3,f4")
+ public static class D {
+
+ private List<String> f1 = new LinkedList<String>(), f3 = new LinkedList<String>();
+ private String[] f2, f4;
+
+ @Xml(format=COLLAPSED)
+ public List<String> getF1() {
+ return f1;
+ }
+
+ @Xml(format=COLLAPSED)
+ public String[] getF2() {
+ return f2;
+ }
+ public void setF2(String[] f2) {
+ this.f2 = f2;
+ }
+
+ @Xml(format=COLLAPSED,childName="xf3")
+ public List<String> getF3() {
+ return f3;
+ }
+
+ @Xml(format=COLLAPSED,childName="xf4")
+ public String[] getF4() {
+ return f4;
+ }
+ public void setF4(String[] f4) {
+ this.f4 = f4;
+ }
+ }
+
+ //====================================================================================================
+ // testNullConstructibleCollectionFields - @Xml.format=COLLAPSED, null constructible collection fields.
+ //====================================================================================================
+ @Test
+ @SuppressWarnings("synthetic-access")
+ public void testNullConstructibleCollectionFields() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ E t = new E();
+
+ t.f1 = new AList<String>().append("f1a");
+ t.f2 = new AList<String>().append("f2a");
+
+ String xml = s.serialize(t);
+ assertEquals("<object><f1>f1a</f1><xf2>f2a</xf2></object>", xml);
+
+ // Note that existing fields should be reused and appended to.
+ t = p.parse(xml, E.class);
+ assertEquals("f1a", t.f1.get(0));
+ assertEquals("f2a", t.f2.get(0));
+
+ validateXml(t, s);
+ }
+
+ @Bean(properties="f1,f2")
+ public static class E {
+
+ private LinkedList<String> f1, f2;
+
+ @Xml(format=COLLAPSED)
+ public LinkedList<String> getF1() {
+ return f1;
+ }
+ public void setF1(LinkedList<String> f1) {
+ this.f1 = f1;
+ }
+
+ @Xml(format=COLLAPSED,childName="xf2")
+ public LinkedList<String> getF2() {
+ return f2;
+ }
+ public void setF2(LinkedList<String> f2) {
+ this.f2 = f2;
+ }
+ }
+
+
+ //====================================================================================================
+ // testElementNameOnElementClass - @Xml.format=COLLAPSED, element name defined on element class.
+ //====================================================================================================
+ @Test
+ public void testElementNameOnElementClass() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ Object t1 = FA.newInstance(), t2;
+ String r;
+
+ r = s.serialize(t1);
+ assertEquals("<object><xf1>x1</xf1><xf1>x2</xf1></object>", r);
+ t2 = p.parse(r, FA.class);
+ assertEqualObjects(t1, t2);
+ validateXml(t1, s);
+
+ t1 = FB.newInstance();
+ r = s.serialize(t1);
+ assertEquals("<object><xf1>x1</xf1><xf1>x2</xf1></object>", r);
+ t2 = p.parse(r, FB.class);
+ assertEqualObjects(t1, t2);
+ validateXml(t1, s);
+ }
+
+ public static class FA {
+
+ @Xml(format=COLLAPSED)
+ public List<F1> f1;
+
+ public static FA newInstance() {
+ FA t = new FA();
+ t.f1 = new LinkedList<F1>();
+ t.f1.add(F1.newInstance("x1"));
+ t.f1.add(F1.newInstance("x2"));
+ return t;
+ }
+ }
+
+ public static class FB {
+ @Xml(format=COLLAPSED)
+ public F1[] f1;
+
+ public static FB newInstance() {
+ FB t = new FB();
+ t.f1 = new F1[]{
+ F1.newInstance("x1"),
+ F1.newInstance("x2")
+ };
+ return t;
+ }
+ }
+
+ @Bean(typeName="xf1")
+ public static class F1 {
+
+ @Xml(format=TEXT)
+ public String text;
+
+ public static F1 newInstance(String text) {
+ F1 t = new F1();
+ t.text = text;
+ return t;
+ }
+ }
+
+
+ //====================================================================================================
+ // testElementNameOnElementClassOverridden - @Xml.format=COLLAPSED, element name defined on element class,
+ // but overridden by @Xml.childName on property.
+ //====================================================================================================
+ @Test
+ public void testElementNameOnElementClassOverridden() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ G t = G.newInstance(), t2;
+
+ String xml = s.serialize(t);
+ assertEquals("<object><yf1>x1</yf1><yf1>x2</yf1></object>", xml);
+
+ // Note that existing fields should be reused and appended to.
+ t2 = p.parse(xml, G.class);
+ assertEqualObjects(t, t2);
+
+ validateXml(t, s);
+ }
+
+ public static class G {
+
+ @Xml(format=COLLAPSED, childName="yf1")
+ public List<F1> f1;
+
+ public static G newInstance() {
+ G t = new G();
+ t.f1 = new LinkedList<F1>();
+ t.f1.add(F1.newInstance("x1"));
+ t.f1.add(F1.newInstance("x2"));
+ return t;
+ }
+ }
+
+
+ //====================================================================================================
+ // testElementNameOnCollectionClass - @Xml.format=COLLAPSED, element name defined on bean class.
+ //====================================================================================================
+ @Test
+ public void testElementNameOnCollectionClass() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ H t = H.newInstance(), t2;
+
+ String xml = s.serialize(t);
+ assertEquals("<object><xf1>x1</xf1><xf1>x2</xf1></object>", xml);
+
+ // Note that existing fields should be reused and appended to.
+ t2 = p.parse(xml, H.class);
+ assertEqualObjects(t, t2);
+
+ validateXml(t, s);
+ }
+
+ public static class H {
+
+ @Xml(format=COLLAPSED)
+ public H1 f1;
+
+ public static H newInstance() {
+ H t = new H();
+ t.f1 = new H1();
+ t.f1.add("x1");
+ t.f1.add("x2");
+ return t;
+ }
+ }
+
+ @Xml(childName="xf1")
+ public static class H1 extends LinkedList<String> {
+ }
+
+
+ //====================================================================================================
+ // testElementNameOnCollectionClassOverridden - @Xml.format=COLLAPSED, element name defined on element class,
+ // but overridden by @Xml.childName on property.
+ //====================================================================================================
+ @Test
+ public void testElementNameOnCollectionClassOverridden() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ G t = G.newInstance(), t2;
+
+ String xml = s.serialize(t);
+ assertEquals("<object><yf1>x1</yf1><yf1>x2</yf1></object>", xml);
+
+ // Note that existing fields should be reused and appended to.
+ t2 = p.parse(xml, G.class);
+ assertEqualObjects(t, t2);
+
+ validateXml(t, s);
+ }
+
+ public static class I {
+
+ @Xml(format=COLLAPSED, childName="yf1")
+ public H1 f1;
+
+ public static I newInstance() {
+ I t = new I();
+ t.f1 = new H1();
+ t.f1.add("x1");
+ t.f1.add("x2");
+ return t;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java
new file mode 100755
index 0000000..21f778a
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java
@@ -0,0 +1,261 @@
+// ***************************************************************************************************************************
+// * 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.xml;
+
+import static org.apache.juneau.TestUtils.*;
+import static org.apache.juneau.serializer.SerializerContext.*;
+import static org.apache.juneau.xml.annotation.XmlFormat.*;
+import static org.junit.Assert.*;
+
+import java.io.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.serializer.*;
+import org.apache.juneau.xml.annotation.*;
+import org.junit.*;
+
+@SuppressWarnings("javadoc")
+public class XmlContentTest {
+
+ //--------------------------------------------------------------------------------
+ // Test beans with @Xml(format=CONTENT)
+ //--------------------------------------------------------------------------------
+ @Test
+ public void testContentFormat() throws Exception {
+ A t = A.newInstance(), t2;
+ XmlSerializer s1 = XmlSerializer.DEFAULT_SQ,
+ s2 = new XmlSerializerBuilder().sq().ws().enableNamespaces(false).build();
+ XmlParser p = XmlParser.DEFAULT;
+ WriterSerializerSession session;
+ String r;
+ StringWriter sw;
+
+ //----------------------------------------------------------------
+ // Null
+ //----------------------------------------------------------------
+ t.f2 = null;
+
+ sw = new StringWriter();
+ session = s1.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null));
+ session.serialize(sw, t);
+ r = sw.toString();
+ assertEquals("<A f1='f1'>_x0000_</A>", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ sw = new StringWriter();
+ session = s2.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null));
+ session.serialize(sw, t);
+ r = sw.toString();
+ assertEquals("<A f1='f1'>_x0000_</A>\n", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // Normal text
+ //----------------------------------------------------------------
+ t.f2 = "foobar";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'>foobar</A>", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'>foobar</A>\n", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // Special characters
+ //----------------------------------------------------------------
+ t.f2 = "~!@#$%^&*()_+`-={}|[]\\:\";'<>?,.\n\r\t\b";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'>~!@#$%^&*()_+`-={}|[]\\:\";'<>?,.

	_x0008_</A>", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'>~!@#$%^&*()_+`-={}|[]\\:\";'<>?,.

	_x0008_</A>\n", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // Leading spaces
+ //----------------------------------------------------------------
+ t.f2 = " foobar";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'>_x0020_ foobar</A>", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'>_x0020_ foobar</A>\n", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // Trailing spaces
+ //----------------------------------------------------------------
+ t.f2 = "foobar ";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'>foobar _x0020_</A>", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'>foobar _x0020_</A>\n", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+ }
+
+ @Bean(typeName="A")
+ public static class A {
+ @Xml(format=ATTR) public String f1;
+ @Xml(format=TEXT) public String f2;
+
+ public static A newInstance() {
+ A t = new A();
+ t.f1 = "f1";
+ t.f2 = null;
+ return t;
+ }
+ }
+
+ //--------------------------------------------------------------------------------
+ // Test beans with @Xml(format=MIXED)
+ //--------------------------------------------------------------------------------
+ @Test
+ public void testXmlMixed() throws Exception {
+ B t = B.newInstance(), t2;
+ XmlSerializer s1 = XmlSerializer.DEFAULT_SQ,
+ s2 = new XmlSerializerBuilder().sq().ws().enableNamespaces(false).build();
+ XmlParser p = XmlParser.DEFAULT;
+ WriterSerializerSession session;
+ String r;
+ StringWriter sw;
+
+ //----------------------------------------------------------------
+ // Null
+ //----------------------------------------------------------------
+ t.f2 = null;
+
+ sw = new StringWriter();
+ session = s1.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null));
+ session.serialize(sw, t);
+ r = sw.toString();
+ assertEquals("<A f1='f1'>_x0000_</A>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ sw = new StringWriter();
+ session = s2.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null));
+ session.serialize(sw, t);
+ r = sw.toString();
+ assertEquals("<A f1='f1'>_x0000_</A>\n", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // Normal text
+ //----------------------------------------------------------------
+ t.f2 = "foobar";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'>foobar</A>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'>foobar</A>\n", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // Normal XML
+ //----------------------------------------------------------------
+ t.f2 = "<xxx>foobar<yyy>baz</yyy>foobar</xxx>";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'><xxx>foobar<yyy>baz</yyy>foobar</xxx></A>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'><xxx>foobar<yyy>baz</yyy>foobar</xxx></A>\n", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // Normal XML with leading and trailing space
+ //----------------------------------------------------------------
+ t.f2 = " <xxx>foobar<yyy>baz</yyy>foobar</xxx> ";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'>_x0020_ <xxx>foobar<yyy>baz</yyy>foobar</xxx> _x0020_</A>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'>_x0020_ <xxx>foobar<yyy>baz</yyy>foobar</xxx> _x0020_</A>\n", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // XML with attributes
+ //----------------------------------------------------------------
+ t.f2 = "<xxx x=\"x\">foobar</xxx>";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'><xxx x=\"x\">foobar</xxx></A>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'><xxx x=\"x\">foobar</xxx></A>\n", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ //----------------------------------------------------------------
+ // XML with embedded entities
+ //----------------------------------------------------------------
+ t.f2 = "<xxx x=\"x\">foo<>bar</xxx>";
+
+ r = s1.serialize(t);
+ assertEquals("<A f1='f1'><xxx x=\"x\">foo&lt;&gt;bar</xxx></A>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'><xxx x=\"x\">foo&lt;&gt;bar</xxx></A>\n", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+ }
+
+ @Bean(typeName="A")
+ public static class B {
+ @Xml(format=ATTR) public String f1;
+ @Xml(format=TEXT) public String f2;
+
+ public static B newInstance() {
+ B t = new B();
+ t.f1 = "f1";
+ t.f2 = null;
+ return t;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlParserTest.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlParserTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlParserTest.java
new file mode 100755
index 0000000..9e1f571
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlParserTest.java
@@ -0,0 +1,95 @@
+// ***************************************************************************************************************************
+// * 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.xml;
+
+import static org.junit.Assert.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.parser.*;
+import org.junit.*;
+
+@SuppressWarnings("javadoc")
+public class XmlParserTest {
+
+ @Test
+ public void testGenericAttributes() throws Exception {
+ String xml = "<A b='1'><c>2</c></A>";
+ ObjectMap m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{b:'1',c:'2'}", m.toString());
+ }
+
+ @Test
+ public void testGenericWithChildElements() throws Exception {
+ String xml;
+ ObjectMap m;
+
+ xml = "<A><B><C>c</C></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{C:'c'}}", m.toString());
+
+ xml = "<A><B><C1>c1</C1><C2>c2</C2></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{C1:'c1',C2:'c2'}}", m.toString());
+
+ xml = "<A><B><C><D1>d1</D1><D2>d2</D2></C></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{C:{D1:'d1',D2:'d2'}}}", m.toString());
+
+ xml = "<A><B><C><D1 d1a='d1av'><E1>e1</E1></D1><D2 d2a='d2av'><E2>e2</E2></D2></C></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{C:{D1:{d1a:'d1av',E1:'e1'},D2:{d2a:'d2av',E2:'e2'}}}}", m.toString());
+
+ xml = "<A><B b='b'><C>c</C></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{b:'b',C:'c'}}", m.toString());
+
+ xml = "<A><B b='b'>c</B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{b:'b',contents:'c'}}", m.toString());
+
+ xml = "<A><B>b1</B><B>b2</B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:['b1','b2']}", m.toString());
+
+ xml = "<A><B><C>c1</C><C>c2</C></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{C:['c1','c2']}}", m.toString());
+
+ xml = "<A><B v='v1'>b1</B><B v='v2'>b2</B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:[{v:'v1',contents:'b1'},{v:'v2',contents:'b2'}]}", m.toString());
+
+ xml = "<A><B><C v='v1'>c1</C><C v='v2'>c2</C></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{C:[{v:'v1',contents:'c1'},{v:'v2',contents:'c2'}]}}", m.toString());
+
+ xml = "<A><B c='c1'><c>c2</c></B></A>";
+ m = XmlParser.DEFAULT.parse(xml, ObjectMap.class);
+ assertEquals("{B:{c:['c1','c2']}}", m.toString());
+ }
+
+ @Test
+ public void testPreserveRootElement() throws Exception {
+ String xml;
+ ObjectMap m;
+ ReaderParser p = new XmlParserBuilder().preserveRootElement(true).build();
+
+ xml = "<A><B><C>c</C></B></A>";
+ m = p.parse(xml, ObjectMap.class);
+ assertEquals("{A:{B:{C:'c'}}}", m.toString());
+
+ xml = "<A></A>";
+ m = p.parse(xml, ObjectMap.class);
+ assertEquals("{A:{}}", m.toString());
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlTest.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlTest.java
new file mode 100755
index 0000000..ffcca5a
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlTest.java
@@ -0,0 +1,1032 @@
+// ***************************************************************************************************************************
+// * 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.xml;
+
+import static org.apache.juneau.TestUtils.*;
+import static org.apache.juneau.xml.annotation.XmlFormat.*;
+import static org.junit.Assert.*;
+
+import java.net.*;
+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.utils.*;
+import org.apache.juneau.xml.annotation.*;
+import org.apache.juneau.xml.xml1a.*;
+import org.apache.juneau.xml.xml1b.*;
+import org.apache.juneau.xml.xml1c.*;
+import org.junit.*;
+
+@SuppressWarnings({"serial","javadoc"})
+public class XmlTest {
+
+ //====================================================================================================
+ // Simple comparison test with JSON serializer
+ //====================================================================================================
+ @Test
+ public void testComparisonWithJson() throws Exception {
+
+ String json1 =
+ "{\n"
+ +" name: \"John Smith\",\n"
+ +" address: {\n"
+ +" streetAddress: \"21 2nd Street\",\n"
+ +" city: \"New York\",\n"
+ +" state: \"NY\",\n"
+ +" postalCode: 10021\n"
+ +" },\n"
+ +" phoneNumbers: [\n"
+ +" \"212 555-1111\",\n"
+ +" \"212 555-2222\"\n"
+ +" ],\n"
+ +" additionalInfo: null,\n"
+ +" remote: false,\n"
+ +" height: 62.4,\n"
+ +" \"fico score\": \" > 640\"\n"
+ +"}";
+
+ String xml1 =
+ "<object>\n"
+ +" <name>John Smith</name>\n"
+ +" <address _type='object'>\n"
+ +" <streetAddress>21 2nd Street</streetAddress>\n"
+ +" <city>New York</city>\n"
+ +" <state>NY</state>\n"
+ +" <postalCode _type='number'>10021</postalCode>\n"
+ +" </address>\n"
+ +" <phoneNumbers _type='array'>\n"
+ +" <string>212 555-1111</string>\n"
+ +" <string>212 555-2222</string>\n"
+ +" </phoneNumbers>\n"
+ +" <additionalInfo _type='null'/>\n"
+ +" <remote _type='boolean'>false</remote>\n"
+ +" <height _type='number'>62.4</height>\n"
+ +" <fico_x0020_score>_x0020_> 640</fico_x0020_score>\n"
+ +"</object>\n";
+
+ ObjectMap m = (ObjectMap) XmlParser.DEFAULT.parse(xml1, Object.class);
+ String json2 = new JsonSerializerBuilder().simple().ws().quoteChar('"').trimNullProperties(false).build().serialize(m);
+ assertEquals(json1, json2);
+
+ m = (ObjectMap) JsonParser.DEFAULT.parse(json1, Object.class);
+ String xml2 = new XmlSerializerBuilder().sq().ws()
+ .trimNullProperties(false)
+ .build()
+ .serialize(m);
+ assertEquals(xml1, xml2);
+ }
+
+ //====================================================================================================
+ // Test namespacing
+ //====================================================================================================
+ @Test
+ public void testNamespaces() throws Exception {
+
+ String json1 =
+ "{\n"
+ +" name: \"John Smith\", \n"
+ +" address: {\n"
+ +" streetAddress: \"21 2nd Street\", \n"
+ +" city: \"New York\", \n"
+ +" state: \"NY\", \n"
+ +" postalCode: 10021\n"
+ +" }, \n"
+ +" phoneNumbers: [\n"
+ +" \"212 555-1111\", \n"
+ +" \"212 555-2222\"\n"
+ +" ], \n"
+ +" additionalInfo: null, \n"
+ +" remote: false, \n"
+ +" height: 62.4, \n"
+ +" \"fico score\": \" > 640\"\n"
+ +"}";
+
+ String xml1 =
+ "<object xmlns='http://www.apache.org'>\n"
+ +" <name>John Smith</name>\n"
+ +" <address _type='object'>\n"
+ +" <streetAddress>21 2nd Street</streetAddress>\n"
+ +" <city>New York</city>\n"
+ +" <state>NY</state>\n"
+ +" <postalCode _type='number'>10021</postalCode>\n"
+ +" </address>\n"
+ +" <phoneNumbers _type='array'>\n"
+ +" <string>212 555-1111</string>\n"
+ +" <string>212 555-2222</string>\n"
+ +" </phoneNumbers>\n"
+ +" <additionalInfo _type='null'/>\n"
+ +" <remote _type='boolean'>false</remote>\n"
+ +" <height _type='number'>62.4</height>\n"
+ +" <fico_x0020_score>_x0020_> 640</fico_x0020_score>\n"
+ +"</object>\n";
+
+ ObjectMap m = (ObjectMap) JsonParser.DEFAULT.parse(json1, Object.class);
+ String r = new XmlSerializerBuilder().ns().sq().ws()
+ .addNamespaceUrisToRoot(true)
+ .defaultNamespace("http://www.apache.org")
+ .trimNullProperties(false)
+ .build()
+ .serialize(m);
+ assertEquals(xml1, r);
+ }
+
+ //====================================================================================================
+ // Test bean name annotation
+ //====================================================================================================
+ @Test
+ public void testBeanNameAnnotation() throws Exception {
+ String e =
+ "<Person1>\n"
+ + " <name>John Smith</name>\n"
+ + " <age>123</age>\n"
+ + "</Person1>\n";
+ String r = XmlSerializer.DEFAULT_SQ_READABLE.serialize(new Person1("John Smith", 123));
+ assertEquals(e, r);
+ }
+
+ /** Class with explicitly specified properties */
+ @Bean(typeName="Person1", properties="name,age")
+ public static class Person1 {
+ public int age;
+ private String name;
+ protected Person1(String name, int age) {
+ this.name = name;
+ this.age = age;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+
+ //====================================================================================================
+ // Test trimNulls property.
+ //====================================================================================================
+ @Test
+ public void testTrimNulls() throws Exception {
+ String e =
+ "<Person1>\n"
+ + " <age>123</age>\n"
+ + "</Person1>\n";
+ String r = XmlSerializer.DEFAULT_SQ_READABLE.serialize(new Person1(null, 123));
+ assertEquals(e, r);
+ }
+
+ //====================================================================================================
+ // Element name.
+ //====================================================================================================
+ @Test
+ public void testElementName() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ A t = new A();
+ String r = s.serialize(t);
+ assertEquals("<foo><f1>1</f1></foo>", r);
+ validateXml(t);
+ }
+
+ @Bean(typeName="foo")
+ public static class A {
+ public int f1 = 1;
+ }
+
+ //====================================================================================================
+ // Element name on superclass.
+ //====================================================================================================
+ @Test
+ public void testElementNameOnSuperclass() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ B2 t = new B2();
+ String r = s.serialize(t);
+ assertEquals("<foo><f1>1</f1></foo>", r);
+ validateXml(t);
+ }
+
+ public static class B1 extends A {}
+ public static class B2 extends B1 {}
+
+ //====================================================================================================
+ // Element name on interface.
+ //====================================================================================================
+ @Test
+ public void testElementNameOnInterface() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ C3 t = new C3();
+ String r = s.serialize(t);
+ assertEquals("<foo><f1>1</f1></foo>", r);
+ validateXml(t);
+ }
+
+ @Bean(typeName="foo")
+ public static interface C1 {}
+ public static class C2 implements C1 {}
+ public static class C3 extends C2 {
+ public int f1 = 1;
+ }
+
+ //====================================================================================================
+ // Element name with invalid XML characters.
+ //====================================================================================================
+ @Test
+ public void testElementNameWithInvalidChars() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ D t = new D();
+ String r = s.serialize(t);
+ assertEquals("<_x007E__x0021__x0040__x0023__x0024__x0025__x005E__x0026__x002A__x0028__x0029___x002B__x0060_-_x003D__x007B__x007D__x007C__x005B__x005D__x005C__x003A__x0022__x003B__x0027__x003C__x003E__x003F__x002C_._x000A__x000D__x0009__x0008_><f1>1</f1></_x007E__x0021__x0040__x0023__x0024__x0025__x005E__x0026__x002A__x0028__x0029___x002B__x0060_-_x003D__x007B__x007D__x007C__x005B__x005D__x005C__x003A__x0022__x003B__x0027__x003C__x003E__x003F__x002C_._x000A__x000D__x0009__x0008_>", r);
+ t = p.parse(r, D.class);
+ validateXml(t);
+ }
+
+ @Bean(typeName="~!@#$%^&*()_+`-={}|[]\\:\";'<>?,.\n\r\t\b")
+ public static class D {
+ public int f1 = 1;
+ }
+
+ //====================================================================================================
+ // Field of type collection with element name.
+ // Element name should be ignored.
+ //====================================================================================================
+ @Test
+ public void testIgnoreCollectionFieldWithElementName() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ G t = new G();
+ t.f1.add("bar");
+ String r = s.serialize(t);
+ assertEquals("<bar><f1><string>bar</string></f1></bar>", r);
+ t = p.parse(r, G.class);
+ validateXml(t);
+ }
+
+ @Bean(typeName="foo")
+ public static class F extends LinkedList<String>{}
+
+ @Bean(typeName="bar")
+ public static class G {
+ public F f1 = new F();
+ }
+
+ //====================================================================================================
+ // Element name on beans of a collection.
+ //====================================================================================================
+ @Test
+ public void testElementNameOnBeansOfCollection() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ Object o = new J1();
+ String r = s.serialize(o);
+ assertEquals("<foo><f1><bar><f2>2</f2></bar></f1></foo>", r);
+ }
+
+ @Bean(typeName="foo")
+ public static class J1 {
+ @BeanProperty(properties="f2") public List<J2> f1 = new AList<J2>().append(new J2());
+ }
+
+ @Bean(typeName="bar")
+ public static class J2 {
+ public int f2 = 2;
+ public int f3 = 3;
+ }
+
+ //====================================================================================================
+ // @Xml.ns without matching nsUri.
+ //====================================================================================================
+ @Test
+ public void testXmlNsWithoutMatchingNsUri() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ K t = new K();
+ try {
+ s.serialize(t);
+ fail("Exception not thrown");
+ } catch (Exception e) {
+ assertEquals("Found @Xml.prefix annotation with no matching URI. prefix='foo'", e.getLocalizedMessage());
+ }
+ }
+
+ @Xml(prefix="foo")
+ public static class K {
+ public int f1;
+ }
+
+ //====================================================================================================
+ // @Xml.format=ATTR.
+ //====================================================================================================
+ @Test
+ public void testXmlFormatAttr() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ L t = new L();
+ String r = s.serialize(t);
+ assertEquals("<object f2='2'><f1>1</f1><f3>3</f3></object>", r);
+ t.f1 = 4; t.f2 = 5; t.f3 = 6;
+ t = p.parse(s.serialize(t), L.class);
+ assertEquals(4, t.f1);
+ assertEquals(5, t.f2);
+ assertEquals(6, t.f3);
+ validateXml(t);
+ }
+
+ public static class L {
+ public int f1 = 1;
+ @Xml(format=ATTR)
+ public int f2 = 2;
+ public int f3 = 3;
+ }
+
+ //====================================================================================================
+ // @Xml.format=ATTR with namespaces.
+ //====================================================================================================
+ @Test
+ public void testXmlFormatAttrWithNs() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq();
+ XmlParser p = XmlParser.DEFAULT;
+ M t = new M();
+ String r = null;
+ r = s.build().serialize(t);
+ assertEquals("<object f1='1' f2='2' f3='3'/>", r);
+ s.enableNamespaces(true).addNamespaceUrisToRoot(true).autoDetectNamespaces(true).trimNullProperties(false);
+ t.f1 = 4; t.f2 = 5; t.f3 = 6;
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:bar='http://bar' xmlns:foo='http://foo' xmlns:baz='http://baz' bar:f1='4' foo:f2='5' baz:f3='6'/>", r);
+ t = p.parse(r, M.class);
+ assertEquals(4, t.f1);
+ assertEquals(5, t.f2);
+ assertEquals(6, t.f3);
+ validateXml(t, s.build());
+ }
+
+ @Xml(prefix="bar", namespace="http://bar")
+ public static class M {
+ @Xml(format=ATTR)
+ public int f1 = 1;
+ @Xml(prefix="foo", format=ATTR, namespace="http://foo")
+ public int f2 = 2;
+ @Xml(prefix="baz", namespace="http://baz", format=ATTR)
+ public int f3 = 3;
+ }
+
+ //====================================================================================================
+ // _xXXXX_ notation.
+ //====================================================================================================
+ @Test
+ public void testXXXXNotation() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ String in, r;
+
+ in = "\u0001";
+ r = s.serialize(in);
+ assertEquals("<string>_x0001_</string>", r);
+ in = p.parse(r, String.class);
+ assertEquals("\u0001", in);
+
+ in = "_x0001_";
+ r = s.serialize(in);
+ assertEquals("<string>_x005F_x0001_</string>", r);
+ in = p.parse(r, String.class);
+ assertEquals("_x0001_", in);
+
+ in = "_x001_";
+ r = s.serialize(in);
+ assertEquals("<string>_x001_</string>", r);
+ in = p.parse(r, String.class);
+ assertEquals("_x001_", in);
+
+ in = "_x00001_";
+ r = s.serialize(in);
+ assertEquals("<string>_x00001_</string>", r);
+ in = p.parse(r, String.class);
+ assertEquals("_x00001_", in);
+
+ in = "_xx001_";
+ r = s.serialize(in);
+ assertEquals("<string>_xx001_</string>", r);
+ in = p.parse(r, String.class);
+ assertEquals("_xx001_", in);
+ }
+
+ //====================================================================================================
+ // @Bean.uri annotation formatted as element
+ //====================================================================================================
+ @Test
+ public void testBeanUriAnnotationFormattedAsElement() throws Exception {
+ XmlParser p = XmlParser.DEFAULT;
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+
+ N t = new N("http://foo",123, "bar");
+ String r = s.serialize(t);
+ assertEquals("<object><url>http://foo</url><id>123</id><name>bar</name></object>", r);
+
+ t = p.parse(r, N.class);
+ assertEquals("http://foo", t.url.toString());
+ assertEquals(123, t.id);
+ assertEquals("bar", t.name);
+
+ validateXml(t, s);
+ }
+
+ public static class N {
+ @Rdf(beanUri=true) @Xml(format=ELEMENT) public URL url;
+ public int id;
+ public String name;
+ public N() {}
+ public N(String url, int id, String name) throws Exception {
+ this.url = new URL(url);
+ this.id = id;
+ this.name = name;
+ }
+ }
+
+ //====================================================================================================
+ // @Bean.uri as elements, overridden element names
+ //====================================================================================================
+ @Test
+ public void testOverriddenBeanUriAsElementNames() throws Exception {
+ XmlParser p = XmlParser.DEFAULT;
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+
+ O t = new O("http://foo", 123, "bar");
+ String r = s.serialize(t);
+ assertEquals("<object><url2>http://foo</url2><id2>123</id2><name>bar</name></object>", r);
+
+ t = p.parse(r, O.class);
+ assertEquals("http://foo", t.url.toString());
+ assertEquals(123, t.id);
+ assertEquals("bar", t.name);
+
+ validateXml(t, s);
+ }
+
+ public static class O {
+ @BeanProperty(name="url2") @Xml(format=ELEMENT) public URL url;
+ @BeanProperty(name="id2") public int id;
+ public String name;
+ public O() {}
+ public O(String url, int id, String name) throws Exception {
+ this.url = new URL(url);
+ this.id = id;
+ this.name = name;
+ }
+ }
+
+ //====================================================================================================
+ // @Bean.uri and @Bean.id annotations, overridden attribute names
+ //====================================================================================================
+ @Test
+ public void testOverriddenBeanUriAndIdAnnotations() throws Exception {
+ XmlParser p = XmlParser.DEFAULT;
+ XmlSerializer s = XmlSerializer.DEFAULT_SQ;
+
+ P t = new P("http://foo", 123, "bar");
+ String r = s.serialize(t);
+ assertEquals("<object url2='http://foo' id2='123'><name>bar</name></object>", r);
+
+ t = p.parse(r, P.class);
+ assertEquals("http://foo", t.url.toString());
+ assertEquals(123, t.id);
+ assertEquals("bar", t.name);
+
+ validateXml(t, s);
+ }
+
+ public static class P {
+ @BeanProperty(name="url2") @Xml(format=ATTR) public URL url;
+ @BeanProperty(name="id2") @Xml(format=ATTR) public int id;
+ public String name;
+ public P() {}
+ public P(String url, int id, String name) throws Exception {
+ this.url = new URL(url);
+ this.id = id;
+ this.name = name;
+ }
+ }
+
+ //====================================================================================================
+ // Namespace on class
+ //====================================================================================================
+ @Test
+ public void testNsOnClass() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq().autoDetectNamespaces(false);
+ XmlParser p = XmlParser.DEFAULT;
+
+ T1 t = new T1();
+ String r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T1.class)));
+
+ s.enableNamespaces(true).addNamespaceUrisToRoot(false);
+ r = s.build().serialize(t);
+ assertEquals("<object><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T1.class)));
+ validateXml(t, s.build());
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T1.class)));
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T1.class)));
+
+ s.enableNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T1.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // Namespace on class with element name.
+ //====================================================================================================
+ @Test
+ public void testNsOnClassWithElementName() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq().autoDetectNamespaces(false);
+ XmlParser p = XmlParser.DEFAULT;
+
+ T2 t = new T2();
+ String r = s.build().serialize(t);
+ assertEquals("<T2><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T2>", r);
+ assertTrue(t.equals(p.parse(r, T2.class)));
+
+ s.enableNamespaces(true).addNamespaceUrisToRoot(false);
+ r = s.build().serialize(t);
+ assertEquals("<foo:T2><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T2>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<foo:T2 xmlns='http://www.apache.org/2013/Juneau'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T2>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<foo:T2 xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T2>", r);
+ assertTrue(t.equals(p.parse(r, T2.class)));
+ validateXml(t, s.build());
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<T2><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T2>", r);
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<T2><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T2>", r);
+
+ s.enableNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<foo:T2 xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T2>", r);
+ assertTrue(t.equals(p.parse(r, T2.class)));
+ validateXml(t, s.build());
+ }
+
+
+ //====================================================================================================
+ // Namespace on package, no namespace on class.
+ //====================================================================================================
+ @Test
+ public void testNsOnPackageNoNsOnClass() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq();
+ XmlParser p = XmlParser.DEFAULT;
+
+ T3 t = new T3();
+ String r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T3.class)));
+ validateXml(t, s.build());
+
+ s.enableNamespaces(true).addNamespaceUrisToRoot(false);
+ r = s.build().serialize(t);
+ assertEquals("<object><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></object>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true).autoDetectNamespaces(false);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau'><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></object>", r);
+
+ // Manually set namespaces
+ s.autoDetectNamespaces(false);
+ s.namespaces(
+ NamespaceFactory.get("p1","http://p1"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz'><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T3.class)));
+ validateXml(t, s.build());
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+
+ s.enableNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz'><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T3.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // Namespace on package, no namespace on class, element name on class.
+ //====================================================================================================
+ @Test
+ public void testNsOnPackageNoNsOnClassElementNameOnClass() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq().autoDetectNamespaces(false);
+ XmlParser p = XmlParser.DEFAULT;
+
+ T4 t = new T4();
+ String r = s.build().serialize(t);
+ assertEquals("<T4><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T4>", r);
+ assertTrue(t.equals(p.parse(r, T4.class)));
+
+ s.enableNamespaces(true).addNamespaceUrisToRoot(false);
+ r = s.build().serialize(t);
+ assertEquals("<p1:T4><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></p1:T4>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<p1:T4 xmlns='http://www.apache.org/2013/Juneau'><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></p1:T4>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz"),
+ NamespaceFactory.get("p1","http://p1")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<p1:T4 xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:p1='http://p1'><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></p1:T4>", r);
+ assertTrue(t.equals(p.parse(r, T4.class)));
+ validateXml(t, s.build());
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<T4><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T4>", r);
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<T4><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T4>", r);
+
+ s.enableNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<p1:T4 xmlns='http://www.apache.org/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz'><p1:f1>1</p1:f1><bar:f2>2</bar:f2><p1:f3>3</p1:f3><baz:f4>4</baz:f4></p1:T4>", r);
+ assertTrue(t.equals(p.parse(r, T4.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // Namespace on package, namespace on class, element name on class.
+ //====================================================================================================
+ @Test
+ public void testNsOnPackageNsOnClassElementNameOnClass() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq();
+ XmlParser p = XmlParser.DEFAULT;
+
+ T5 t = new T5();
+ String r = s.build().serialize(t);
+ assertEquals("<T5><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T5>", r);
+ assertTrue(t.equals(p.parse(r, T5.class)));
+ validateXml(t, s.build());
+
+ s.ns().addNamespaceUrisToRoot(false).autoDetectNamespaces(false);
+ r = s.build().serialize(t);
+ assertEquals("<foo:T5><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T5>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<foo:T5 xmlns='http://www.apache.org/2013/Juneau'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T5>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<foo:T5 xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T5>", r);
+ assertTrue(t.equals(p.parse(r, T5.class)));
+ validateXml(t, s.build());
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<T5><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T5>", r);
+ validateXml(t, s.build());
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<T5><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T5>", r);
+ validateXml(t, s.build());
+
+ s.ns();
+ r = s.build().serialize(t);
+ assertEquals("<foo:T5 xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></foo:T5>", r);
+ assertTrue(t.equals(p.parse(r, T5.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // Namespace on package, namespace on class, no element name on class.
+ //====================================================================================================
+ @Test
+ public void testNsOnPackageNsOnClassNoElementNameOnClass() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq().autoDetectNamespaces(false);
+ XmlParser p = XmlParser.DEFAULT;
+
+ T6 t = new T6();
+ String r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T6.class)));
+
+ s.ns().addNamespaceUrisToRoot(false);
+ r = s.build().serialize(t);
+ assertEquals("<object><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true).autoDetectNamespaces(false);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T6.class)));
+ validateXml(t, s.build());
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ validateXml(t, s.build());
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ validateXml(t, s.build());
+
+ s.ns();
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><foo:f1>1</foo:f1><bar:f2>2</bar:f2><foo:f3>3</foo:f3><baz:f4>4</baz:f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T6.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // Combination of namespaces and overridden bean property names.
+ //====================================================================================================
+ @Test
+ public void testComboOfNsAndOverriddenBeanPropertyNames() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq().autoDetectNamespaces(false);
+ XmlParser p = XmlParser.DEFAULT;
+
+ T7 t = new T7();
+ String r = s.build().serialize(t);
+ assertEquals("<object><g1>1</g1><g2>2</g2><g3>3</g3><g4>4</g4></object>", r);
+ assertTrue(t.equals(p.parse(r, T7.class)));
+
+ s.enableNamespaces(true).addNamespaceUrisToRoot(false);
+ r = s.build().serialize(t);
+ assertEquals("<object><p1:g1>1</p1:g1><bar:g2>2</bar:g2><p1:g3>3</p1:g3><baz:g4>4</baz:g4></object>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true).autoDetectNamespaces(false);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau'><p1:g1>1</p1:g1><bar:g2>2</bar:g2><p1:g3>3</p1:g3><baz:g4>4</baz:g4></object>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz"),
+ NamespaceFactory.get("p1","http://p1")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:p1='http://p1'><p1:g1>1</p1:g1><bar:g2>2</bar:g2><p1:g3>3</p1:g3><baz:g4>4</baz:g4></object>", r);
+ assertTrue(t.equals(p.parse(r, T7.class)));
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><g1>1</g1><g2>2</g2><g3>3</g3><g4>4</g4></object>", r);
+
+ s.enableNamespaces(false);
+ r = s.build().serialize(t);
+ assertEquals("<object><g1>1</g1><g2>2</g2><g3>3</g3><g4>4</g4></object>", r);
+
+ s.ns().addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz'><p1:g1>1</p1:g1><bar:g2>2</bar:g2><p1:g3>3</p1:g3><baz:g4>4</baz:g4></object>", r);
+ assertTrue(t.equals(p.parse(r, T7.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // @XmlNs annotation
+ //====================================================================================================
+ @Test
+ public void testXmlNsAnnotation() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq().autoDetectNamespaces(false);
+ XmlParser p = XmlParser.DEFAULT;
+
+ T8 t = new T8();
+ String r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T8.class)));
+
+ s.ns().addNamespaceUrisToRoot(false).autoDetectNamespaces(false);
+ r = s.build().serialize(t);
+ assertEquals("<object><p2:f1>1</p2:f1><p1:f2>2</p1:f2><c1:f3>3</c1:f3><f1:f4>4</f1:f4></object>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau'><p2:f1>1</p2:f1><p1:f2>2</p1:f2><c1:f3>3</c1:f3><f1:f4>4</f1:f4></object>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><p2:f1>1</p2:f1><p1:f2>2</p1:f2><c1:f3>3</c1:f3><f1:f4>4</f1:f4></object>", r);
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T8.class)));
+ validateXml(t, s.build());
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r);
+ validateXml(t, s.build());
+
+ s.ns();
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:p2='http://p2' xmlns:p1='http://p1' xmlns:c1='http://c1' xmlns:f1='http://f1'><p2:f1>1</p2:f1><p1:f2>2</p1:f2><c1:f3>3</c1:f3><f1:f4>4</f1:f4></object>", r);
+ assertTrue(t.equals(p.parse(r, T8.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // @Xml.ns on package, @Xml.nsUri not on package but in @XmlNs.
+ //====================================================================================================
+ @Test
+ public void testXmlNsOnPackageNsUriInXmlNs() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq().autoDetectNamespaces(false);
+ XmlParser p = XmlParser.DEFAULT;
+
+ T9 t = new T9();
+ String r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1></object>", r);
+ assertTrue(t.equals(p.parse(r, T9.class)));
+
+ s.ns().autoDetectNamespaces(false).addNamespaceUrisToRoot(false);
+ r = s.build().serialize(t);
+ assertEquals("<object><p1:f1>1</p1:f1></object>", r);
+
+ // Add namespace URIs to root, but don't auto-detect.
+ // Only xsi should be added to root.
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau'><p1:f1>1</p1:f1></object>", r);
+
+ // Manually set namespaces
+ s.namespaces(
+ NamespaceFactory.get("foo","http://foo"),
+ NamespaceFactory.get("bar","http://bar"),
+ NamespaceFactory.get("baz","http://baz")
+ );
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz'><p1:f1>1</p1:f1></object>", r);
+
+ // Auto-detect namespaces.
+ s = new XmlSerializerBuilder().sq().autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1></object>", r);
+ assertTrue(t.equals(p.parse(r, T9.class)));
+ validateXml(t, s.build());
+
+ s.addNamespaceUrisToRoot(true);
+ r = s.build().serialize(t);
+ assertEquals("<object><f1>1</f1></object>", r);
+ validateXml(t, s.build());
+
+ s.ns();
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:p1='http://p1'><p1:f1>1</p1:f1></object>", r);
+ assertTrue(t.equals(p.parse(r, T9.class)));
+ validateXml(t, s.build());
+ }
+
+ //====================================================================================================
+ // @Xml.format=ATTR
+ //====================================================================================================
+ @Test
+ public void testXmlAttrs() throws Exception {
+ XmlSerializerBuilder s = new XmlSerializerBuilder().sq();
+ XmlParser p = XmlParser.DEFAULT;
+ String r;
+
+ Q t = new Q();
+ t.f1 = new URL("http://xf1");
+ t.f2 = "xf2";
+ t.f3 = "xf3";
+ r = s.build().serialize(t);
+ assertEquals("<object f1='http://xf1' f2='xf2' x3='xf3'/>", r);
+ t = p.parse(r, Q.class);
+ assertEquals("http://xf1", t.f1.toString());
+ assertEquals("xf2", t.f2);
+ assertEquals("xf3", t.f3);
+
+ s.ns().addNamespaceUrisToRoot(true).autoDetectNamespaces(true);
+ r = s.build().serialize(t);
+ assertEquals("<object xmlns='http://www.apache.org/2013/Juneau' xmlns:ns='http://ns' xmlns:nsf1='http://nsf1' xmlns:nsf3='http://nsf3' nsf1:f1='http://xf1' ns:f2='xf2' nsf3:x3='xf3'/>", r);
+ validateXml(t, s.build());
+
+ t = p.parse(r, Q.class);
+ assertEquals("http://xf1", t.f1.toString());
+ assertEquals("xf2", t.f2);
+ assertEquals("xf3", t.f3);
+ }
+
+ @Xml(prefix="ns", namespace="http://ns")
+ public static class Q {
+
+ @Xml(format=ATTR, prefix="nsf1", namespace="http://nsf1")
+ public URL f1;
+
+ @Xml(format=ATTR)
+ public String f2;
+
+ @BeanProperty(name="x3")
+ @Xml(format=ATTR, prefix="nsf3", namespace="http://nsf3")
+ public String f3;
+
+ public Q() throws Exception {
+ f1 = new URL("http://f1");
+ f2 = "f2";
+ f3 = "f3";
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T1.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T1.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T1.java
new file mode 100755
index 0000000..9831372
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T1.java
@@ -0,0 +1,38 @@
+// ***************************************************************************************************************************
+// * 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.xml.xml1a;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.xml.annotation.*;
+
+@Xml(prefix="foo",namespace="http://foo")
+@Bean(sort=true)
+@SuppressWarnings("javadoc")
+public class T1 {
+
+ public int f1 = 1;
+
+ @Xml(prefix="bar",namespace="http://bar") public int f2 = 2;
+
+ private int f3 = 3;
+ public int getF3() { return f3;}
+ public void setF3(int f3) { this.f3 = f3;}
+
+ private int f4 = 4;
+ @Xml(prefix="baz",namespace="http://baz") public int getF4() { return f4; }
+ public void setF4(int f4) { this.f4 = f4;}
+
+ public boolean equals(T1 x) {
+ return x.f1 == f1 && x.f2 == f2 && x.f3 == f3 && x.f4 == f4;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T2.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T2.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T2.java
new file mode 100755
index 0000000..5c43344
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1a/T2.java
@@ -0,0 +1,38 @@
+// ***************************************************************************************************************************
+// * 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.xml.xml1a;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.xml.annotation.*;
+
+@Xml(prefix="foo",namespace="http://foo")
+@Bean(typeName="T2", sort=true)
+@SuppressWarnings("javadoc")
+public class T2 {
+
+ public int f1 = 1;
+
+ @Xml(prefix="bar",namespace="http://bar") public int f2 = 2;
+
+ private int f3 = 3;
+ public int getF3() { return f3;}
+ public void setF3(int f3) { this.f3 = f3;}
+
+ private int f4 = 4;
+ @Xml(prefix="baz",namespace="http://baz") public int getF4() { return f4; }
+ public void setF4(int f4) { this.f4 = f4;}
+
+ public boolean equals(T2 x) {
+ return x.f1 == f1 && x.f2 == f2 && x.f3 == f3 && x.f4 == f4;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T3.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T3.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T3.java
new file mode 100755
index 0000000..96e7cce
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T3.java
@@ -0,0 +1,37 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
+// * with the License. You may obtain a copy of the License at *
+// * *
+// * http://www.apache.org/licenses/LICENSE-2.0 *
+// * *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
+// * specific language governing permissions and limitations under the License. *
+// ***************************************************************************************************************************
+package org.apache.juneau.xml.xml1b;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.xml.annotation.*;
+
+@Bean(sort=true)
+@SuppressWarnings("javadoc")
+public class T3 {
+
+ public int f1 = 1;
+
+ @Xml(prefix="bar",namespace="http://bar") public int f2 = 2;
+
+ private int f3 = 3;
+ public int getF3() { return f3; }
+ public void setF3(int f3) { this.f3 = f3; }
+
+ private int f4 = 4;
+ @Xml(prefix="baz",namespace="http://baz") public int getF4() { return f4; }
+ public void setF4(int f4) { this.f4 = f4; }
+
+ public boolean equals(T3 x) {
+ return x.f1 == f1 && x.f2 == f2 && x.f3 == f3 && x.f4 == f4;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T4.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T4.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T4.java
new file mode 100755
index 0000000..b1acc8a
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T4.java
@@ -0,0 +1,37 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
+// * with the License. You may obtain a copy of the License at *
+// * *
+// * http://www.apache.org/licenses/LICENSE-2.0 *
+// * *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
+// * specific language governing permissions and limitations under the License. *
+// ***************************************************************************************************************************
+package org.apache.juneau.xml.xml1b;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.xml.annotation.*;
+
+@Bean(typeName="T4", sort=true)
+@SuppressWarnings("javadoc")
+public class T4 {
+
+ public int f1 = 1;
+
+ @Xml(prefix="bar",namespace="http://bar") public int f2 = 2;
+
+ private int f3 = 3;
+ public int getF3() { return f3; }
+ public void setF3(int f3) { this.f3 = f3; }
+
+ private int f4 = 4;
+ @Xml(prefix="baz",namespace="http://baz") public int getF4() { return f4;}
+ public void setF4(int f4) { this.f4 = f4; }
+
+ public boolean equals(T4 x) {
+ return x.f1 == f1 && x.f2 == f2 && x.f3 == f3 && x.f4 == f4;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/75b0d8ee/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T5.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T5.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T5.java
new file mode 100755
index 0000000..9aefbff
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/xml/xml1b/T5.java
@@ -0,0 +1,38 @@
+// ***************************************************************************************************************************
+// * 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.xml.xml1b;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.xml.annotation.*;
+
+@Xml(prefix="foo",namespace="http://foo")
+@Bean(typeName="T5", sort=true)
+@SuppressWarnings("javadoc")
+public class T5 {
+
+ public int f1 = 1;
+
+ @Xml(prefix="bar",namespace="http://bar") public int f2 = 2;
+
+ private int f3 = 3;
+ public int getF3() { return f3; }
+ public void setF3(int f3) { this.f3 = f3; }
+
+ private int f4 = 4;
+ @Xml(prefix="baz",namespace="http://baz") public int getF4() { return f4; }
+ public void setF4(int f4) { this.f4 = f4; }
+
+ public boolean equals(T5 x) {
+ return x.f1 == f1 && x.f2 == f2 && x.f3 == f3 && x.f4 == f4;
+ }
+}