1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.dotbetwixt;
18
19 import java.io.StringWriter;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import org.apache.commons.betwixt.io.BeanWriter;
25 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
26 import org.apache.commons.betwixt.xmlunit.XmlTestCase;
27
28
29 /***
30 * Provides xml test utilities.
31 * Hopefully, these might be moved into [xmlunit] sometime.
32 *
33 * @author Robert Burrell Donkin
34 */
35 public class TestBeanToXml extends XmlTestCase {
36
37
38
39 public static Test suite() {
40 return new TestSuite(TestBeanToXml.class);
41 }
42
43
44
45 public TestBeanToXml(String testName) {
46 super(testName);
47 }
48
49
50
51 public void testOne() throws Exception {
52
53 xmlAssertIsomorphicContent(
54 parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"),
55 parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"));
56 }
57
58 public void testSimpleBean() throws Exception {
59 StringWriter out = new StringWriter();
60 out.write("<?xml version='1.0' encoding='UTF-8'?>");
61
62
63 BeanWriter writer = new BeanWriter(out);
64 writer.setWriteEmptyElements( true );
65
66
67
68
69
70 writer.setWriteIDs(false);
71 SimpleTestBean bean = new SimpleTestBean("alpha-value","beta-value","gamma-value");
72 writer.write(bean);
73 out.flush();
74 String xml = out.toString();
75
76 xmlAssertIsomorphicContent(
77 parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/simpletestone.xml"),
78 parseString(xml));
79
80 }
81
82 public void testWriteRecursiveBean() throws Exception {
83
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 * This tests that only well formed names for elements and attributes are allowed by .betwixt files
111 */
112 public void testBadDotBetwixtNames() throws Exception {
113
114
115 StringWriter out = new StringWriter();
116 out.write("<?xml version='1.0' encoding='UTF-8'?>");
117 BeanWriter writer = new BeanWriter(out);
118 writer.setWriteEmptyElements( true );
119 writer.write(new BadDotBetwixtNamesBean("one", "two"));
120
121
122
123
124 parseString(out.toString());
125 }
126
127 /*** Test output of bean with mixed content */
128 public void testMixedContent() throws Exception {
129 StringWriter out = new StringWriter();
130 out.write("<?xml version='1.0' encoding='UTF-8'?>");
131 BeanWriter writer = new BeanWriter( out );
132 writer.write( new MixedContentBean("First", "Last", "Always") );
133
134 String xml = "<?xml version='1.0' encoding='UTF-8'?><foo version='1.0'>"
135 + "<bar version='First'>Fiddle sticks<baa>Last</baa>Always</bar></foo>";
136
137 xmlAssertIsomorphicContent(
138 parseString(xml),
139 parseString(out.toString()));
140 }
141
142 /*** Test output of bean with mixed content */
143 public void testSimpleMixedContent() throws Exception {
144 StringWriter out = new StringWriter();
145 out.write("<?xml version='1.0' encoding='UTF-8'?>");
146 BeanWriter writer = new BeanWriter( out );
147 writer.write( new MixedContentOne("Life,", "The Universe And Everything", 42) );
148
149 String xml = "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life,' gamma='42'>"
150 + "The Universe And Everything</deep-thought>";
151
152 xmlAssertIsomorphicContent(
153 parseString(xml),
154 parseString(out.toString()));
155 }
156
157 /*** Tests basic use of an implementation for an interface */
158 public void testBasicInterfaceImpl() throws Exception {
159 ExampleBean bean = new ExampleBean("Alice");
160 bean.addExample(new ExampleImpl(1, "Mad Hatter"));
161 bean.addExample(new ExampleImpl(2, "March Hare"));
162 bean.addExample(new ExampleImpl(3, "Dormouse"));
163
164 StringWriter out = new StringWriter();
165 out.write("<?xml version='1.0' encoding='UTF-8'?>");
166
167 BeanWriter writer = new BeanWriter( out );
168 writer.getXMLIntrospector().setElementNameMapper(new HyphenatedNameMapper());
169 writer.getXMLIntrospector().setWrapCollectionsInElement(false);
170
171 writer.write( bean );
172
173 String xml = "<?xml version='1.0' encoding='UTF-8'?>"
174 + "<example-bean><name>Alice</name>"
175 + "<example><id>1</id><name>Mad Hatter</name></example>"
176 + "<example><id>2</id><name>March Hare</name></example>"
177 + "<example><id>3</id><name>Dormouse</name></example>"
178 + "</example-bean>";
179
180 xmlAssertIsomorphicContent(
181 parseString(xml),
182 parseString(out.toString()),
183 true);
184 }
185 }
186