2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.io;
18 import java.io.ByteArrayInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import junit.framework.Assert;
27 * TestData provides a convenient interface for managing test output files.
29 * @author Erik Brakkee
31 public final class TestData {
35 * Test data to be constructed in the setUp of a test. {@link #clean()} must
36 * be called to make sure that this directory is empty before executing a
39 public TestData(Object aTestcase) {
40 root = getTestRootDir(aTestcase);
41 FileSystemUtils.createDir(root);
45 * Obtain root directory of JUnit tests.
47 * @return Directory name.
49 private static File getTestRootDir(Object aTestcase) {
50 return FileSystemUtils.getTestOutputDir(aTestcase.getClass());
53 public void createFile(String aRelative, String aFile, InputStream aContents) {
54 FileSystemUtils.createFile(new File(root, aRelative), aFile, aContents);
57 public void createFile(String aFile, String aContents) {
58 createFile(".", aFile, aContents);
61 public void createFile(String aRelative, String aFile, String aContents) {
62 InputStream is = new ByteArrayInputStream(aContents.getBytes());
63 FileSystemUtils.createFile(new File(root, aRelative), aFile, is);
66 public void deleteFile(String aFile) {
67 deleteFile(".", aFile);
70 public void deleteFile(String aRelative, String aFile) {
71 FileSystemUtils.delete(new File(root, aFile));
75 * Returns a temporary directory.
77 * @return Temporary directory.
79 public File getTmpDir() {
80 return new File(root, "tmpdir");
84 * Cleans up the test output directory.
87 FileSystemUtils.deleteDirRecursively(root);
88 FileSystemUtils.createDir(root);
92 * Recursively copy a directory contents to the test output directory.
95 * Source directory to copy.
97 public void copyDir(File aSrc) {
98 FileSystemUtils.copyDir(aSrc, root);
102 * Copies a classpath resource to a relative path in the test output
107 * @param aRelativePath
110 public void copyResource(String aResource, String aRelativePath) {
112 InputStream is = new ClassPathResource(aResource).getInputStream();
113 FileOutputStream fos = new FileOutputStream(new File(root,
115 FileSystemUtils.copyStream(is, fos);
116 } catch (IOException e) {
118 Assert.fail(e.getMessage());
123 * Copies a resource to the root directory of the test output.
128 public void copyResource(String aResource) {
129 String basename = new File(aResource).getName();
130 copyResource(aResource, basename);
133 public void createDir(String aRelative) {
134 FileSystemUtils.createDir(new File(root, aRelative));
138 * Deletes a file or directory relative to the test output root.
141 * Relative path. The testcase will fail if the file or directory
144 public void delete(String aRelative) {
145 FileSystemUtils.delete(new File(root, aRelative));
149 * Deletes a directory including its contents.
154 public void deleteDir(String aRelative) {
155 FileSystemUtils.deleteDir(new File(root, aRelative));
159 * Deletes a directory recursively.
164 public void deleteDirRecursively(String aRelative) {
165 FileSystemUtils.deleteDir(new File(root, aRelative));
169 * Gets the root of the test output directory.
171 * @return Root of the test output.
173 public File getRoot() {
178 * Gets a file object for a relative path.
180 public File getFile(String aRelative) {
181 return new File(root, aRelative);