1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  package org.apache.commons.betwixt.registry;
17  
18  import java.io.StringReader;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.betwixt.BindingConfiguration;
24  import org.apache.commons.betwixt.ElementDescriptor;
25  import org.apache.commons.betwixt.XMLIntrospector;
26  import org.apache.commons.betwixt.TestCollectionMapping.Element;
27  import org.apache.commons.betwixt.io.read.ElementMapping;
28  import org.apache.commons.betwixt.io.read.ReadConfiguration;
29  import org.apache.commons.betwixt.io.read.ReadContext;
30  import org.xml.sax.InputSource;
31  import org.xml.sax.helpers.AttributesImpl;
32  
33  import junit.framework.TestCase;
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 }