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.11 2004/06/13 21:32:48 rdonkin 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.getConfiguration().setWrapCollectionsInElement(true);
92
93
94 registry.flush();
95
96 reader.setXMLIntrospector(intro);
97 reader.deregisterBeanClass(PhysicalSchema.class);
98 reader.getRules().clear();
99 reader.registerBeanClass(PhysicalSchema.class);
100 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
101 buffer.close();
102 write(schema,buffer, true);
103 assertEquals(schema, schemaSecond);
104 }
105 /***
106 * Tests we can round trip from the XML -> bean -> XML -> bean.
107 * It will test if both object are identical.
108 * For this to actually work I implemented a details equals in my
109 * Beans..
110 */
111 public void testRoundTripWithoutCollectionElement()
112 throws Exception
113 {
114 BeanReader reader = createBeanReader();
115 PhysicalSchema schema = (PhysicalSchema) reader.parse(
116 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
117 StringWriter buffer = new StringWriter();
118 write(schema, buffer, false);
119 StringReader in = new StringReader(buffer.getBuffer().toString());
120 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
121 assertEquals(schemaSecond, schema);
122 }
123
124 /***
125 * Creates a beanReader
126 */
127 protected BeanReader createBeanReader()
128 throws Exception
129 {
130 BeanReader reader = new BeanReader();
131 reader.setXMLIntrospector(createXMLIntrospector());
132
133
134 reader.registerBeanClass(PhysicalSchema.class);
135 return reader;
136 }
137
138 /***
139 * Set up the XMLIntroSpector
140 */
141 protected XMLIntrospector createXMLIntrospector() {
142 XMLIntrospector introspector = new XMLIntrospector();
143
144
145 introspector.getConfiguration().setAttributesForPrimitives(true);
146
147
148
149
150 introspector.getConfiguration().setWrapCollectionsInElement(false);
151
152
153
154
155
156
157
158 introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper(true, "_"));
159
160
161
162 introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper());
163
164 return introspector;
165 }
166
167 /***
168 * Opens a writer and writes an object model according to the
169 * retrieved bean
170 */
171 private void write(Object bean, Writer out, boolean wrapCollectionsInElement)
172 throws Exception
173 {
174 BeanWriter writer = new BeanWriter(out);
175 writer.setWriteEmptyElements( true );
176 writer.setXMLIntrospector(createXMLIntrospector());
177
178 writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(wrapCollectionsInElement);
179
180
181 writer.getBindingConfiguration().setMapIDs(false);
182
183 writer.setIndent(" ");
184 writer.setEndOfLine("\n");
185 writer.write(bean);
186 }
187 }
188