Author: veithen
Date: Thu Apr 12 06:18:10 2012
New Revision: 1325130

URL: http://svn.apache.org/viewvc?rev=1325130&view=rev
Log:
Clarified the OMElement#removeAttribute API and fixed a couple of issues and 
inconsistencies. Also added a couple of test cases. The API had no useful 
documentation and no test coverage.

Also fixed an issue in DOOM's Element#removeAttributeNode implementation.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestRemoveAttributeNotOwner.java
   (with props)
    
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttribute.java
   (with props)
    
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttributeNotOwner.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.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-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java?rev=1325130&r1=1325129&r2=1325130&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
 Thu Apr 12 06:18:10 2012
@@ -372,9 +372,12 @@ public interface OMElement extends OMNod
     OMAttribute addAttribute(String localName, String value, OMNamespace ns);
 
     /**
-     * Method removeAttribute
-     *
+     * Removes the given attribute from this element.
+     * 
      * @param attr
+     *            the attribute to remove
+     * @throws OMException
+     *             if the attribute is not owned by this element
      */
     void removeAttribute(OMAttribute attr);
 

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java?rev=1325130&r1=1325129&r2=1325130&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
 Thu Apr 12 06:18:10 2012
@@ -70,6 +70,7 @@ public class DOMTestSuiteBuilder extends
         addTest(new org.apache.axiom.ts.dom.element.TestGetTextContent(dbf));
         addTest(new org.apache.axiom.ts.dom.element.TestInsertBefore(dbf));
         addTest(new 
org.apache.axiom.ts.dom.element.TestInsertBeforeWithDocumentFragment(dbf));
+        addTest(new 
org.apache.axiom.ts.dom.element.TestRemoveAttributeNotOwner(dbf));
         addTest(new org.apache.axiom.ts.dom.element.TestRemoveFirstChild(dbf));
         addTest(new org.apache.axiom.ts.dom.element.TestRemoveLastChild(dbf));
         addTest(new 
org.apache.axiom.ts.dom.element.TestRemoveSingleChild(dbf));

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestRemoveAttributeNotOwner.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestRemoveAttributeNotOwner.java?rev=1325130&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestRemoveAttributeNotOwner.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestRemoveAttributeNotOwner.java
 Thu Apr 12 06:18:10 2012
@@ -0,0 +1,53 @@
+/*
+ * 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.dom.element;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.apache.axiom.ts.dom.DOMTestCase;
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Tests the behavior of {@link Element#removeAttributeNode(Attr)} if the 
given attribute is not
+ * owned by the element.
+ */
+public class TestRemoveAttributeNotOwner extends DOMTestCase {
+    public TestRemoveAttributeNotOwner(DocumentBuilderFactory dbf) {
+        super(dbf);
+    }
+
+    protected void runTest() throws Throwable {
+        Document document = dbf.newDocumentBuilder().newDocument();
+        Element element1 = document.createElementNS(null, "test");
+        Attr attr1 = document.createAttributeNS(null, "attr");
+        element1.setAttributeNodeNS(attr1);
+        Element element2 = document.createElementNS(null, "test");
+        Attr attr2 = document.createAttributeNS(null, "attr");
+        element2.setAttributeNodeNS(attr2);
+        try {
+            element1.removeAttributeNode(attr2);
+            fail("Expected DOMException");
+        } catch (DOMException ex) {
+            assertEquals(DOMException.NOT_FOUND_ERR, ex.code);
+        }
+    }
+}

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

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java?rev=1325130&r1=1325129&r2=1325130&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java
 Thu Apr 12 06:18:10 2012
@@ -426,4 +426,10 @@ public class AttributeMap implements Nam
             nodes.removeAllElements();
         }
     }
+    
+    void remove(AttrImpl attr) {
+        if (nodes.remove(attr)) {
+            attr.setOwnerElement(null);
+        }
+    }
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java?rev=1325130&r1=1325129&r2=1325130&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
 Thu Apr 12 06:18:10 2012
@@ -334,14 +334,13 @@ public class ElementImpl extends ParentN
      * @see org.w3c.dom.Element#removeAttributeNode(org.w3c.dom.Attr)
      */
     public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
