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