1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.schema;
19
20 import java.io.StringReader;
21 import java.io.StringWriter;
22 import java.io.Writer;
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.registry.DefaultXMLBeanInfoRegistry;
32 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
33 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
34
35
36
37
38
39
40
41
42
43
44
45 public class TestSchema extends AbstractTestCase
46 {
47
48 public static Test suite()
49 {
50 return new TestSuite(TestSchema.class);
51 }
52
53
54 public TestSchema(String testName)
55 {
56 super(testName);
57 }
58
59
60
61
62
63
64
65 public void testCombinedRoundTrip()
66 throws Exception
67 {
68
69
70
71
72
73
74
75 BeanReader reader = createBeanReader();
76
77 PhysicalSchema schema = (PhysicalSchema) reader.parse(
78 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
79 StringWriter buffer = new StringWriter();
80 write(schema, buffer, true);
81
82
83
84 StringReader in = new StringReader(buffer.getBuffer().toString());
85 reader = createBeanReader();
86 XMLIntrospector intro = createXMLIntrospector();
87 DefaultXMLBeanInfoRegistry registry = new DefaultXMLBeanInfoRegistry();
88 intro.setRegistry(registry);
89
90
91
92 intro.getConfiguration().setWrapCollectionsInElement(true);
93
94
95 registry.flush();
96
97 reader.setXMLIntrospector(intro);
98 reader.deregisterBeanClass(PhysicalSchema.class);
99 reader.getRules().clear();
100 reader.registerBeanClass(PhysicalSchema.class);
101 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
102 buffer.close();
103 write(schema,buffer, true);
104 assertEquals(schema, schemaSecond);
105 }
106
107
108
109
110
111
112 public void testRoundTripWithoutCollectionElement()
113 throws Exception
114 {
115 BeanReader reader = createBeanReader();
116 PhysicalSchema schema = (PhysicalSchema) reader.parse(
117 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
118 StringWriter buffer = new StringWriter();
119 write(schema, buffer, false);
120 StringReader in = new StringReader(buffer.getBuffer().toString());
121 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
122 assertEquals(schemaSecond, schema);
123 }
124
125
126
127
128 protected BeanReader createBeanReader()
129 throws Exception
130 {
131 BeanReader reader = new BeanReader();
132 reader.setXMLIntrospector(createXMLIntrospector());
133
134
135 reader.registerBeanClass(PhysicalSchema.class);
136 return reader;
137 }
138
139
140
141
142 protected XMLIntrospector createXMLIntrospector() {
143 XMLIntrospector introspector = new XMLIntrospector();
144
145
146 introspector.getConfiguration().setAttributesForPrimitives(true);
147
148
149
150
151 introspector.getConfiguration().setWrapCollectionsInElement(false);
152
153
154
155
156
157
158
159 introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper(true, "_"));
160
161
162
163 introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper());
164
165 return introspector;
166 }
167
168
169
170
171
172 private void write(Object bean, Writer out, boolean wrapCollectionsInElement)
173 throws Exception
174 {
175 BeanWriter writer = new BeanWriter(out);
176 writer.setWriteEmptyElements( true );
177 writer.setXMLIntrospector(createXMLIntrospector());
178
179 writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(wrapCollectionsInElement);
180
181
182 writer.getBindingConfiguration().setMapIDs(false);
183
184 writer.setIndent(" ");
185 writer.setEndOfLine("\n");
186 writer.write(bean);
187 }
188 }
189