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