-        if (this.attributes == null
-                || this.attributes.getNamedItem(oldAttr.getName()) == null) {
+        if (oldAttr.getOwnerElement() != this) {
             String msg = DOMMessageFormatter.formatMessage(
                     DOMMessageFormatter.DOM_DOMAIN, 
DOMException.NOT_FOUND_ERR, null);
             throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
         }
-        return (AttrImpl) this.attributes.removeNamedItem(oldAttr
-                .getName());
+        attributes.remove((AttrImpl)oldAttr);
+        return oldAttr;
     }
 
     /*
@@ -973,13 +972,11 @@ public class ElementImpl extends ParentN
         OMElementImplUtil.writeTextTo(this, out, cache);
     }
 
-    /**
-     * Removes an attribute from the element.
-     *
-     * @see org.apache.axiom.om.OMElement#removeAttribute 
(org.apache.axiom.om.OMAttribute)
-     */
     public void removeAttribute(OMAttribute attr) {
-        this.removeAttributeNode((AttrImpl) attr);
+        if (attr.getOwner() != this) {
+            throw new OMException("The attribute is not owned by this 
element");
+        }
+        attributes.remove((AttrImpl)attr);
     }
 
     /**

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=1325130&r1=1325129&r2=1325130&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
 Thu Apr 12 06:18:10 2012
@@ -665,13 +665,13 @@ public class OMElementImpl extends OMNod
         return attr;
     }
 
-    /** Method removeAttribute. */
     public void removeAttribute(OMAttribute attr) {
-        if (attributes != null) {
-            // Remove the owner from this attribute
-            ((OMAttributeImpl)attr).owner = null;
-            attributes.remove(attr.getQName());
+        if (attr.getOwner() != this) {
+            throw new OMException("The attribute is not owned by this 
element");
         }
+        // Remove the owner from this attribute
+        ((OMAttributeImpl)attr).owner = null;
+        attributes.remove(attr.getQName());
     }
 
     public OMAttribute addAttribute(String attributeName, String value,

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1325130&r1=1325129&r2=1325130&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
 Thu Apr 12 06:18:10 2012
@@ -211,6 +211,8 @@ public class OMTestSuiteBuilder extends 
         addTest(new 
org.apache.axiom.ts.om.element.TestGetXMLStreamReaderWithPreserveNamespaceContext(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestIsCompleteAfterAddingIncompleteChild(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestMultipleDefaultNS(metaFactory));
+        addTest(new 
org.apache.axiom.ts.om.element.TestRemoveAttribute(metaFactory));
+        addTest(new 
org.apache.axiom.ts.om.element.TestRemoveAttributeNotOwner(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestResolveQNameWithDefaultNamespace(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestResolveQNameWithNonDefaultNamespace(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestResolveQNameWithoutNamespace(metaFactory));

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttribute.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttribute.java?rev=1325130&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttribute.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttribute.java
 Thu Apr 12 06:18:10 2012
@@ -0,0 +1,49 @@
+/*
+ * 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.Iterator;
+
+import org.apache.axiom.om.OMAttribute;
+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 the behavior of {@link OMElement#removeAttribute(OMAttribute)}.
+ */
+public class TestRemoveAttribute extends AxiomTestCase {
+    public TestRemoveAttribute(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement element = factory.createOMElement("test", null);
+        OMAttribute attr1 = element.addAttribute("attr1", "value1", null);
+        OMAttribute attr2 = element.addAttribute("attr2", "value2", null);
+        element.removeAttribute(attr1);
+        assertNull(attr1.getOwner());
+        Iterator it = element.getAllAttributes();
+        assertTrue(it.hasNext());
+        assertSame(attr2, it.next());
+        assertFalse(it.hasNext());
+    }
+}

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

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttributeNotOwner.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttributeNotOwner.java?rev=1325130&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttributeNotOwner.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveAttributeNotOwner.java
 Thu Apr 12 06:18:10 2012
@@ -0,0 +1,50 @@
+/*
+ * 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.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests the behavior of {@link OMElement#removeAttribute(OMAttribute)} if the 
given attribute is
+ * not owned by the element.
+ */
+public class TestRemoveAttributeNotOwner extends AxiomTestCase {
+    public TestRemoveAttributeNotOwner(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement element1 = factory.createOMElement("test", null);
+        element1.addAttribute("attr", "value", null);
+        OMElement element2 = factory.createOMElement("test", null);
+        OMAttribute attr = element2.addAttribute("attr", "value", null);
+        try {
+            element1.removeAttribute(attr);
+            fail("Expected OMException");
+        } catch (OMException ex) {
+            // Expected
+        }
+    }
+}

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


Reply via email to