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;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.nio.MappedByteBuffer;
25 import java.nio.channels.FileChannel;
27 import junit.framework.Assert;
30 * TestData provides a convenient interface for managing test output files.
32 * @author Erik Brakkee
34 public final class TestData {
36 private Object _testcase;
40 * Test data to be constructed in the setUp of a test.
41 * {@link #clean()} must be called to make sure that this
42 * directory is empty before executing a test.
44 public TestData(Object aTestcase) {
45 _testcase = aTestcase;
46 _root = getTestRootDir(aTestcase);
50 * Obtain root directory of JUnit tests.
52 * @return Directory name.
54 private static File getTestRootDir(Object aTestcase) {
55 return FileSystemUtils.getTestOutputDir(aTestcase.getClass());
59 * Returns a temporary directory.
61 * @return Temporary directory.
63 public File getTmpDir() {
64 return new File(_root, "tmpdir");
68 * Cleans up the test output directory.
71 FileSystemUtils.deleteDirRecursively(_root);
75 * Recursively copy a directory contents to the test output directory.
78 * Source directory to copy.
80 public void copyDir(File aSrc) {
81 FileSystemUtils.copyDir(aSrc, _root);
85 * Copies a classpath resource to a relative path in the test output
90 * @param aRelativePath
93 public void copyResource(String aResource, String aRelativePath) {
95 InputStream is = new ClassPathResource(aResource).getInputStream();
96 FileOutputStream fos = new FileOutputStream(new File(_root,
98 FileSystemUtils.copyStream(is, fos);
99 } catch (IOException e) {
101 Assert.fail(e.getMessage());
106 * Copies a resource to the root directory of the test output.
107 * @param aResource Resource.
109 public void copyResource(String aResource) {
110 String basename = new File(aResource).getName();
111 copyResource(aResource, basename);
115 * Deletes a file or directory relative to the test output root.
118 * Relative path. The testcase will fail if the file or directory
121 public void delete(String aRelative) {
122 FileSystemUtils.delete(new File(_root, aRelative));
126 * Deletes a directory including its contents.
127 * @param aRelative Relative path.
129 public void deleteDir(String aRelative) {
130 FileSystemUtils.deleteDir(new File(_root, aRelative));
134 * Deletes a directory recursively.
135 * @param aRelative Relative path.
137 public void deleteDirRecursively(String aRelative) {
138 FileSystemUtils.deleteDir(new File(_root, aRelative));
142 * Gets a file object for a relative path.
144 public File getFile(String aRelative) {
145 return new File(_root, aRelative);