1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.betwixt;
20
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.util.Iterator;
24
25 import org.apache.commons.betwixt.expression.IteratorExpression;
26 import org.apache.commons.betwixt.io.BeanReader;
27 import org.apache.commons.betwixt.io.BeanWriter;
28 import org.apache.commons.betwixt.strategy.CapitalizeNameMapper;
29
30
31
32
33
34 public class TestCollectives extends AbstractTestCase{
35
36 private IntrospectionConfiguration categoriesIntrospectionConfiguration = new IntrospectionConfiguration();
37 private BindingConfiguration noIDsBindingConfiguration = new BindingConfiguration();
38
39 public TestCollectives(String testName) {
40 super(testName);
41
42 CapitalizeNameMapper capitalizeNameMapper = new CapitalizeNameMapper();
43 categoriesIntrospectionConfiguration.setAttributesForPrimitives(false);
44 categoriesIntrospectionConfiguration.setElementNameMapper(capitalizeNameMapper);
45 categoriesIntrospectionConfiguration.setAttributeNameMapper(capitalizeNameMapper);
46 categoriesIntrospectionConfiguration.setWrapCollectionsInElement(false);
47
48 noIDsBindingConfiguration.setMapIDs(false);
49 }
50
51
52 public void testWriteCategories() throws Exception {
53 StringWriter out = new StringWriter();
54 out.write("<?xml version='1.0'?>");
55 BeanWriter writer = new BeanWriter(out);
56 writer.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration);
57 writer.setBindingConfiguration(noIDsBindingConfiguration);
58
59 Categories categories = new Categories();
60 categories.addCategory(new Category("Runs"));
61 categories.addCategory(new Category("Innings"));
62 categories.addCategory(new Category("Dismissals"));
63 categories.addCategory(new Category("High Score"));
64 categories.addCategory(new Category("Average"));
65
66 writer.write(categories);
67
68 String xml = out.getBuffer().toString();
69 String expected = "<?xml version='1.0'?><Categories>" +
70 "<Category><Name>Runs</Name></Category>" +
71 "<Category><Name>Innings</Name></Category>" +
72 "<Category><Name>Dismissals</Name></Category>" +
73 "<Category><Name>High Score</Name></Category>" +
74 "<Category><Name>Average</Name></Category>" +
75 "</Categories>";
76
77 xmlAssertIsomorphicContent(parseString(expected), parseString(xml));
78 }
79
80 public void testReadCategories() throws Exception {
81 BeanReader beanReader = new BeanReader();
82 beanReader.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration);
83 beanReader.setBindingConfiguration(noIDsBindingConfiguration);
84 beanReader.registerBeanClass(Categories.class);
85
86 String xml = "<?xml version='1.0'?><Categories>" +
87 "<Category><Name>Runs</Name></Category>" +
88 "<Category><Name>Innings</Name></Category>" +
89 "<Category><Name>Dismissals</Name></Category>" +
90 "<Category><Name>High Score</Name></Category>" +
91 "<Category><Name>Average</Name></Category>" +
92 "</Categories>";
93
94 StringReader in = new StringReader(xml);
95
96 Categories bean = (Categories) beanReader.parse(in);
97
98 assertEquals("5 categories", 5, bean.size());
99
100 Iterator it = bean.getCategories();
101 assertEquals("Runs category", new Category("Runs"), it.next());
102 assertEquals("Runs category", new Category("Innings"), it.next());
103 assertEquals("Runs category", new Category("Dismissals"), it.next());
104 assertEquals("Runs category", new Category("High Score"), it.next());
105 assertEquals("Runs category", new Category("Average"), it.next());
106
107 }
108
109 public void testIntrospectListExtension() throws Exception
110 {
111 XMLIntrospector xmlIntrospector = new XMLIntrospector();
112 XMLBeanInfo beanInfo = xmlIntrospector.introspect(ArrayListExtender.class);
113
114 ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
115 ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors();
116 assertEquals(2, childDescriptors.length);
117 assertEquals("another", childDescriptors[0].getPropertyName());
118 assertTrue(childDescriptors[1].getContextExpression() instanceof IteratorExpression);
119 }
120
121 public void testWriteListExtension() throws Exception
122 {
123 ArrayListExtender bean = new ArrayListExtender("Whatever");
124 bean.add(new Long(11));
125 bean.add(new Long(12));
126 bean.add(new Long(13));
127
128 StringWriter out = new StringWriter();
129 out.write("<?xml version='1.0'?>");
130
131 BeanWriter writer = new BeanWriter(out);
132 writer.getBindingConfiguration().setMapIDs( false );
133 writer.write(bean);
134
135 String expected = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>" +
136 "<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>";
137
138 xmlAssertIsomorphicContent(parseString( expected ), parseString( out ));
139 }
140
141
142 public void testReadListExtension() throws Exception
143 {
144 String xml = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>" +
145 "<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>";
146
147 StringReader in = new StringReader( xml );
148
149 BeanReader reader = new BeanReader();
150 reader.getBindingConfiguration().setMapIDs( false );
151
152 reader.registerBeanClass( ArrayListExtender.class );
153 ArrayListExtender bean = (ArrayListExtender) reader.parse( in );
154
155 assertEquals("Whatever", bean.getAnother());
156 }
157 }