Author: rdonkin
Date: Wed Nov  9 15:05:13 2005
New Revision: 332171

URL: http://svn.apache.org/viewcvs?rev=332171&view=rev
Log:
Added code that checks polymorpic code. Submitted by Thomas Dudziak. 
Issue#37369.

Added:
    
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping2.java
Modified:
    
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreatorFactory.java
    
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java
    
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestMultipleCollectionMappings.java

Modified: 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreatorFactory.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreatorFactory.java?rev=332171&r1=332170&r2=332171&view=diff
==============================================================================
--- 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreatorFactory.java
 (original)
+++ 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreatorFactory.java
 Wed Nov  9 15:05:13 2005
@@ -15,9 +15,11 @@
  */ 
 package org.apache.commons.betwixt.io.read;
 
+import java.beans.IntrospectionException;
 import java.lang.reflect.Constructor;
 
 import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.XMLBeanInfo;
 import org.apache.commons.betwixt.registry.PolymorphicReferenceResolver;
 import org.apache.commons.logging.Log;
 
@@ -146,6 +148,31 @@
                 if ( theClass == null ) {
                     // create based on type
                     theClass = element.getType();
+                }
+                
+                if (descriptor != null && descriptor.isPolymorphic()) {
+                    // check that the type is suitably named
+                    try {
+                        XMLBeanInfo xmlBeanInfo = 
context.getXMLIntrospector().introspect(theClass);
+                        String namespace = element.getNamespace();
+                        String name = element.getName();
+                        if (namespace == null) {
+                            if 
(!name.equals(xmlBeanInfo.getElementDescriptor().getQualifiedName())) {
+                                context.getLog().debug("Polymorphic type does 
not match element");
+                                return null;
+                            }
+                        } else if 
(!namespace.equals(xmlBeanInfo.getElementDescriptor().getURI())
+                                || 
!name.equals(xmlBeanInfo.getElementDescriptor().getLocalName())) {
+                            context.getLog().debug("Polymorphic type does not 
match element");
+                            return null;
+                        }
+                    } catch (IntrospectionException e) {
+                        context.getLog().warn( 
+                            "Could not introspect type to test introspection: 
" + theClass.getName() );
+                        context.getLog().debug( "Introspection failed: ", e );
+                        return null;
+                    }
+                    
                 }
                 
                 if ( log.isTraceEnabled() ) {

Modified: 
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=332171&r1=332170&r2=332171&view=diff
==============================================================================
--- 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java
 (original)
+++ 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping.java
 Wed Nov  9 15:05:13 2005
@@ -60,6 +60,9 @@
     public static class ElementB implements Element
     {}
 
+    public static class ElementC
+    {}
+
     private static final String MAPPING =
         "<?xml version=\"1.0\"?>\n"+
         "<betwixt-config>\n"+
@@ -76,6 +79,9 @@
         "  <class 
name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementB\">\n"+
         "    <element name=\"elementB\"/>\n"+
         "  </class>\n"+
+        "  <class 
name=\"org.apache.commons.betwixt.TestCollectionMapping$ElementC\">\n"+
+        "    <element name=\"elementC\"/>\n"+
+        "  </class>\n"+
         "</betwixt-config>";
     private static final String EXPECTED =
         "<?xml version=\"1.0\" ?>\n"+
@@ -85,6 +91,13 @@
         "      <elementA/>\n"+
         "    </elements>\n"+
         "  </container>\n";
+    private static final String INVALID_XML =
+        "<?xml version=\"1.0\" ?>\n"+
+        "  <container>\n"+
+        "    <elements>\n"+
+        "      <elementC/>\n"+
+        "    </elements>\n"+
+        "  </container>\n";
     
     public TestCollectionMapping(String testName)
     {
@@ -129,5 +142,18 @@
         assertTrue(it.next() instanceof ElementA);
         assertFalse(it.hasNext());
     }
-    
+
+    public void testInvalidXML() throws IOException, IntrospectionException, 
SAXException
+    {
+        BeanReader beanReader = new BeanReader();
+
+        beanReader.registerMultiMapping(new InputSource(new 
StringReader(MAPPING)));
+
+        StringReader xmlReader = new StringReader(INVALID_XML);
+        Container    container = (Container)beanReader.parse(xmlReader);
+
+        // either we get an exception in the parse method (would perhaps be 
better)
+        // or the collection is empty (ElementC cannot be added)
+        assertFalse(container.getElements().hasNext());
+    }
 }

Added: 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping2.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping2.java?rev=332171&view=auto
==============================================================================
--- 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping2.java
 (added)
+++ 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestCollectionMapping2.java
 Wed Nov  9 15:05:13 2005
@@ -0,0 +1,120 @@
+/*
+ * 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.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.betwixt.io.BeanReader;
+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 TestCollectionMapping2 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 class Element
+    {
+        private List _subElements = new ArrayList();
+
+        public Iterator getSubElements()
+        {
+            return _subElements.iterator();
+        }
+
+        public void addSubElement(SubElement subElement)
+        {
+            _subElements.add(subElement);
+        }
+    }
+
+    public static interface SubElement
+    {}
+
+    public static class SubElementA implements SubElement
+    {}
+
+    public static class SubElementB implements SubElement
+    {}
+
+    private static final String MAPPING =
+        "<?xml version='1.0'?>\n"+
+        "<betwixt-config>\n"+
+        "  <class name='"+Container.class.getName()+"'>\n"+
+        "    <element name='container'>\n"+
+        "      <element property='elements' updater='addElement'/>\n"+
+        "    </element>\n"+
+        "  </class>\n"+
+        "  <class name='"+Element.class.getName()+"'>\n"+
+        "    <element name='element'>\n"+
+        "      <element property='subElements' updater='addSubElement'/>\n"+
+        "    </element>\n"+
+        "  </class>\n"+
+        "  <class name='"+SubElementA.class.getName()+"'>\n"+
+        "    <element name='subElementA'/>\n"+
+        "  </class>\n"+
+        "  <class name='"+SubElementB.class.getName()+"'>\n"+
+        "    <element name='subElementB'/>\n"+
+        "  </class>\n"+
+        "</betwixt-config>";
+    private static final String INVALID_XML =
+        "<?xml version=\"1.0\" ?>\n"+
+        "  <container>\n"+
+        "    <subElementB/>\n"+
+        "  </container>\n";
+    
+    public TestCollectionMapping2(String testName)
+    {
+        super(testName);
+    }
+
+    public void testInvalidXML() throws IOException, IntrospectionException, 
SAXException
+    {
+        BeanReader beanReader = new BeanReader();
+
+        beanReader.registerMultiMapping(new InputSource(new 
StringReader(MAPPING)));
+
+        StringReader xmlReader = new StringReader(INVALID_XML);
+        Container     database  = (Container) beanReader.parse(xmlReader);
+
+        // either we get an exception in the parse method (would perhaps be 
better)
+        // or the collection is empty (SubElementB cannot be added to 
Container)
+        assertFalse(database.getElements().hasNext());
+    }
+}

Modified: 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestMultipleCollectionMappings.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestMultipleCollectionMappings.java?rev=332171&r1=332170&r2=332171&view=diff
==============================================================================
--- 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestMultipleCollectionMappings.java
 (original)
+++ 
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestMultipleCollectionMappings.java
 Wed Nov  9 15:05:13 2005
@@ -56,7 +56,7 @@
             return _nodes.iterator();
         }
 
-        public void addNode(Node node)
+        public void addNode(Node1 node)
         {
             _nodes.add(node);
         }
@@ -71,13 +71,55 @@
     public static class ElementB implements Element
     {}
 
-    public static abstract class Node
+    public static class ElementC
     {}
 
-    public static class Node1 extends Node
-    {}
+    public static class Node1
+    {
+        private String name;
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public void setName(String name)
+        {
+            this.name = name;
+        }
+    }
+
+    public static class Node2 extends Node1
+    {
+        private String name;
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public void setName(String name)
+        {
+            this.name = name;
+        }
+    }
+
+    public static class Node3
+    {
+        private List _innerNodes = new ArrayList();
 
-    public static class Node2 extends Node
+        public Iterator getInnerNodes()
+        {
+            return _innerNodes.iterator();
+        }
+
+        public void addInnerNode(InnerNode node)
+        {
+            _innerNodes.add(node);
+        }
+    }
+
+    public static class InnerNode
     {}
 
     private static final String MAPPING =
@@ -95,11 +137,27 @@
         "  <class name=\""+ElementB.class.getName()+"\">\n"+
         "    <element name=\"elementB\"/>\n"+
         "  </class>\n"+
+        "  <class name=\""+ElementC.class.getName()+"\">\n"+
+        "    <element name=\"elementC\"/>\n"+
+        "  </class>\n"+
         "  <class name=\""+Node1.class.getName()+"\">\n"+
-        "    <element name=\"node1\"/>\n"+
+        "    <element name=\"node1\">\n"+
+        "      <attribute name=\"name\" property=\"name\"/>\n"+
+        "    </element>\n"+
         "  </class>\n"+
         "  <class name=\""+Node2.class.getName()+"\">\n"+
-        "    <element name=\"node2\"/>\n"+
+        "    <element name=\"node2\">\n"+
+        "      <attribute name=\"name\" property=\"name\"/>\n"+
+        "    </element>\n"+
+        "  </class>\n"+
+        "  <class name=\""+Node3.class.getName()+"\">\n"+
+        "    <element name=\"node2\">\n"+
+        "      <attribute name=\"name\" property=\"name\"/>\n"+
+        "      <element property=\"innerNodes\" updater=\"addInnerNode\"/>\n"+
+        "    </element>\n"+
+        "  </class>\n"+
+        "  <class name=\""+InnerNode.class.getName()+"\">\n"+
+        "    <element name=\"innerNode\"/>\n"+
         "  </class>\n"+
         "</betwixt-config>";
     private static final String EXPECTED1 =
@@ -124,6 +182,14 @@
         "    <node1/>\n"+
         "    <node2/>\n"+
         "  </container>\n";
+    private static final String INVALID_XML =
+        "<?xml version=\"1.0\" ?>\n"+
+        "  <container>\n"+
+        "    <elementA/>\n"+
+        "    <elementC/>\n"+
+        "    <node3 name=\"test\"/>\n"+
+        "    <innerNode/>\n"+
+        "  </container>\n";
     
     public TestMultipleCollectionMappings(String testName)
     {
@@ -262,5 +328,23 @@
         assertTrue(it.next() instanceof Node2);
         assertFalse(it.hasNext());
 
+    }
+
+    public void testInvalidXML() throws IOException, SAXException, 
IntrospectionException
+    {
+        BeanReader beanReader = new BeanReader();
+
+        beanReader.registerMultiMapping(new InputSource(new 
StringReader(MAPPING)));
+
+        StringReader xmlReader = new StringReader(INVALID_XML);
+        Container    container = (Container)beanReader.parse(xmlReader);
+        Iterator     it        = container.getElements();
+
+        assertTrue(it.next() instanceof ElementA);
+        assertFalse(it.hasNext());
+
+        it = container.getNodes();
+
+        assertFalse(it.hasNext());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to