Author: rdonkin Date: Wed Apr 20 14:40:03 2005 New Revision: 162076 URL: http://svn.apache.org/viewcvs?rev=162076&view=rev Log: Fixed some problems with polymorphic binding in collections
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/registry/TestRegistryPolymorphicResolution.java Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/registry/DefaultXMLBeanInfoRegistry.java Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java?rev=162076&r1=162075&r2=162076&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java (original) +++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java Wed Apr 20 14:40:03 2005 @@ -313,8 +313,12 @@ ElementDescriptor elementDescriptor = null; ElementDescriptor descriptorWithNullName = null; + ElementDescriptor firstPolymorphic = null; ElementDescriptor[] elementDescriptors = getElementDescriptors(); for (int i=0, size=elementDescriptors.length; i<size; i++) { + if (firstPolymorphic == null && elementDescriptors[i].isPolymorphic()) { + firstPolymorphic = elementDescriptors[i]; + } String elementName = elementDescriptors[i].getQualifiedName(); if (name.equals(elementName)) { elementDescriptor = elementDescriptors[i]; @@ -323,6 +327,9 @@ if (descriptorWithNullName == null && elementName == null) { descriptorWithNullName = elementDescriptors[i]; } + } + if (elementDescriptor == null) { + elementDescriptor = firstPolymorphic; } if (elementDescriptor == null) { elementDescriptor = descriptorWithNullName; Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/registry/DefaultXMLBeanInfoRegistry.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/registry/DefaultXMLBeanInfoRegistry.java?rev=162076&r1=162075&r2=162076&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/registry/DefaultXMLBeanInfoRegistry.java (original) +++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/registry/DefaultXMLBeanInfoRegistry.java Wed Apr 20 14:40:03 2005 @@ -78,10 +78,14 @@ for (Iterator it = cachedClasses.iterator(); it.hasNext();) { XMLBeanInfo beanInfo = get((Class)it.next()); ElementDescriptor typeDescriptor = beanInfo.getElementDescriptor(); - if (mapping.getName().equals(typeDescriptor.getQualifiedName()) && - mappedDescriptor.getPropertyType().isAssignableFrom(beanInfo.getBeanClass())) { - result = beanInfo.getBeanClass(); - break; + boolean sameName = mapping.getName().equals(typeDescriptor.getQualifiedName()); + if (sameName) + { + boolean compatibleClass = mappedDescriptor.getSingularPropertyType().isAssignableFrom(beanInfo.getBeanClass()); + if (compatibleClass ) { + result = beanInfo.getBeanClass(); + break; + } } } return result; Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java?rev=162076&view=auto ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java (added) +++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java Wed Apr 20 14:40:03 2005 @@ -0,0 +1,132 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.betwixt; + + +import java.beans.IntrospectionException; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.commons.betwixt.io.BeanReader; +import org.apache.commons.betwixt.io.BeanWriter; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Tests the multi-mapping of collections with polymorphic entries. + * + * @author Thomas Dudziak ([EMAIL PROTECTED]) + */ +public class TestCollectionMapping extends AbstractTestCase +{ + public static class Container + { + private List _elements = new ArrayList(); + + public Iterator getElements() + { + return _elements.iterator(); + } + + public void addElement(Element element) + { + _elements.add(element); + } + } + + public static interface Element + {} + + public static class ElementA implements Element + {} + + public static class ElementB implements Element + {} + + private static final String MAPPING = + "<?xml version=\"1.0\"?>\n"+ + "<betwixt-config>\n"+ + " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$Container\">\n"+ + " <element name=\"container\">\n"+ + " <element name=\"elements\">\n"+ + " <element property=\"elements\" updater='addElement'/>\n"+ + " </element>\n"+ + " </element>\n"+ + " </class>\n"+ + " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementA\">\n"+ + " <element name=\"elementA\"/>\n"+ + " </class>\n"+ + " <class name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementB\">\n"+ + " <element name=\"elementB\"/>\n"+ + " </class>\n"+ + "</betwixt-config>"; + private static final String EXPECTED = + "<?xml version=\"1.0\" ?>\n"+ + " <container>\n"+ + " <elements>\n"+ + " <elementB/>\n"+ + " <elementA/>\n"+ + " </elements>\n"+ + " </container>\n"; + + public TestCollectionMapping(String testName) + { + super(testName); + } + + public void testRoundTripWithSingleMappingFile() throws IOException, SAXException, IntrospectionException + { + Container container = new Container(); + + container.addElement(new ElementB()); + container.addElement(new ElementA()); + + StringWriter outputWriter = new StringWriter(); + + outputWriter.write("<?xml version=\"1.0\" ?>\n"); + + BeanWriter beanWriter = new BeanWriter(outputWriter); + + beanWriter.enablePrettyPrint(); + beanWriter.setWriteEmptyElements(true); + beanWriter.getBindingConfiguration().setMapIDs(false); + beanWriter.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING))); + beanWriter.write(container); + + String output = outputWriter.toString(); + + assertEquals(EXPECTED, output); + + BeanReader beanReader = new BeanReader(); + + beanReader.registerMultiMapping(new InputSource(new StringReader(MAPPING))); + + StringReader xmlReader = new StringReader(output); + + container = (Container)beanReader.parse(xmlReader); + + Iterator it = container.getElements(); + + assertTrue(it.next() instanceof ElementB); + assertTrue(it.next() instanceof ElementA); + assertFalse(it.hasNext()); + } + +} Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/registry/TestRegistryPolymorphicResolution.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/registry/TestRegistryPolymorphicResolution.java?rev=162076&view=auto ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/registry/TestRegistryPolymorphicResolution.java (added) +++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/registry/TestRegistryPolymorphicResolution.java Wed Apr 20 14:40:03 2005 @@ -0,0 +1,102 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.betwixt.registry; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.commons.betwixt.BindingConfiguration; +import org.apache.commons.betwixt.ElementDescriptor; +import org.apache.commons.betwixt.XMLIntrospector; +import org.apache.commons.betwixt.TestCollectionMapping.Element; +import org.apache.commons.betwixt.io.read.ElementMapping; +import org.apache.commons.betwixt.io.read.ReadConfiguration; +import org.apache.commons.betwixt.io.read.ReadContext; +import org.xml.sax.InputSource; +import org.xml.sax.helpers.AttributesImpl; + +import junit.framework.TestCase; + +/** + * @author Thomas Dudziak ([EMAIL PROTECTED]) + */ +public class TestRegistryPolymorphicResolution extends TestCase { + + public static class Container + { + private List _elements = new ArrayList(); + + public Iterator getElements() + { + return _elements.iterator(); + } + + public void addElement(Element element) + { + _elements.add(element); + } + } + + public static interface Element + {} + + public static class ElementA implements Element + {} + + public static class ElementB implements Element + {} + + private static final String MAPPING = + "<?xml version=\"1.0\"?>\n"+ + "<betwixt-config>\n"+ + " <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$Container\">\n"+ + " <element name=\"container\">\n"+ + " <element name=\"elements\">\n"+ + " <element property=\"elements\"/>\n"+ + " </element>\n"+ + " </element>\n"+ + " </class>\n"+ + " <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$ElementA\">\n"+ + " <element name=\"elementA\"/>\n"+ + " </class>\n"+ + " <class name=\"org.apache.commons.betwixt.registry.TestRegistryPolymorphicResolution$ElementB\">\n"+ + " <element name=\"elementB\"/>\n"+ + " </class>\n"+ + "</betwixt-config>"; + + public void testRegisterThenResolve() throws Exception + { + XMLIntrospector introspector = new XMLIntrospector(); + introspector.register(new InputSource(new StringReader(MAPPING))); + + + ElementDescriptor descriptor = introspector.introspect(Element.class).getElementDescriptor(); + ElementMapping elementMapping = new ElementMapping(); + elementMapping.setAttributes(new AttributesImpl()); + elementMapping.setName("Bogus"); + elementMapping.setDescriptor(descriptor); + elementMapping.setType(Iterator.class); + ReadContext readContext = new ReadContext(new BindingConfiguration(), new ReadConfiguration()); + + assertNull(introspector.getPolymorphicReferenceResolver().resolveType(elementMapping, readContext)); + + elementMapping.setName("elementA"); + Class resolution = introspector.getPolymorphicReferenceResolver().resolveType(elementMapping, readContext); + assertEquals("Should resolve to the element about", ElementA.class, resolution); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]