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: package org.apache.commons.betwixt;
018:
019: import java.beans.IntrospectionException;
020: import java.io.IOException;
021: import java.io.StringReader;
022: import java.io.StringWriter;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.commons.betwixt.io.BeanReader;
028: import org.apache.commons.betwixt.io.BeanWriter;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * Tests the multi-mapping of collections with polymorphic entries.
034: *
035: * @author Thomas Dudziak (tomdz@apache.org)
036: */
037: public class TestCollectionMapping extends AbstractTestCase {
038: public static class Container {
039: private List _elements = new ArrayList();
040:
041: public Iterator getElements() {
042: return _elements.iterator();
043: }
044:
045: public void addElement(Element element) {
046: _elements.add(element);
047: }
048: }
049:
050: public static interface Element {
051: }
052:
053: public static class ElementA implements Element {
054: }
055:
056: public static class ElementB implements Element {
057: }
058:
059: public static class ElementC {
060: }
061:
062: private static final String MAPPING = "<?xml version=\"1.0\"?>\n"
063: + "<betwixt-config>\n"
064: + " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$Container\">\n"
065: + " <element name=\"container\">\n"
066: + " <element name=\"elements\">\n"
067: + " <element property=\"elements\" updater='addElement'/>\n"
068: + " </element>\n"
069: + " </element>\n"
070: + " </class>\n"
071: + " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementA\">\n"
072: + " <element name=\"elementA\"/>\n"
073: + " </class>\n"
074: + " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementB\">\n"
075: + " <element name=\"elementB\"/>\n"
076: + " </class>\n"
077: + " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementC\">\n"
078: + " <element name=\"elementC\"/>\n" + " </class>\n"
079: + "</betwixt-config>";
080: private static final String EXPECTED = "<?xml version=\"1.0\" ?>\n"
081: + " <container>\n" + " <elements>\n"
082: + " <elementB/>\n" + " <elementA/>\n"
083: + " </elements>\n" + " </container>\n";
084: private static final String INVALID_XML = "<?xml version=\"1.0\" ?>\n"
085: + " <container>\n"
086: + " <elements>\n"
087: + " <elementC/>\n"
088: + " </elements>\n"
089: + " </container>\n";
090:
091: public TestCollectionMapping(String testName) {
092: super (testName);
093: }
094:
095: public void testRoundTripWithSingleMappingFile()
096: throws IOException, SAXException, IntrospectionException {
097: Container container = new Container();
098:
099: container.addElement(new ElementB());
100: container.addElement(new ElementA());
101:
102: StringWriter outputWriter = new StringWriter();
103:
104: outputWriter.write("<?xml version=\"1.0\" ?>\n");
105:
106: BeanWriter beanWriter = new BeanWriter(outputWriter);
107: beanWriter.setEndOfLine("\n");
108: beanWriter.enablePrettyPrint();
109: beanWriter.setWriteEmptyElements(true);
110: beanWriter.getBindingConfiguration().setMapIDs(false);
111: beanWriter.getXMLIntrospector().register(
112: new InputSource(new StringReader(MAPPING)));
113: beanWriter.setEndOfLine("\n"); //force to \n so expected values match for sure
114: beanWriter.write(container);
115:
116: String output = outputWriter.toString();
117:
118: assertEquals(EXPECTED, output);
119:
120: BeanReader beanReader = new BeanReader();
121:
122: beanReader.registerMultiMapping(new InputSource(
123: new StringReader(MAPPING)));
124:
125: StringReader xmlReader = new StringReader(output);
126:
127: container = (Container) beanReader.parse(xmlReader);
128:
129: Iterator it = container.getElements();
130:
131: assertTrue(it.next() instanceof ElementB);
132: assertTrue(it.next() instanceof ElementA);
133: assertFalse(it.hasNext());
134: }
135:
136: public void testInvalidXML() throws IOException,
137: IntrospectionException, SAXException {
138: BeanReader beanReader = new BeanReader();
139:
140: beanReader.registerMultiMapping(new InputSource(
141: new StringReader(MAPPING)));
142:
143: StringReader xmlReader = new StringReader(INVALID_XML);
144: Container container = (Container) beanReader.parse(xmlReader);
145:
146: // either we get an exception in the parse method (would perhaps be better)
147: // or the collection is empty (ElementC cannot be added)
148: assertFalse(container.getElements().hasNext());
149: }
150: }
|