1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.digester;
18
19 import java.io.StringReader;
20 import java.util.Map;
21
22 import junit.framework.TestCase;
23
24 import org.apache.commons.betwixt.ElementDescriptor;
25 import org.apache.commons.betwixt.XMLBeanInfo;
26 import org.apache.commons.betwixt.XMLIntrospector;
27 import org.apache.commons.betwixt.dotbetwixt.ExampleBean;
28
29
30
31
32
33
34 public class TestDigestMultiMapping extends TestCase {
35
36
37 public void testDigestWithOptions() throws Exception {
38 String xml = "<?xml version='1.0'?>" +
39 "<betwixt-config>" +
40 " <class name=\"" + TestDigestMultiMapping.class.getName() + "\">" +
41 " <element name=\"test-multi-mapping\">" +
42 " <option>" +
43 " <name>test-key-a</name>" +
44 " <value>test-value-a</value>" +
45 " </option>" +
46 " <option>" +
47 " <name>test-key-b</name>" +
48 " <value>test-value-b</value>" +
49 " </option>" +
50 " <element name=\"maps\" property=\"maps\">" +
51 " <option>" +
52 " <name>test-key-c</name>" +
53 " <value>test-value-c</value>" +
54 " </option>" +
55 " </element>" +
56 " </element>" +
57 " </class>" +
58 "</betwixt-config>";
59
60 MultiMappingBeanInfoDigester digester = new MultiMappingBeanInfoDigester();
61 digester.setXMLIntrospector(new XMLIntrospector());
62 digester.setBeanClass(ExampleBean.class);
63
64 digester.parse(new StringReader(xml));
65 Map beanInfoMap = digester.getBeanInfoMap();
66 assertTrue( beanInfoMap.containsKey(TestDigestMultiMapping.class));
67
68 XMLBeanInfo xmlBeanInfo = (XMLBeanInfo) beanInfoMap.get(TestDigestMultiMapping.class);
69 assertNotNull(xmlBeanInfo);
70
71 ElementDescriptor baseDescriptor = xmlBeanInfo.getElementDescriptor();
72
73 assertEquals("Value one set on base", "test-value-a", baseDescriptor.getOptions().getValue("test-key-a"));
74 assertEquals("Value two set on base", "test-value-b", baseDescriptor.getOptions().getValue("test-key-b"));
75 assertNull("Value three not set on base", baseDescriptor.getOptions().getValue("three"));
76
77 assertEquals("Number of child elements", 1, baseDescriptor.getElementDescriptors().length);
78
79 ElementDescriptor childDescriptor = baseDescriptor.getElementDescriptors()[0];
80 assertNull("Value one set on base", childDescriptor.getOptions().getValue("test-key-a"));
81 assertNull("Value two set on base", childDescriptor.getOptions().getValue("test-key-b"));
82 assertEquals("Value three set on child", "test-value-c", childDescriptor.getOptions().getValue("test-key-c"));
83 }
84 }