1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.nowrap;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.StringWriter;
23 import java.util.List;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27
28 import org.apache.commons.betwixt.AbstractTestCase;
29 import org.apache.commons.betwixt.XMLIntrospector;
30 import org.apache.commons.betwixt.io.BeanReader;
31 import org.apache.commons.betwixt.io.BeanWriter;
32 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
33 import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
34
35
36
37
38
39
40
41 public class TestNoWrap
42 extends AbstractTestCase
43 {
44 private POTest po;
45
46
47
48
49 public static Test suite()
50 {
51 return new TestSuite(TestNoWrap.class);
52 }
53
54
55
56
57
58
59 public TestNoWrap(String testName)
60 {
61 super(testName);
62 }
63
64
65
66
67 public void testRoundTrip()
68 throws Exception
69 {
70 load();
71 write();
72 }
73
74
75
76
77 public void load()
78 throws Exception
79 {
80 String xmlLocation = getTestFile("src/test/org/apache/commons/betwixt/nowrap/po_add_test.xml");
81
82 FileInputStream in = new FileInputStream(new File(xmlLocation));
83
84
85 BeanReader reader = createBeanReader(POTest.class);
86 po = (POTest) reader.parse(in);
87 assertEquals("PO Printing No", "555008805581", po.getPrintingNumber());
88 List componentTests = po.getComponenttests();
89
90 assertEquals("#Component tests", 3, componentTests.size());
91 Componenttest testOne = (Componenttest) componentTests.get(0);
92 assertEquals("Component Test One", "Text", testOne.getCompDescription());
93 Componenttest testTwo = (Componenttest) componentTests.get(1);
94 assertEquals("Component Test Two", "Binding", testTwo.getCompDescription());
95 Componenttest testThree = (Componenttest) componentTests.get(2);
96 assertEquals("Component Test Three", "Paper Cover", testThree.getCompDescription());
97 }
98
99
100
101
102 public void write()
103 throws Exception
104 {
105
106 StringWriter out = new StringWriter();
107 out.write("<?xml version='1.0'?>");
108 BeanWriter beanWriter = new BeanWriter(out);
109 beanWriter.setXMLIntrospector(createXMLIntrospector());
110 beanWriter.getBindingConfiguration().setMapIDs(false);
111 beanWriter.setEndOfLine("\n");
112 beanWriter.enablePrettyPrint();
113
114 beanWriter.write(po);
115 String xml = "<?xml version='1.0'?><content><printingno>555008805581</printingno>"
116 + "<componenttest><compdescription>Text</compdescription></componenttest>"
117 + "<componenttest><compdescription>Binding</compdescription></componenttest>"
118 + "<componenttest><compdescription>Paper Cover</compdescription>"
119 + "</componenttest></content>";
120
121 xmlAssertIsomorphicContent(
122 parseString(xml),
123 parseString(out.getBuffer().toString()),
124 true);
125 }
126
127
128
129
130
131
132
133 protected BeanReader createBeanReader(Class beanClass)
134 throws Exception
135 {
136 BeanReader reader = new BeanReader();
137 reader.setXMLIntrospector(createXMLIntrospector());
138 reader.registerBeanClass(beanClass);
139 return reader;
140 }
141
142
143
144
145
146
147 protected XMLIntrospector createXMLIntrospector()
148 {
149 XMLIntrospector introspector = new XMLIntrospector();
150
151
152 introspector.getConfiguration().setAttributesForPrimitives(false);
153
154
155 introspector.getConfiguration().setWrapCollectionsInElement(false);
156
157
158 introspector.getConfiguration().setElementNameMapper( new DecapitalizeNameMapper() );
159
160
161 introspector.getConfiguration().setPluralStemmer( new DefaultPluralStemmer() );
162
163 return introspector;
164 }
165 }
166