You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ch...@apache.org on 2005/10/21 05:39:52 UTC

svn commit: r327084 - /webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/TestClone.java

Author: chinthaka
Date: Thu Oct 20 20:39:46 2005
New Revision: 327084

URL: http://svn.apache.org/viewcvs?rev=327084&view=rev
Log:
Proving of concept. Cloning of an OMElement can be easily achieved by the concept tested in this test case. 
If we want to clone elementOne, get a pull parser (of course SHOULD be with caching) from elementOne. Create a StaxOMBuilder from that and get the document element. That is a clone of elementOne.

Added:
    webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/TestClone.java

Added: webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/TestClone.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/TestClone.java?rev=327084&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/TestClone.java (added)
+++ webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/TestClone.java Thu Oct 20 20:39:46 2005
@@ -0,0 +1,70 @@
+package org.apache.axis2.om;
+
+import junit.framework.TestCase;
+import org.apache.axis2.soap.SOAPEnvelope;
+import org.apache.axis2.soap.SOAPBody;
+import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.custommonkey.xmlunit.XMLTestCase;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import java.io.File;
+import java.io.IOException;
+import java.io.ByteArrayInputStream;
+
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ *
+ * @author : Eran Chinthaka (chinthaka@apache.org)
+ */
+
+public class TestClone extends XMLTestCase {
+
+    File dir = new File("test-resources", "soap");
+
+    public void testElementCloning() throws Exception {
+        SOAPEnvelope soapEnvelope =
+                (SOAPEnvelope) OMTestUtils.getOMBuilder(
+                        new File(dir, "soapmessage.xml"))
+                        .getDocumentElement();
+        SOAPBody body = soapEnvelope.getBody();
+
+        OMElement firstClonedBodyElement = new StAXOMBuilder(body.getXMLStreamReader()).getDocumentElement();
+        OMElement secondClonedBodyElement = new StAXOMBuilder(body.getXMLStreamReader()).getDocumentElement();
+
+        // first check whether both have the same information
+        assertXMLEqual(newDocument(body.toString()), newDocument(firstClonedBodyElement.toString()));
+        assertXMLEqual(newDocument(body.toString()), newDocument(secondClonedBodyElement.toString()));
+        assertXMLEqual(newDocument(firstClonedBodyElement.toString()), newDocument(secondClonedBodyElement.toString()));
+
+        // lets check some links. They must not be equal
+        assertNotSame(body.getParent(), firstClonedBodyElement.getParent());
+        assertNotSame(body.getParent(), secondClonedBodyElement.getParent());
+        assertNotSame(firstClonedBodyElement.getParent(), secondClonedBodyElement.getParent());
+
+    }
+
+    public Document newDocument(String xml)
+            throws ParserConfigurationException, SAXException, IOException {
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setNamespaceAware(true);
+        DocumentBuilder db = dbf.newDocumentBuilder();
+        return db.parse(new ByteArrayInputStream(xml.getBytes()));
+    }
+}