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.registry;
18  
19  import java.io.StringReader;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.commons.betwixt.BindingConfiguration;
27  import org.apache.commons.betwixt.ElementDescriptor;
28  import org.apache.commons.betwixt.XMLIntrospector;
29  import org.apache.commons.betwixt.io.read.ElementMapping;
30  import org.apache.commons.betwixt.io.read.ReadConfiguration;
31  import org.apache.commons.betwixt.io.read.ReadContext;
32  import org.xml.sax.InputSource;
33  import org.xml.sax.helpers.AttributesImpl;
34  
35  /**
36   * @author Thomas Dudziak (tomdz@apache.org)
37   */
38  public class TestRegistryPolymorphicResolution extends TestCase {
39      
40      public static class Container
41      {
42          private List _elements = new ArrayList();
43  
44          public Iterator getElements()
45          {
46              return _elements.iterator();
47          }
48  
49          public void addElement(Element element)
50          {
51              _elements.add(element);
52          }
53      }
54  
55      public static interface Element
56      {}
57  
58      public static class ElementA implements Element
59      {}
60  
61      public static class ElementB implements Element
62      {}
63      
64      private static final String MAPPING =
65          "<?xml version=\"1.0\"?>\n"+
66          "<betwixt-config>\n"+
67          "  <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$Container\">\n"+
68          "    <element name=\"container\">\n"+
69          "      <element name=\"elements\">\n"+
70          "        <element property=\"elements\"/>\n"+
71          "      </element>\n"+
72          "    </element>\n"+
73          "  </class>\n"+
74          "  <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$ElementA\">\n"+
75          "    <element name=\"elementA\"/>\n"+
76          "  </class>\n"+
77          "  <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$ElementB\">\n"+
78          "    <element name=\"elementB\"/>\n"+
79          "  </class>\n"+
80          "</betwixt-config>";
81      
82      public void testRegisterThenResolve() throws Exception
83      {
84          XMLIntrospector introspector = new XMLIntrospector();
85          introspector.register(new InputSource(new StringReader(MAPPING)));
86          
87          
88          ElementDescriptor descriptor = introspector.introspect(Element.class).getElementDescriptor();
89          ElementMapping elementMapping = new ElementMapping();
90          elementMapping.setAttributes(new AttributesImpl());
91          elementMapping.setName("Bogus");
92          elementMapping.setDescriptor(descriptor);
93          elementMapping.setType(Iterator.class);
94          ReadContext readContext = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
95          
96          assertNull(introspector.getPolymorphicReferenceResolver().resolveType(elementMapping, readContext));
97          
98          elementMapping.setName("elementA");
99          Class resolution = introspector.getPolymorphicReferenceResolver().resolveType(elementMapping, readContext);
100         assertEquals("Should resolve to the element about", ElementA.class, resolution);
101     }
102 }