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 2011/08/28 13:42:01 UTC

svn commit: r1162500 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/ axiom-api/src/main/java/org/apache/axiom/om/impl/builder/ axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ axiom-dom/src/main/...

Author: veithen
Date: Sun Aug 28 11:42:00 2011
New Revision: 1162500

URL: http://svn.apache.org/viewvc?rev=1162500&view=rev
Log:
* Fixed various issues with getOMDocumentElement and setOMDocumentElement, in particular AXIOM-361.
* Make sure that the behavior of setOMDocumentElement is well defined and doesn't violate encapsulation (i.e. that it doesn't allow application code to put the OMDocument instance into an inconsistent state).

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElement.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementAfterDetach.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementWithParser.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNew.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNull.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementReplace.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDocument.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.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-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPMessageImpl.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/OMDocument.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDocument.java?rev=1162500&r1=1162499&r2=1162500&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDocument.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDocument.java Sun Aug 28 11:42:00 2011
@@ -28,22 +28,25 @@ public interface OMDocument extends OMCo
     final static String XML_11 = "1.1";
 
     /**
-     * Returns the document element.
-     *
-     * @return Returns OMElement.
+     * Get the document element.
+     * 
+     * @return the document element, or <code>null</code> if the document doesn't have any children
+     *         of type {@link OMElement}
      */
     OMElement getOMDocumentElement();
 
     /**
-     * Sets the document element of the XML document.
-     *
-     * @param rootElement
+     * Set the document element of the XML document. If the document has no document element, then
+     * the new document element will be appended as the last child. If the document already has a
+     * document element, then it will be replaced by the new one and the position of the other
+     * children relative to the document element is preserved.
+     * 
+     * @param documentElement
+     *            the new document element; must not be <code>null</code>
+     * @throws IllegalArgumentException
+     *             if the parameter is <code>null</code>
      */
-    // TODO: this method and its implementations need review:
-    //        - LLOM doesn't add the element as a child (!!!)
-    //        - Neither LLOM nor DOOM updates the parent of the element
-    // Note that OMSourcedElementImpl seems to depend on this behavior
-    void setOMDocumentElement(OMElement rootElement);
+    void setOMDocumentElement(OMElement documentElement);
 
     /**
      * Get the character set encoding scheme. This is the encoding that was used used for this

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=1162500&r1=1162499&r2=1162500&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java Sun Aug 28 11:42:00 2011
@@ -124,7 +124,6 @@ public class StAXOMBuilder extends StAXB
         lookAheadToken = -1;
         document = createDocument();
         lastNode = element;
-        document.setOMDocumentElement(element);
         populateOMElement(element);
     }
     

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java?rev=1162500&r1=1162499&r2=1162500&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java Sun Aug 28 11:42:00 2011
@@ -30,6 +30,7 @@ import org.apache.axiom.om.OMOutputForma
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.apache.axiom.om.impl.OMDocumentImplUtil;
+import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
 import org.w3c.dom.Attr;
 import org.w3c.dom.CDATASection;
@@ -68,8 +69,6 @@ public class DocumentImpl extends Parent
 
     private Vector idAttrs;
 
-    protected ElementImpl documentElement;
-
     protected Hashtable identifiers;
 
     /** @param ownerDocument  */
@@ -402,8 +401,22 @@ public class DocumentImpl extends Parent
         this.charEncoding = charsetEncoding;
     }
 
