1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.schema;
18
19 import java.io.StringReader;
20 import java.io.StringWriter;
21 import java.io.Writer;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.betwixt.AbstractTestCase;
27 import org.apache.commons.betwixt.XMLIntrospector;
28 import org.apache.commons.betwixt.io.BeanReader;
29 import org.apache.commons.betwixt.io.BeanWriter;
30 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
31 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
32 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
33
34
35
36
37 /***
38 * This will test betwixt on handling a different kind of xml file, without
39 * a "collection" tag.
40 *
41 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
42 * @version $Id: TestSchema.java,v 1.10 2004/02/28 13:38:36 yoavs Exp $
43 */
44 public class TestSchema extends AbstractTestCase
45 {
46
47 public static Test suite()
48 {
49 return new TestSuite(TestSchema.class);
50 }
51
52
53 public TestSchema(String testName)
54 {
55 super(testName);
56 }
57
58 /***
59 * Test the roundtrip with an xml file that doesn't have
60 * collection elements, writes it with collection elements
61 * and then compares the 2 object, which should end up
62 * equal..
63 */
64 public void testCombinedRoundTrip()
65 throws Exception
66 {
67
68
69
70
71
72
73
74 BeanReader reader = createBeanReader();
75
76 PhysicalSchema schema = (PhysicalSchema) reader.parse(
77 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
78 StringWriter buffer = new StringWriter();
79 write(schema, buffer, true);
80
81
82
83 StringReader in = new StringReader(buffer.getBuffer().toString());
84 reader = createBeanReader();
85 XMLIntrospector intro = createXMLIntrospector();
86 DefaultXMLBeanInfoRegistry registry = new DefaultXMLBeanInfoRegistry();
87 intro.setRegistry(registry);
88
89
90
91 intro.setWrapCollectionsInElement(true);
92
93
94 registry.flush();
95
96 reader.setXMLIntrospector(intro);
97 reader.deregisterBeanClass(PhysicalSchema.class);
98 reader.registerBeanClass(PhysicalSchema.class);
99 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
100 buffer.close();
101 write(schema,buffer, true);
102 assertEquals(schema, schemaSecond);
103 }
104 /***
105 * Tests we can round trip from the XML -> bean -> XML -> bean.
106 * It will test if both object are identical.
107 * For this to actually work I implemented a details equals in my
108 * Beans..
109 */
110 public void testRoundTripWithoutCollectionElement()
111 throws Exception
112 {
113 BeanReader reader = createBeanReader();
114 PhysicalSchema schema = (PhysicalSchema) reader.parse(
115 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
116 StringWriter buffer = new StringWriter();
117 write(schema, buffer, false);
118 StringReader in = new StringReader(buffer.getBuffer().toString());
119 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
120 assertEquals(schemaSecond, schema);
121 }
122
123 /***
124 * Creates a beanReader
125 */
126 protected BeanReader createBeanReader()
127 throws Exception
128 {
129 BeanReader reader = new BeanReader();
130 reader.setXMLIntrospector(createXMLIntrospector());
131
132
133 reader.registerBeanClass(PhysicalSchema.class);
134 return reader;
135 }
136
137 /***
138 * Set up the XMLIntroSpector
139 */
140 protected XMLIntrospector createXMLIntrospector() {
141 XMLIntrospector introspector = new XMLIntrospector();
142
143
144 introspector.setAttributesForPrimitives(true);
145
146
147
148
149 introspector.setWrapCollectionsInElement(false);
150
151
152
153
154
155
156
157 introspector.setElementNameMapper(new HyphenatedNameMapper(true, "_"));
158
159
160
161 introspector.setAttributeNameMapper(new DecapitalizeNameMapper());
162
163 return introspector;
164 }
165
166 /***
167 * Opens a writer and writes an object model according to the
168 * retrieved bean
169 */
170 private void write(Object bean, Writer out, boolean wrapCollectionsInElement)
171 throws Exception
172 {
173 BeanWriter writer = new BeanWriter(out);
174 writer.setWriteEmptyElements( true );
175 writer.setXMLIntrospector(createXMLIntrospector());
176
177 writer.getXMLIntrospector().setWrapCollectionsInElement(wrapCollectionsInElement);
178
179
180 writer.setWriteIDs(false);
181
182 writer.setIndent(" ");
183 writer.setEndOfLine("\n");
184 writer.write(bean);
185 }
186 }
187