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 import java.io.StringWriter;
20
21 import org.apache.commons.betwixt.AbstractTestCase;
22 import org.apache.commons.betwixt.ElementDescriptor;
23 import org.apache.commons.betwixt.XMLBeanInfo;
24 import org.apache.commons.betwixt.XMLIntrospector;
25 import org.apache.commons.betwixt.io.BeanReader;
26 import org.apache.commons.betwixt.io.BeanWriter;
27 import org.xml.sax.InputSource;
28
29 /***
30 * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
31 */
32 public class TestCustomDotBetwixt extends AbstractTestCase {
33
34 public TestCustomDotBetwixt(String testName) {
35 super(testName);
36 }
37
38 public void testIntrospectWithCustomDotBetwixt() throws Exception {
39 StringReader reader = new StringReader(
40 "<?xml version='1.0' ?>" +
41 "<info>" +
42 " <element name='jelly'>" +
43 " <element name='wibble' property='alpha'/>" +
44 " <element name='wobble' property='beta'/>" +
45 " </element>" +
46 "</info>");
47 XMLIntrospector introspector = new XMLIntrospector();
48 XMLBeanInfo xmlBeanInfo = introspector.introspect(SimpleTestBean.class, new InputSource(reader));
49
50 ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor();
51 assertEquals("Root is jelly", "jelly", elementDescriptor.getLocalName());
52 ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors();
53 assertEquals("Expected two child elements", 2, childDescriptors.length);
54 assertEquals("Wibble comes first", "wibble", childDescriptors[0].getLocalName());
55 assertEquals("Wobble comes last", "wobble", childDescriptors[1].getLocalName());
56
57 reader = new StringReader(
58 "<?xml version='1.0' ?>" +
59 "<info>" +
60 " <element name='not-jelly'>" +
61 " <element name='no-wibble' property='alpha'/>" +
62 " <element name='no-wobble' property='beta'/>" +
63 " </element>" +
64 "</info>");
65
66 xmlBeanInfo = introspector.introspect(SimpleTestBean.class, new InputSource(reader));
67
68 elementDescriptor = xmlBeanInfo.getElementDescriptor();
69 assertEquals("Root is not-jelly", "not-jelly", elementDescriptor.getLocalName());
70 childDescriptors = elementDescriptor.getElementDescriptors();
71 assertEquals("Expected two child elements", 2, childDescriptors.length);
72 assertEquals("No wibble comes first", "no-wibble", childDescriptors[0].getLocalName());
73 assertEquals("No wobble comes last", "no-wobble", childDescriptors[1].getLocalName());
74 }
75
76
77 public void testRegisterCustomDotBetwixt() throws Exception {
78 StringReader reader = new StringReader(
79 "<?xml version='1.0' ?>" +
80 "<info>" +
81 " <element name='jelly'>" +
82 " <element name='wibble' property='alpha'/>" +
83 " <element name='wobble' property='beta'/>" +
84 " </element>" +
85 "</info>");
86 XMLIntrospector introspector = new XMLIntrospector();
87 introspector.register(SimpleTestBean.class, new InputSource(reader));
88 XMLBeanInfo xmlBeanInfo = introspector.introspect(SimpleTestBean.class);
89
90 ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor();
91 assertEquals("Root is jelly", "jelly", elementDescriptor.getLocalName());
92 ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors();
93 assertEquals("Expected two child elements", 2, childDescriptors.length);
94 assertEquals("Wibble comes first", "wibble", childDescriptors[0].getLocalName());
95 assertEquals("Wobble comes last", "wobble", childDescriptors[1].getLocalName());
96 }
97
98 public void testWriteCustomDotBetwixt() throws Exception {
99 StringReader reader = new StringReader(
100 "<?xml version='1.0' ?>" +
101 "<info>" +
102 " <element name='jelly'>" +
103 " <element name='wibble' property='alpha'/>" +
104 " <element name='wobble' property='beta'/>" +
105 " </element>" +
106 "</info>");
107
108 StringWriter out = new StringWriter();
109 out.write("<?xml version='1.0'?>");
110 SimpleTestBean bean = new SimpleTestBean("one", "two", "three");
111
112 BeanWriter writer = new BeanWriter(out);
113 writer.getBindingConfiguration().setMapIDs(false);
114 writer.write(bean, new InputSource(reader));
115
116 String expected = "<?xml version='1.0'?>" +
117 "<jelly><wibble>one</wibble><wobble>two</wobble></jelly>";
118 xmlAssertIsomorphic(parseString(expected), parseString(out));
119 }
120
121
122 public void testReadCustomDotBetwixt() throws Exception {
123 String xml = "<?xml version='1.0'?>" +
124 "<jelly><wibble>one</wibble><wobble>two</wobble></jelly>";
125 StringReader in = new StringReader(xml);
126
127 StringReader dotBetwixt = new StringReader(
128 "<?xml version='1.0' ?>" +
129 "<info>" +
130 " <element name='jelly'>" +
131 " <element name='wibble' property='alpha'/>" +
132 " <element name='wobble' property='beta'/>" +
133 " </element>" +
134 "</info>");
135
136 BeanReader reader = new BeanReader();
137 reader.getBindingConfiguration().setMapIDs(false);
138 reader.registerBeanClass(new InputSource(dotBetwixt), SimpleTestBean.class);
139 SimpleTestBean bean = (SimpleTestBean) reader.parse(in);
140 assertNotNull("Bean not mapped", bean);
141 assertEquals("Property alpha mapping", "one", bean.getAlpha());
142 assertEquals("Property beta mapping", "two", bean.getBeta());
143 }
144 }