added test cases for directorymonitor.
[utils] / support / src / test / java / org / wamblee / io / TestData.java
index cc815c1bbe4b33083788b941e717f2fbc4ae8506..58bea68ec119becf74222451cf497c40ba3b8f06 100644 (file)
@@ -15,6 +15,7 @@
  */
 package org.wamblee.io;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -27,7 +28,7 @@ import java.nio.channels.FileChannel;
 import junit.framework.Assert;
 
 /**
- * TestData provides a convenient interface for managing test output files.  
+ * TestData provides a convenient interface for managing test output files.
  * 
  * @author Erik Brakkee
  */
@@ -37,13 +38,14 @@ public final class TestData {
        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. 
+        * 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);
+               FileSystemUtils.createDir(_root);
        }
 
        /**
@@ -55,6 +57,28 @@ public final class TestData {
                return FileSystemUtils.getTestOutputDir(aTestcase.getClass());
        }
 
+       public void createFile(String aRelative, String aFile, InputStream aContents) {
+               FileSystemUtils
+                               .createFile(new File(_root, aRelative), aFile, aContents);
+       }
+
+       public void createFile(String aFile, String aContents) {
+               createFile(".", aFile, aContents);
+       }
+
+       public void createFile(String aRelative, String aFile, String aContents) {
+               InputStream is = new ByteArrayInputStream(aContents.getBytes());
+               FileSystemUtils.createFile(new File(_root, aRelative), aFile, is);
+       }
+       
+       public void deleteFile(String aFile) {
+               deleteFile(".", aFile);
+       }
+
+       public void deleteFile(String aRelative, String aFile) {
+               FileSystemUtils.delete(new File(_root, aFile));
+       }
+
        /**
         * Returns a temporary directory.
         * 
@@ -69,6 +93,7 @@ public final class TestData {
         */
        public void clean() {
                FileSystemUtils.deleteDirRecursively(_root);
+               FileSystemUtils.createDir(_root);
        }
 
        /**
@@ -80,7 +105,7 @@ public final class TestData {
        public void copyDir(File aSrc) {
                FileSystemUtils.copyDir(aSrc, _root);
        }
-       
+
        /**
         * Copies a classpath resource to a relative path in the test output
         * directory.
@@ -104,13 +129,19 @@ public final class TestData {
 
        /**
         * Copies a resource to the root directory of the test output.
-        * @param aResource Resource.
+        * 
+        * @param aResource
+        *            Resource.
         */
        public void copyResource(String aResource) {
                String basename = new File(aResource).getName();
                copyResource(aResource, basename);
        }
 
+       public void createDir(String aRelative) {
+               FileSystemUtils.createDir(new File(_root, aRelative));
+       }
+
        /**
         * Deletes a file or directory relative to the test output root.
         * 
@@ -123,25 +154,38 @@ public final class TestData {
        }
 
        /**
-        * Deletes a directory including its contents. 
-        * @param aRelative Relative path. 
+        * Deletes a directory including its contents.
+        * 
+        * @param aRelative
+        *            Relative path.
         */
-       public void deleteDir(String aRelative) { 
+       public void deleteDir(String aRelative) {
                FileSystemUtils.deleteDir(new File(_root, aRelative));
        }
 
        /**
-        * Deletes a directory recursively. 
-        * @param aRelative Relative path. 
+        * Deletes a directory recursively.
+        * 
+        * @param aRelative
+        *            Relative path.
         */
-       public void deleteDirRecursively(String aRelative) { 
+       public void deleteDirRecursively(String aRelative) {
                FileSystemUtils.deleteDir(new File(_root, aRelative));
        }
-       
+
+       /**
+        * Gets the root of the test output directory.
+        * 
+        * @return Root of the test output.
+        */
+       public File getRoot() {
+               return _root;
+       }
+
        /**
-        * Gets a file object for a relative path. 
+        * Gets a file object for a relative path.
         */
-       public File getFile(String aRelative) { 
-               return new File(_root, aRelative); 
+       public File getFile(String aRelative) {
+               return new File(_root, aRelative);
        }
 }