1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt;
17
18
19 import java.beans.IntrospectionException;
20 import java.io.IOException;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.commons.betwixt.io.BeanReader;
28 import org.apache.commons.betwixt.io.BeanWriter;
29 import org.xml.sax.InputSource;
30 import org.xml.sax.SAXException;
31
32 /***
33 * Tests the multi-mapping of collections with polymorphic entries.
34 *
35 * @author Thomas Dudziak (tomdz@apache.org)
36 */
37 public class TestCollectionMapping extends AbstractTestCase
38 {
39 public static class Container
40 {
41 private List _elements = new ArrayList();
42
43 public Iterator getElements()
44 {
45 return _elements.iterator();
46 }
47
48 public void addElement(Element element)
49 {
50 _elements.add(element);
51 }
52 }
53
54 public static interface Element
55 {}
56
57 public static class ElementA implements Element
58 {}
59
60 public static class ElementB implements Element
61 {}
62
63 private static final String MAPPING =
64 "<?xml version=\"1.0\"?>\n"+
65 "<betwixt-config>\n"+
66 " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$Container\">\n"+
67 " <element name=\"container\">\n"+
68 " <element name=\"elements\">\n"+
69 " <element property=\"elements\" updater='addElement'/>\n"+
70 " </element>\n"+
71 " </element>\n"+
72 " </class>\n"+
73 " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementA\">\n"+
74 " <element name=\"elementA\"/>\n"+
75 " </class>\n"+
76 " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementB\">\n"+
77 " <element name=\"elementB\"/>\n"+
78 " </class>\n"+
79 "</betwixt-config>";
80 private static final String EXPECTED =
81 "<?xml version=\"1.0\" ?>\n"+
82 " <container>\n"+
83 " <elements>\n"+
84 " <elementB/>\n"+
85 " <elementA/>\n"+
86 " </elements>\n"+
87 " </container>\n";
88
89 public TestCollectionMapping(String testName)
90 {
91 super(testName);
92 }
93
94 public void testRoundTripWithSingleMappingFile() throws IOException, SAXException, IntrospectionException
95 {
96 Container container = new Container();
97
98 container.addElement(new ElementB());
99 container.addElement(new ElementA());
100
101 StringWriter outputWriter = new StringWriter();
102
103 outputWriter.write("<?xml version=\"1.0\" ?>\n");
104
105 BeanWriter beanWriter = new BeanWriter(outputWriter);
106
107 beanWriter.enablePrettyPrint();
108 beanWriter.setWriteEmptyElements(true);
109 beanWriter.getBindingConfiguration().setMapIDs(false);
110 beanWriter.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
111 beanWriter.write(container);
112
113 String output = outputWriter.toString();
114
115 assertEquals(EXPECTED, output);
116
117 BeanReader beanReader = new BeanReader();
118
119 beanReader.registerMultiMapping(new InputSource(new StringReader(MAPPING)));
120
121 StringReader xmlReader = new StringReader(output);
122
123 container = (Container)beanReader.parse(xmlReader);
124
125 Iterator it = container.getElements();
126
127 assertTrue(it.next() instanceof ElementB);
128 assertTrue(it.next() instanceof ElementA);
129 assertFalse(it.hasNext());
130 }
131
132 }