1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.dotbetwixt;
19
20 import java.io.StringWriter;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.betwixt.io.BeanWriter;
26 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
27 import org.apache.commons.betwixt.xmlunit.XmlTestCase;
28
29
30
31
32
33
34
35
36 public class TestBeanToXml extends XmlTestCase {
37
38
39
40 public static Test suite() {
41 return new TestSuite(TestBeanToXml.class);
42 }
43
44
45
46 public TestBeanToXml(String testName) {
47 super(testName);
48 }
49
50
51
52 public void testOne() throws Exception {
53
54 xmlAssertIsomorphicContent(
55 parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"),
56 parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"));
57 }
58
59 public void testSimpleBean() throws Exception {
60 StringWriter out = new StringWriter();
61 out.write("<?xml version='1.0' encoding='UTF-8'?>");
62
63
64 BeanWriter writer = new BeanWriter(out);
65 writer.setWriteEmptyElements( true );
66
67
68
69
70
71 writer.getBindingConfiguration().setMapIDs(false);
72 SimpleTestBean bean = new SimpleTestBean("alpha-value","beta-value","gamma-value");
73 writer.write(bean);
74 out.flush();
75 String xml = out.toString();
76
77 xmlAssertIsomorphicContent(
78 parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/simpletestone.xml"),
79 parseString(xml));
80
81 }
82
83 public void testWriteRecursiveBean() throws Exception {
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 }
109
110
111
112
113 public void testBadDotBetwixtNames() throws Exception {
114
115
116 StringWriter out = new StringWriter();
117 out.write("<?xml version='1.0' encoding='UTF-8'?>");
118 BeanWriter writer = new BeanWriter(out);
119 writer.setWriteEmptyElements( true );
120 writer.write(new BadDotBetwixtNamesBean("one", "two"));
121
122
123
124
125 parseString(out.toString());
126 }
127
128
129 public void testMixedContent() throws Exception {
130 StringWriter out = new StringWriter();
131 out.write("<?xml version='1.0' encoding='UTF-8'?>");
132 BeanWriter writer = new BeanWriter( out );
133 writer.getBindingConfiguration().setMapIDs(false);
134 writer.write( new MixedContentBean("First", "Last", "Always") );
135
136 String xml = "<?xml version='1.0' encoding='UTF-8'?><foo version='1.0'>"
137 + "<bar version='First'>Fiddle sticks<baa>Last</baa>Always</bar></foo>";
138
139 xmlAssertIsomorphicContent(
140 parseString(xml),
141 parseString(out.toString()));
142 }
143
144
145 public void testSimpleMixedContent() throws Exception {
146 StringWriter out = new StringWriter();
147 out.write("<?xml version='1.0' encoding='UTF-8'?>");
148 BeanWriter writer = new BeanWriter( out );
149 writer.getBindingConfiguration().setMapIDs(false);
150 writer.write( new MixedContentOne("Life,", "The Universe And Everything", 42) );
151
152 String xml = "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life,' gamma='42'>"
153 + "The Universe And Everything</deep-thought>";
154
155 xmlAssertIsomorphicContent(
156 parseString(xml),
157 parseString(out.toString()));
158 }
159
160
161 public void testBasicInterfaceImpl() throws Exception {
162 ExampleBean bean = new ExampleBean("Alice");
163 bean.addExample(new ExampleImpl(1, "Mad Hatter"));
164 bean.addExample(new ExampleImpl(2, "March Hare"));
165 bean.addExample(new ExampleImpl(3, "Dormouse"));
166
167 StringWriter out = new StringWriter();
168 out.write("<?xml version='1.0' encoding='UTF-8'?>");
169
170 BeanWriter writer = new BeanWriter( out );
171 writer.getBindingConfiguration().setMapIDs(false);
172 writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
173 writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
174
175 writer.write( bean );
176
177 String xml = "<?xml version='1.0' encoding='UTF-8'?>"
178 + "<example-bean><name>Alice</name>"
179 + "<example><id>1</id><name>Mad Hatter</name></example>"
180 + "<example><id>2</id><name>March Hare</name></example>"
181 + "<example><id>3</id><name>Dormouse</name></example>"
182 + "</example-bean>";
183
184 xmlAssertIsomorphicContent(
185 parseString(xml),
186 parseString(out.toString()),
187 true);
188 }
189 }
190