1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.io;
18
19 import java.io.StringReader;
20 import java.io.StringWriter;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.betwixt.AbstractTestCase;
25 import org.xml.sax.InputSource;
26
27
28 public class TestMixedCollection extends AbstractTestCase {
29 public TestMixedCollection(String name) {
30 super(name);
31 }
32
33 public void testWithDefaults() throws Exception {
34 toXml(true);
35 }
36
37 public void testWithoutDefaults() throws Exception {
38 toXml(false);
39 }
40
41 protected void toXml(boolean addAdders) throws Exception {
42 StringReader configReader = new StringReader(
43 "<?xml version='1.0' ?>"
44 + "<betwixt-config primitiveTypes='attribute'>"
45 + " <class name='org.apache.commons.betwixt.io.TestMixedCollection$ParentBean'>"
46 + " <element name='parentBean'>"
47 + " <element name='childBeans'>"
48 + " <element property='childBeans'/>"
49 + " </element>"
50 + " <addDefaults add-properties='true' guess-names='false' add-adders='"
51 + addAdders
52 + "'/>"
53 + " </element>"
54 + " </class>"
55 + " <class name='org.apache.commons.betwixt.io.TestMixedCollection$ChildBean1'>"
56 + " <element name='childBean1'>"
57 + " <addDefaults/>"
58 + " </element>"
59 + " </class>"
60 + " <class name='org.apache.commons.betwixt.io.TestMixedCollection$ChildBean2'>"
61 + " <element name='childBean2'>"
62 + " <addDefaults/>" + " </element>"
63 + " </class>" + "</betwixt-config>");
64
65 ParentBean pb = new ParentBean();
66 pb.setStuff("stuff");
67 ChildBean1 cb1 = new ChildBean1();
68 pb.getChildBeans().add(cb1);
69 ChildBean2 cb2 = new ChildBean2();
70 pb.getChildBeans().add(cb2);
71
72 StringWriter writer = new StringWriter();
73 BeanWriter beanWriter = new BeanWriter(writer);
74 beanWriter.enablePrettyPrint();
75 beanWriter.getXMLIntrospector().register(
76 new InputSource(configReader));
77 beanWriter.writeXmlDeclaration("<?xml version=\"1.0\"?>");
78 beanWriter.write(pb);
79
80 String expected = "<?xml version='1.0'?>" +
81 "<parentBean stuff='stuff' id='1'>" +
82 " <childBeans>" +
83 " <childBean1/>" +
84 " <childBean2/>" +
85 " </childBeans>" +
86 "</parentBean>";
87
88 xmlAssertIsomorphic(parseString(expected), parseString(writer));
89 }
90
91 public static class ParentBean {
92 private List childBeans = new ArrayList();
93
94 private String stuff = null;
95
96 public List getChildBeans() {
97 return childBeans;
98 }
99
100 public void setChildBeans(List childBeans) {
101 this.childBeans = childBeans;
102 }
103
104 public void addChildBean(ChildBean childBean) {
105 getChildBeans().add(childBean);
106 }
107
108 public String getStuff() {
109 return stuff;
110 }
111
112 public void setStuff(String stuff) {
113 this.stuff = stuff;
114 }
115 }
116
117 public static abstract class ChildBean {
118 }
119
120 public static class ChildBean1 extends ChildBean {
121 }
122
123 public static class ChildBean2 extends ChildBean {
124 }
125 }