-    public void setOMDocumentElement(OMElement rootElement) {
-        this.firstChild = (ElementImpl) rootElement;
+    public void setOMDocumentElement(OMElement documentElement) {
+        if (documentElement == null) {
+            throw new IllegalArgumentException("documentElement must not be null");
+        }
+        OMElement existingDocumentElement = getOMDocumentElement();
+        if (existingDocumentElement == null) {
+            addChild(documentElement);
+        } else {
+            OMNode nextSibling = existingDocumentElement.getNextOMSibling();
+            existingDocumentElement.detach();
+            if (nextSibling == null) {
+                addChild(documentElement);
+            } else {
+                nextSibling.insertSiblingBefore(documentElement);
+            }
+        }
     }
 
     public void setStandalone(String isStandalone) {
@@ -435,17 +448,19 @@ public class DocumentImpl extends Parent
         this.xmlEncoding = encoding;
     }
     
-    /**
-     * Returns the document element.
-     *
-     * @see org.apache.axiom.om.OMDocument#getOMDocumentElement()
-     */
     public OMElement getOMDocumentElement() {
+        return getOMDocumentElement(true);
+    }
 
-        while (this.documentElement == null && !this.done) {
-            this.builder.next();
+    OMElement getOMDocumentElement(boolean build) {
+        OMNode child = build ? getFirstOMChild() : getFirstOMChildIfAvailable();
+        while (child != null) {
+            if (child instanceof OMElement) {
+                return (OMElement)child;
+            }
+            child = build ? child.getNextOMSibling() : ((OMNodeEx)child).getNextOMSiblingIfAvailable();
         }
-        return this.documentElement;
+        return null;
     }
 
     /**

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=1162500&r1=1162499&r2=1162500&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 Sun Aug 28 11:42:00 2011
@@ -232,7 +232,7 @@ public abstract class ParentNode extends
 
         if (this instanceof Document) {
             if (newDomChild instanceof ElementImpl) {
-                if (((DocumentImpl) this).documentElement != null) {
+                if (((DocumentImpl) this).getOMDocumentElement(false) != null) {
                     // Throw exception since there cannot be two document elements
                     throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                                            DOMMessageFormatter.formatMessage(
@@ -242,8 +242,6 @@ public abstract class ParentNode extends
                 if (newDomChild.parentNode == null) {
                     newDomChild.parentNode = this;
                 }
-                // set the document element
-                ((DocumentImpl) this).documentElement = (ElementImpl) newDomChild;
             } else if (!(newDomChild instanceof CommentImpl
                     || newDomChild instanceof ProcessingInstructionImpl
                     || newDomChild instanceof DocumentFragmentImpl

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java?rev=1162500&r1=1162499&r2=1162500&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java Sun Aug 28 11:42:00 2011
@@ -126,18 +126,20 @@ public class OMDOMFactory implements OMF
         }
 
         switch (((ParentNode) parent).getNodeType()) {
-            case Node.ELEMENT_NODE: // We are adding a new child to an elem
+            case Node.ELEMENT_NODE: { // We are adding a new child to an elem
                 ElementImpl parentElem = (ElementImpl) parent;
                 ElementImpl elem = new ElementImpl((DocumentImpl) parentElem
                         .getOwnerDocument(), localName, (NamespaceImpl) ns, this);
                 parentElem.appendChild(elem);
                 return elem;
-
-            case Node.DOCUMENT_NODE:
+            }
+            case Node.DOCUMENT_NODE: {
                 DocumentImpl docImpl = (DocumentImpl) parent;
-                return new ElementImpl(docImpl, localName,
+                ElementImpl elem = new ElementImpl(docImpl, localName,
                                        (NamespaceImpl) ns, this);
-
+                docImpl.appendChild(elem);
+                return elem;
+            }
             case Node.DOCUMENT_FRAGMENT_NODE:
                 DocumentFragmentImpl docFragImpl = (DocumentFragmentImpl) parent;
                 return new ElementImpl((DocumentImpl) docFragImpl

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java?rev=1162500&r1=1162499&r2=1162500&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java Sun Aug 28 11:42:00 2011
@@ -45,9 +45,6 @@ import java.util.Iterator;
 
 /** Class OMDocumentImpl */
 public class OMDocumentImpl extends OMSerializableImpl implements OMDocument, OMContainerEx {
-    /** Field documentElement */
-    protected OMElement documentElement;
-
     /** Field firstChild */
     protected OMNode firstChild;
 
@@ -96,33 +93,45 @@ public class OMDocumentImpl extends OMSe
     public OMDocumentImpl(OMElement documentElement, OMXMLParserWrapper parserWrapper,
                           OMFactory factory) {
         super(factory);
-        this.documentElement = documentElement;
         this.builder = parserWrapper;
+        setOMDocumentElement(documentElement);
     }
 
     public OMXMLParserWrapper getBuilder() {
         return builder;
     }
 
-    /**
-     * Method getDocumentElement.
-     *
-     * @return Returns OMElement.
-     */
     public OMElement getOMDocumentElement() {
-        while (documentElement == null && builder != null) {
-            builder.next();
+        return getOMDocumentElement(true);
+    }
+
+    private OMElement getOMDocumentElement(boolean build) {
+        OMNode child = build ? getFirstOMChild() : getFirstOMChildIfAvailable();
+        while (child != null) {
+            if (child instanceof OMElement) {
+                return (OMElement)child;
+            }
+            child = build ? child.getNextOMSibling() : ((OMNodeEx)child).getNextOMSiblingIfAvailable();
         }
-        return documentElement;
+        return null;
     }
 
-    /**
-     * Method setDocumentElement.
-     *
-     * @param documentElement
-     */
     public void setOMDocumentElement(OMElement documentElement) {
-        this.documentElement = documentElement;
+        if (documentElement == null) {
+            throw new IllegalArgumentException("documentElement must not be null");
+        }
+        OMElement existingDocumentElement = getOMDocumentElement();
+        if (existingDocumentElement == null) {
+            addChild(documentElement);
+        } else {
+            OMNode nextSibling = existingDocumentElement.getNextOMSibling();
+            existingDocumentElement.detach();
+            if (nextSibling == null) {
+                addChild(documentElement);
+            } else {
+                nextSibling.insertSiblingBefore(documentElement);
+            }
+        }
     }
 
     /**
@@ -149,9 +158,8 @@ public class OMDocumentImpl extends OMSe
      */
     public void addChild(OMNode child) {
         if (child instanceof OMElement) {
-            if (this.documentElement == null) {
+            if (getOMDocumentElement(false) == null) {
                 addChild((OMNodeImpl) child);
-                this.documentElement = (OMElement) child;
             } else {
                 throw new OMException("Document element already exists");
             }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPMessageImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPMessageImpl.java?rev=1162500&r1=1162499&r2=1162500&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPMessageImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPMessageImpl.java Sun Aug 28 11:42:00 2011
@@ -54,7 +54,6 @@ public class SOAPMessageImpl extends OMD
 
     public void setSOAPEnvelope(SOAPEnvelope envelope) throws SOAPProcessingException {
         super.addChild(envelope);
-        this.documentElement = envelope;
     }
 
     public void setOMDocumentElement(OMElement rootElement) {
@@ -69,6 +68,6 @@ public class SOAPMessageImpl extends OMD
 
     protected void internalSerialize(XMLStreamWriter writer, boolean cache,
                                      boolean includeXMLDeclaration) throws XMLStreamException {
-        ((OMNodeEx) this.documentElement).internalSerialize(writer, cache);
+        ((OMNodeEx)getOMDocumentElement()).internalSerialize(writer, cache);
     }
 }

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=1162500&r1=1162499&r2=1162500&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 Sun Aug 28 11:42:00 2011
@@ -67,9 +67,15 @@ public class OMTestSuiteBuilder extends 
                 }
             }
         }
+        addTest(new org.apache.axiom.ts.om.document.TestGetOMDocumentElement(metaFactory));
+        addTest(new org.apache.axiom.ts.om.document.TestGetOMDocumentElementAfterDetach(metaFactory));
+        addTest(new org.apache.axiom.ts.om.document.TestGetOMDocumentElementWithParser(metaFactory));
         addTest(new org.apache.axiom.ts.om.document.TestIsCompleteAfterAddingIncompleteChild(metaFactory));
         addTest(new org.apache.axiom.ts.om.document.TestSerializeAndConsume(metaFactory));
         addTest(new org.apache.axiom.ts.om.document.TestSerializeAndConsumeWithIncompleteDescendant(metaFactory));
+        addTest(new org.apache.axiom.ts.om.document.TestSetOMDocumentElementNew(metaFactory));
+        addTest(new org.apache.axiom.ts.om.document.TestSetOMDocumentElementNull(metaFactory));
+        addTest(new org.apache.axiom.ts.om.document.TestSetOMDocumentElementReplace(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddAttributeAlreadyOwnedByElement(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddAttributeAlreadyOwnedByOtherElement(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddAttributeFromOMAttributeMultiple(metaFactory));

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElement.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElement.java?rev=1162500&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElement.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElement.java Sun Aug 28 11:42:00 2011
@@ -0,0 +1,42 @@
+/*
+ * 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.document;
+
+import org.apache.axiom.om.OMDocument;
+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 result of {@link OMDocument#getOMDocumentElement()} after adding a child element to an
+ * empty document.
+ */
+public class TestGetOMDocumentElement extends AxiomTestCase {
+    public TestGetOMDocumentElement(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMDocument document = factory.createOMDocument();
+        OMElement documentElement = factory.createOMElement("root", null, document);
+        assertSame(documentElement, document.getOMDocumentElement());
+    }
+}

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

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementAfterDetach.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementAfterDetach.java?rev=1162500&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementAfterDetach.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementAfterDetach.java Sun Aug 28 11:42:00 2011
@@ -0,0 +1,46 @@
+/*
+ * 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.document;
+
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that {@link OMDocument#getOMDocumentElement()} returns <code>null</code> after the existing
+ * document element has been removed using {@link OMNode#detach()}. This is a regression test for
+ * <a href="https://issues.apache.org/jira/browse/AXIOM-361">AXIOM-361</a>.
+ */
+public class TestGetOMDocumentElementAfterDetach extends AxiomTestCase {
+    public TestGetOMDocumentElementAfterDetach(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMDocument document = factory.createOMDocument();
+        OMElement documentElement = factory.createOMElement("root", null, document);
+        assertSame(documentElement, document.getOMDocumentElement());
+        documentElement.detach();
+        assertNull(document.getOMDocumentElement());
+    }
+}

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

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementWithParser.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementWithParser.java?rev=1162500&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementWithParser.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestGetOMDocumentElementWithParser.java Sun Aug 28 11:42:00 2011
@@ -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.document;
+
+import java.io.StringReader;
+
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests the result of {@link OMDocument#getOMDocumentElement()} for an {@link OMDocument}
+ * constructed from a stream.
+ */
+public class TestGetOMDocumentElementWithParser extends AxiomTestCase {
+    public TestGetOMDocumentElementWithParser(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMDocument document = OMXMLBuilderFactory.createOMBuilder(factory,
+                new StringReader("<!-- comment --><root/><!-- comment -->")).getDocument();
+        OMElement documentElement = document.getOMDocumentElement();
+        assertNotNull(documentElement);
+        assertEquals("root", documentElement.getLocalName());
+    }
+}

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

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNew.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNew.java?rev=1162500&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNew.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNew.java Sun Aug 28 11:42:00 2011
@@ -0,0 +1,54 @@
+/*
+ * 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.document;
+
+import java.util.Iterator;
+
+import org.apache.axiom.om.OMComment;
+import org.apache.axiom.om.OMDocument;
+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 OMDocument#setOMDocumentElement(OMElement)} if there is no existing
+ * document element.
+ */
+public class TestSetOMDocumentElementNew extends AxiomTestCase {
+    public TestSetOMDocumentElementNew(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMDocument document = factory.createOMDocument();
+        OMComment comment = factory.createOMComment(document, "some comment");
+        OMElement documentElement = factory.createOMElement("root", null);
+        document.setOMDocumentElement(documentElement);
+        assertSame(documentElement, document.getOMDocumentElement());
+        assertSame(document, documentElement.getParent());
+        Iterator it = document.getChildren();
+        assertTrue(it.hasNext());
+        assertSame(comment, it.next());
+        assertTrue(it.hasNext());
+        assertSame(documentElement, it.next());
+        assertFalse(it.hasNext());
+    }
+}

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

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNull.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNull.java?rev=1162500&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNull.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementNull.java Sun Aug 28 11:42:00 2011
@@ -0,0 +1,46 @@
+/*
+ * 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.document;
+
+import org.apache.axiom.om.OMDocument;
+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 OMDocument#setOMDocumentElement(OMElement)} with a <code>null</code>
+ * parameter.
+ */
+public class TestSetOMDocumentElementNull extends AxiomTestCase {
+    public TestSetOMDocumentElementNull(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMDocument document = factory.createOMDocument();
+        try {
+            document.setOMDocumentElement(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            // Expected
+        }
+    }
+}

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

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementReplace.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementReplace.java?rev=1162500&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementReplace.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestSetOMDocumentElementReplace.java Sun Aug 28 11:42:00 2011
@@ -0,0 +1,62 @@
+/*
+ * 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.document;
+
+import java.io.StringReader;
+import java.util.Iterator;
+
+import org.apache.axiom.om.OMComment;
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests the behavior of {@link OMDocument#setOMDocumentElement(OMElement)} if the document already
+ * has a document element.
+ */
+public class TestSetOMDocumentElementReplace extends AxiomTestCase {
+    public TestSetOMDocumentElementReplace(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMDocument document = OMXMLBuilderFactory.createOMBuilder(factory,
+                new StringReader("<!--comment1--><root/><!--comment2-->")).getDocument();
+        OMElement documentElement = factory.createOMElement("new", null);
+        document.setOMDocumentElement(documentElement);
+        assertSame(documentElement, document.getOMDocumentElement());
+        Iterator it = document.getChildren();
+        assertTrue(it.hasNext());
+        OMNode child = (OMNode)it.next();
+        assertTrue(child instanceof OMComment);
+        assertEquals("comment1", ((OMComment)child).getValue());
+        assertTrue(it.hasNext());
+        assertSame(documentElement, it.next());
+        assertTrue(it.hasNext());
+        child = (OMNode)it.next();
+        assertTrue(child instanceof OMComment);
+        assertEquals("comment2", ((OMComment)child).getValue());
+        assertFalse(it.hasNext());
+    }
+}

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