001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022:
023: import org.apache.commons.betwixt.io.BeanReader;
024: import org.apache.commons.betwixt.io.BeanWriter;
025:
026: /**
027: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
028: * @version $Revision: 438373 $
029: */
030: public class TestArrays extends AbstractTestCase {
031:
032: public TestArrays(String testName) {
033: super (testName);
034: }
035:
036: public void testWriteArray() throws Exception {
037: StringWriter out = new StringWriter();
038: out.write("<?xml version='1.0'?>");
039: BeanWriter writer = new BeanWriter(out);
040: writer.getXMLIntrospector().getConfiguration()
041: .setAttributesForPrimitives(true);
042: writer.getBindingConfiguration().setMapIDs(false);
043:
044: LibraryBean libraryBean = new LibraryBean();
045: libraryBean.addBook(new BookBean("Martin Fowler",
046: "Refactoring", "Addision Wesley"));
047: libraryBean.addBook(new BookBean("Ben Laurie", "Apache",
048: "O'Reilly"));
049: libraryBean.addBook(new BookBean("Kent Beck",
050: "Test Driven Development", "Addision Wesley"));
051:
052: writer.write(libraryBean);
053: String xml = out.toString();
054: String expected = "<?xml version='1.0'?><LibraryBean>"
055: + "<books>"
056: + "<book author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>"
057: + "<book author='Ben Laurie' title='Apache' publisher='O'Reilly'/>"
058: + "<book author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>"
059: + "</books>" + "</LibraryBean>";
060:
061: xmlAssertIsomorphicContent(parseString(xml),
062: parseString(expected), true);
063: }
064:
065: public void testReadArray() throws Exception {
066: String xml = "<?xml version='1.0'?><LibraryBean>"
067: + "<books>"
068: + "<book author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>"
069: + "<book author='Ben Laurie' title='Apache' publisher='O'Reilly'/>"
070: + "<book author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>"
071: + "</books>" + "</LibraryBean>";
072:
073: BeanReader reader = new BeanReader();
074: reader.getXMLIntrospector().getConfiguration()
075: .setAttributesForPrimitives(true);
076: reader.getBindingConfiguration().setMapIDs(false);
077: reader.registerBeanClass(LibraryBean.class);
078: LibraryBean bean = (LibraryBean) reader.parse(new StringReader(
079: xml));
080:
081: BookBean[] books = bean.getBooks();
082: assertEquals("Three books read", 3, books.length);
083: assertEquals("Book one", new BookBean("Martin Fowler",
084: "Refactoring", "Addision Wesley"), books[0]);
085: assertEquals("Book two", new BookBean("Ben Laurie", "Apache",
086: "O'Reilly"), books[1]);
087: assertEquals("Book three", new BookBean("Kent Beck",
088: "Test Driven Development", "Addision Wesley"), books[2]);
089:
090: }
091:
092: public void testWriteArrayWithSetter() throws Exception {
093: StringWriter out = new StringWriter();
094: out.write("<?xml version='1.0'?>");
095: BeanWriter writer = new BeanWriter(out);
096: writer.getXMLIntrospector().getConfiguration()
097: .setAttributesForPrimitives(true);
098: writer.getBindingConfiguration().setMapIDs(false);
099:
100: LibraryBeanWithArraySetter libraryBean = new LibraryBeanWithArraySetter();
101: BookBean[] books = {
102: new BookBean("Martin Fowler", "Refactoring",
103: "Addision Wesley"),
104: new BookBean("Ben Laurie", "Apache", "O'Reilly"),
105: new BookBean("Kent Beck", "Test Driven Development",
106: "Addision Wesley") };
107: libraryBean.setBooks(books);
108:
109: writer.write(libraryBean);
110: String xml = out.toString();
111: String expected = "<?xml version='1.0'?><LibraryBeanWithArraySetter>"
112: + "<books>"
113: + "<BookBean author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>"
114: + "<BookBean author='Ben Laurie' title='Apache' publisher='O'Reilly'/>"
115: + "<BookBean author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>"
116: + "</books>" + "</LibraryBeanWithArraySetter>";
117:
118: xmlAssertIsomorphicContent(parseString(xml),
119: parseString(expected), true);
120: }
121:
122: public void testReadArrayWithSetter() throws Exception {
123: String xml = "<?xml version='1.0'?><LibraryBeanWithArraySetter>"
124: + "<books>"
125: + "<BookBean author='Martin Fowler' title='Refactoring' publisher='Addision Wesley'/>"
126: + "<BookBean author='Ben Laurie' title='Apache' publisher='O'Reilly'/>"
127: + "<BookBean author='Kent Beck' title='Test Driven Development' publisher='Addision Wesley'/>"
128: + "</books>" + "</LibraryBeanWithArraySetter>";
129:
130: BeanReader reader = new BeanReader();
131: reader.getXMLIntrospector().getConfiguration()
132: .setAttributesForPrimitives(true);
133: reader.getBindingConfiguration().setMapIDs(false);
134: reader.registerBeanClass(LibraryBeanWithArraySetter.class);
135: LibraryBeanWithArraySetter bean = (LibraryBeanWithArraySetter) reader
136: .parse(new StringReader(xml));
137:
138: BookBean[] books = bean.getBooks();
139: assertEquals("Three books read", 3, books.length);
140: assertEquals("Book one", new BookBean("Martin Fowler",
141: "Refactoring", "Addision Wesley"), books[0]);
142: assertEquals("Book two", new BookBean("Ben Laurie", "Apache",
143: "O'Reilly"), books[1]);
144: assertEquals("Book three", new BookBean("Kent Beck",
145: "Test Driven Development", "Addision Wesley"), books[2]);
146:
147: }
148:
149: public void testIntrospectArrayWithSetter() throws Exception {
150: XMLIntrospector introspector = new XMLIntrospector();
151:
152: XMLBeanInfo xmlBeanInfo = introspector
153: .introspect(LibraryBeanWithArraySetter.class);
154:
155: ElementDescriptor beanDescriptor = xmlBeanInfo
156: .getElementDescriptor();
157: ElementDescriptor[] childDescriptors = beanDescriptor
158: .getElementDescriptors();
159: assertEquals("Only one child element", 1,
160: childDescriptors.length);
161:
162: ElementDescriptor booksWrapperDescriptor = childDescriptors[0];
163: ElementDescriptor[] wrapperChildren = booksWrapperDescriptor
164: .getElementDescriptors();
165: assertEquals("Only one child element", 1,
166: childDescriptors.length);
167: ElementDescriptor booksDescriptor = wrapperChildren[0];
168: assertNotNull("Updater for property", booksDescriptor
169: .getUpdater());
170: }
171:
172: }
|