*/
package org.wamblee.io;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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
*/
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);
}
/**
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.
*
*/
public void clean() {
FileSystemUtils.deleteDirRecursively(_root);
+ FileSystemUtils.createDir(_root);
}
/**
public void copyDir(File aSrc) {
FileSystemUtils.copyDir(aSrc, _root);
}
-
+
/**
* Copies a classpath resource to a relative path in the test output
* directory.
/**
* 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.
*
}
/**
- * 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);
}
}