(no commit message)
[utils] / support / test / org / wamblee / test / TestSupport.java
diff --git a/support/test/org/wamblee/test/TestSupport.java b/support/test/org/wamblee/test/TestSupport.java
new file mode 100644 (file)
index 0000000..4cb23e3
--- /dev/null
@@ -0,0 +1,195 @@
+/*
+ * 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.
+ */
+public class TestSupport {
+
+       /**
+        * 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());
+       }
+}