Author: veithen
Date: Fri Jun 10 19:25:07 2011
New Revision: 1134422

URL: http://svn.apache.org/viewvc?rev=1134422&view=rev
Log:
AXIOM-365:
* Modified the iterator implementation so that a 
ConcurrentModificationException is thrown if the current node is removed using 
a method other than Iterator#remove().
* Fixed two instances where Axiom itself uses iterators in an incorrect way.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetChildrenConcurrentModification.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/traverse/OMAbstractIterator.java
    
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/OMSourcedElementImpl.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-api/src/main/java/org/apache/axiom/om/impl/traverse/OMAbstractIterator.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/traverse/OMAbstractIterator.java?rev=1134422&r1=1134421&r2=1134422&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/traverse/OMAbstractIterator.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/traverse/OMAbstractIterator.java
 Fri Jun 10 19:25:07 2011
@@ -19,9 +19,11 @@
 
 package org.apache.axiom.om.impl.traverse;
 
+import java.util.ConcurrentModificationException;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
+import org.apache.axiom.om.OMContainer;
 import org.apache.axiom.om.OMNode;
 
 /**
@@ -29,6 +31,12 @@ import org.apache.axiom.om.OMNode;
  */
 public abstract class OMAbstractIterator implements Iterator {
     private OMNode currentNode;
+    
+    /**
+     * The parent of the current node. This is used to detect concurrent 
modifications.
+     */
+    private OMContainer currentParent;
+    
     private OMNode nextNode;
     private boolean noMoreNodes;
     private boolean nextCalled;
@@ -55,6 +63,9 @@ public abstract class OMAbstractIterator
         } else if (nextNode != null) {
             return true;
         } else {
+            if (currentNode.getParent() != currentParent) {
+                throw new ConcurrentModificationException("The current node 
has been removed using a method other than Iterator#remove()");
+            }
             nextNode = getNextNode(currentNode);
             noMoreNodes = nextNode == null;
             return !noMoreNodes;
@@ -64,6 +75,7 @@ public abstract class OMAbstractIterator
     public Object next() {
         if (hasNext()) {
             currentNode = nextNode;
+            currentParent = currentNode.getParent();
             nextNode = null;
             nextCalled = true;
             return currentNode;

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=1134422&r1=1134421&r2=1134422&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
 Fri Jun 10 19:25:07 2011
@@ -484,7 +484,7 @@ public abstract class ParentNode extends
         // Check if the Child is there
         Iterator children = this.getChildren();
         boolean childFound = false;
-        while (children.hasNext()) {
+        while (!childFound && children.hasNext()) {
             ChildNode tempNode = (ChildNode) children.next();
             if (tempNode.equals(oldChild)) {
 

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=1134422&r1=1134421&r2=1134422&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 Fri Jun 10 19:25:07 2011
@@ -1019,8 +1019,8 @@ public class OMSourcedElementImpl extend
             OMDataSource oldDS = this.dataSource;
             Iterator it = getChildren();
             while(it.hasNext()) {
-                OMNode node = (OMNode) it.next();
-                node.detach();
+                it.next();
+                it.remove();
             }
             this.dataSource = dataSource;
             setComplete(false);

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=1134422&r1=1134421&r2=1134422&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
 Fri Jun 10 19:25:07 2011
@@ -88,6 +88,7 @@ public class OMTestSuiteBuilder extends 
         addTest(new 
org.apache.axiom.ts.om.element.TestGetAttributeWithXmlPrefix2(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetChildElements(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetChildren(metaFactory));
+        addTest(new 
org.apache.axiom.ts.om.element.TestGetChildrenConcurrentModification(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetChildrenRemove1(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetChildrenRemove2(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetChildrenRemove3(metaFactory));

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetChildrenConcurrentModification.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetChildrenConcurrentModification.java?rev=1134422&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetChildrenConcurrentModification.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetChildrenConcurrentModification.java
 Fri Jun 10 19:25:07 2011
@@ -0,0 +1,57 @@
+/*
+ * 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 java.util.ConcurrentModificationException;
+import java.util.Iterator;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that the iterator returned by {@link OMContainer#getChildren()} 
throws a
+ * {@link ConcurrentModificationException} if the current node is removed 
using a method other than
+ * {@link Iterator#remove()}.
+ */
+public class TestGetChildrenConcurrentModification extends AxiomTestCase {
+    public TestGetChildrenConcurrentModification(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement parent = factory.createOMElement("parent", null);
+        factory.createOMElement("child1", null, parent);
+        factory.createOMElement("child2", null, parent);
+        factory.createOMElement("child3", null, parent);
+        Iterator it = parent.getChildren();
+        it.next();
+        OMElement child2 = (OMElement)it.next();
+        child2.detach();
+        try {
+            it.next();
+            fail("Expected ConcurrentModificationException");
+        } catch (ConcurrentModificationException ex) {
+            // Expected
+        }
+    }
+}

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


Reply via email to