You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2009/02/04 20:50:18 UTC

svn commit: r740841 - in /webservices/commons/trunk/modules/axiom/modules/axiom-tests: ./ src/test/java/org/apache/axiom/attachments/ src/test/java/org/apache/axiom/om/

Author: veithen
Date: Wed Feb  4 19:50:18 2009
New Revision: 740841

URL: http://svn.apache.org/viewvc?rev=740841&view=rev
Log:
WSCOMMONS-419: Load test files as classpath resources instead of accessing them directly. This will allow us to reuse test files across multiple Maven modules.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/pom.xml
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/AbstractTestCase.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/CompareOMWithDOMTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/pom.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/pom.xml?rev=740841&r1=740840&r2=740841&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/pom.xml (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/pom.xml Wed Feb  4 19:50:18 2009
@@ -117,6 +117,11 @@
         </dependency>
     </dependencies>
     <build>
+        <testResources>
+            <testResource>
+                <directory>test-resources</directory>
+            </testResource>
+        </testResources>
         <plugins>
             <plugin>
                 <artifactId>maven-surefire-plugin</artifactId>

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java?rev=740841&r1=740840&r2=740841&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java Wed Feb  4 19:50:18 2009
@@ -19,6 +19,7 @@
 
 package org.apache.axiom.attachments;
 
+import org.apache.axiom.attachments.utils.IOUtils;
 import org.apache.axiom.om.AbstractTestCase;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMOutputFormat;
@@ -30,8 +31,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
@@ -296,8 +295,7 @@
 
     public void testGetAllContentIDs() throws Exception {
 
-        File f = getTestResourceFile(inMimeFileName);
-        InputStream inStream = new FileInputStream(f);
+        InputStream inStream = getTestResource(inMimeFileName);
         Attachments attachments = new Attachments(inStream, contentTypeString);
 
         String[] contentIDs = attachments.getAllContentIDs();
@@ -313,7 +311,7 @@
         
         // Make sure the length is correct
         long length = attachments.getContentLength();
-        long fileSize = f.length();
+        long fileSize = IOUtils.getStreamAsByteArray(getTestResource(inMimeFileName)).length;
         assertTrue("Expected MessageContent Length of " + fileSize + " but received " + length,
                    length == fileSize);
     }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/AbstractTestCase.java?rev=740841&r1=740840&r2=740841&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/AbstractTestCase.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/AbstractTestCase.java Wed Feb  4 19:50:18 2009
@@ -20,28 +20,35 @@
 package org.apache.axiom.om;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.InputStream;
+import java.net.URL;
 
 import javax.activation.DataSource;
-import javax.activation.FileDataSource;
+import javax.activation.URLDataSource;
 
 import org.custommonkey.xmlunit.XMLTestCase;
 
 /** Abstract base class for test cases. */
 public abstract class AbstractTestCase
         extends XMLTestCase {
-    protected String testDir = "test" + File.separator;
-    protected String sampleDir = "samples" + File.separator;
-    protected String outDir = "target" + File.separator + "generated" +
-            File.separator +
-            "samples" +
-            File.separator;
     protected String tempDir = "target" + File.separator + "generated" +
             File.separator +
             "temp";
-    protected String testResourceDir = "test-resources";
+    
+    public static final String[] soapFiles = {
+        "emtyBodymessage.xml",
+        "invalidMustUnderstandSOAP12.xml",
+        "minimalMessage.xml",
+        "OMElementTest.xml",
+        "reallyReallyBigMessage.xml",
+        "sample1.xml",
+        "security2-soap.xml",
+        "soap12message.xml",
+        "soap12RoleMessage.xml",
+        "soapmessage.xml",
+        "soapmessage1.xml",
+        "whitespacedMessage.xml"
+    };
 
     /** Basedir for all file I/O. Important when running tests from the reactor. */
     public String basedir = System.getProperty("basedir");
@@ -56,27 +63,23 @@
         if (basedir == null) {
             basedir = new File(".").getAbsolutePath();
         }
-        testDir = new File(basedir, testDir).getAbsolutePath();
-        sampleDir = new File(basedir, sampleDir).getAbsolutePath();
-        outDir = new File(basedir, outDir).getAbsolutePath();
         tempDir = new File(basedir, tempDir).getAbsolutePath();
     }
 
-    public File getTestResourceFile(String relativePath) {
-        return new File(testResourceDir, relativePath);
-    }
-    
     public DataSource getTestResourceDataSource(String relativePath) {
-        return new FileDataSource(getTestResourceFile(relativePath));
+        URL url = AbstractTestCase.class.getClassLoader().getResource(relativePath);
+        if (url == null) {
+            fail("The test resource " + relativePath + " could not be found");
+        }
+        return new URLDataSource(url);
     }
 
     public InputStream getTestResource(String relativePath) {
-        try {
-            return new FileInputStream(getTestResourceFile(relativePath));
-        } catch (FileNotFoundException ex) {
+        InputStream in = AbstractTestCase.class.getClassLoader().getResourceAsStream(relativePath);
+        if (in == null) {
             fail("The test resource " + relativePath + " could not be found");
-            return null;
         }
+        return in;
     }
 
     public File getTempOutputFile(String filename) {

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/CompareOMWithDOMTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/CompareOMWithDOMTest.java?rev=740841&r1=740840&r2=740841&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/CompareOMWithDOMTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/CompareOMWithDOMTest.java Wed Feb  4 19:50:18 2009
@@ -24,7 +24,6 @@
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import java.io.File;
 
 /** @version $Rev: $ $Date: $ */
 public class CompareOMWithDOMTest extends AbstractTestCase {
@@ -34,24 +33,16 @@
     }
 
     public void testAllMessagesInSOAP() throws OMException, Exception {
-        File dir = new File(testResourceDir, "soap");
-        File[] files = dir.listFiles();
-        if (files != null) {
-            for (int i = 0; i < files.length; i++) {
-                if (files[i].isFile() && files[i].getName().endsWith(".xml") &&
-                        !files[i].getName().startsWith("wrong")) {
-                    SOAPEnvelope soapEnvelope = (SOAPEnvelope) OMTestUtils.getOMBuilder(
-                            files[i])
-                            .getDocumentElement();
-                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-                    dbf.setNamespaceAware(true);
-                    DocumentBuilder builder = dbf.newDocumentBuilder();
-                    Document doc = builder.parse(files[i].getAbsolutePath());
-                    OMTestUtils.compare(doc.getDocumentElement(),
-                                        soapEnvelope);
-                }
-            }
-
+        for (int i = 0; i < soapFiles.length; i++) {
+            String file = "soap/" + soapFiles[i];
+            SOAPEnvelope soapEnvelope = (SOAPEnvelope) OMTestUtils.getOMBuilder(
+                    getTestResource(file)).getDocumentElement();
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            dbf.setNamespaceAware(true);
+            DocumentBuilder builder = dbf.newDocumentBuilder();
+            Document doc = builder.parse(getTestResource(file));
+            OMTestUtils.compare(doc.getDocumentElement(),
+                                soapEnvelope);
         }
     }
 }