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: package org.apache.commons.betwixt;
018:
019: import java.beans.IntrospectionException;
020: import java.io.IOException;
021: import java.io.StringReader;
022: import java.io.StringWriter;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.commons.betwixt.io.BeanReader;
028: import org.apache.commons.betwixt.io.BeanWriter;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * Tests the multi-mapping of collections with polymorphic entries.
034: *
035: * @author Thomas Dudziak (tomdz@apache.org)
036: */
037: public class TestMultipleCollectionMappings extends AbstractTestCase {
038: public static class Container {
039: private List _elements = new ArrayList();
040: private List _nodes = new ArrayList();
041:
042: public Iterator getElements() {
043: return _elements.iterator();
044: }
045:
046: public void addElement(Element element) {
047: _elements.add(element);
048: }
049:
050: public Iterator getNodes() {
051: return _nodes.iterator();
052: }
053:
054: public void addNode(Node1 node) {
055: _nodes.add(node);
056: }
057: }
058:
059: public static interface Element {
060: }
061:
062: public static class ElementA implements Element {
063: }
064:
065: public static class ElementB implements Element {
066: }
067:
068: public static class ElementC {
069: }
070:
071: public static class Node1 {
072: private String name;
073:
074: public String getName() {
075: return name;
076: }
077:
078: public void setName(String name) {
079: this .name = name;
080: }
081: }
082:
083: public static class Node2 extends Node1 {
084: private String name;
085:
086: public String getName() {
087: return name;
088: }
089:
090: public void setName(String name) {
091: this .name = name;
092: }
093: }
094:
095: public static class Node3 {
096: private List _innerNodes = new ArrayList();
097:
098: public Iterator getInnerNodes() {
099: return _innerNodes.iterator();
100: }
101:
102: public void addInnerNode(InnerNode node) {
103: _innerNodes.add(node);
104: }
105: }
106:
107: public static class InnerNode {
108: }
109:
110: private static final String MAPPING = "<?xml version=\"1.0\"?>\n"
111: + "<betwixt-config>\n" + " <class name=\""
112: + Container.class.getName()
113: + "\">\n"
114: + " <element name=\"container\">\n"
115: + " <element property=\"elements\" updater=\"addElement\"/>\n"
116: + " <element property=\"nodes\" updater=\"addNode\"/>\n"
117: + " </element>\n"
118: + " </class>\n"
119: + " <class name=\""
120: + ElementA.class.getName()
121: + "\">\n"
122: + " <element name=\"elementA\"/>\n"
123: + " </class>\n"
124: + " <class name=\""
125: + ElementB.class.getName()
126: + "\">\n"
127: + " <element name=\"elementB\"/>\n"
128: + " </class>\n"
129: + " <class name=\""
130: + ElementC.class.getName()
131: + "\">\n"
132: + " <element name=\"elementC\"/>\n"
133: + " </class>\n"
134: + " <class name=\""
135: + Node1.class.getName()
136: + "\">\n"
137: + " <element name=\"node1\">\n"
138: + " <attribute name=\"name\" property=\"name\"/>\n"
139: + " </element>\n"
140: + " </class>\n"
141: + " <class name=\""
142: + Node2.class.getName()
143: + "\">\n"
144: + " <element name=\"node2\">\n"
145: + " <attribute name=\"name\" property=\"name\"/>\n"
146: + " </element>\n"
147: + " </class>\n"
148: + " <class name=\""
149: + Node3.class.getName()
150: + "\">\n"
151: + " <element name=\"node2\">\n"
152: + " <attribute name=\"name\" property=\"name\"/>\n"
153: + " <element property=\"innerNodes\" updater=\"addInnerNode\"/>\n"
154: + " </element>\n"
155: + " </class>\n"
156: + " <class name=\""
157: + InnerNode.class.getName()
158: + "\">\n"
159: + " <element name=\"innerNode\"/>\n"
160: + " </class>\n"
161: + "</betwixt-config>";
162: private static final String EXPECTED1 = "<?xml version=\"1.0\" ?>\n"
163: + " <container>\n"
164: + " <elementB/>\n"
165: + " <elementA/>\n" + " </container>\n";
166: private static final String EXPECTED2 = "<?xml version=\"1.0\" ?>\n"
167: + " <container>\n"
168: + " <node1/>\n"
169: + " <node2/>\n"
170: + " <node2/>\n" + " </container>\n";
171: private static final String EXPECTED3 = "<?xml version=\"1.0\" ?>\n"
172: + " <container>\n"
173: + " <elementA/>\n"
174: + " <elementB/>\n"
175: + " <node2/>\n"
176: + " <node1/>\n"
177: + " <node2/>\n" + " </container>\n";
178: private static final String INVALID_XML = "<?xml version=\"1.0\" ?>\n"
179: + " <container>\n"
180: + " <elementA/>\n"
181: + " <elementC/>\n"
182: + " <node3 name=\"test\"/>\n"
183: + " <innerNode/>\n" + " </container>\n";
184:
185: public TestMultipleCollectionMappings(String testName) {
186: super (testName);
187: }
188:
189: public void testOnlyElements() throws IOException, SAXException,
190: IntrospectionException {
191: Container container = new Container();
192:
193: container.addElement(new ElementB());
194: container.addElement(new ElementA());
195:
196: StringWriter outputWriter = new StringWriter();
197:
198: outputWriter.write("<?xml version=\"1.0\" ?>\n");
199:
200: BeanWriter beanWriter = new BeanWriter(outputWriter);
201: beanWriter.setEndOfLine("\n");
202: beanWriter.enablePrettyPrint();
203: beanWriter.setWriteEmptyElements(true);
204: beanWriter.getBindingConfiguration().setMapIDs(false);
205: beanWriter.getXMLIntrospector().register(
206: new InputSource(new StringReader(MAPPING)));
207: beanWriter.setEndOfLine("\n"); //force to \n so expected values match for sure
208: beanWriter.write(container);
209:
210: String output = outputWriter.toString();
211:
212: assertEquals(EXPECTED1, output);
213:
214: BeanReader beanReader = new BeanReader();
215:
216: beanReader.registerMultiMapping(new InputSource(
217: new StringReader(MAPPING)));
218:
219: StringReader xmlReader = new StringReader(output);
220:
221: container = (Container) beanReader.parse(xmlReader);
222:
223: Iterator it = container.getElements();
224:
225: assertTrue(it.next() instanceof ElementB);
226: assertTrue(it.next() instanceof ElementA);
227: assertFalse(it.hasNext());
228:
229: assertFalse(container.getNodes().hasNext());
230: }
231:
232: public void testOnlyNodes() throws IOException, SAXException,
233: IntrospectionException {
234: Container container = new Container();
235:
236: container.addNode(new Node1());
237: container.addNode(new Node2());
238: container.addNode(new Node2());
239:
240: StringWriter outputWriter = new StringWriter();
241:
242: outputWriter.write("<?xml version=\"1.0\" ?>\n");
243:
244: BeanWriter beanWriter = new BeanWriter(outputWriter);
245: beanWriter.setEndOfLine("\n");
246: beanWriter.enablePrettyPrint();
247: beanWriter.setWriteEmptyElements(true);
248: beanWriter.getBindingConfiguration().setMapIDs(false);
249: beanWriter.getXMLIntrospector().register(
250: new InputSource(new StringReader(MAPPING)));
251: beanWriter.setEndOfLine("\n"); //force to \n so expected values match for sure
252: beanWriter.write(container);
253:
254: String output = outputWriter.toString();
255:
256: assertEquals(EXPECTED2, output);
257:
258: BeanReader beanReader = new BeanReader();
259:
260: beanReader.registerMultiMapping(new InputSource(
261: new StringReader(MAPPING)));
262:
263: StringReader xmlReader = new StringReader(output);
264:
265: container = (Container) beanReader.parse(xmlReader);
266:
267: assertFalse(container.getElements().hasNext());
268:
269: Iterator it = container.getNodes();
270:
271: assertTrue(it.next() instanceof Node1);
272: assertTrue(it.next() instanceof Node2);
273: assertTrue(it.next() instanceof Node2);
274: assertFalse(it.hasNext());
275: }
276:
277: public void testMixed() throws IOException, SAXException,
278: IntrospectionException {
279: Container container = new Container();
280:
281: container.addNode(new Node2());
282: container.addNode(new Node1());
283: container.addNode(new Node2());
284: container.addElement(new ElementA());
285: container.addElement(new ElementB());
286:
287: StringWriter outputWriter = new StringWriter();
288:
289: outputWriter.write("<?xml version=\"1.0\" ?>\n");
290:
291: BeanWriter beanWriter = new BeanWriter(outputWriter);
292: beanWriter.setEndOfLine("\n");
293: beanWriter.enablePrettyPrint();
294: beanWriter.setWriteEmptyElements(true);
295: beanWriter.getBindingConfiguration().setMapIDs(false);
296: beanWriter.getXMLIntrospector().register(
297: new InputSource(new StringReader(MAPPING)));
298: beanWriter.setEndOfLine("\n"); //force to \n so expected values match for sure
299: beanWriter.write(container);
300:
301: String output = outputWriter.toString();
302:
303: assertEquals(EXPECTED3, output);
304:
305: BeanReader beanReader = new BeanReader();
306:
307: beanReader.registerMultiMapping(new InputSource(
308: new StringReader(MAPPING)));
309:
310: StringReader xmlReader = new StringReader(output);
311:
312: container = (Container) beanReader.parse(xmlReader);
313:
314: Iterator it = container.getElements();
315:
316: assertTrue(it.next() instanceof ElementA);
317: assertTrue(it.next() instanceof ElementB);
318: assertFalse(it.hasNext());
319:
320: it = container.getNodes();
321:
322: assertTrue(it.next() instanceof Node2);
323: assertTrue(it.next() instanceof Node1);
324: assertTrue(it.next() instanceof Node2);
325: assertFalse(it.hasNext());
326:
327: }
328:
329: public void testInvalidXML() throws IOException, SAXException,
330: IntrospectionException {
331: BeanReader beanReader = new BeanReader();
332:
333: beanReader.registerMultiMapping(new InputSource(
334: new StringReader(MAPPING)));
335:
336: StringReader xmlReader = new StringReader(INVALID_XML);
337: Container container = (Container) beanReader.parse(xmlReader);
338: Iterator it = container.getElements();
339:
340: assertTrue(it.next() instanceof ElementA);
341: assertFalse(it.hasNext());
342:
343: it = container.getNodes();
344:
345: assertFalse(it.hasNext());
346: }
347: }
|