001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.versioning;
019:
020: import java.io.StringWriter;
021:
022: import org.apache.commons.betwixt.AbstractTestCase;
023: import org.apache.commons.betwixt.AttributeDescriptor;
024: import org.apache.commons.betwixt.BindingConfiguration;
025: import org.apache.commons.betwixt.ElementDescriptor;
026: import org.apache.commons.betwixt.Options;
027: import org.apache.commons.betwixt.XMLBeanInfo;
028: import org.apache.commons.betwixt.XMLIntrospector;
029: import org.apache.commons.betwixt.io.BeanWriter;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: public class TestVersioning extends AbstractTestCase {
034: public static Log log = LogFactory.getLog(TestVersioning.class);
035:
036: public TestVersioning(String testName) {
037: super (testName);
038: }
039:
040: private void configure(BindingConfiguration configuration) {
041: configuration.setMapIDs(false);
042: }
043:
044: public void testIntrospection() throws Exception {
045: log.info("testIntrospection() started");
046:
047: XMLIntrospector introspector = new XMLIntrospector();
048: XMLBeanInfo beanInfo = introspector
049: .introspect(VersioningTestData.class);
050:
051: // 2 Element descriptors
052: ElementDescriptor[] elementDescriptors = beanInfo
053: .getElementDescriptor().getElementDescriptors();
054: assertEquals("Need 2 element descriptors", 2,
055: elementDescriptors.length);
056:
057: ElementDescriptor element1Descriptor = beanInfo
058: .getElementDescriptor()
059: .getElementDescriptor("element1");
060: log.info("element1Descriptor: " + element1Descriptor);
061: debugOptions(element1Descriptor.getOptions());
062: assertNotNull(element1Descriptor);
063: assertEquals("1", element1Descriptor.getOptions().getValue(
064: "version-from"));
065: assertNull(element1Descriptor.getOptions().getValue(
066: "version-until"));
067:
068: ElementDescriptor element2Descriptor = beanInfo
069: .getElementDescriptor()
070: .getElementDescriptor("element2");
071: log.info("element2Descriptor: " + element2Descriptor);
072: debugOptions(element2Descriptor.getOptions());
073: assertNotNull(element2Descriptor);
074: assertEquals("2", element2Descriptor.getOptions().getValue(
075: "version-from"));
076: assertNull(element2Descriptor.getOptions().getValue(
077: "version-until"));
078:
079: // 2 Attribute descriptors
080: AttributeDescriptor[] attributeDescriptors = beanInfo
081: .getElementDescriptor().getAttributeDescriptors();
082: assertEquals("Need 2 attribute descriptors", 2,
083: attributeDescriptors.length);
084:
085: AttributeDescriptor attribute1Descriptor = beanInfo
086: .getElementDescriptor().getAttributeDescriptor(
087: "attribute1");
088: log.info("attribute1Descriptor: " + attribute1Descriptor);
089: debugOptions(attribute1Descriptor.getOptions());
090: assertNotNull(attribute1Descriptor);
091: assertEquals("2", attribute1Descriptor.getOptions().getValue(
092: "version-from"));
093: assertNull(attribute1Descriptor.getOptions().getValue(
094: "version-until"));
095:
096: AttributeDescriptor attribute2Descriptor = beanInfo
097: .getElementDescriptor().getAttributeDescriptor(
098: "attribute2");
099: log.info("attribute2Descriptor: " + attribute2Descriptor);
100: debugOptions(attribute2Descriptor.getOptions());
101: assertNotNull(attribute2Descriptor);
102: assertEquals("1", attribute2Descriptor.getOptions().getValue(
103: "version-from"));
104: assertEquals("2", attribute2Descriptor.getOptions().getValue(
105: "version-until"));
106:
107: log.info("testIntrospection() complete");
108: }
109:
110: /**
111: * Simple test case with no version specified: All elements/attributes will
112: * be written.
113: *
114: * @throws Exception
115: */
116: public void testWrite1() throws Exception {
117: log.info("testWrite1() started");
118:
119: final VersioningTestData data = new VersioningTestData();
120: data.setAttribute1("attributevalue1");
121: data.setAttribute2("attributevalue2");
122: data.setElement1("elementvalue1");
123: data.setElement2("elementvalue2");
124:
125: StringWriter out = new StringWriter();
126: BeanWriter writer = new BeanWriter(out);
127: configure(writer.getBindingConfiguration());
128: writer.write(data);
129:
130: final String written = out.toString();
131: log.info("Written:\n" + written);
132:
133: final String expected = "<VersioningTestData attribute1=\"attributevalue1\" attribute2=\"attributevalue2\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
134: xmlAssertIsomorphicContent(parseString(expected),
135: parseString(written), true);
136:
137: log.info("testWrite1() complete");
138: }
139:
140: /**
141: * Version = 1
142: *
143: * <ul>
144: * <li>Attribute1 (2-/): Not written
145: * <li>Attribute2 (1-2): Written
146: * <li>Element1 (1-/): Written
147: * <li>Element2 (2-/): Not written
148: * </ul>
149: *
150: * @throws Exception
151: */
152: public void testWrite2() throws Exception {
153: log.info("testWrite2() started");
154:
155: final VersioningTestData data = new VersioningTestData();
156: data.setAttribute1("attributevalue1");
157: data.setAttribute2("attributevalue2");
158: data.setElement1("elementvalue1");
159: data.setElement2("elementvalue2");
160:
161: StringWriter out = new StringWriter();
162: BeanWriter writer = new BeanWriter(out);
163:
164: final VersioningStrategy versioningStrategy = new VersioningStrategy(
165: "1");
166: writer.getXMLIntrospector().getConfiguration()
167: .setAttributeSuppressionStrategy(versioningStrategy);
168: writer.getXMLIntrospector().getConfiguration()
169: .setElementSuppressionStrategy(versioningStrategy);
170:
171: configure(writer.getBindingConfiguration());
172: writer.write(data);
173:
174: final String written = out.toString();
175: log.info("Written:\n" + written);
176:
177: final String expected = "<VersioningTestData attribute2=\"attributevalue2\"><element1>elementvalue1</element1></VersioningTestData>";
178: xmlAssertIsomorphicContent(parseString(expected),
179: parseString(written), true);
180:
181: log.info("testWrite1() complete");
182: }
183:
184: private final void debugOptions(final Options options) {
185: final String[] names = options.getNames();
186:
187: log.info("Names:");
188:
189: for (int ii = 0; ii < names.length; ii++) {
190: final String name = names[ii];
191:
192: log.info(" Name " + ii + ": " + name + "="
193: + options.getValue(name));
194: }
195: }
196:
197: /**
198: * Version = 2
199: *
200: * <ul>
201: * <li>Attribute1 (2-/): written
202: * <li>Attribute2 (1-2): Written
203: * <li>Element1 (1-/): Written
204: * <li>Element2 (2-/): written
205: * </ul>
206: *
207: * @throws Exception
208: */
209: public void testWrite3() throws Exception {
210: log.info("testWrite2() started");
211:
212: final VersioningTestData data = new VersioningTestData();
213: data.setAttribute1("attributevalue1");
214: data.setAttribute2("attributevalue2");
215: data.setElement1("elementvalue1");
216: data.setElement2("elementvalue2");
217:
218: StringWriter out = new StringWriter();
219: BeanWriter writer = new BeanWriter(out);
220:
221: final VersioningStrategy versioningStrategy = new VersioningStrategy(
222: "2");
223: writer.getXMLIntrospector().getConfiguration()
224: .setAttributeSuppressionStrategy(versioningStrategy);
225: writer.getXMLIntrospector().getConfiguration()
226: .setElementSuppressionStrategy(versioningStrategy);
227:
228: configure(writer.getBindingConfiguration());
229: writer.write(data);
230:
231: final String written = out.toString();
232: log.info("Written:\n" + written);
233:
234: final String expected = "<VersioningTestData attribute1=\"attributevalue1\" attribute2=\"attributevalue2\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
235: xmlAssertIsomorphicContent(parseString(expected),
236: parseString(written), true);
237:
238: log.info("testWrite1() complete");
239: }
240:
241: /**
242: * Version = 3
243: *
244: * <ul>
245: * <li>Attribute1 (2-/): written
246: * <li>Attribute2 (1-2): Not Written
247: * <li>Element1 (1-/): Written
248: * <li>Element2 (2-/): written
249: * </ul>
250: *
251: * @throws Exception
252: */
253: public void testWrite4() throws Exception {
254: log.info("testWrite2() started");
255:
256: final VersioningTestData data = new VersioningTestData();
257: data.setAttribute1("attributevalue1");
258: data.setAttribute2("attributevalue2");
259: data.setElement1("elementvalue1");
260: data.setElement2("elementvalue2");
261:
262: StringWriter out = new StringWriter();
263: BeanWriter writer = new BeanWriter(out);
264:
265: final VersioningStrategy versioningStrategy = new VersioningStrategy(
266: "3");
267: writer.getXMLIntrospector().getConfiguration()
268: .setAttributeSuppressionStrategy(versioningStrategy);
269: writer.getXMLIntrospector().getConfiguration()
270: .setElementSuppressionStrategy(versioningStrategy);
271:
272: configure(writer.getBindingConfiguration());
273: writer.write(data);
274:
275: final String written = out.toString();
276: log.info("Written:\n" + written);
277:
278: final String expected = "<VersioningTestData attribute1=\"attributevalue1\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
279: xmlAssertIsomorphicContent(parseString(expected),
280: parseString(written), true);
281:
282: log.info("testWrite1() complete");
283: }
284: }
|