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.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.commons.betwixt.io.BeanReader;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXException;
029:
030: /**
031: * Tests the multi-mapping of collections with polymorphic entries.
032: *
033: * @author Thomas Dudziak (tomdz@apache.org)
034: */
035: public class TestCollectionMapping2 extends AbstractTestCase {
036: public static class Container {
037: private List _elements = new ArrayList();
038:
039: public Iterator getElements() {
040: return _elements.iterator();
041: }
042:
043: public void addElement(Element element) {
044: _elements.add(element);
045: }
046: }
047:
048: public static class Element {
049: private List _subElements = new ArrayList();
050:
051: public Iterator getSubElements() {
052: return _subElements.iterator();
053: }
054:
055: public void addSubElement(SubElement subElement) {
056: _subElements.add(subElement);
057: }
058: }
059:
060: public static interface SubElement {
061: }
062:
063: public static class SubElementA implements SubElement {
064: }
065:
066: public static class SubElementB implements SubElement {
067: }
068:
069: private static final String MAPPING = "<?xml version='1.0'?>\n"
070: + "<betwixt-config>\n" + " <class name='"
071: + Container.class.getName()
072: + "'>\n"
073: + " <element name='container'>\n"
074: + " <element property='elements' updater='addElement'/>\n"
075: + " </element>\n"
076: + " </class>\n"
077: + " <class name='"
078: + Element.class.getName()
079: + "'>\n"
080: + " <element name='element'>\n"
081: + " <element property='subElements' updater='addSubElement'/>\n"
082: + " </element>\n"
083: + " </class>\n"
084: + " <class name='"
085: + SubElementA.class.getName()
086: + "'>\n"
087: + " <element name='subElementA'/>\n"
088: + " </class>\n"
089: + " <class name='"
090: + SubElementB.class.getName()
091: + "'>\n"
092: + " <element name='subElementB'/>\n"
093: + " </class>\n"
094: + "</betwixt-config>";
095: private static final String INVALID_XML = "<?xml version=\"1.0\" ?>\n"
096: + " <container>\n"
097: + " <subElementB/>\n"
098: + " </container>\n";
099:
100: public TestCollectionMapping2(String testName) {
101: super (testName);
102: }
103:
104: public void testInvalidXML() throws IOException,
105: IntrospectionException, SAXException {
106: BeanReader beanReader = new BeanReader();
107:
108: beanReader.registerMultiMapping(new InputSource(
109: new StringReader(MAPPING)));
110:
111: StringReader xmlReader = new StringReader(INVALID_XML);
112: Container database = (Container) beanReader.parse(xmlReader);
113:
114: // either we get an exception in the parse method (would perhaps be better)
115: // or the collection is empty (SubElementB cannot be added to Container)
116: assertFalse(database.getElements().hasNext());
117: }
118: }
|