1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt.dotbetwixt;
17
18 import java.io.StringReader;
19
20 import org.apache.commons.betwixt.AbstractTestCase;
21 import org.apache.commons.betwixt.AddressBean;
22 import org.apache.commons.betwixt.ElementDescriptor;
23 import org.apache.commons.betwixt.XMLBeanInfo;
24 import org.apache.commons.betwixt.XMLIntrospector;
25 import org.xml.sax.InputSource;
26
27 /***
28 * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
29 */
30 public class TestMultiMap extends AbstractTestCase {
31
32 public TestMultiMap(String testName) {
33 super(testName);
34 }
35
36 private static final String MAPPING = "<?xml version='1.0'?>" +
37 " <betwixt-config>" +
38 " <class name='org.apache.commons.betwixt.PartyBean'>" +
39 " <element name='party'>" +
40 " <element name='the-excuse' property='excuse'/>" +
41 " <element name='location' property='venue'/> " +
42 " <element name='time' property='fromHour'/>" +
43 " </element>" +
44 " </class>" +
45 " <class name='org.apache.commons.betwixt.AddressBean'>" +
46 " <element name='not-address'>" +
47 " <element name='not-street' property='street'/>" +
48 " <element name='not-city' property='city'/>" +
49 " <element name='not-code' property='code'/>" +
50 " <element name='not-country' property='country'/>" +
51 " </element>" +
52 " </class>" +
53 " <class name='org.apache.commons.betwixt.dotbetwixt.SimpleTestBean'>" +
54 " <element name='jelly'>" +
55 " <element name='wibble' property='alpha'/>" +
56 " <element name='wobble' property='beta'/>" +
57 " </element>" +
58 " </class>" +
59 " </betwixt-config>";
60
61 public void testRegisterMultiMapping() throws Exception {
62 XMLIntrospector xmlIntrospector = new XMLIntrospector();
63 Class[] mapped = xmlIntrospector.register(new InputSource(new StringReader(MAPPING)));
64
65 assertEquals("Mapped classes", 3, mapped.length);
66
67 XMLBeanInfo beanInfo = xmlIntrospector.introspect(AddressBean.class);
68 assertNotNull("Bean info mapping", beanInfo);
69 ElementDescriptor descriptor = beanInfo.getElementDescriptor();
70 assertEquals("Root element name", "not-address", descriptor.getLocalName());
71 ElementDescriptor[] childDescriptors = descriptor.getElementDescriptors();
72 assertEquals("4 child elements", 4, childDescriptors.length);
73 assertEquals("First element", "not-street", childDescriptors[0].getLocalName());
74 assertEquals("Second element", "not-city", childDescriptors[1].getLocalName());
75 assertEquals("Third element", "not-code", childDescriptors[2].getLocalName());
76 assertEquals("Forth element", "not-country", childDescriptors[3].getLocalName());
77
78 beanInfo = xmlIntrospector.introspect(SimpleTestBean.class);
79 assertNotNull("Bean info mapping", beanInfo);
80 descriptor = beanInfo.getElementDescriptor();
81 assertEquals("Root element name", "jelly", descriptor.getLocalName());
82 childDescriptors = descriptor.getElementDescriptors();
83 assertEquals("Child elements", 2, childDescriptors.length);
84 assertEquals("First element", "wibble", childDescriptors[0].getLocalName());
85 assertEquals("Second element", "wobble", childDescriptors[1].getLocalName());
86
87 }
88 }