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 java.io.ByteArrayInputStream;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.MappedByteBuffer;
26 import java.nio.channels.FileChannel;
28 import junit.framework.Assert;
31 * TestData provides a convenient interface for managing test output files.
33 * @author Erik Brakkee
35 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) {
46 _testcase = 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) {
62 .createFile(new File(_root, aRelative), aFile, aContents);
65 public void createFile(String aFile, String aContents) {
66 createFile(".", aFile, aContents);
69 public void createFile(String aRelative, String aFile, String aContents) {
70 InputStream is = new ByteArrayInputStream(aContents.getBytes());
71 FileSystemUtils.createFile(new File(_root, aRelative), aFile, is);
74 public void deleteFile(String aFile) {
75 deleteFile(".", aFile);
78 public void deleteFile(String aRelative, String aFile) {
79 FileSystemUtils.delete(new File(_root, aFile));
83 * Returns a temporary directory.
85 * @return Temporary directory.
87 public File getTmpDir() {
88 return new File(_root, "tmpdir");
92 * Cleans up the test output directory.
95 FileSystemUtils.deleteDirRecursively(_root);
96 FileSystemUtils.createDir(_root);
100 * Recursively copy a directory contents to the test output directory.
103 * Source directory to copy.
105 public void copyDir(File aSrc) {
106 FileSystemUtils.copyDir(aSrc, _root);
110 * Copies a classpath resource to a relative path in the test output
115 * @param aRelativePath
118 public void copyResource(String aResource, String aRelativePath) {
120 InputStream is = new ClassPathResource(aResource).getInputStream();
121 FileOutputStream fos = new FileOutputStream(new File(_root,
123 FileSystemUtils.copyStream(is, fos);
124 } catch (IOException e) {
126 Assert.fail(e.getMessage());
131 * Copies a resource to the root directory of the test output.
136 public void copyResource(String aResource) {
137 String basename = new File(aResource).getName();
138 copyResource(aResource, basename);
141 public void createDir(String aRelative) {
142 FileSystemUtils.createDir(new File(_root, aRelative));
146 * Deletes a file or directory relative to the test output root.
149 * Relative path. The testcase will fail if the file or directory
152 public void delete(String aRelative) {
153 FileSystemUtils.delete(new File(_root, aRelative));
157 * Deletes a directory including its contents.
162 public void deleteDir(String aRelative) {
163 FileSystemUtils.deleteDir(new File(_root, aRelative));
167 * Deletes a directory recursively.
172 public void deleteDirRecursively(String aRelative) {
173 FileSystemUtils.deleteDir(new File(_root, aRelative));
177 * Gets the root of the test output directory.
179 * @return Root of the test output.
181 public File getRoot() {
186 * Gets a file object for a relative path.
188 public File getFile(String aRelative) {
189 return new File(_root, aRelative);