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 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 {
40 * Test data to be constructed in the setUp of a test. {@link #clean()} must
41 * be called to make sure that this directory is empty before executing a
44 public TestData(Object aTestcase) {
45 root = getTestRootDir(aTestcase);
46 FileSystemUtils.createDir(root);
50 * Obtain root directory of JUnit tests.
52 * @return Directory name.
54 private static File getTestRootDir(Object aTestcase) {
55 return FileSystemUtils.getTestOutputDir(aTestcase.getClass());
58 public void createFile(String aRelative, String aFile, InputStream aContents) {
59 FileSystemUtils.createFile(new File(root, aRelative), aFile, aContents);
62 public void createFile(String aFile, String aContents) {
63 createFile(".", aFile, aContents);
66 public void createFile(String aRelative, String aFile, String aContents) {
67 InputStream is = new ByteArrayInputStream(aContents.getBytes());
68 FileSystemUtils.createFile(new File(root, aRelative), aFile, is);
71 public void deleteFile(String aFile) {
72 deleteFile(".", aFile);
75 public void deleteFile(String aRelative, String aFile) {
76 FileSystemUtils.delete(new File(root, aFile));
80 * Returns a temporary directory.
82 * @return Temporary directory.
84 public File getTmpDir() {
85 return new File(root, "tmpdir");
89 * Cleans up the test output directory.
92 FileSystemUtils.deleteDirRecursively(root);
93 FileSystemUtils.createDir(root);
97 * Recursively copy a directory contents to the test output directory.
100 * Source directory to copy.
102 public void copyDir(File aSrc) {
103 FileSystemUtils.copyDir(aSrc, root);
107 * Copies a classpath resource to a relative path in the test output
112 * @param aRelativePath
115 public void copyResource(String aResource, String aRelativePath) {
117 InputStream is = new ClassPathResource(aResource).getInputStream();
118 FileOutputStream fos = new FileOutputStream(new File(root,
120 FileSystemUtils.copyStream(is, fos);
121 } catch (IOException e) {
123 Assert.fail(e.getMessage());
128 * Copies a resource to the root directory of the test output.
133 public void copyResource(String aResource) {
134 String basename = new File(aResource).getName();
135 copyResource(aResource, basename);
138 public void createDir(String aRelative) {
139 FileSystemUtils.createDir(new File(root, aRelative));
143 * Deletes a file or directory relative to the test output root.
146 * Relative path. The testcase will fail if the file or directory
149 public void delete(String aRelative) {
150 FileSystemUtils.delete(new File(root, aRelative));
154 * Deletes a directory including its contents.
159 public void deleteDir(String aRelative) {
160 FileSystemUtils.deleteDir(new File(root, aRelative));
164 * Deletes a directory recursively.
169 public void deleteDirRecursively(String aRelative) {
170 FileSystemUtils.deleteDir(new File(root, aRelative));
174 * Gets the root of the test output directory.
176 * @return Root of the test output.
178 public File getRoot() {
183 * Gets a file object for a relative path.
185 public File getFile(String aRelative) {
186 return new File(root, aRelative);