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