Created a TestData test support class for managing test output files.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 30 Mar 2008 11:20:29 +0000 (11:20 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 30 Mar 2008 11:20:29 +0000 (11:20 +0000)
trunk/support/src/test/java/org/wamblee/io/FileSystemUtils.java
trunk/support/src/test/java/org/wamblee/io/TestData.java [new file with mode: 0644]
trunk/support/src/test/java/org/wamblee/test/TestSupport.java [deleted file]

index 3e36219d453d80d3a2125ee4d562356cb624693d..3d0f3b3b164cb8744355b4b905791230d3c70355 100644 (file)
 package org.wamblee.io;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLDecoder;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
 import java.security.CodeSource;
 
+import junit.framework.Assert;
 import junit.framework.TestCase;
 
 import org.apache.commons.logging.Log;
@@ -224,4 +230,120 @@ public final class FileSystemUtils {
             aIs.close();
         }
     }
+    
+    /**
+        * Copies an input stream to an output stream. 
+        * @param aIs Input stream. 
+        * @param aOs Output stream. 
+        */
+       public static void copyStream(InputStream aIs, OutputStream aOs) {
+               try {
+                       int c;
+                       while ((c = aIs.read()) != -1) {
+                               aOs.write(c);
+                       }
+                       aIs.close();
+                       aOs.close(); 
+               } catch (IOException e) {
+                       e.printStackTrace();
+                       Assert.fail(e.getMessage());
+               }
+       }
+
+       /**
+        * Recursively copy a directory.
+        * 
+        * @param aSrc
+        *            Source directory
+        * @param aTarget
+        *            Target directory.
+        */
+       public static void copyDir(File aSrc, File aTarget) {
+               Assert.assertTrue(aSrc.isDirectory());
+               Assert.assertTrue(!aTarget.exists());
+
+               aTarget.mkdirs();
+
+               File[] files = aSrc.listFiles();
+               for (int i = 0; i < files.length; i++) {
+                       File file = files[i];
+                       if (file.isDirectory()) {
+                               if (!file.getName().equals(".svn")) {
+                                       copyDir(new File(aSrc, file.getName()), new File(aTarget,
+                                                       file.getName()));
+                               }
+                       } else {
+                               copyFile(file, new File(aTarget, file.getName()));
+                       }
+               }
+       }
+
+       /**
+        * Copy a file. If copying fails then the testcase will fail.
+        * 
+        * @param aSrc
+        *            Source file.
+        * @param aTarget
+        *            Target file.
+        */
+       public static void copyFile(File aSrc, File aTarget) {
+
+               try {
+                       FileInputStream fis = new FileInputStream(aSrc);
+                       FileOutputStream fos = new FileOutputStream(aTarget);
+                       FileChannel fcin = fis.getChannel();
+                       FileChannel fcout = fos.getChannel();
+
+                       // map input file
+
+                       MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
+                                       fcin.size());
+
+                       // do the file copy
+                       fcout.write(mbb);
+
+                       // finish up
+
+                       fcin.close();
+                       fcout.close();
+                       fis.close();
+                       fos.close();
+               } catch (IOException e) {
+                       Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
+                                       + aTarget.getPath() + " failed.", false);
+               }
+       }
+       
+       /**
+        * Remove all files within a given directory including the directory itself.
+        * This only attempts to remove regular files and not directories within the
+        * directory. If the directory contains a nested directory, the deletion
+        * will fail. The test case will fail if this fails.
+        * 
+        * @param aDir
+        *            Directory to remove.
+        */
+       public static void deleteDir(File aDir) {
+               cleanDir(aDir);
+               delete(aDir);
+       }
+       
+       /**
+        * Remove all regular files within a given directory.
+        * 
+        * @param outputDirName
+        */
+       public static void cleanDir(File aDir) {
+               if (!aDir.exists()) {
+                       return; // nothing to do.
+               }
+               File[] entries = aDir.listFiles();
+               for (int i = 0; i < entries.length; i++) {
+                       File file = entries[i];
+                       if (file.isFile()) {
+                               Assert.assertTrue("Could not delete " + entries[i].getPath(),
+                                               entries[i].delete());
+                       }
+               }
+       }
 }
