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.registry;
018:
019: import java.io.StringReader;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.commons.betwixt.BindingConfiguration;
027: import org.apache.commons.betwixt.ElementDescriptor;
028: import org.apache.commons.betwixt.XMLIntrospector;
029: import org.apache.commons.betwixt.io.read.ElementMapping;
030: import org.apache.commons.betwixt.io.read.ReadConfiguration;
031: import org.apache.commons.betwixt.io.read.ReadContext;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.helpers.AttributesImpl;
034:
035: /**
036: * @author Thomas Dudziak (tomdz@apache.org)
037: */
038: public class TestRegistryPolymorphicResolution extends TestCase {
039:
040: public static class Container {
041: private List _elements = new ArrayList();
042:
043: public Iterator getElements() {
044: return _elements.iterator();
045: }
046:
047: public void addElement(Element element) {
048: _elements.add(element);
049: }
050: }
051:
052: public static interface Element {
053: }
054:
055: public static class ElementA implements Element {
056: }
057:
058: public static class ElementB implements Element {
059: }
060:
061: private static final String MAPPING = "<?xml version=\"1.0\"?>\n"
062: + "<betwixt-config>\n"
063: + " <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$Container\">\n"
064: + " <element name=\"container\">\n"
065: + " <element name=\"elements\">\n"
066: + " <element property=\"elements\"/>\n"
067: + " </element>\n"
068: + " </element>\n"
069: + " </class>\n"
070: + " <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$ElementA\">\n"
071: + " <element name=\"elementA\"/>\n"
072: + " </class>\n"
073: + " <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$ElementB\">\n"
074: + " <element name=\"elementB\"/>\n" + " </class>\n"
075: + "</betwixt-config>";
076:
077: public void testRegisterThenResolve() throws Exception {
078: XMLIntrospector introspector = new XMLIntrospector();
079: introspector
080: .register(new InputSource(new StringReader(MAPPING)));
081:
082: ElementDescriptor descriptor = introspector.introspect(
083: Element.class).getElementDescriptor();
084: ElementMapping elementMapping = new ElementMapping();
085: elementMapping.setAttributes(new AttributesImpl());
086: elementMapping.setName("Bogus");
087: elementMapping.setDescriptor(descriptor);
088: elementMapping.setType(Iterator.class);
089: ReadContext readContext = new ReadContext(
090: new BindingConfiguration(), new ReadConfiguration());
091:
092: assertNull(introspector.getPolymorphicReferenceResolver()
093: .resolveType(elementMapping, readContext));
094:
095: elementMapping.setName("elementA");
096: Class resolution = introspector
097: .getPolymorphicReferenceResolver().resolveType(
098: elementMapping, readContext);
099: assertEquals("Should resolve to the element about",
100: ElementA.class, resolution);
101: }
102: }
|