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 2016/08/09 17:15:23 UTC
[02/44] incubator-juneau git commit: Rename CT_* testcases.
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlCollapsed.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlCollapsed.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlCollapsed.java
deleted file mode 100755
index f1387bc..0000000
--- a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlCollapsed.java
+++ /dev/null
@@ -1,459 +0,0 @@
-/***************************************************************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- ***************************************************************************************************************************/
-package org.apache.juneau.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.*;
-import org.apache.juneau.annotation.*;
-import org.apache.juneau.serializer.*;
-import org.apache.juneau.xml.annotation.*;
-import org.junit.*;
-
-@SuppressWarnings({"serial"})
-public class CT_XmlCollapsed {
-
- //====================================================================================================
- // testBasic - @Xml.format=COLLAPSED
- //====================================================================================================
- @Test
- public void testBasic() throws Exception {
- XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
- XmlParser p = XmlParser.DEFAULT;
- A t = new A();
-
- t.f1 = new LinkedList<String>(){{add("f1a");add("f1b");}};
- t.f2 = new String[]{"f2a","f2b"};
- t.f3 = new LinkedList<String>(){{add("f3a");add("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_SIMPLE_SQ;
- XmlParser p = XmlParser.DEFAULT;
- B t = new B();
-
- t.f1 = new LinkedList<String>(){{add("f1a");add("f1b");}};
- t.f2 = new String[]{"f2a","f2b"};
- t.f3 = new LinkedList<String>(){{add("f3a");add("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_SIMPLE_SQ;
- XmlParser p = XmlParser.DEFAULT;
- C t = new C();
-
- t.f1 = new LinkedList<String>(){{add("f1b");}};
- t.f2 = new String[]{"f2b"};
- t.f3 = new LinkedList<String>(){{add("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 LinkedList<String>(){{add("f1a");}};
-
- @Xml(format=COLLAPSED)
- public String[] f2 = {"f2a"};
-
- @Xml(format=COLLAPSED,childName="xf3")
- public List<String> f3 = new LinkedList<String>(){{add("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_SIMPLE_SQ;
- XmlParser p = XmlParser.DEFAULT;
- D t = new D();
-
- t.f1 = new LinkedList<String>(){{add("f1a");}};
- t.f2 = new String[]{"f2a"};
- t.f3 = new LinkedList<String>(){{add("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_SIMPLE_SQ;
- XmlParser p = XmlParser.DEFAULT;
- E t = new E();
-
- t.f1 = new LinkedList<String>(){{add("f1a");}};
- t.f2 = new LinkedList<String>(){{add("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_SIMPLE_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);
-
- t1 = FC.newInstance();
- try {
- r = s.serialize(t1);
- fail("Exception expected.");
- } catch (SerializeException e) {
- assertEquals("org.apache.juneau.xml.CT_XmlCollapsed$FC: Multiple properties found with the name 'xf1'.", e.getLocalizedMessage());
- }
- }
-
- 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;
- }
- }
-
- // Should cause name collision.
- public static class FC {
-
- @Xml(format=COLLAPSED)
- public List<F1> f1;
-
- @Xml(format=COLLAPSED)
- public F1[] f2;
-
- public static FC newInstance() {
- FC t = new FC();
- return t;
- }
- }
-
- @Xml(name="xf1")
- public static class F1 {
-
- @Xml(format=CONTENT)
- 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_SIMPLE_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_SIMPLE_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_SIMPLE_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/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlContent.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlContent.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlContent.java
deleted file mode 100755
index d111b1f..0000000
--- a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlContent.java
+++ /dev/null
@@ -1,301 +0,0 @@
-/***************************************************************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- ***************************************************************************************************************************/
-package org.apache.juneau.xml;
-
-import static org.apache.juneau.TestUtils.*;
-import static org.apache.juneau.serializer.SerializerContext.*;
-import static org.apache.juneau.xml.XmlSerializerContext.*;
-import static org.apache.juneau.xml.XmlUtils.*;
-import static org.apache.juneau.xml.annotation.XmlFormat.*;
-import static org.junit.Assert.*;
-
-import java.io.*;
-
-import javax.xml.stream.*;
-
-import org.apache.juneau.*;
-import org.apache.juneau.xml.annotation.*;
-import org.junit.*;
-
-public class CT_XmlContent {
-
- //--------------------------------------------------------------------------------
- // Test beans with @Xml(format=CONTENT)
- //--------------------------------------------------------------------------------
- @Test
- public void testContentFormat() throws Exception {
- A t = A.newInstance(), t2;
- XmlSerializer s1 = XmlSerializer.DEFAULT_SIMPLE_SQ,
- s2 = new XmlSerializer().setProperty(SERIALIZER_quoteChar, '\'').setProperty(SERIALIZER_useIndentation, true).setProperty(XML_enableNamespaces, false);
- XmlParser p = XmlParser.DEFAULT;
- XmlSerializerSession session;
- String r;
- StringWriter sw;
-
- //----------------------------------------------------------------
- // Null
- //----------------------------------------------------------------
- t.f2 = null;
-
- sw = new StringWriter();
- session = s1.createSession(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
- s1.serialize(session, 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(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
- s2.serialize(session, t);
- r = sw.toString();
- assertEquals("<A f1='f1'>\n\t_x0000_\n</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'>\n\tfoobar\n</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'>\n\t~!@#$%^&*()_+`-={}|[]\\:\";'<>?,.

	_x0008_\n</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'>\n\t_x0020_ foobar\n</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'>\n\tfoobar _x0020_\n</A>\n", r);
- t2 = p.parse(r, A.class);
- assertEqualObjects(t, t2);
- }
-
- @Xml(name="A")
- public static class A {
- @Xml(format=ATTR) public String f1;
- @Xml(format=CONTENT) public String f2;
-
- public static A newInstance() {
- A t = new A();
- t.f1 = "f1";
- t.f2 = null;
- return t;
- }
- }
-
- //--------------------------------------------------------------------------------
- // Test beans with @Xml(format=XMLCONTENT)
- //--------------------------------------------------------------------------------
- @Test
- public void testXmlContentFormat() throws Exception {
- B t = B.newInstance(), t2;
- XmlSerializer s1 = XmlSerializer.DEFAULT_SIMPLE_SQ,
- s2 = new XmlSerializer().setProperty(SERIALIZER_quoteChar, '\'').setProperty(SERIALIZER_useIndentation, true).setProperty(XML_enableNamespaces, false);
- XmlParser p = XmlParser.DEFAULT;
- XmlSerializerSession session;
- String r;
- StringWriter sw;
-
- //----------------------------------------------------------------
- // Null
- //----------------------------------------------------------------
- t.f2 = null;
-
- sw = new StringWriter();
- session = s1.createSession(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
- s1.serialize(session, 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(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
- s2.serialize(session, t);
- r = sw.toString();
- assertEquals("<A f1='f1'>\n\t_x0000_\n</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'>\n\tfoobar\n</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'>\n\t<xxx>foobar<yyy>baz</yyy>foobar</xxx>\n</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'>\n\t_x0020_ <xxx>foobar<yyy>baz</yyy>foobar</xxx> _x0020_\n</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'>\n\t<xxx x=\"x\">foobar</xxx>\n</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<>bar</xxx></A>", r);
- t2 = p.parse(r, B.class);
- assertEqualObjects(t, t2);
-
- r = s2.serialize(t);
- assertEquals("<A f1='f1'>\n\t<xxx x=\"x\">foo<>bar</xxx>\n</A>\n", r);
- t2 = p.parse(r, B.class);
- assertEqualObjects(t, t2);
- }
-
- @Xml(name="A")
- public static class B {
- @Xml(format=ATTR) public String f1;
- @Xml(format=CONTENT, contentHandler=BContentHandler.class) public String f2;
-
- public static B newInstance() {
- B t = new B();
- t.f1 = "f1";
- t.f2 = null;
- return t;
- }
- }
-
- public static class BContentHandler implements XmlContentHandler<B> {
-
- @Override /* XmlContentHandler */
- public void parse(XMLStreamReader r, B b) throws Exception {
- b.f2 = decode(readXmlContents(r).trim());
- }
-
- @Override /* XmlContentHandler */
- public void serialize(XmlWriter w, B b) throws Exception {
- w.encodeTextInvalidChars(b.f2);
- }
-
- }
-
- //--------------------------------------------------------------------------------
- // Test beans with too many @Xml.format=CONTENT/XMLCONTENT annotations.
- //--------------------------------------------------------------------------------
- @Test
- public void testBadContent() throws Exception {
- XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
- try {
- s.serialize(new C1());
- fail("Exception expected");
- } catch (Exception e) {
- assertTrue(e.getMessage().contains("Multiple instances of CONTENT properties defined on class"));
- }
- // Run twice to make sure we throw exceptions after the first call.
- try {
- s.serialize(new C1());
- fail("Exception expected");
- } catch (Exception e) {
- assertTrue(e.getMessage().contains("Multiple instances of CONTENT properties defined on class"));
- }
- }
- public static class C1 {
- @Xml(format=CONTENT) public String f1;
- @Xml(format=CONTENT) public String f2;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlParser.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlParser.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlParser.java
deleted file mode 100755
index 450ea4d..0000000
--- a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_XmlParser.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/***************************************************************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- ***************************************************************************************************************************/
-package org.apache.juneau.xml;
-
-import static org.apache.juneau.xml.XmlParserContext.*;
-import static org.junit.Assert.*;
-
-import org.apache.juneau.*;
-import org.apache.juneau.parser.*;
-import org.junit.*;
-
-public class CT_XmlParser {
-
- @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 XmlParser().setProperty(XML_preserveRootElement, true);
-
- 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/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonParserTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonParserTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonParserTest.java
new file mode 100755
index 0000000..9432288
--- /dev/null
+++ b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonParserTest.java
@@ -0,0 +1,179 @@
+/***************************************************************************************************************************
+ * 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.BeanContext.*;
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.parser.*;
+import org.junit.*;
+
+@SuppressWarnings({"rawtypes","serial"})
+public class CommonParserTest {
+
+ //====================================================================================================
+ // testFromSerializer
+ //====================================================================================================
+ @Test
+ public void testFromSerializer() throws Exception {
+ ReaderParser p = XmlParser.DEFAULT;
+
+ Map m = null;
+ m = (Map)p.parse("<object><a type='number'>1</a></object>", Object.class);
+ assertEquals(1, m.get("a"));
+ m = (Map)p.parse("<object><a type='number'>1</a><b type='string'>foo bar</b></object>", Object.class);
+ assertEquals(1, m.get("a"));
+ assertEquals("foo bar", m.get("b"));
+ m = (Map)p.parse("<object><a type='number'>1</a><b type='string'>foo bar</b><c type='boolean'>false</c></object>", Object.class);
+ assertEquals(1, m.get("a"));
+ assertEquals(false, m.get("c"));
+ m = (Map)p.parse(" <object> <a type='number'> 1 </a> <b type='string'> foo </b> <c type='boolean'> false </c> </object> ", Object.class);
+ assertEquals(1, m.get("a"));
+ assertEquals("foo", m.get("b"));
+ assertEquals(false, m.get("c"));
+
+ m = (Map)p.parse("<object><x type='string'>org.apache.juneau.test.Person</x><addresses type='array'><object><x type='string'>org.apache.juneau.test.Address</x><city type='string'>city A</city><state type='string'>state A</state><street type='string'>street A</street><zip type='number'>12345</zip></object></addresses></object>", Object.class);
+ assertEquals("org.apache.juneau.test.Person", m.get("x"));
+ List l = (List)m.get("addresses");
+ assertNotNull(l);
+ m = (Map)l.get(0);
+ assertNotNull(m);
+ assertEquals("org.apache.juneau.test.Address", m.get("x"));
+ assertEquals("city A", m.get("city"));
+ assertEquals("state A", m.get("state"));
+ assertEquals("street A", m.get("street"));
+ assertEquals(12345, m.get("zip"));
+
+ ObjectList jl = (ObjectList)p.parse("<array><object><attribute type='string'>value</attribute></object><object><attribute type='string'>value</attribute></object></array>", Object.class);
+ assertEquals("value", jl.getObjectMap(0).getString("attribute"));
+ assertEquals("value", jl.getObjectMap(1).getString("attribute"));
+
+ try {
+ jl = (ObjectList)p.parse("<array><object><attribute type='string'>value</attribute></object><object><attribute type='string'>value</attribute></object></array>", Object.class);
+ assertEquals("value", jl.getObjectMap(0).getString("attribute"));
+ assertEquals("value", jl.getObjectMap(1).getString("attribute"));
+ } catch (Exception e) {
+ fail(e.getLocalizedMessage());
+ }
+
+ A1 t1 = new A1();
+ A2 t2 = new A2();
+ t2.add(new A3("name0","value0"));
+ t2.add(new A3("name1","value1"));
+ t1.list = t2;
+ String r = XmlSerializer.DEFAULT.serialize(t1);
+ t1 = p.parse(r, A1.class);
+ assertEquals("value1", t1.list.get(1).value);
+
+ r = XmlSerializer.DEFAULT.serialize(t1);
+ t1 = p.parse(r, A1.class);
+ assertEquals("value1", t1.list.get(1).value);
+ }
+
+ public static class A1 {
+ public A2 list;
+ }
+
+ public static class A2 extends LinkedList<A3> {
+ }
+
+ public static class A3 {
+ public String name, value;
+ public A3(){}
+ public A3(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+ }
+
+ //====================================================================================================
+ // Correct handling of unknown properties.
+ //====================================================================================================
+ @Test
+ public void testCorrectHandlingOfUnknownProperties() throws Exception {
+ ReaderParser p = new XmlParser().setProperty(BEAN_ignoreUnknownBeanProperties, true);
+ B t;
+
+ String in = "<object><a>1</a><unknown>foo</unknown><b>2</b></object>";
+ t = p.parse(in, B.class);
+ assertEquals(t.a, 1);
+ assertEquals(t.b, 2);
+
+ in = "<object><a>1</a><unknown><object><a type='string'>foo</a></object></unknown><b>2</b></object>";
+ t = p.parse(in, B.class);
+ assertEquals(t.a, 1);
+ assertEquals(t.b, 2);
+
+
+ try {
+ p = new XmlParser();
+ p.parse(in, B.class);
+ fail("Exception expected");
+ } catch (ParseException e) {}
+ }
+
+ public static class B {
+ public int a, b;
+ }
+
+ //====================================================================================================
+ // Writing to Collection properties with no setters.
+ //====================================================================================================
+ @Test
+ public void testCollectionPropertiesWithNoSetters() throws Exception {
+
+ ReaderParser p = XmlParser.DEFAULT;
+
+ String in = "<object><ints type='array'><number>1</number><number>2</number><number>3</number></ints><beans type='array'><object><a type='number'>1</a><b type='number'>2</b></object></beans></object>";
+ C t = p.parse(in, C.class);
+ assertEquals(t.getInts().size(), 3);
+ assertEquals(t.getBeans().get(0).b, 2);
+ }
+
+ public static class C {
+ private Collection<Integer> ints = new LinkedList<Integer>();
+ private List<B> beans = new LinkedList<B>();
+ public Collection<Integer> getInts() {
+ return ints;
+ }
+ public List<B> getBeans() {
+ return beans;
+ }
+ }
+
+ //====================================================================================================
+ // Parser listeners.
+ //====================================================================================================
+ @Test
+ public void testParserListeners() throws Exception {
+ final List<String> events = new LinkedList<String>();
+ XmlParser p = new XmlParser().setProperty(BEAN_ignoreUnknownBeanProperties, true);
+ p.addListener(
+ new ParserListener() {
+ @Override /* ParserListener */
+ public <T> void onUnknownProperty(String propertyName, Class<T> beanClass, T bean, int line, int col) {
+ events.add(propertyName + "," + line + "," + col);
+ }
+ }
+ );
+
+ String in = "<object><a type='number'>1</a><unknownProperty type='string'>foo</unknownProperty><b type='number'>2</b></object>";
+ p.parse(in, B.class);
+ assertEquals(1, events.size());
+ // XML parser may or may not support line numbers.
+ assertTrue(events.get(0).startsWith("unknownProperty,"));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonTest.java
new file mode 100755
index 0000000..886f615
--- /dev/null
+++ b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonTest.java
@@ -0,0 +1,453 @@
+/***************************************************************************************************************************
+ * 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.XmlSerializerContext.*;
+import static org.apache.juneau.xml.annotation.XmlFormat.*;
+import static org.junit.Assert.*;
+
+import java.net.*;
+import java.net.URI;
+import java.util.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.serializer.*;
+import org.apache.juneau.testbeans.*;
+import org.apache.juneau.xml.annotation.*;
+import org.junit.*;
+
+@SuppressWarnings({"serial"})
+public class CommonTest {
+
+ //====================================================================================================
+ // Trim nulls from beans
+ //====================================================================================================
+ @Test
+ public void testTrimNullsFromBeans() throws Exception {
+ XmlSerializer s = new XmlSerializer.SimpleSq();
+ XmlParser p = new XmlParser();
+ A t1 = A.create(), t2;
+
+ s.setProperty(SERIALIZER_trimNullProperties, false);
+ String r = s.serialize(t1);
+ assertEquals("<object><s1 nil='true'/><s2>s2</s2></object>", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t1, t2);
+
+ s.setProperty(SERIALIZER_trimNullProperties, true);
+ r = s.serialize(t1);
+ assertEquals("<object><s2>s2</s2></object>", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t1, t2);
+ }
+
+ public static class A {
+ public String s1, s2;
+
+ public static A create() {
+ A t = new A();
+ t.s2 = "s2";
+ return t;
+ }
+ }
+
+ //====================================================================================================
+ // Trim empty maps
+ //====================================================================================================
+ @Test
+ public void testTrimEmptyMaps() throws Exception {
+ XmlSerializer s = new XmlSerializer.SimpleSq();
+ XmlParser p = XmlParser.DEFAULT;
+ B t1 = B.create(), t2;
+ String r;
+
+ s.setProperty(SERIALIZER_trimEmptyMaps, false);
+ r = s.serialize(t1);
+ assertEquals("<object><f1/><f2><f2a nil='true'/><f2b><s2>s2</s2></f2b></f2></object>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t1, t2);
+
+ s.setProperty(SERIALIZER_trimEmptyMaps, true);
+ r = s.serialize(t1);
+ assertEquals("<object><f2><f2a nil='true'/><f2b><s2>s2</s2></f2b></f2></object>", r);
+ t2 = p.parse(r, B.class);
+ assertNull(t2.f1);
+ }
+
+ public static class B {
+ public TreeMap<String,A> f1, f2;
+
+ public static B create() {
+ B t = new B();
+ t.f1 = new TreeMap<String,A>();
+ t.f2 = new TreeMap<String,A>(){{put("f2a",null);put("f2b",A.create());}};
+ return t;
+ }
+ }
+
+ //====================================================================================================
+ // Trim empty lists
+ //====================================================================================================
+ @Test
+ public void testTrimEmptyLists() throws Exception {
+ XmlSerializer s = new XmlSerializer.SimpleSq();
+ XmlParser p = XmlParser.DEFAULT;
+ C t1 = C.create(), t2;
+ String r;
+
+ s.setProperty(SERIALIZER_trimEmptyLists, false);
+ r = s.serialize(t1);
+ assertEquals("<object><f1></f1><f2><null/><object><s2>s2</s2></object></f2></object>", r);
+ t2 = p.parse(r, C.class);
+ assertEqualObjects(t1, t2);
+
+ s.setProperty(SERIALIZER_trimEmptyLists, true);
+ r = s.serialize(t1);
+ assertEquals("<object><f2><null/><object><s2>s2</s2></object></f2></object>", r);
+ t2 = p.parse(r, C.class);
+ assertNull(t2.f1);
+ }
+
+ public static class C {
+ public List<A> f1, f2;
+
+ public static C create() {
+ C t = new C();
+ t.f1 = new LinkedList<A>();
+ t.f2 = new LinkedList<A>(){{add(null);add(A.create());}};
+ return t;
+ }
+ }
+
+ //====================================================================================================
+ // Trim empty arrays
+ //====================================================================================================
+ @Test
+ public void testTrimEmptyArrays() throws Exception {
+ XmlSerializer s = new XmlSerializer.SimpleSq();
+ XmlParser p = XmlParser.DEFAULT;
+ D t1 = D.create(), t2;
+ String r;
+
+ s.setProperty(SERIALIZER_trimEmptyLists, false);
+ r = s.serialize(t1);
+ assertEquals("<object><f1></f1><f2><null/><object><s2>s2</s2></object></f2></object>", r);
+ t2 = p.parse(r, D.class);
+ assertEqualObjects(t1, t2);
+
+ s.setProperty(SERIALIZER_trimEmptyLists, true);
+ r = s.serialize(t1);
+ assertEquals("<object><f2><null/><object><s2>s2</s2></object></f2></object>", r);
+ t2 = p.parse(r, D.class);
+ assertNull(t2.f1);
+ }
+
+ public static class D {
+ public A[] f1, f2;
+
+ public static D create() {
+ D t = new D();
+ t.f1 = new A[]{};
+ t.f2 = new A[]{null, A.create()};
+ return t;
+ }
+ }
+
+ //====================================================================================================
+ // @BeanProperty.properties annotation.
+ //====================================================================================================
+ @Test
+ public void testBeanPropertyProperties() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
+ E1 t = new E1();
+ String r = s.serialize(t);
+ assertEquals("<object><x1 f2='2'><f1>1</f1></x1><x2><f1>1</f1></x2><x3><object f2='2'><f1>1</f1></object></x3><x4><object f2='2'><f1>1</f1></object></x4><x5><object><f1>1</f1></object></x5><x6><object><f1>1</f1></object></x6></object>", r);
+ TestUtils.validateXml(t);
+ }
+
+ public static class E1 {
+ @BeanProperty(properties={"f1","f2"}) public E2 x1 = new E2();
+ @BeanProperty(properties={"f1","f2"}) public Map<String,Integer> x2 = new LinkedHashMap<String,Integer>() {{
+ put("f1",1); put("f3",3);
+ }};
+ @BeanProperty(properties={"f1","f2"}) public E2[] x3 = {new E2()};
+ @BeanProperty(properties={"f1","f2"}) public List<E2> x4 = new LinkedList<E2>() {{
+ add(new E2());
+ }};
+ @BeanProperty(properties={"f1"}) public ObjectMap[] x5 = {new ObjectMap().append("f1",1).append("f3",3)};
+ @BeanProperty(properties={"f1"}) public List<ObjectMap> x6 = new LinkedList<ObjectMap>() {{
+ add(new ObjectMap().append("f1",1).append("f3",3));
+ }};
+ }
+
+ public static class E2 {
+ public int f1 = 1;
+ @Xml(format=ATTR) public int f2 = 2;
+ public int f3 = 3;
+ @Xml(format=ATTR) public int f4 = 4;
+ }
+
+ //====================================================================================================
+ // @BeanProperty.properties annotation on list of beans.
+ //====================================================================================================
+ @Test
+ public void testBeanPropertyPropertiesOnListOfBeans() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
+ List<Test7b> l = new LinkedList<Test7b>();
+ Test7b t = new Test7b();
+ t.x1.add(new Test7b());
+ l.add(t);
+ String xml = s.serialize(l);
+ assertEquals("<array><object><x1><object><x2>2</x2></object></x1><x2>2</x2></object></array>", xml);
+ }
+
+ public static class Test7b {
+ @BeanProperty(properties={"x2"}) public List<Test7b> x1 = new LinkedList<Test7b>();
+ public int x2 = 2;
+ }
+
+ //====================================================================================================
+ // Test that URLs and URIs are serialized and parsed correctly.
+ //====================================================================================================
+ @Test
+ public void testURIAttr() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+
+ G t = new G();
+ t.uri = new URI("http://uri");
+ t.f1 = new URI("http://f1");
+ t.f2 = new URL("http://f2");
+
+ String xml = s.serialize(t);
+ t = p.parse(xml, G.class);
+ assertEquals("http://uri", t.uri.toString());
+ assertEquals("http://f1", t.f1.toString());
+ assertEquals("http://f2", t.f2.toString());
+ }
+
+ public static class G {
+ @BeanProperty(beanUri=true) public URI uri;
+ public URI f1;
+ public URL f2;
+ }
+
+ //====================================================================================================
+ // Test URIs with URI_CONTEXT and URI_AUTHORITY
+ //====================================================================================================
+ @Test
+ public void testUris() throws Exception {
+ WriterSerializer s = new XmlSerializer.SimpleSq();
+ TestURI t = new TestURI();
+ String r;
+ String expected;
+
+ s.setProperty(SERIALIZER_relativeUriBase, null);
+ r = s.serialize(t);
+ expected = ""
+ +"<object f0='f0/x0'>"
+ +"<f1>f1/x1</f1>"
+ +"<f2>/f2/x2</f2>"
+ +"<f3>http://www.ibm.com/f3/x3</f3>"
+ +"<f4>f4/x4</f4>"
+ +"<f5>/f5/x5</f5>"
+ +"<f6>http://www.ibm.com/f6/x6</f6>"
+ +"<f7>http://www.ibm.com/f7/x7</f7>"
+ +"<f8>f8/x8</f8>"
+ +"<f9>f9/x9</f9>"
+ +"<fa>http://www.ibm.com/fa/xa#MY_LABEL</fa>"
+ +"<fb>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</fb>"
+ +"<fc>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</fc>"
+ +"<fd>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</fd>"
+ +"<fe>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</fe>"
+ +"</object>";
+ assertEquals(expected, r);
+
+ s.setProperty(SERIALIZER_relativeUriBase, ""); // Same as null.
+ r = s.serialize(t);
+ assertEquals(expected, r);
+
+ s.setProperty(SERIALIZER_relativeUriBase, "/cr");
+ r = s.serialize(t);
+ expected = ""
+ +"<object f0='/cr/f0/x0'>"
+ +"<f1>/cr/f1/x1</f1>"
+ +"<f2>/f2/x2</f2>"
+ +"<f3>http://www.ibm.com/f3/x3</f3>"
+ +"<f4>/cr/f4/x4</f4>"
+ +"<f5>/f5/x5</f5>"
+ +"<f6>http://www.ibm.com/f6/x6</f6>"
+ +"<f7>http://www.ibm.com/f7/x7</f7>"
+ +"<f8>/cr/f8/x8</f8>"
+ +"<f9>/cr/f9/x9</f9>"
+ +"<fa>http://www.ibm.com/fa/xa#MY_LABEL</fa>"
+ +"<fb>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</fb>"
+ +"<fc>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</fc>"
+ +"<fd>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</fd>"
+ +"<fe>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</fe>"
+ +"</object>";
+ assertEquals(expected, r);
+
+ s.setProperty(SERIALIZER_relativeUriBase, "/cr/"); // Same as above
+ r = s.serialize(t);
+ assertEquals(expected, r);
+
+ s.setProperty(SERIALIZER_relativeUriBase, "/");
+ r = s.serialize(t);
+ expected = ""
+ +"<object f0='/f0/x0'>"
+ +"<f1>/f1/x1</f1>"
+ +"<f2>/f2/x2</f2>"
+ +"<f3>http://www.ibm.com/f3/x3</f3>"
+ +"<f4>/f4/x4</f4>"
+ +"<f5>/f5/x5</f5>"
+ +"<f6>http://www.ibm.com/f6/x6</f6>"
+ +"<f7>http://www.ibm.com/f7/x7</f7>"
+ +"<f8>/f8/x8</f8>"
+ +"<f9>/f9/x9</f9>"
+ +"<fa>http://www.ibm.com/fa/xa#MY_LABEL</fa>"
+ +"<fb>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</fb>"
+ +"<fc>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</fc>"
+ +"<fd>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</fd>"
+ +"<fe>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</fe>"
+ +"</object>";
+ assertEquals(expected, r);
+
+ s.setProperty(SERIALIZER_relativeUriBase, null);
+
+ s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo");
+ r = s.serialize(t);
+ expected = ""
+ +"<object f0='f0/x0'>"
+ +"<f1>f1/x1</f1>"
+ +"<f2>http://foo/f2/x2</f2>"
+ +"<f3>http://www.ibm.com/f3/x3</f3>"
+ +"<f4>f4/x4</f4>"
+ +"<f5>http://foo/f5/x5</f5>"
+ +"<f6>http://www.ibm.com/f6/x6</f6>"
+ +"<f7>http://www.ibm.com/f7/x7</f7>"
+ +"<f8>f8/x8</f8>"
+ +"<f9>f9/x9</f9>"
+ +"<fa>http://www.ibm.com/fa/xa#MY_LABEL</fa>"
+ +"<fb>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</fb>"
+ +"<fc>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</fc>"
+ +"<fd>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</fd>"
+ +"<fe>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</fe>"
+ +"</object>";
+ assertEquals(expected, r);
+
+ s.setProperty(SERIALIZER_absolutePathUriBase, "http://foo/");
+ r = s.serialize(t);
+ assertEquals(expected, r);
+
+ s.setProperty(SERIALIZER_absolutePathUriBase, ""); // Same as null.
+ r = s.serialize(t);
+ expected = ""
+ +"<object f0='f0/x0'>"
+ +"<f1>f1/x1</f1>"
+ +"<f2>/f2/x2</f2>"
+ +"<f3>http://www.ibm.com/f3/x3</f3>"
+ +"<f4>f4/x4</f4>"
+ +"<f5>/f5/x5</f5>"
+ +"<f6>http://www.ibm.com/f6/x6</f6>"
+ +"<f7>http://www.ibm.com/f7/x7</f7>"
+ +"<f8>f8/x8</f8>"
+ +"<f9>f9/x9</f9>"
+ +"<fa>http://www.ibm.com/fa/xa#MY_LABEL</fa>"
+ +"<fb>http://www.ibm.com/fb/xb?label=MY_LABEL&foo=bar</fb>"
+ +"<fc>http://www.ibm.com/fc/xc?foo=bar&label=MY_LABEL</fc>"
+ +"<fd>http://www.ibm.com/fd/xd?label2=MY_LABEL&foo=bar</fd>"
+ +"<fe>http://www.ibm.com/fe/xe?foo=bar&label2=MY_LABEL</fe>"
+ +"</object>";
+ assertEquals(expected, r);
+ }
+
+ //====================================================================================================
+ // Validate that you cannot update properties on locked serializer.
+ //====================================================================================================
+ @Test
+ public void testLockedSerializer() throws Exception {
+ XmlSerializer s = new XmlSerializer().lock();
+ try {
+ s.setProperty(XmlSerializerContext.XML_enableNamespaces, true);
+ fail("Locked exception not thrown");
+ } catch (LockedException e) {}
+ try {
+ s.setProperty(SerializerContext.SERIALIZER_addClassAttrs, true);
+ fail("Locked exception not thrown");
+ } catch (LockedException e) {}
+ try {
+ s.setProperty(BeanContext.BEAN_beanMapPutReturnsOldValue, true);
+ fail("Locked exception not thrown");
+ } catch (LockedException e) {}
+ }
+
+ //====================================================================================================
+ // Recursion
+ //====================================================================================================
+ @Test
+ public void testRecursion() throws Exception {
+ XmlSerializer s = new XmlSerializer().setProperty(XML_enableNamespaces, false);
+
+ R1 r1 = new R1();
+ R2 r2 = new R2();
+ R3 r3 = new R3();
+ r1.r2 = r2;
+ r2.r3 = r3;
+ r3.r1 = r1;
+
+ // No recursion detection
+ try {
+ s.serialize(r1);
+ fail("Exception expected!");
+ } catch (Exception e) {
+ String msg = e.getLocalizedMessage();
+ assertTrue(msg.contains("It's recommended you use the SerializerContext.SERIALIZER_detectRecursions setting to help locate the loop."));
+ }
+
+ // Recursion detection, no ignore
+ s.setProperty(SERIALIZER_detectRecursions, true);
+ try {
+ s.serialize(r1);
+ fail("Exception expected!");
+ } catch (Exception e) {
+ String msg = e.getLocalizedMessage();
+ assertTrue(msg.contains("[0]<noname>:org.apache.juneau.xml.CommonTest$R1"));
+ assertTrue(msg.contains("->[1]r2:org.apache.juneau.xml.CommonTest$R2"));
+ assertTrue(msg.contains("->[2]r3:org.apache.juneau.xml.CommonTest$R3"));
+ assertTrue(msg.contains("->[3]r1:org.apache.juneau.xml.CommonTest$R1"));
+ }
+
+ s.setProperty(SERIALIZER_ignoreRecursions, true);
+ assertEquals("<object><name>foo</name><r2><name>bar</name><r3><name>baz</name></r3></r2></object>", s.serialize(r1));
+
+ // Make sure this doesn't blow up.
+ s.getSchemaSerializer().serialize(r1);
+ }
+
+ public static class R1 {
+ public String name = "foo";
+ public R2 r2;
+ }
+ public static class R2 {
+ public String name = "bar";
+ public R3 r3;
+ }
+ public static class R3 {
+ public String name = "baz";
+ public R1 r1;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonXmlTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonXmlTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonXmlTest.java
new file mode 100755
index 0000000..c5e4c3b
--- /dev/null
+++ b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CommonXmlTest.java
@@ -0,0 +1,81 @@
+/***************************************************************************************************************************
+ * 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.XmlSerializerContext.*;
+import static org.apache.juneau.xml.annotation.XmlFormat.*;
+import static org.junit.Assert.*;
+
+import java.net.*;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.xml.annotation.*;
+import org.junit.*;
+
+public class CommonXmlTest {
+
+ //====================================================================================================
+ // Test 18a - @Bean.uri annotation
+ //====================================================================================================
+ @Test
+ public void testBeanUriAnnotation() throws Exception {
+ XmlParser p = XmlParser.DEFAULT;
+ XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
+
+ A t = new A("http://foo", 123, "bar");
+ String xml = s.serialize(t);
+ assertEquals("<object url='http://foo' id='123'><name>bar</name></object>", xml);
+
+ t = p.parse(xml, A.class);
+ assertEquals("http://foo", t.url.toString());
+ assertEquals(123, t.id);
+ assertEquals("bar", t.name);
+
+ validateXml(t, s);
+ }
+
+ public static class A {
+ @BeanProperty(beanUri=true) public URL url;
+ @Xml(format=ATTR) public int id;
+ public String name;
+ public A() {}
+ public A(String url, int id, String name) throws Exception {
+ this.url = new URL(url);
+ this.id = id;
+ this.name = name;
+ }
+ }
+
+ //====================================================================================================
+ // Bean.uri annotation, only uri property
+ //====================================================================================================
+ @Test
+ public void testBeanUriAnnotationOnlyUriProperty() throws Exception {
+ XmlSerializer s = new XmlSerializer.Sq().setProperty(XML_addNamespaceUrisToRoot, false);
+
+ B t = new B("http://foo");
+ String xml = s.serialize(t);
+ assertEquals("<object url='http://foo'><url2>http://foo/2</url2></object>", xml);
+ }
+
+ public static class B {
+ @BeanProperty(beanUri=true) public URL url;
+ public URL url2;
+ public B() {}
+ public B(String url) throws Exception {
+ this.url = new URL(url);
+ this.url2 = new URL(url+"/2");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java
new file mode 100755
index 0000000..2bfa4e6
--- /dev/null
+++ b/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlCollapsedTest.java
@@ -0,0 +1,459 @@
+/***************************************************************************************************************************
+ * 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.*;
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.serializer.*;
+import org.apache.juneau.xml.annotation.*;
+import org.junit.*;
+
+@SuppressWarnings({"serial"})
+public class XmlCollapsedTest {
+
+ //====================================================================================================
+ // testBasic - @Xml.format=COLLAPSED
+ //====================================================================================================
+ @Test
+ public void testBasic() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ A t = new A();
+
+ t.f1 = new LinkedList<String>(){{add("f1a");add("f1b");}};
+ t.f2 = new String[]{"f2a","f2b"};
+ t.f3 = new LinkedList<String>(){{add("f3a");add("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_SIMPLE_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ B t = new B();
+
+ t.f1 = new LinkedList<String>(){{add("f1a");add("f1b");}};
+ t.f2 = new String[]{"f2a","f2b"};
+ t.f3 = new LinkedList<String>(){{add("f3a");add("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_SIMPLE_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ C t = new C();
+
+ t.f1 = new LinkedList<String>(){{add("f1b");}};
+ t.f2 = new String[]{"f2b"};
+ t.f3 = new LinkedList<String>(){{add("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 LinkedList<String>(){{add("f1a");}};
+
+ @Xml(format=COLLAPSED)
+ public String[] f2 = {"f2a"};
+
+ @Xml(format=COLLAPSED,childName="xf3")
+ public List<String> f3 = new LinkedList<String>(){{add("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_SIMPLE_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ D t = new D();
+
+ t.f1 = new LinkedList<String>(){{add("f1a");}};
+ t.f2 = new String[]{"f2a"};
+ t.f3 = new LinkedList<String>(){{add("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_SIMPLE_SQ;
+ XmlParser p = XmlParser.DEFAULT;
+ E t = new E();
+
+ t.f1 = new LinkedList<String>(){{add("f1a");}};
+ t.f2 = new LinkedList<String>(){{add("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_SIMPLE_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);
+
+ t1 = FC.newInstance();
+ try {
+ r = s.serialize(t1);
+ fail("Exception expected.");
+ } catch (SerializeException e) {
+ assertEquals("org.apache.juneau.xml.XmlCollapsedTest$FC: Multiple properties found with the name 'xf1'.", e.getLocalizedMessage());
+ }
+ }
+
+ 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;
+ }
+ }
+
+ // Should cause name collision.
+ public static class FC {
+
+ @Xml(format=COLLAPSED)
+ public List<F1> f1;
+
+ @Xml(format=COLLAPSED)
+ public F1[] f2;
+
+ public static FC newInstance() {
+ FC t = new FC();
+ return t;
+ }
+ }
+
+ @Xml(name="xf1")
+ public static class F1 {
+
+ @Xml(format=CONTENT)
+ 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_SIMPLE_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_SIMPLE_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_SIMPLE_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/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlContentTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlContentTest.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlContentTest.java
new file mode 100755
index 0000000..42fb2b7
--- /dev/null
+++ b/org.apache.juneau/src/test/java/org/apache/juneau/xml/XmlContentTest.java
@@ -0,0 +1,301 @@
+/***************************************************************************************************************************
+ * 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.XmlSerializerContext.*;
+import static org.apache.juneau.xml.XmlUtils.*;
+import static org.apache.juneau.xml.annotation.XmlFormat.*;
+import static org.junit.Assert.*;
+
+import java.io.*;
+
+import javax.xml.stream.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.xml.annotation.*;
+import org.junit.*;
+
+public class XmlContentTest {
+
+ //--------------------------------------------------------------------------------
+ // Test beans with @Xml(format=CONTENT)
+ //--------------------------------------------------------------------------------
+ @Test
+ public void testContentFormat() throws Exception {
+ A t = A.newInstance(), t2;
+ XmlSerializer s1 = XmlSerializer.DEFAULT_SIMPLE_SQ,
+ s2 = new XmlSerializer().setProperty(SERIALIZER_quoteChar, '\'').setProperty(SERIALIZER_useIndentation, true).setProperty(XML_enableNamespaces, false);
+ XmlParser p = XmlParser.DEFAULT;
+ XmlSerializerSession session;
+ String r;
+ StringWriter sw;
+
+ //----------------------------------------------------------------
+ // Null
+ //----------------------------------------------------------------
+ t.f2 = null;
+
+ sw = new StringWriter();
+ session = s1.createSession(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
+ s1.serialize(session, 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(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
+ s2.serialize(session, t);
+ r = sw.toString();
+ assertEquals("<A f1='f1'>\n\t_x0000_\n</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'>\n\tfoobar\n</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'>\n\t~!@#$%^&*()_+`-={}|[]\\:\";'<>?,.

	_x0008_\n</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'>\n\t_x0020_ foobar\n</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'>\n\tfoobar _x0020_\n</A>\n", r);
+ t2 = p.parse(r, A.class);
+ assertEqualObjects(t, t2);
+ }
+
+ @Xml(name="A")
+ public static class A {
+ @Xml(format=ATTR) public String f1;
+ @Xml(format=CONTENT) public String f2;
+
+ public static A newInstance() {
+ A t = new A();
+ t.f1 = "f1";
+ t.f2 = null;
+ return t;
+ }
+ }
+
+ //--------------------------------------------------------------------------------
+ // Test beans with @Xml(format=XMLCONTENT)
+ //--------------------------------------------------------------------------------
+ @Test
+ public void testXmlContentFormat() throws Exception {
+ B t = B.newInstance(), t2;
+ XmlSerializer s1 = XmlSerializer.DEFAULT_SIMPLE_SQ,
+ s2 = new XmlSerializer().setProperty(SERIALIZER_quoteChar, '\'').setProperty(SERIALIZER_useIndentation, true).setProperty(XML_enableNamespaces, false);
+ XmlParser p = XmlParser.DEFAULT;
+ XmlSerializerSession session;
+ String r;
+ StringWriter sw;
+
+ //----------------------------------------------------------------
+ // Null
+ //----------------------------------------------------------------
+ t.f2 = null;
+
+ sw = new StringWriter();
+ session = s1.createSession(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
+ s1.serialize(session, 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(sw, new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null);
+ s2.serialize(session, t);
+ r = sw.toString();
+ assertEquals("<A f1='f1'>\n\t_x0000_\n</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'>\n\tfoobar\n</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'>\n\t<xxx>foobar<yyy>baz</yyy>foobar</xxx>\n</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'>\n\t_x0020_ <xxx>foobar<yyy>baz</yyy>foobar</xxx> _x0020_\n</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'>\n\t<xxx x=\"x\">foobar</xxx>\n</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<>bar</xxx></A>", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+
+ r = s2.serialize(t);
+ assertEquals("<A f1='f1'>\n\t<xxx x=\"x\">foo<>bar</xxx>\n</A>\n", r);
+ t2 = p.parse(r, B.class);
+ assertEqualObjects(t, t2);
+ }
+
+ @Xml(name="A")
+ public static class B {
+ @Xml(format=ATTR) public String f1;
+ @Xml(format=CONTENT, contentHandler=BContentHandler.class) public String f2;
+
+ public static B newInstance() {
+ B t = new B();
+ t.f1 = "f1";
+ t.f2 = null;
+ return t;
+ }
+ }
+
+ public static class BContentHandler implements XmlContentHandler<B> {
+
+ @Override /* XmlContentHandler */
+ public void parse(XMLStreamReader r, B b) throws Exception {
+ b.f2 = decode(readXmlContents(r).trim());
+ }
+
+ @Override /* XmlContentHandler */
+ public void serialize(XmlWriter w, B b) throws Exception {
+ w.encodeTextInvalidChars(b.f2);
+ }
+
+ }
+
+ //--------------------------------------------------------------------------------
+ // Test beans with too many @Xml.format=CONTENT/XMLCONTENT annotations.
+ //--------------------------------------------------------------------------------
+ @Test
+ public void testBadContent() throws Exception {
+ XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ;
+ try {
+ s.serialize(new C1());
+ fail("Exception expected");
+ } catch (Exception e) {
+ assertTrue(e.getMessage().contains("Multiple instances of CONTENT properties defined on class"));
+ }
+ // Run twice to make sure we throw exceptions after the first call.
+ try {
+ s.serialize(new C1());
+ fail("Exception expected");
+ } catch (Exception e) {
+ assertTrue(e.getMessage().contains("Multiple instances of CONTENT properties defined on class"));
+ }
+ }
+ public static class C1 {
+ @Xml(format=CONTENT) public String f1;
+ @Xml(format=CONTENT) public String f2;
+ }
+}