1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.poly;
19
20 import java.io.StringReader;
21 import java.io.StringWriter;
22
23 import org.apache.commons.betwixt.AbstractTestCase;
24 import org.apache.commons.betwixt.BindingConfiguration;
25 import org.apache.commons.betwixt.ElementDescriptor;
26 import org.apache.commons.betwixt.XMLBeanInfo;
27 import org.apache.commons.betwixt.XMLIntrospector;
28 import org.apache.commons.betwixt.io.BeanReader;
29 import org.apache.commons.betwixt.io.BeanWriter;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.xml.sax.InputSource;
33
34
35
36 public class TestPolyList extends AbstractTestCase {
37 public static Log log = LogFactory.getLog(TestPolyList.class);
38
39 public TestPolyList(String testName) {
40 super(testName);
41
42 log.info("Mapping:\n" + MAPPING);
43 }
44
45 private static final String XML = "<AlphaList><AlphaOneImpl><one>1</one></AlphaOneImpl><AlphaTwoImpl><two>2</two></AlphaTwoImpl></AlphaList>";
46
47 private static final String MAPPING = "<?xml version='1.0'?>"
48 + "<betwixt-config>"
49 + " <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
50 + " <element name='AlphaOneImpl'>"
51 + " <element name='one' property='one'/>"
52 + " </element>"
53 + " </class>"
54 + " <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
55 + " <element name='AlphaTwoImpl'>"
56 + " <element name='two' property='two'/>"
57 + " </element>"
58 + " </class>"
59 + "</betwixt-config>";
60
61 public void testWrite() throws Exception {
62 AlphaList bean = new AlphaList();
63 AlphaOneImpl one = new AlphaOneImpl("1");
64 bean.add(one);
65 AlphaTwoImpl two = new AlphaTwoImpl("2");
66 bean.add(two);
67
68 StringWriter out = new StringWriter();
69 BeanWriter writer = new BeanWriter(out);
70 StringReader mapping = new StringReader(MAPPING);
71 writer.getXMLIntrospector().register(new InputSource(mapping));
72 configure(writer.getBindingConfiguration());
73 writer.write(bean);
74
75 String written = out.getBuffer().toString();
76 log.info("Written:\n" + written);
77
78 xmlAssertIsomorphicContent(
79 parseString(XML),
80 parseString(written),
81 true);
82 }
83
84 public void testRead() throws Exception {
85 StringReader in = new StringReader(XML);
86 BeanReader reader = new BeanReader();
87 StringReader mapping = new StringReader(MAPPING);
88 reader.registerMultiMapping(new InputSource(mapping));
89 reader.registerBeanClass(AlphaList.class);
90 configure(reader.getBindingConfiguration());
91 Object bean = reader.parse(in);
92 assertTrue(bean instanceof AlphaList);
93 AlphaList list = (AlphaList) bean;
94 assertEquals(2, list.size());
95
96 assertTrue(list.get(0) instanceof AlphaOneImpl);
97 AlphaOneImpl one = (AlphaOneImpl)list.get(0);
98 assertEquals("1", one.alpha());
99
100 assertTrue(list.get(1) instanceof AlphaTwoImpl);
101 AlphaTwoImpl two = (AlphaTwoImpl)list.get(1);
102 assertEquals("2", two.alpha());
103 }
104
105 public void testIntrospection() throws Exception {
106 XMLIntrospector introspector = new XMLIntrospector();
107 XMLBeanInfo beanInfo = introspector.introspect(AlphaList.class);
108 ElementDescriptor[] descriptors = beanInfo.getElementDescriptor().getElementDescriptors();
109 assertEquals("One descriptor", 1, descriptors.length);
110 assertTrue(descriptors[0].isHollow());
111 assertNotNull(descriptors[0].getContextExpression());
112 assertNotNull(descriptors[0].getUpdater());
113 assertEquals("A list can contain any object", Object.class, descriptors[0].getSingularPropertyType());
114 }
115
116 private void configure(BindingConfiguration configuration) {
117 configuration.setMapIDs(false);
118 }
119 }