KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > TestArrays


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.betwixt;
18
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import org.apache.commons.betwixt.io.BeanReader;
23 import org.apache.commons.betwixt.io.BeanWriter;
24
25 /**
26  * @author <a HREF='http://jakarta.apache.org/'>Jakarta Commons Team</a>
27  * @version $Revision: 1.2 $
28  */

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