You are viewing a plain text version of this content. The canonical link for it is here.
Posted to yoko-commits@incubator.apache.org by br...@apache.org on 2007/03/07 14:46:31 UTC

svn commit: r515589 - /incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/processors/IDLToWSDLGenerationTest.java

Author: bravi
Date: Wed Mar  7 06:46:30 2007
New Revision: 515589

URL: http://svn.apache.org/viewvc?view=rev&rev=515589
Log:
[YOKO-300] - Fixed the IDLToWSDL tests to compare XML instead of strings.

Modified:
    incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/processors/IDLToWSDLGenerationTest.java

Modified: incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/processors/IDLToWSDLGenerationTest.java
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/processors/IDLToWSDLGenerationTest.java?view=diff&rev=515589&r1=515588&r2=515589
==============================================================================
--- incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/processors/IDLToWSDLGenerationTest.java (original)
+++ incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/processors/IDLToWSDLGenerationTest.java Wed Mar  7 06:46:30 2007
@@ -19,6 +19,7 @@
 
 package org.apache.yoko.tools.processors;
 
+import java.io.ByteArrayInputStream;
 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -26,6 +27,10 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+
 import junit.framework.TestCase;
 
 import org.apache.yoko.tools.common.ProcessorEnvironment;
@@ -34,14 +39,14 @@
 
 public class IDLToWSDLGenerationTest extends TestCase {
 
-    public static final  String START_COMMENT = "<!--";
-    public static final  String END_COMMENT = "-->";
+    public XMLInputFactory factory;
 
     public IDLToWSDLGenerationTest(String name) {
         super(name);
     }
     
     protected void setUp() {
+        factory = XMLInputFactory.newInstance();
     }
 
     protected void tearDown() {
@@ -65,66 +70,79 @@
         processor.setOutputWriter(out);
         processor.process();
 
-        InputStream origstream = getClass().getResourceAsStream(expectedWsdlFilename);
-        byte orig[] = inputStreamToBytes(origstream);
-        checkWSDLStrings(orig, out.toString().getBytes());
-    }
-
-    private void checkWSDLStrings(byte orig[], byte generated[]) throws Exception {
-        BufferedReader origReader = 
-            new BufferedReader(new InputStreamReader(new java.io.ByteArrayInputStream(orig)));
-        BufferedReader genReader = 
-            new BufferedReader(new InputStreamReader(new java.io.ByteArrayInputStream(generated)));
-
-        String sorig = origReader.readLine();
-        String sgen = genReader.readLine();
-
-        boolean origComment = false;
-        boolean genComment = false;
-        while (sorig != null && sgen != null) {
-            if (sorig.trim().startsWith(START_COMMENT)) {
-                origComment = true;
-            }
-            if (sgen.trim().startsWith(START_COMMENT)) {
-                genComment = true;
-            }
-            if ((!origComment) && (!genComment)) {
-                assertEquals(sorig, sgen);
-                sgen = genReader.readLine();
-                sorig = origReader.readLine();
-            }
-            if (sorig != null && sgen != null) {
-                if (sorig.trim().endsWith(END_COMMENT)) {
-                    origComment = false;
-                    sorig = origReader.readLine();
-                }
-                if (sgen.trim().endsWith(END_COMMENT)) {
-                    genComment = false;
-                    sgen = genReader.readLine();
+        InputStream origStream = getClass().getResourceAsStream(expectedWsdlFilename);  
+        InputStream actualStream = new ByteArrayInputStream(out.toString().getBytes());
+
+        XMLStreamReader orig = factory.createXMLStreamReader(origStream);
+        XMLStreamReader actual = factory.createXMLStreamReader(actualStream);
+
+        compare(orig, actual);
+
+    }
+
+    private void compare(XMLStreamReader orig, XMLStreamReader actual)
+        throws Exception {
+
+        boolean origEnd = false;
+        boolean actualEnd = false;
+        while (orig.hasNext() || actual.hasNext()) {
+            int origTag = orig.next();
+            while (!orig.isStartElement() && !orig.isEndElement() && !orig.isCharacters()) {
+                if (orig.hasNext()) {
+                    origTag = orig.next();
+                } else {
+                    origEnd = true;
+                    break;
                 }
-                if (genComment) {
-                    sgen = genReader.readLine();
+            }
+            int actualTag = actual.next();
+            while (!actual.isStartElement() && !actual.isEndElement() && !actual.isCharacters()) {
+                if (actual.hasNext()) {
+                    actualTag = actual.next();
+                } else {
+                    actualEnd = true;
+                    break;
                 }
-                if (origComment) {
-                    sorig = origReader.readLine();
+            }
+            if (!origEnd && !actualEnd) {
+                assertEquals("XML mismatch", origTag, actualTag);
+                if (orig.isStartElement()) {
+                    compareStartElement(orig, actual);
+                } else if (orig.isEndElement()) {
+                    compareEndElement(orig, actual);
+                } else if (orig.isCharacters()) {
+                    compareCharacters(orig, actual);
                 }
+            } else {
+                break;
             }
         }
     }
 
-    public byte[] inputStreamToBytes(InputStream in) throws Exception {
-        java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(1024);
-        byte[] buffer = new byte[1024];
-        int len;
-
-        while ((len = in.read(buffer)) >= 0) {
-            out.write(buffer, 0, len);
+    private void compareStartElement(XMLStreamReader orig, XMLStreamReader actual)
+        throws Exception {
+        assertEquals("Start element is not matched", orig.getName(), actual.getName());
+        assertEquals("Attribute count is not matched for element " + orig.getName(),
+                     orig.getAttributeCount(),
+                     actual.getAttributeCount());
+        int count = orig.getAttributeCount();
+        for (int i = 0; i < count; i++) {
+            QName attrName = orig.getAttributeName(i);
+            assertEquals("Attribute " + attrName + " not found or value not matching",
+                         orig.getAttributeValue(attrName.getNamespaceURI(), attrName.getLocalPart()),
+                         actual.getAttributeValue(attrName.getNamespaceURI(), attrName.getLocalPart()));
         }
-
-        in.close();
-        out.close();
-        return out.toByteArray();
-    } 
+    }
+    
+   private void compareEndElement(XMLStreamReader orig, XMLStreamReader actual)
+       throws Exception {
+        assertEquals("End element is not matched", orig.getName(), actual.getName());
+   }
+                                 
+    private void compareCharacters(XMLStreamReader orig, XMLStreamReader actual)
+        throws Exception {
+        assertEquals("Element Characters not matched", orig.getText(), actual.getText());
+    }                                                                       
 
     public void testHelloWorldWSDLGeneration() throws Exception {
         testWSDLGeneration("/idl/HelloWorld.idl", "/idl/expected_HelloWorld.wsdl");