Author: veithen Date: Sun Jan 3 14:21:40 2010 New Revision: 895397 URL: http://svn.apache.org/viewvc?rev=895397&view=rev Log: WSCOMMONS-513: Clarified the expected behavior of OMNode#insertSibling(Before|After), changed the behavior of these methods in DOOM (to match the behavior of LLOM) and added a couple of unit tests to enforce the behavior.
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNode.java webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMNodeTestBase.java webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ChildNode.java Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNode.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNode.java?rev=895397&r1=895396&r2=895397&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNode.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNode.java Sun Jan 3 14:21:40 2010 @@ -140,18 +140,26 @@ void discard() throws OMException; /** - * Inserts a new sibling after the current node. - * - * @param sibling The node that will be added after the current node. + * Inserts a new sibling after the current node. The current node must have a parent for this + * operation to succeed. If the node to be inserted has a parent, then it will first be + * detached. + * + * @param sibling + * The node that will be added after the current node. * @throws OMException + * if the current node has no parent */ void insertSiblingAfter(OMNode sibling) throws OMException; /** - * Inserts a sibling just before the current node. - * - * @param sibling The node that will be added before the current node. + * Inserts a sibling just before the current node. The current node must have a parent for this + * operation to succeed. If the node to be inserted has a parent, then it will first be + * detached. + * + * @param sibling + * The node that will be added before the current node. * @throws OMException + * if the current node has no parent */ void insertSiblingBefore(OMNode sibling) throws OMException; Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMNodeTestBase.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMNodeTestBase.java?rev=895397&r1=895396&r2=895397&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMNodeTestBase.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMNodeTestBase.java Sun Jan 3 14:21:40 2010 @@ -28,6 +28,27 @@ this.omMetaFactory = omMetaFactory; } + public void testInsertSiblingAfter() { + OMFactory fac = omMetaFactory.getOMFactory(); + OMElement parent = fac.createOMElement("test", null); + OMText text1 = fac.createOMText("text1"); + OMText text2 = fac.createOMText("text2"); + parent.addChild(text1); + text1.insertSiblingAfter(text2); + assertSame(parent, text2.getParent()); + } + + public void testInsertSiblingBefore() { + OMFactory fac = omMetaFactory.getOMFactory(); + OMElement parent = fac.createOMElement("test", null); + OMText text1 = fac.createOMText("text1"); + OMText text2 = fac.createOMText("text2"); + parent.addChild(text1); + text1.insertSiblingBefore(text2); + assertSame(parent, text2.getParent()); + assertSame(text2, parent.getFirstOMChild()); + } + // Regression test for WSCOMMONS-337 public void testInsertSiblingAfterLastChild() throws Exception { OMFactory fac = omMetaFactory.getOMFactory(); @@ -49,6 +70,30 @@ "<ns:c1 /><ns:c2 /><ns:c3 /></ns:parent>", parent.toString()); } + public void testInsertSiblingAfterOnOrphan() { + OMFactory fac = omMetaFactory.getOMFactory(); + OMText text1 = fac.createOMText("text1"); + OMText text2 = fac.createOMText("text2"); + try { + text1.insertSiblingAfter(text2); + fail("Expected OMException because node has no parent"); + } catch (OMException ex) { + // Expected + } + } + + public void testInsertSiblingBeforeOnOrphan() { + OMFactory fac = omMetaFactory.getOMFactory(); + OMText text1 = fac.createOMText("text1"); + OMText text2 = fac.createOMText("text2"); + try { + text1.insertSiblingBefore(text2); + fail("Expected OMException because node has no parent"); + } catch (OMException ex) { + // Expected + } + } + private void testDetach(boolean build) throws Exception { OMElement root = AXIOMUtil.stringToOM(omMetaFactory.getOMFactory(), "<root><a/><b/><c/></root>"); if (build) { Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ChildNode.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ChildNode.java?rev=895397&r1=895396&r2=895397&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ChildNode.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ChildNode.java Sun Jan 3 14:21:40 2010 @@ -149,13 +149,12 @@ /** Inserts the given sibling next to this item. */ public void insertSiblingAfter(OMNode sibling) throws OMException { - - if (this.parentNode != null) { - ((OMNodeEx) sibling).setParent(this.parentNode); + if (this.parentNode == null) { + throw new OMException("Parent can not be null"); } else if (this == sibling) { throw new OMException("Inserting self as the sibling is not allowed"); } - + ((OMNodeEx) sibling).setParent(this.parentNode); if (sibling instanceof ChildNode) { ChildNode domSibling = (ChildNode) sibling; domSibling.previousSibling = this; @@ -176,7 +175,9 @@ /** Inserts the given sibling before this item. */ public void insertSiblingBefore(OMNode sibling) throws OMException { // ((OMNodeEx)sibling).setParent(this.parentNode); - if (this == sibling) { + if (this.parentNode == null) { + throw new OMException("Parent can not be null"); + } else if (this == sibling) { throw new OMException("Inserting self as the sibling is not allowed"); } if (sibling instanceof ChildNode) {