1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt;
19
20 import java.io.StringReader;
21 import java.io.StringWriter;
22
23 import org.apache.commons.betwixt.io.BeanReader;
24 import org.apache.commons.betwixt.io.BeanWriter;
25
26
27
28
29
30 public class TestArrays extends AbstractTestCase {
31
32 public TestArrays(String testName) {
33 super(testName);
34 }
35
36 public void testWriteArray() throws Exception {
37 StringWriter out = new StringWriter();
38 out.write("<?xml version='1.0'?>");
39 BeanWriter writer = new BeanWriter(out);
40 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
41 writer.getBindingConfiguration().setMapIDs(false);
42
43 LibraryBean libraryBean = new LibraryBean();
44 libraryBean.addBook(new BookBean("Martin Fowler", "Refactoring", "Addision Wesley"));
45 libraryBean.addBook(new BookBean("Ben Laurie", "Apache", "O'Reilly"));
46 libraryBean.addBook(new BookBean("Kent Beck", "Test Driven Development", "Addision Wesley"));
47
48 writer.write(libraryBean);
49 String xml = out.toString();
50 String expected = "<?xml version='1.0'?><LibraryBean>" +
51 "<books>" +
52 "<book author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>" +
53 "<book author='Ben Laurie' title='Apache' publisher='O'Reilly'/>" +
54 "<book author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>" +
55 "</books>" +
56 "</LibraryBean>";
57
58 xmlAssertIsomorphicContent(
59 parseString(xml),
60 parseString(expected),
61 true);
62 }
63
64 public void testReadArray() throws Exception {
65 String xml = "<?xml version='1.0'?><LibraryBean>" +
66 "<books>" +
67 "<book author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>" +
68 "<book author='Ben Laurie' title='Apache' publisher='O'Reilly'/>" +
69 "<book author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>" +
70 "</books>" +
71 "</LibraryBean>";
72
73 BeanReader reader = new BeanReader();
74 reader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
75 reader.getBindingConfiguration().setMapIDs(false);
76 reader.registerBeanClass(LibraryBean.class);
77 LibraryBean bean = (LibraryBean) reader.parse(new StringReader(xml));
78
79 BookBean[] books = bean.getBooks();
80 assertEquals("Three books read", 3, books.length);
81 assertEquals("Book one", new BookBean("Martin Fowler", "Refactoring", "Addision Wesley"), books[0]);
82 assertEquals("Book two", new BookBean("Ben Laurie", "Apache", "O'Reilly"), books[1]);
83 assertEquals("Book three", new BookBean("Kent Beck", "Test Driven Development", "Addision Wesley"), books[2]);
84
85 }
86
87 public void testWriteArrayWithSetter() throws Exception {
88 StringWriter out = new StringWriter();
89 out.write("<?xml version='1.0'?>");
90 BeanWriter writer = new BeanWriter(out);
91 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
92 writer.getBindingConfiguration().setMapIDs(false);
93
94
95 LibraryBeanWithArraySetter libraryBean = new LibraryBeanWithArraySetter();
96 BookBean[] books = {
97 new BookBean("Martin Fowler", "Refactoring", "Addision Wesley"),
98 new BookBean("Ben Laurie", "Apache", "O'Reilly"),
99 new BookBean("Kent Beck", "Test Driven Development", "Addision Wesley")};
100 libraryBean.setBooks(books);
101
102 writer.write(libraryBean);
103 String xml = out.toString();
104 String expected = "<?xml version='1.0'?><LibraryBeanWithArraySetter>" +
105 "<books>" +
106 "<BookBean author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>" +
107 "<BookBean author='Ben Laurie' title='Apache' publisher='O'Reilly'/>" +
108 "<BookBean author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>" +
109 "</books>" +
110 "</LibraryBeanWithArraySetter>";
111
112 xmlAssertIsomorphicContent(
113 parseString(xml),
114 parseString(expected),
115 true);
116 }
117
118 public void testReadArrayWithSetter() throws Exception {
119 String xml = "<?xml version='1.0'?><LibraryBeanWithArraySetter>" +
120 "<books>" +
121 "<BookBean author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>" +
122 "<BookBean author='Ben Laurie' title='Apache' publisher='O'Reilly'/>" +
123 "<BookBean author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>" +
124 "</books>" +
125 "</LibraryBeanWithArraySetter>";
126
127 BeanReader reader = new BeanReader();
128 reader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
129 reader.getBindingConfiguration().setMapIDs(false);
130 reader.registerBeanClass(LibraryBeanWithArraySetter.class);
131 LibraryBeanWithArraySetter bean = (LibraryBeanWithArraySetter) reader.parse(new StringReader(xml));
132
133 BookBean[] books = bean.getBooks();
134 assertEquals("Three books read", 3, books.length);
135 assertEquals("Book one", new BookBean("Martin Fowler", "Refactoring", "Addision Wesley"), books[0]);
136 assertEquals("Book two", new BookBean("Ben Laurie", "Apache", "O'Reilly"), books[1]);
137 assertEquals("Book three", new BookBean("Kent Beck", "Test Driven Development", "Addision Wesley"), books[2]);
138
139 }
140
141 public void testIntrospectArrayWithSetter() throws Exception {
142 XMLIntrospector introspector = new XMLIntrospector();
143
144 XMLBeanInfo xmlBeanInfo = introspector.introspect(LibraryBeanWithArraySetter.class);
145
146 ElementDescriptor beanDescriptor = xmlBeanInfo.getElementDescriptor();
147 ElementDescriptor[] childDescriptors = beanDescriptor.getElementDescriptors();
148 assertEquals("Only one child element", 1, childDescriptors.length);
149
150 ElementDescriptor booksWrapperDescriptor = childDescriptors[0];
151 ElementDescriptor[] wrapperChildren = booksWrapperDescriptor.getElementDescriptors();
152 assertEquals("Only one child element", 1, childDescriptors.length);
153 ElementDescriptor booksDescriptor = wrapperChildren[0];
154 assertNotNull("Updater for property", booksDescriptor.getUpdater());
155 }
156
157 }