1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.versioning;
19
20 import java.io.StringWriter;
21
22 import org.apache.commons.betwixt.AbstractTestCase;
23 import org.apache.commons.betwixt.AttributeDescriptor;
24 import org.apache.commons.betwixt.BindingConfiguration;
25 import org.apache.commons.betwixt.ElementDescriptor;
26 import org.apache.commons.betwixt.Options;
27 import org.apache.commons.betwixt.XMLBeanInfo;
28 import org.apache.commons.betwixt.XMLIntrospector;
29 import org.apache.commons.betwixt.io.BeanWriter;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 public class TestVersioning extends AbstractTestCase {
34 public static Log log = LogFactory.getLog(TestVersioning.class);
35
36 public TestVersioning(String testName) {
37 super(testName);
38 }
39
40 private void configure(BindingConfiguration configuration) {
41 configuration.setMapIDs(false);
42 }
43
44 public void testIntrospection() throws Exception {
45 log.info("testIntrospection() started");
46
47 XMLIntrospector introspector = new XMLIntrospector();
48 XMLBeanInfo beanInfo = introspector
49 .introspect(VersioningTestData.class);
50
51
52 ElementDescriptor[] elementDescriptors = beanInfo
53 .getElementDescriptor().getElementDescriptors();
54 assertEquals("Need 2 element descriptors", 2, elementDescriptors.length);
55
56 ElementDescriptor element1Descriptor = beanInfo.getElementDescriptor()
57 .getElementDescriptor("element1");
58 log.info("element1Descriptor: " + element1Descriptor);
59 debugOptions(element1Descriptor.getOptions());
60 assertNotNull(element1Descriptor);
61 assertEquals("1", element1Descriptor.getOptions().getValue(
62 "version-from"));
63 assertNull(element1Descriptor.getOptions().getValue("version-until"));
64
65 ElementDescriptor element2Descriptor = beanInfo.getElementDescriptor()
66 .getElementDescriptor("element2");
67 log.info("element2Descriptor: " + element2Descriptor);
68 debugOptions(element2Descriptor.getOptions());
69 assertNotNull(element2Descriptor);
70 assertEquals("2", element2Descriptor.getOptions().getValue(
71 "version-from"));
72 assertNull(element2Descriptor.getOptions().getValue("version-until"));
73
74
75 AttributeDescriptor[] attributeDescriptors = beanInfo
76 .getElementDescriptor().getAttributeDescriptors();
77 assertEquals("Need 2 attribute descriptors", 2,
78 attributeDescriptors.length);
79
80 AttributeDescriptor attribute1Descriptor = beanInfo
81 .getElementDescriptor().getAttributeDescriptor("attribute1");
82 log.info("attribute1Descriptor: " + attribute1Descriptor);
83 debugOptions(attribute1Descriptor.getOptions());
84 assertNotNull(attribute1Descriptor);
85 assertEquals("2", attribute1Descriptor.getOptions().getValue(
86 "version-from"));
87 assertNull(attribute1Descriptor.getOptions().getValue("version-until"));
88
89 AttributeDescriptor attribute2Descriptor = beanInfo
90 .getElementDescriptor().getAttributeDescriptor("attribute2");
91 log.info("attribute2Descriptor: " + attribute2Descriptor);
92 debugOptions(attribute2Descriptor.getOptions());
93 assertNotNull(attribute2Descriptor);
94 assertEquals("1", attribute2Descriptor.getOptions().getValue(
95 "version-from"));
96 assertEquals("2", attribute2Descriptor.getOptions().getValue(
97 "version-until"));
98
99 log.info("testIntrospection() complete");
100 }
101
102
103
104
105
106
107
108 public void testWrite1() throws Exception {
109 log.info("testWrite1() started");
110
111 final VersioningTestData data = new VersioningTestData();
112 data.setAttribute1("attributevalue1");
113 data.setAttribute2("attributevalue2");
114 data.setElement1("elementvalue1");
115 data.setElement2("elementvalue2");
116
117 StringWriter out = new StringWriter();
118 BeanWriter writer = new BeanWriter(out);
119 configure(writer.getBindingConfiguration());
120 writer.write(data);
121
122 final String written = out.toString();
123 log.info("Written:\n" + written);
124
125 final String expected = "<VersioningTestData attribute1=\"attributevalue1\" attribute2=\"attributevalue2\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
126 xmlAssertIsomorphicContent(parseString(expected), parseString(written),
127 true);
128
129 log.info("testWrite1() complete");
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144 public void testWrite2() throws Exception {
145 log.info("testWrite2() started");
146
147 final VersioningTestData data = new VersioningTestData();
148 data.setAttribute1("attributevalue1");
149 data.setAttribute2("attributevalue2");
150 data.setElement1("elementvalue1");
151 data.setElement2("elementvalue2");
152
153 StringWriter out = new StringWriter();
154 BeanWriter writer = new BeanWriter(out);
155
156 final VersioningStrategy versioningStrategy = new VersioningStrategy(
157 "1");
158 writer.getXMLIntrospector().getConfiguration()
159 .setAttributeSuppressionStrategy(versioningStrategy);
160 writer.getXMLIntrospector().getConfiguration()
161 .setElementSuppressionStrategy(versioningStrategy);
162
163 configure(writer.getBindingConfiguration());
164 writer.write(data);
165
166 final String written = out.toString();
167 log.info("Written:\n" + written);
168
169 final String expected = "<VersioningTestData attribute2=\"attributevalue2\"><element1>elementvalue1</element1></VersioningTestData>";
170 xmlAssertIsomorphicContent(parseString(expected), parseString(written),
171 true);
172
173 log.info("testWrite1() complete");
174 }
175
176 private final void debugOptions(final Options options) {
177 final String[] names = options.getNames();
178
179 log.info("Names:");
180
181 for (int ii = 0; ii < names.length; ii++) {
182 final String name = names[ii];
183
184 log.info(" Name " + ii + ": " + name + "="
185 + options.getValue(name));
186 }
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public void testWrite3() throws Exception {
203 log.info("testWrite2() started");
204
205 final VersioningTestData data = new VersioningTestData();
206 data.setAttribute1("attributevalue1");
207 data.setAttribute2("attributevalue2");
208 data.setElement1("elementvalue1");
209 data.setElement2("elementvalue2");
210
211 StringWriter out = new StringWriter();
212 BeanWriter writer = new BeanWriter(out);
213
214 final VersioningStrategy versioningStrategy = new VersioningStrategy(
215 "2");
216 writer.getXMLIntrospector().getConfiguration()
217 .setAttributeSuppressionStrategy(versioningStrategy);
218 writer.getXMLIntrospector().getConfiguration()
219 .setElementSuppressionStrategy(versioningStrategy);
220
221 configure(writer.getBindingConfiguration());
222 writer.write(data);
223
224 final String written = out.toString();
225 log.info("Written:\n" + written);
226
227 final String expected = "<VersioningTestData attribute1=\"attributevalue1\" attribute2=\"attributevalue2\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
228 xmlAssertIsomorphicContent(parseString(expected), parseString(written),
229 true);
230
231 log.info("testWrite1() complete");
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 public void testWrite4() throws Exception {
248 log.info("testWrite2() started");
249
250 final VersioningTestData data = new VersioningTestData();
251 data.setAttribute1("attributevalue1");
252 data.setAttribute2("attributevalue2");
253 data.setElement1("elementvalue1");
254 data.setElement2("elementvalue2");
255
256 StringWriter out = new StringWriter();
257 BeanWriter writer = new BeanWriter(out);
258
259 final VersioningStrategy versioningStrategy = new VersioningStrategy(
260 "3");
261 writer.getXMLIntrospector().getConfiguration()
262 .setAttributeSuppressionStrategy(versioningStrategy);
263 writer.getXMLIntrospector().getConfiguration()
264 .setElementSuppressionStrategy(versioningStrategy);
265
266 configure(writer.getBindingConfiguration());
267 writer.write(data);
268
269 final String written = out.toString();
270 log.info("Written:\n" + written);
271
272 final String expected = "<VersioningTestData attribute1=\"attributevalue1\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
273 xmlAssertIsomorphicContent(parseString(expected), parseString(written),
274 true);
275
276 log.info("testWrite1() complete");
277 }
278 }