1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt;
18  
19  
20  import java.beans.IntrospectionException;
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.commons.betwixt.io.BeanReader;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * Tests the multi-mapping of collections with polymorphic entries.
33   * 
34   * @author Thomas Dudziak (tomdz@apache.org)
35   */
36  public class TestCollectionMapping2 extends AbstractTestCase
37  {
38      public static class Container
39      {
40          private List _elements = new ArrayList();
41  
42          public Iterator getElements()
43          {
44              return _elements.iterator();
45          }
46  
47          public void addElement(Element element)
48          {
49              _elements.add(element);
50          }
51      }
52  
53      public static class Element
54      {
55          private List _subElements = new ArrayList();
56  
57          public Iterator getSubElements()
58          {
59              return _subElements.iterator();
60          }
61  
62          public void addSubElement(SubElement subElement)
63          {
64              _subElements.add(subElement);
65          }
66      }
67  
68      public static interface SubElement
69      {}
70  
71      public static class SubElementA implements SubElement
72      {}
73  
74      public static class SubElementB implements SubElement
75      {}
76  
77      private static final String MAPPING =
78          "<?xml version='1.0'?>\n"+
79          "<betwixt-config>\n"+
80          "  <class name='"+Container.class.getName()+"'>\n"+
81          "    <element name='container'>\n"+
82          "      <element property='elements' updater='addElement'/>\n"+
83          "    </element>\n"+
84          "  </class>\n"+
85          "  <class name='"+Element.class.getName()+"'>\n"+
86          "    <element name='element'>\n"+
87          "      <element property='subElements' updater='addSubElement'/>\n"+
88          "    </element>\n"+
89          "  </class>\n"+
90          "  <class name='"+SubElementA.class.getName()+"'>\n"+
91          "    <element name='subElementA'/>\n"+
92          "  </class>\n"+
93          "  <class name='"+SubElementB.class.getName()+"'>\n"+
94          "    <element name='subElementB'/>\n"+
95          "  </class>\n"+
96          "</betwixt-config>";
97      private static final String INVALID_XML =
98          "<?xml version=\"1.0\" ?>\n"+
99          "  <container>\n"+
100         "    <subElementB/>\n"+
101         "  </container>\n";
102     
103     public TestCollectionMapping2(String testName)
104     {
105         super(testName);
106     }
107 
108     public void testInvalidXML() throws IOException, IntrospectionException, SAXException
109     {
110         BeanReader beanReader = new BeanReader();
111 
112         beanReader.registerMultiMapping(new InputSource(new StringReader(MAPPING)));
113 
114         StringReader xmlReader = new StringReader(INVALID_XML);
115         Container     database  = (Container) beanReader.parse(xmlReader);
116 
117         // either we get an exception in the parse method (would perhaps be better)
118         // or the collection is empty (SubElementB cannot be added to Container)
119         assertFalse(database.getElements().hasNext());
120     }
121 }