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