(no commit message)
[utils] / support / general / src / test / java / org / wamblee / io / TestData.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.io;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import junit.framework.Assert;
25
26 /**
27  * TestData provides a convenient interface for managing test output files.
28  * 
29  * @author Erik Brakkee
30  */
31 public final class TestData {
32     private File root;
33
34     /**
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
37      * test.
38      */
39     public TestData(Object aTestcase) {
40         root = getTestRootDir(aTestcase);
41         FileSystemUtils.createDir(root);
42     }
43
44     /**
45      * Obtain root directory of JUnit tests.
46      * 
47      * @return Directory name.
48      */
49     private static File getTestRootDir(Object aTestcase) {
50         return FileSystemUtils.getTestOutputDir(aTestcase.getClass());
51     }
52
53     public void createFile(String aRelative, String aFile, InputStream aContents) {
54         FileSystemUtils.createFile(new File(root, aRelative), aFile, aContents);
55     }
56
57     public void createFile(String aFile, String aContents) {
58         createFile(".", aFile, aContents);
59     }
60
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);
64     }
65
66     public void deleteFile(String aFile) {
67         deleteFile(".", aFile);
68     }
69
70     public void deleteFile(String aRelative, String aFile) {
71         FileSystemUtils.delete(new File(root, aFile));
72     }
73
74     /**
75      * Returns a temporary directory.
76      * 
77      * @return Temporary directory.
78      */
79     public File getTmpDir() {
80         return new File(root, "tmpdir");
81     }
82
83     /**
84      * Cleans up the test output directory.
85      */
86     public void clean() {
87         FileSystemUtils.deleteDirRecursively(root);
88         FileSystemUtils.createDir(root);
89     }
90
91     /**
92      * Recursively copy a directory contents to the test output directory.
93      * 
94      * @param sSrc
95      *            Source directory to copy.
96      */
97     public void copyDir(File aSrc) {
98         FileSystemUtils.copyDir(aSrc, root);
99     }
100
101     /**
102      * Copies a classpath resource to a relative path in the test output
103      * directory.
104      * 
105      * @param aResource
106      *            Resource to copy.
107      * @param aRelativePath
108      *            Relative path.
109      */
110     public void copyResource(String aResource, String aRelativePath) {
111         try {
112             InputStream is = new ClassPathResource(aResource).getInputStream();
113             FileOutputStream fos = new FileOutputStream(new File(root,
114                 aRelativePath));
115             FileSystemUtils.copyStream(is, fos);
116         } catch (IOException e) {
117             e.printStackTrace();
118             Assert.fail(e.getMessage());
119         }
120     }
121
122     /**
123      * Copies a resource to the root directory of the test output.
124      * 
125      * @param aResource
126      *            Resource.
127      */
128     public void copyResource(String aResource) {
129         String basename = new File(aResource).getName();
130         copyResource(aResource, basename);
131     }
132
133     public void createDir(String aRelative) {
134         FileSystemUtils.createDir(new File(root, aRelative));
135     }
136
137     /**
138      * Deletes a file or directory relative to the test output root.
139      * 
140      * @param aRelative
141      *            Relative path. The testcase will fail if the file or directory
142      *            cannot be removed.
143      */
144     public void delete(String aRelative) {
145         FileSystemUtils.delete(new File(root, aRelative));
146     }
147
148     /**
149      * Deletes a directory including its contents.
150      * 
151      * @param aRelative
152      *            Relative path.
153      */
154     public void deleteDir(String aRelative) {
155         FileSystemUtils.deleteDir(new File(root, aRelative));
156     }
157
158     /**
159      * Deletes a directory recursively.
160      * 
161      * @param aRelative
162      *            Relative path.
163      */
164     public void deleteDirRecursively(String aRelative) {
165         FileSystemUtils.deleteDir(new File(root, aRelative));
166     }
167
168     /**
169      * Gets the root of the test output directory.
170      * 
171      * @return Root of the test output.
172      */
173     public File getRoot() {
174         return root;
175     }
176
177     /**
178      * Gets a file object for a relative path.
179      */
180     public File getFile(String aRelative) {
181         return new File(root, aRelative);
182     }
183 }