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