1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.strategy.alt;
19
20 import java.io.StringReader;
21 import java.io.StringWriter;
22 import java.io.Writer;
23
24 import junit.framework.TestCase;
25
26 import org.apache.commons.betwixt.XMLIntrospector;
27 import org.apache.commons.betwixt.io.BeanReader;
28 import org.apache.commons.betwixt.io.BeanWriter;
29 import org.apache.commons.betwixt.strategy.CapitalizeNameMapper;
30 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
31 import org.apache.commons.betwixt.strategy.DefaultNameMapper;
32 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
33 import org.apache.commons.betwixt.strategy.NameMapper;
34
35
36
37
38
39
40
41
42
43
44 public class TestElementsAlt extends TestCase {
45
46
47
48 public TestElementsAlt(String name) {
49 super(name);
50
51
52 }
53
54 public void testCapitalizeNameMapper() throws Exception {
55
56 doTest(new CapitalizeNameMapper(), "capitalize name mapper");
57 }
58
59 public void testDecapitalizeNameMapper() throws Exception {
60
61 doTest(new DecapitalizeNameMapper(), "decapitalize name mapper");
62 }
63
64 public void testDefaultElementMapper() throws Exception {
65
66 doTest(new DefaultNameMapper(), "default name mapper");
67 }
68
69 public void testHyphenatedNameMapper() throws Exception {
70
71 doTest(new HyphenatedNameMapper(), "hyphenated name mapper");
72 }
73
74 private void doTest(NameMapper mapper, String testName) throws Exception {
75 Elements elements = new Elements();
76 elements.addElement(new Element("a"));
77 elements.addElement(new Element("b"));
78 elements.addElement(new Element("c"));
79
80 StringWriter out = new StringWriter();
81 BeanWriter writer = newBeanWriter(out, mapper);
82 writer.setWriteEmptyElements( true );
83 writer.write(elements);
84 writer.flush();
85
86 String xmlOut = out.toString();
87
88
89
90 StringReader in = new StringReader(xmlOut);
91
92
93
94
95 BeanReader reader = new BeanReader();
96
97
98
99
100
101
102 reader.setXMLIntrospector(newXMLIntrospector(mapper));
103 reader.registerBeanClass(Elements.class);
104 Elements result = (Elements) reader.parse(in);
105
106 assertNotNull("Element 'a' is null (" + testName + ")", result.getElement("a"));
107 assertNotNull("Element 'b' is null (" + testName + ")", result.getElement("b"));
108 assertNotNull("Element 'c' is null (" + testName + ")", result.getElement("c"));
109 }
110
111 private BeanWriter newBeanWriter(Writer writer, NameMapper mapper) {
112 BeanWriter result = new BeanWriter(writer);
113 result.setWriteEmptyElements( true );
114
115 result.setXMLIntrospector(newXMLIntrospector(mapper));
116 result.setEndOfLine("\n");
117 result.enablePrettyPrint();
118 result.getBindingConfiguration().setMapIDs(false);
119 return result;
120 }
121
122 private XMLIntrospector newXMLIntrospector(NameMapper mapper) {
123 XMLIntrospector introspector = new XMLIntrospector();
124 introspector.getConfiguration().setAttributesForPrimitives(true);
125 introspector.getConfiguration().setWrapCollectionsInElement(false);
126 introspector.getConfiguration().setElementNameMapper(mapper);
127 return introspector;
128 }
129 }
130