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/08/31 09:10:46 UTC

svn commit: r264955 - in /webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om: AbstractOMSerializationTest.java AttrNsTest.java

Author: chinthaka
Date: Wed Aug 31 00:10:21 2005
New Revision: 264955

URL: http://svn.apache.org/viewcvs?rev=264955&view=rev
Log:
commiting completed attr namespace test

Modified:
    webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AbstractOMSerializationTest.java
    webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AttrNsTest.java

Modified: webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AbstractOMSerializationTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AbstractOMSerializationTest.java?rev=264955&r1=264954&r2=264955&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AbstractOMSerializationTest.java (original)
+++ webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AbstractOMSerializationTest.java Wed Aug 31 00:10:21 2005
@@ -14,6 +14,9 @@
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.transform.*;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 import java.io.*;
 
 /*
@@ -44,14 +47,42 @@
      * @param xmlString - remember this is not the file path. this is the xml string
      */
     public Diff getDiffForComparison(String xmlString) throws Exception {
-        return getDiff(new ByteArrayInputStream(xmlString.getBytes()));
+        return getDiffForComparison(new ByteArrayInputStream(xmlString.getBytes()));
     }
 
     public Diff getDiffForComparison(File xmlFile) throws Exception {
-        return getDiff(new FileInputStream(xmlFile));
+        return getDiffForComparison(new FileInputStream(xmlFile));
     }
 
-    private Diff getDiff(InputStream inStream) throws Exception {
+    public String getSerializedOM(String xmlString) throws Exception {
+        try {
+            XMLInputFactory factory = XMLInputFactory.newInstance();
+//            factory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", Boolean.TRUE);
+
+            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlString.getBytes());
+            StAXOMBuilder staxOMBuilder = OMXMLBuilderFactory.
+                    createStAXOMBuilder(OMAbstractFactory.getOMFactory(),
+                            factory.createXMLStreamReader(byteArrayInputStream));
+            staxOMBuilder.setDoDebug(true);
+            OMElement rootElement = staxOMBuilder.getDocumentElement();
+
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+            OMOutputImpl omOutput = new OMOutputImpl(baos, false);
+            omOutput.ignoreXMLDeclaration(ignoreXMLDeclaration);
+
+//            rootElement.serializeWithCache(omOutput);
+            ((OMDocument) rootElement.getParent()).serializeWithCache(omOutput);
+            omOutput.flush();
+
+            return new String(baos.toByteArray());
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw e;
+        }
+    }
+
+    public Diff getDiffForComparison(InputStream inStream) throws Exception {
 
         try {
             XMLInputFactory factory = XMLInputFactory.newInstance();
@@ -66,7 +97,7 @@
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
             OMOutputImpl omOutput = new OMOutputImpl(baos, false);
-            omOutput.ignoreXMLDeclaration(ignoreXMLDeclaration);
+//            omOutput.ignoreXMLDeclaration(ignoreXMLDeclaration);
 
             if (ignoreDocument) {
                 rootElement.serializeWithCache(omOutput);
@@ -80,8 +111,8 @@
             InputSource resultXML = new InputSource(new InputStreamReader(
                     new ByteArrayInputStream(baos.toByteArray())));
 
-            Document dom1 = newDocument(inStream);
             Document dom2 = newDocument(resultXML);
+            Document dom1 = newDocument(inStream);
 
             return compareXML(dom1, dom2);
 //            assertXMLEqual(diff, true);
@@ -92,9 +123,11 @@
             fail(e.getMessage());
             throw new Exception(e);
         } catch (SAXException e) {
+            e.printStackTrace();
             fail(e.getMessage());
             throw new Exception(e);
         } catch (IOException e) {
+            e.printStackTrace();
             fail(e.getMessage());
             throw new Exception(e);
         }
@@ -116,5 +149,34 @@
         return db.parse(in);
     }
 
+    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()));
+    }
+
+    public String writeXmlFile(Document doc) {
+        try {
+            // Prepare the DOM document for writing
+            Source source = new DOMSource(doc);
+
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            Result result = new StreamResult(baos);
+
+            // Write the DOM document to the file
+            Transformer xformer = TransformerFactory.newInstance().newTransformer();
+            xformer.transform(source, result);
+            return new String(baos.toByteArray());
+        } catch (TransformerConfigurationException e) {
+            e.printStackTrace();
+        } catch (TransformerException e) {
+            e.printStackTrace();
+
+        }
+        return null;
+
+    }
 
 }

Modified: webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AttrNsTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AttrNsTest.java?rev=264955&r1=264954&r2=264955&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AttrNsTest.java (original)
+++ webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/AttrNsTest.java Wed Aug 31 00:10:21 2005
@@ -18,54 +18,28 @@
  * @author : Eran Chinthaka (chinthaka@apache.org)
  */
 
-import org.apache.axis2.om.impl.OMOutputImpl;
-import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
-
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLStreamWriter;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import org.custommonkey.xmlunit.Diff;
+import org.w3c.dom.Document;
 
 public class AttrNsTest extends AbstractOMSerializationTest {
 
-    String xml = "<foo xmlns:a=\"http://opensource.lk\">\n" +
-            "<bar1 b:attr=\"test attr value1\" xmlns:b=\"http://opensource.lk/ns1\">test1</bar1>\n" +
-            "<bar2 b:attr=\"test attr value2\" xmlns:b=\"http://opensource.lk/ns1\">test2</bar2>\n" +
+    private String attrNamespaceTestXML = "<?xml version='1.0' encoding='UTF-8'?>\n" +
+            "<foo xmlns:a=\"http://opensource.lk\">" +
+            "    <bar1 b:attr=\"test attr value1\" xmlns:b=\"http://opensource.lk/ns1\">test1</bar1>" +
+            "    <bar2 b:attr=\"test attr value2\" xmlns:b=\"http://opensource.lk/ns1\">test2</bar2>" +
             "</foo>";
 
     public void testAttributeNamespaces() throws Exception {
-//        ignoreXMLDeclaration = true;
-//        ignoreDocument = true;
-//        Diff diffForComparison = getDiffForComparison(xml);
-//        assertXMLEqual(diffForComparison, true);
-        assertTrue(true);
-    }
+        ignoreXMLDeclaration = true;
+        ignoreDocument = true;
+
+        Document document1 = newDocument(attrNamespaceTestXML);
+        String serializedOM = getSerializedOM(attrNamespaceTestXML);
+        Document document2 = newDocument(serializedOM);
 
-    public static void main(String[] args) {
-        //File f = new File("/home/ruchith/temp/attr.ns.1.xml");
-        String xml = "<foo xmlns:a=\"http://opensource.lk\">\n" +
-                "<bar1 b:attr=\"test attr value1\" xmlns:b=\"http://opensource.lk/ns1\">test1</bar1>\n" +
-                "<bar2 b:attr=\"test attr value2\" xmlns:b=\"http://opensource.lk/ns1\">test2</bar2>\n" +
-                "</foo>";
-        try {
-
-            ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
-
-            XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bais);
-            StAXOMBuilder builder = new StAXOMBuilder(reader);
-            OMElement elem = builder.getDocumentElement();
-            elem.build();
-
-            ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(baos);
-            OMOutputImpl output = new OMOutputImpl(writer);
-            elem.serialize(output);
-            output.flush();
-            System.out.println(new String(baos.toByteArray()));
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+        Diff diff = compareXML(document1, document2);
+        assertXMLEqual(diff, true);
     }
+
+
 }