You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2012/02/27 20:37:25 UTC

svn commit: r1294297 - in /webservices/commons/trunk/modules/axiom/modules: axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/ axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/ axiom-testsu...

Author: veithen
Date: Mon Feb 27 19:37:24 2012
New Revision: 1294297

URL: http://svn.apache.org/viewvc?rev=1294297&view=rev
Log:
AXIOM-411: Fixed multiple issues in addChild when used to add a node to an element that it is already a child of. This change also fixes an issue in DOOM's Node#removeChild.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMDOMImplementationTest.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMDOMTestSuiteBuilder.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/dom/
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/dom/TestRemoveChildIncomplete.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddChildWithSameParent.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMNodeImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java?rev=1294297&r1=1294296&r2=1294297&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java Mon Feb 27 19:37:24 2012
@@ -492,55 +492,15 @@ public abstract class ParentNode extends
                                            DOMException.NO_MODIFICATION_ALLOWED_ERR, null));
         }
 
-        // Check if the Child is there
-        Iterator children = this.getChildren();
-        boolean childFound = false;
-        while (!childFound && children.hasNext()) {
-            ChildNode tempNode = (ChildNode) children.next();
-            if (tempNode.equals(oldChild)) {
-
-                if (this.firstChild == tempNode) {
-                    // If this is the first child
-                    ChildNode nextSib = tempNode.nextSibling;
-                    this.firstChild = nextSib;
-                    if (nextSib == null) {
-                        this.lastChild = null;
-                    } else {
-                        nextSib.previousSibling = null;
-                    }
-                    tempNode.parentNode = null;
-                    tempNode.nextSibling = null;
-                } else if (this.lastChild == tempNode) {
-                    // not the first child, but the last child
-                    ChildNode prevSib = tempNode.previousSibling;
-                    this.lastChild = prevSib;
-                    prevSib.nextSibling = null;
-                    tempNode.parentNode = null;
-                    tempNode.previousSibling = null;
-                } else {
-
-                    ChildNode oldDomChild = (ChildNode) oldChild;
-                    ChildNode privChild = oldDomChild.previousSibling;
-
-                    privChild.nextSibling = oldDomChild.nextSibling;
-                    oldDomChild.nextSibling.previousSibling = privChild;
-
-                    // Remove old child's references to this tree
-                    oldDomChild.nextSibling = null;
-                    oldDomChild.previousSibling = null;
-                }
-                // Child found
-                childFound = true;
-            }
-        }
-
-        if (!childFound)
+        if (oldChild.getParentNode() == this) {
+            ((ChildNode)oldChild).detach();
+            return oldChild;
+        } else {
             throw new DOMException(DOMException.NOT_FOUND_ERR,
                                    DOMMessageFormatter.formatMessage(
                                            DOMMessageFormatter.DOM_DOMAIN, DOMException.NOT_FOUND_ERR,
                                            null));
-
-        return oldChild;
+        }
     }
 
     private boolean isAncestor(Node newNode) {

Added: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMDOMImplementationTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMDOMImplementationTest.java?rev=1294297&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMDOMImplementationTest.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMDOMImplementationTest.java Mon Feb 27 19:37:24 2012
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.om.impl.dom;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactory;
+import org.apache.axiom.ts.OMDOMTestSuiteBuilder;
+
+public class OMDOMImplementationTest extends TestCase {
+    public static TestSuite suite() {
+        OMDOMTestSuiteBuilder builder = new OMDOMTestSuiteBuilder(new OMDOMMetaFactory());
+        return builder.build();
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMDOMImplementationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=1294297&r1=1294296&r2=1294297&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java Mon Feb 27 19:37:24 2012
@@ -307,19 +307,17 @@ public class OMElementImpl extends OMNod
 
     /** Method addChild. */
     private void addChild(OMNodeImpl child) {
-        if (child.parent == this &&
-            child == lastChild) {
+        if (child.parent == this && child == lastChild && done) {
             // The child is already the last node. 
             // We don't need to detach and re-add it.
         } else {
             // Normal Case
             
-            // The order of these statements is VERY important
-            // Since setting the parent has a detach method inside
-            // it strips down all the rerefences to siblings.
-            // setting the siblings should take place AFTER setting the parent
-
-            child.setParent(this);
+            if (child.parent != null) {
+                child.detach();
+            }
+            
+            child.parent = this;
 
             if (firstChild == null) {
                 firstChild = child;

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMNodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMNodeImpl.java?rev=1294297&r1=1294296&r2=1294297&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMNodeImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMNodeImpl.java Mon Feb 27 19:37:24 2012
@@ -70,7 +70,6 @@ public abstract class OMNodeImpl extends
         super(factory);
         this.done = done;
         if ((parent != null)) {
-            this.parent = (OMContainerEx) parent;
             parent.addChild(this);
         }
 

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMDOMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMDOMTestSuiteBuilder.java?rev=1294297&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMDOMTestSuiteBuilder.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMDOMTestSuiteBuilder.java Mon Feb 27 19:37:24 2012
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.ts;
+
+import org.apache.axiom.om.dom.DOMMetaFactory;
+import org.apache.axiom.testutils.suite.TestSuiteBuilder;
+
+/**
+ * Builds a test suite for Axiom implementations that also implement DOM. Note that this test suite
+ * only contains tests that depend on Axiom specific features. Pure DOM tests (that are executable
+ * with a standard DOM implementation) should go to
+ * {@link org.apache.axiom.ts.dom.DOMTestSuiteBuilder}.
+ */
+public class OMDOMTestSuiteBuilder extends TestSuiteBuilder {
+    private final DOMMetaFactory metaFactory;
+
+    public OMDOMTestSuiteBuilder(DOMMetaFactory metaFactory) {
+        this.metaFactory = metaFactory;
+    }
+
+    protected void addTests() {
+        addTest(new org.apache.axiom.ts.om.dom.TestRemoveChildIncomplete(metaFactory));
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMDOMTestSuiteBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java?rev=1294297&r1=1294296&r2=1294297&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java Mon Feb 27 19:37:24 2012
@@ -117,6 +117,8 @@ public class OMTestSuiteBuilder extends 
         addTest(new org.apache.axiom.ts.om.element.TestAddChild(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddChild2(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddChildWithParent(metaFactory));
+        addTest(new org.apache.axiom.ts.om.element.TestAddChildWithSameParent(metaFactory, true));
+        addTest(new org.apache.axiom.ts.om.element.TestAddChildWithSameParent(metaFactory, false));
         addTest(new org.apache.axiom.ts.om.element.TestChildReDeclaringGrandParentsDefaultNSWithPrefix(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestChildReDeclaringParentsDefaultNSWithPrefix(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestDeclareDefaultNamespace1(metaFactory));

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/dom/TestRemoveChildIncomplete.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/dom/TestRemoveChildIncomplete.java?rev=1294297&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/dom/TestRemoveChildIncomplete.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/dom/TestRemoveChildIncomplete.java Mon Feb 27 19:37:24 2012
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.ts.om.dom;
+
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * Tests the behavior of {@link Node#removeChild(Node)} on an element that has not been built
+ * completely.
+ */
+public class TestRemoveChildIncomplete extends AxiomTestCase {
+    public TestRemoveChildIncomplete(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        Element element = (Element)AXIOMUtil.stringToOM(metaFactory.getOMFactory(),
+                "<parent><a/><b/><c/></parent>");
+        Node b = element.getFirstChild().getNextSibling();
+        element.removeChild(b);
+        Node child = element.getFirstChild();
+        assertEquals("a", child.getLocalName());
+        child = child.getNextSibling();
+        assertEquals("c", child.getLocalName());
+        assertNull(child.getNextSibling());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/dom/TestRemoveChildIncomplete.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddChildWithSameParent.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddChildWithSameParent.java?rev=1294297&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddChildWithSameParent.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddChildWithSameParent.java Mon Feb 27 19:37:24 2012
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.ts.om.element;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests the behavior of {@link OMContainer#addChild(OMNode)} when used to add a node to an element
+ * it is already a child of. In this case, the expected result is that the node is moved to the end
+ * of the list of children.
+ */
+public class TestAddChildWithSameParent extends AxiomTestCase {
+    private final boolean build;
+    
+    public TestAddChildWithSameParent(OMMetaFactory metaFactory, boolean build) {
+        super(metaFactory);
+        this.build = build;
+        addTestProperty("build", String.valueOf(build));
+    }
+
+    protected void runTest() throws Throwable {
+        OMElement parent = AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<parent><a/><b/><c/></parent>");
+        if (build) {
+            parent.build();
+        }
+        OMElement b = (OMElement)parent.getFirstOMChild().getNextOMSibling();
+        parent.addChild(b);
+        OMElement child = (OMElement)parent.getFirstOMChild();
+        assertEquals("a", child.getLocalName());
+        child = (OMElement)child.getNextOMSibling();
+        assertEquals("c", child.getLocalName());
+        child = (OMElement)child.getNextOMSibling();
+        assertSame(child, b);
+        assertNull(child.getNextOMSibling());
+        assertSame(parent, b.getParent());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddChildWithSameParent.java
------------------------------------------------------------------------------
    svn:eol-style = native