2 * Copyright 2005 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 junit.framework.Assert;
20 import java.io.ByteArrayInputStream;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
28 import java.nio.MappedByteBuffer;
29 import java.nio.channels.FileChannel;
32 * TestData provides a convenient interface for managing test output files.
34 * @author Erik Brakkee
36 public final class TestData {
37 private Object testcase;
41 * Test data to be constructed in the setUp of a test. {@link #clean()} must
42 * be called to make sure that this directory is empty before executing a
45 public TestData(Object aTestcase) {
47 root = getTestRootDir(aTestcase);
48 FileSystemUtils.createDir(root);
52 * Obtain root directory of JUnit tests.
54 * @return Directory name.
56 private static File getTestRootDir(Object aTestcase) {
57 return FileSystemUtils.getTestOutputDir(aTestcase.getClass());
60 public void createFile(String aRelative, String aFile, InputStream aContents) {
61 FileSystemUtils.createFile(new File(root, aRelative), aFile, aContents);
64 public void createFile(String aFile, String aContents) {
65 createFile(".", aFile, aContents);
68 public void createFile(String aRelative, String aFile, String aContents) {
69 InputStream is = new ByteArrayInputStream(aContents.getBytes());
70 FileSystemUtils.createFile(new File(root, aRelative), aFile, is);
73 public void deleteFile(String aFile) {
74 deleteFile(".", aFile);
77 public void deleteFile(String aRelative, String aFile) {
78 FileSystemUtils.delete(new File(root, aFile));
82 * Returns a temporary directory.
84 * @return Temporary directory.
86 public File getTmpDir() {
87 return new File(root, "tmpdir");
91 * Cleans up the test output directory.
94 FileSystemUtils.deleteDirRecursively(root);
95 FileSystemUtils.createDir(root);
99 * Recursively copy a directory contents to the test output directory.
102 * Source directory to copy.
104 public void copyDir(File aSrc) {
105 FileSystemUtils.copyDir(aSrc, root);
109 * Copies a classpath resource to a relative path in the test output
114 * @param aRelativePath
117 public void copyResource(String aResource, String aRelativePath) {
119 InputStream is = new ClassPathResource(aResource).getInputStream();
120 FileOutputStream fos = new FileOutputStream(new File(root,
122 FileSystemUtils.copyStream(is, fos);
123 } catch (IOException e) {
125 Assert.fail(e.getMessage());
130 * Copies a resource to the root directory of the test output.
135 public void copyResource(String aResource) {
136 String basename = new File(aResource).getName();
137 copyResource(aResource, basename);
140 public void createDir(String aRelative) {
141 FileSystemUtils.createDir(new File(root, aRelative));
145 * Deletes a file or directory relative to the test output root.
148 * Relative path. The testcase will fail if the file or directory
151 public void delete(String aRelative) {
152 FileSystemUtils.delete(new File(root, aRelative));
156 * Deletes a directory including its contents.
161 public void deleteDir(String aRelative) {
162 FileSystemUtils.deleteDir(new File(root, aRelative));
166 * Deletes a directory recursively.
171 public void deleteDirRecursively(String aRelative) {
172 FileSystemUtils.deleteDir(new File(root, aRelative));
176 * Gets the root of the test output directory.
178 * @return Root of the test output.
180 public File getRoot() {
185 * Gets a file object for a relative path.
187 public File getFile(String aRelative) {
188 return new File(root, aRelative);