diff --git a/trunk/support/src/test/java/org/wamblee/io/TestData.java b/trunk/support/src/test/java/org/wamblee/io/TestData.java
new file mode 100644 (file)
index 0000000..cc815c1
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * 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.
+ */
+package org.wamblee.io;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+
+import junit.framework.Assert;
+
+/**
+ * TestData provides a convenient interface for managing test output files.  
+ * 
+ * @author Erik Brakkee
+ */
+public final class TestData {
+
+       private Object _testcase;
+       private File _root;
+
+       /**
+        * Test data to be constructed in the setUp of a test.
+        * {@link #clean()} must be called to make sure that this 
+        * directory is empty before executing a test. 
+        */
+       public TestData(Object aTestcase) {
+               _testcase = aTestcase;
+               _root = getTestRootDir(aTestcase);
+       }
+
+       /**
+        * Obtain root directory of JUnit tests.
+        * 
+        * @return Directory name.
+        */
+       private static File getTestRootDir(Object aTestcase) {
+               return FileSystemUtils.getTestOutputDir(aTestcase.getClass());
+       }
+
+       /**
+        * Returns a temporary directory.
+        * 
+        * @return Temporary directory.
+        */
+       public File getTmpDir() {
+               return new File(_root, "tmpdir");
+       }
+
+       /**
+        * Cleans up the test output directory.
+        */
+       public void clean() {
+               FileSystemUtils.deleteDirRecursively(_root);
+       }
+
+       /**
+        * Recursively copy a directory contents to the test output directory.
+        * 
+        * @param sSrc
+        *            Source directory to copy.
+        */
+       public void copyDir(File aSrc) {
+               FileSystemUtils.copyDir(aSrc, _root);
+       }
+       
+       /**
+        * Copies a classpath resource to a relative path in the test output
+        * directory.
+        * 
+        * @param aResource
+        *            Resource to copy.
+        * @param aRelativePath
+        *            Relative path.
+        */
+       public void copyResource(String aResource, String aRelativePath) {
+               try {
+                       InputStream is = new ClassPathResource(aResource).getInputStream();
+                       FileOutputStream fos = new FileOutputStream(new File(_root,
+                                       aRelativePath));
+                       FileSystemUtils.copyStream(is, fos);
+               } catch (IOException e) {
+                       e.printStackTrace();
+                       Assert.fail(e.getMessage());
+               }
+       }
+
+       /**
+        * Copies a resource to the root directory of the test output.
+        * @param aResource Resource.
+        */
+       public void copyResource(String aResource) {
+               String basename = new File(aResource).getName();
+               copyResource(aResource, basename);
+       }
+
+       /**
+        * Deletes a file or directory relative to the test output root.
+        * 
+        * @param aRelative
+        *            Relative path. The testcase will fail if the file or directory
+        *            cannot be removed.
+        */
+       public void delete(String aRelative) {
+               FileSystemUtils.delete(new File(_root, aRelative));
+       }
+
+       /**
+        * Deletes a directory including its contents. 
+        * @param aRelative Relative path. 
+        */
+       public void deleteDir(String aRelative) { 
+               FileSystemUtils.deleteDir(new File(_root, aRelative));
+       }
+
+       /**
+        * Deletes a directory recursively. 
+        * @param aRelative Relative path. 
+        */
+       public void deleteDirRecursively(String aRelative) { 
+               FileSystemUtils.deleteDir(new File(_root, aRelative));
+       }
+       
+       /**
+        * Gets a file object for a relative path. 
+        */
+       public File getFile(String aRelative) { 
+               return new File(_root, aRelative); 
+       }
+}
diff --git a/trunk/support/src/test/java/org/wamblee/test/TestSupport.java b/trunk/support/src/test/java/org/wamblee/test/TestSupport.java
deleted file mode 100644 (file)
index 4ab9afe..0000000
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Copyright 2005 the original author or authors.
- * 
- * 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.
- */
-package org.wamblee.test;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-
-import junit.framework.Assert;
-
-/**
- * @author Erik Test support utility.
- *
- * @author Erik Brakkee
- */
-public final class TestSupport {
-    
-    /**
-     * Disabled constructor.
-     *
-     */
-    private TestSupport() { 
-        // Empty
-    }
-
-    /**
-     * Obtain root directory of JUnit tests.
-     * 
-     * @return Directory name.
-     */
-    public static File getTestRootDir() {
-        return new File("testdata");
-    }
-
-    /**
-     * Returns a temporary directory.
-     * 
-     * @return Temporary directory.
-     */
-    public static File getTmpDir() {
-        return new File(getTestRootDir(), "tmpdir");
-    }
-
-    /**
-     * Recursively remove a directory.
-     * 
-     * @param aSrc
-     *            Directoryto remove.
-     */
-    public static void removeDir(File aSrc) {
-        if (!aSrc.exists()) {
-            return;
-        }
-        Assert.assertTrue(aSrc.getPath(), aSrc.isDirectory());
-        File[] files = aSrc.listFiles();
-        for (int i = 0; i < files.length; i++) {
-            File file = files[i];
-            if (file.isDirectory()) {
-                removeDir(file);
-            } else {
-                Assert.assertTrue(file.getPath(), file.delete());
-            }
-        }
-        Assert.assertTrue(aSrc.getPath(), aSrc.delete());
-    }
-
-    /**
-     * Recursively copy a directory.
-     * 
-     * @param aSrc
-     *            Source directory
-     * @param aTarget
-     *            Target directory.
-     */
-    public static void copyDir(File aSrc, File aTarget) {
-        Assert.assertTrue(aSrc.isDirectory());
-        Assert.assertTrue(!aTarget.exists());
-
-        aTarget.mkdirs();
-
-        File[] files = aSrc.listFiles();
-        for (int i = 0; i < files.length; i++) {
-            File file = files[i];
-            if (file.isDirectory()) {
-                if (!file.getName().equals(".svn")) {
-                    copyDir(new File(aSrc, file.getName()), new File(aTarget,
-                            file.getName()));
-                }
-            } else {
-                copyFile(file, new File(aTarget, file.getName()));
-            }
-        }
-    }
-
-    /**
-     * Copy a file. If copying fails then the testcase will fail.
-     * 
-     * @param aSrc
-     *            Source file.
-     * @param aTarget
-     *            Target file.
-     */
-    public static void copyFile(File aSrc, File aTarget) {
-
-        try {
-            FileInputStream fis = new FileInputStream(aSrc);
-            FileOutputStream fos = new FileOutputStream(aTarget);
-            FileChannel fcin = fis.getChannel();
-            FileChannel fcout = fos.getChannel();
-
-            // map input file
-
-            MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
-                    fcin.size());
-
-            // do the file copy
-            fcout.write(mbb);
-
-            // finish up
-
-            fcin.close();
-            fcout.close();
-            fis.close();
-            fos.close();
-        } catch (IOException e) {
-            Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
-                    + aTarget.getPath() + " failed.", false);
-        }
-    }
-
-    /**
-     * Remove a file or directory. The test case will fail if this does not
-     * succeed.
-     * 
-     * @param aFile
-     *            entry to remove.
-     */
-    public static void delete(File aFile) {
-        Assert
-                .assertTrue("Could not delete " + aFile.getPath(), aFile
-                        .delete());
-    }
-
-    /**
-     * Remove all files within a given directory including the directory itself.
-     * This only attempts to remove regular files and not directories within the
-     * directory. If the directory contains a nested directory, the deletion
-     * will fail. The test case will fail if this fails.
-     * 
-     * @param aDir
-     *            Directory to remove.
-     */
-    public static void deleteDir(File aDir) {
-        cleanDir(aDir);
-        delete(aDir);
-    }
-
-    /**
-     * Remove all regular files within a given directory.
-     * 
-     * @param outputDirName
-     */
-    public static void cleanDir(File aDir) {
-        if (!aDir.exists()) {
-            return; // nothing to do.
-        }
-        File[] entries = aDir.listFiles();
-        for (int i = 0; i < entries.length; i++) {
-            File file = entries[i];
-            if (file.isFile()) {
-                Assert.assertTrue("Could not delete " + entries[i].getPath(),
-                        entries[i].delete());
-            }
-        }
-    }
-
-    /**
-     * Creates directory if it does not already exist. The test case will fail
-     * if the directory cannot be created.
-     * 
-     * @param aDir
-     *            Directory to create.
-     */
-    public static void createDir(File aDir) {
-        if (aDir.isDirectory()) {
-            return; // nothing to do.
-        }
-        Assert.assertTrue("Could not create directory " + aDir.getPath(), aDir
-                .mkdirs());
-    }
-}