http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_Common.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_Common.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_Common.java deleted file mode 100755 index 1c2caa1..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_Common.java +++ /dev/null @@ -1,453 +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.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 CT_Common { - - //==================================================================================================== - // 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.CT_Common$R1")); - assertTrue(msg.contains("->[1]r2:org.apache.juneau.xml.CT_Common$R2")); - assertTrue(msg.contains("->[2]r3:org.apache.juneau.xml.CT_Common$R3")); - assertTrue(msg.contains("->[3]r1:org.apache.juneau.xml.CT_Common$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/CT_CommonParser.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_CommonParser.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_CommonParser.java deleted file mode 100755 index 054a042..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_CommonParser.java +++ /dev/null @@ -1,179 +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.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 CT_CommonParser { - - //==================================================================================================== - // 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/CT_CommonXml.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_CommonXml.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_CommonXml.java deleted file mode 100755 index 3e09246..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_CommonXml.java +++ /dev/null @@ -1,81 +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.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 CT_CommonXml { - - //==================================================================================================== - // 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/CT_Xml.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_Xml.java b/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_Xml.java deleted file mode 100755 index 5b71d8e..0000000 --- a/org.apache.juneau/src/test/java/org/apache/juneau/xml/CT_Xml.java +++ /dev/null @@ -1,1050 +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.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.json.*; -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"}) -public class CT_Xml { - - //==================================================================================================== - // Simple comparison test with JSON serializer - //==================================================================================================== - @Test - public void testComparisonWithJson() throws Exception { - String json1 = readFile(getClass().getResource("/xml/testComparisonWithJson.json").getPath()); - String xml1 = readFile(getClass().getResource("/xml/testComparisonWithJson.xml").getPath()); - - ObjectMap m = (ObjectMap) XmlParser.DEFAULT.parse(xml1, Object.class); - String json2 = new JsonSerializer.SimpleReadable().setProperty(SERIALIZER_quoteChar, '"').setProperty(SERIALIZER_trimNullProperties, false).serialize(m); - assertEquals(json1, json2); - - m = (ObjectMap) JsonParser.DEFAULT.parse(json1, Object.class); - String xml2 = new XmlSerializer.SimpleXmlJsonSq().setProperty(SERIALIZER_useIndentation, true).setProperty(SERIALIZER_trimNullProperties, false).serialize(m); - assertEquals(xml1, xml2); - } - - //==================================================================================================== - // Test namespacing - //==================================================================================================== - @Test - public void testNamespaces() throws Exception { - String e = readFile(getClass().getResource("/xml/testNamespaces.xml").getPath()); - ObjectMap m = (ObjectMap) JsonParser.DEFAULT.parse(readFile(getClass().getResource("/xml/testComparisonWithJson.json").getPath()), Object.class); - String r = new XmlSerializer.XmlJsonSq() - .setProperty(XML_addNamespaceUrisToRoot, true) - .setProperty(XML_defaultNamespaceUri, "http://www.ibm.com") - .setProperty(SERIALIZER_useIndentation, true) - .setProperty(SERIALIZER_trimNullProperties, false) - .serialize(m); - assertEquals(e, r); - } - - //==================================================================================================== - // Test bean name annotation - //==================================================================================================== - @Test - public void testBeanNameAnnotation() throws Exception { - String e = - "<Person1 type='object'>\n" - + " <name>John Smith</name>\n" - + " <age type='number'>123</age>\n" - + "</Person1>\n"; - String r = new XmlSerializer.SimpleXmlJsonSq().setProperty(SERIALIZER_useIndentation, true).serialize(new Person1("John Smith", 123)); - assertEquals(e, r); - } - - /** Class with explicitly specified properties */ - @Bean(properties = { "name", "age" }) - @Xml(name="Person1") - 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 type='object'>\n" - + " <age type='number'>123</age>\n" - + "</Person1>\n"; - String r = new XmlSerializer.SimpleXmlJsonSq().setProperty(SERIALIZER_useIndentation, true).serialize(new Person1(null, 123)); - assertEquals(e, r); - } - - //==================================================================================================== - // Element name. - //==================================================================================================== - @Test - public void testElementName() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ; - A t = new A(); - String r = s.serialize(t); - assertEquals("<foo><f1>1</f1></foo>", r); - validateXml(t); - } - - @Xml(name="foo") - public static class A { - public int f1 = 1; - } - - //==================================================================================================== - // Element name on superclass. - //==================================================================================================== - @Test - public void testElementNameOnSuperclass() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_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_SIMPLE_SQ; - C3 t = new C3(); - String r = s.serialize(t); - assertEquals("<foo><f1>1</f1></foo>", r); - validateXml(t); - } - - @Xml(name="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_SIMPLE_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); - } - - @Xml(name="~!@#$%^&*()_+`-={}|[]\\:\";'<>?,.\n\r\t\b") - public static class D { - public int f1 = 1; - } - - //==================================================================================================== - // Element name can only be specified on classes. - //==================================================================================================== - @Test - public void testElementNameInInvalidLocations() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ; - E t = new E(); - try { - s.serialize(t); - fail("Exception not thrown"); - } catch (Exception e) { - assertEquals("org.apache.juneau.xml.CT_Xml$E: Annotation error on property 'f1'. Found @Xml.name annotation can only be specified on types.", e.getLocalizedMessage()); - } - } - - public static class E { - @Xml(name="foo") - public int f1 = 1; - } - - //==================================================================================================== - // Element name on collection. - //==================================================================================================== - @Test - public void testElementNameOnCollection() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ; - XmlParser p = XmlParser.DEFAULT; - F t = new F(); - t.add("bar"); - String r = s.serialize(t); - assertEquals("<foo><string>bar</string></foo>", r); - t = p.parse(r, F.class); - assertEquals("bar", t.get(0)); - validateXml(t); - } - - @Xml(name="foo") - public static class F extends LinkedList<String>{} - - //==================================================================================================== - // Field of type collection with element name. - // Element name should be ignored. - //==================================================================================================== - @Test - public void testIgnoreCollectionFieldWithElementName() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_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); - } - - @Xml(name="bar") - public static class G { - public F f1 = new F(); - } - - //==================================================================================================== - // Element name on not-a-bean. - //==================================================================================================== - @Test - public void testElementNameOnNotABean() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ; - H t = new H(); - String r = s.serialize(t); - assertEquals("<foo>bar</foo>", r); - } - - @Xml(name="foo") - public static class H { - @Override /* Object */ - public String toString() { - return "bar"; - } - } - - //==================================================================================================== - // Fields with element name on not-a-beans. - // Element name should be used on array field entries, but not regular field. - //==================================================================================================== - @Test - public void testFieldsWithElementNameOnNotABeans() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ; - I t = new I(); - String r = s.serialize(t); - assertEquals("<object><f1>bar</f1><f2><foo>bar</foo><foo>bar</foo></f2></object>", r); - } - - public static class I { - public H f1 = new H(); - public H[] f2 = { - new H(), - new H() - }; - } - - //==================================================================================================== - // Element name on beans of a collection. - //==================================================================================================== - @Test - public void testElementNameOnBeansOfCollection() throws Exception { - XmlSerializer s = XmlSerializer.DEFAULT_SIMPLE_SQ; - Object o = new J1(); - String r = s.serialize(o); - assertEquals("<foo><f1><bar><f2>2</f2></bar></f1></foo>", r); - } - - @Xml(name="foo") - public static class J1 { - @BeanProperty(properties={"f2"}) public List<J2> f1 = new LinkedList<J2>() {{ - add(new J2()); - }};} - - @Xml(name="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_SIMPLE_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_SIMPLE_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 { - XmlSerializer s = new XmlSerializer.SimpleSq(); - XmlParser p = XmlParser.DEFAULT; - M t = new M(); - String r = null; - r = s.serialize(t); - assertEquals("<object f1='1' f2='2' f3='3'/>", r); - s.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, true).setProperty(XML_autoDetectNamespaces, true).setProperty(SERIALIZER_trimNullProperties, false); - t.f1 = 4; t.f2 = 5; t.f3 = 6; - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:bar='http://bar' xmlns:foo='http://foo' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 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); - } - - @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_SIMPLE_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_SIMPLE_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 { - @BeanProperty(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_SIMPLE_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(beanUri=true, 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_SIMPLE_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(beanUri=true, name="url2") 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 { - XmlSerializer s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, false); - XmlParser p = XmlParser.DEFAULT; - - T1 t = new T1(); - String r = s.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.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz") - } - ); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - - // Auto-detect namespaces. - s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.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.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - //==================================================================================================== - // Namespace on class with element name. - //==================================================================================================== - @Test - public void testNsOnClassWithElementName() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, false); - XmlParser p = XmlParser.DEFAULT; - - T2 t = new T2(); - String r = s.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.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<foo:T2 xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz") - } - ); - r = s.serialize(t); - assertEquals("<foo:T2 xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - - // Auto-detect namespaces. - s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<T2><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T2>", r); - - s.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<T2><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T2>", r); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<foo:T2 xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - - //==================================================================================================== - // Namespace on package, no namespace on class. - //==================================================================================================== - @Test - public void testNsOnPackageNoNsOnClass() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq(); - XmlParser p = XmlParser.DEFAULT; - - T3 t = new T3(); - String r = s.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); - - s.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true).setProperty(XML_autoDetectNamespaces, false); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_autoDetectNamespaces, false); - s.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("p1","http://p1"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz") - } - ); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - - // Auto-detect namespaces. - s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r); - - s.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - //==================================================================================================== - // Namespace on package, no namespace on class, element name on class. - //==================================================================================================== - @Test - public void testNsOnPackageNoNsOnClassElementNameOnClass() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, false); - XmlParser p = XmlParser.DEFAULT; - - T4 t = new T4(); - String r = s.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.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<p1:T4 xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz"), - NamespaceFactory.get("p1","http://p1") - } - ); - r = s.serialize(t); - assertEquals("<p1:T4 xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:p1='http://p1' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - - // Auto-detect namespaces. - s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<T4><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T4>", r); - - s.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<T4><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T4>", r); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<p1:T4 xmlns='http://www.ibm.com/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - //==================================================================================================== - // Namespace on package, namespace on class, element name on class. - //==================================================================================================== - @Test - public void testNsOnPackageNsOnClassElementNameOnClass() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq(); - XmlParser p = XmlParser.DEFAULT; - - T5 t = new T5(); - String r = s.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); - - s.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false).setProperty(XML_autoDetectNamespaces, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<foo:T5 xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz") - } - ); - r = s.serialize(t); - assertEquals("<foo:T5 xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - - // Auto-detect namespaces. - s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<T5><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T5>", r); - validateXml(t, s); - - s.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<T5><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></T5>", r); - validateXml(t, s); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<foo:T5 xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - //==================================================================================================== - // Namespace on package, namespace on class, no element name on class. - //==================================================================================================== - @Test - public void testNsOnPackageNsOnClassNoElementNameOnClass() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, false); - XmlParser p = XmlParser.DEFAULT; - - T6 t = new T6(); - String r = s.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.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true).setProperty(XML_autoDetectNamespaces, false); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz") - } - ); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - - // Auto-detect namespaces. - s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r); - validateXml(t, s); - - s.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r); - validateXml(t, s); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - //==================================================================================================== - // Combination of namespaces and overridden bean property names. - //==================================================================================================== - @Test - public void testComboOfNsAndOverriddenBeanPropertyNames() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, false); - XmlParser p = XmlParser.DEFAULT; - - T7 t = new T7(); - String r = s.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.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true).setProperty(XML_autoDetectNamespaces, false); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz"), - NamespaceFactory.get("p1","http://p1") - } - ); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:p1='http://p1' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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 XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<object><g1>1</g1><g2>2</g2><g3>3</g3><g4>4</g4></object>", r); - - s.setProperty(XML_enableNamespaces, false); - r = s.serialize(t); - assertEquals("<object><g1>1</g1><g2>2</g2><g3>3</g3><g4>4</g4></object>", r); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:p1='http://p1' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - //==================================================================================================== - // @XmlNs annotation - //==================================================================================================== - @Test - public void testXmlNsAnnotation() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, false); - XmlParser p = XmlParser.DEFAULT; - - T8 t = new T8(); - String r = s.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.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, false).setProperty(XML_autoDetectNamespaces, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz") - } - ); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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 XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.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); - - s.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<object><f1>1</f1><f2>2</f2><f3>3</f3><f4>4</f4></object>", r); - validateXml(t, s); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:p2='http://p2' xmlns:p1='http://p1' xmlns:c1='http://c1' xmlns:f1='http://f1' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><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); - } - - //==================================================================================================== - // @Xml.ns on package, @Xml.nsUri not on package but in @XmlNs. - //==================================================================================================== - @Test - public void testXmlNsOnPackageNsUriInXmlNs() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, false); - XmlParser p = XmlParser.DEFAULT; - - T9 t = new T9(); - String r = s.serialize(t); - assertEquals("<object><f1>1</f1></object>", r); - assertTrue(t.equals(p.parse(r, T9.class))); - - s.setProperty(XML_enableNamespaces, true).setProperty(XML_autoDetectNamespaces, false).setProperty(XML_addNamespaceUrisToRoot, false); - r = s.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.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><p1:f1>1</p1:f1></object>", r); - - // Manually set namespaces - s.setProperty(XML_namespaces, - new Namespace[] { - NamespaceFactory.get("foo","http://foo"), - NamespaceFactory.get("bar","http://bar"), - NamespaceFactory.get("baz","http://baz") - } - ); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:foo='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><p1:f1>1</p1:f1></object>", r); - - // Auto-detect namespaces. - s = new XmlSerializer.SimpleSq().setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<object><f1>1</f1></object>", r); - assertTrue(t.equals(p.parse(r, T9.class))); - validateXml(t, s); - - s.setProperty(XML_addNamespaceUrisToRoot, true); - r = s.serialize(t); - assertEquals("<object><f1>1</f1></object>", r); - validateXml(t, s); - - s.setProperty(XML_enableNamespaces, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:p1='http://p1' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><p1:f1>1</p1:f1></object>", r); - assertTrue(t.equals(p.parse(r, T9.class))); - validateXml(t, s); - } - - //==================================================================================================== - // @Xml.format=ATTR - //==================================================================================================== - @Test - public void testXmlAttrs() throws Exception { - XmlSerializer s = new XmlSerializer.SimpleSq(); - XmlParser p = XmlParser.DEFAULT; - String r; - - Q t = new Q(); - t.f1 = new URL("http://xf1"); - t.f2 = "xf2"; - t.f3 = "xf3"; - r = s.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.setProperty(XML_enableNamespaces, true).setProperty(XML_addNamespaceUrisToRoot, true).setProperty(XML_autoDetectNamespaces, true); - r = s.serialize(t); - assertEquals("<object xmlns='http://www.ibm.com/2013/Juneau' xmlns:ns='http://ns' xmlns:nsf1='http://nsf1' xmlns:nsf3='http://nsf3' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' nsf1:f1='http://xf1' ns:f2='xf2' nsf3:x3='xf3'/>", r); - validateXml(t, s); - - 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
