Testoutput is now redirected to a separate file. Makes console output
[utils] / support / src / test / java / org / wamblee / test / TestSupport.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.test;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.nio.MappedByteBuffer;
23 import java.nio.channels.FileChannel;
24
25 import junit.framework.Assert;
26
27 /**
28  * @author Erik Test support utility.
29  *
30  * @author Erik Brakkee
31  */
32 public final class TestSupport {
33     
34     /**
35      * Disabled constructor.
36      *
37      */
38     private TestSupport() { 
39         // Empty
40     }
41
42     /**
43      * Obtain root directory of JUnit tests.
44      * 
45      * @return Directory name.
46      */
47     public static File getTestRootDir() {
48         return new File("testdata");
49     }
50
51     /**
52      * Returns a temporary directory.
53      * 
54      * @return Temporary directory.
55      */
56     public static File getTmpDir() {
57         return new File(getTestRootDir(), "tmpdir");
58     }
59
60     /**
61      * Recursively remove a directory.
62      * 
63      * @param aSrc
64      *            Directoryto remove.
65      */
66     public static void removeDir(File aSrc) {
67         if (!aSrc.exists()) {
68             return;
69         }
70         Assert.assertTrue(aSrc.getPath(), aSrc.isDirectory());
71         File[] files = aSrc.listFiles();
72         for (int i = 0; i < files.length; i++) {
73             File file = files[i];
74             if (file.isDirectory()) {
75                 removeDir(file);
76             } else {
77                 Assert.assertTrue(file.getPath(), file.delete());
78             }
79         }
80         Assert.assertTrue(aSrc.getPath(), aSrc.delete());
81     }
82
83     /**
84      * Recursively copy a directory.
85      * 
86      * @param aSrc
87      *            Source directory
88      * @param aTarget
89      *            Target directory.
90      */
91     public static void copyDir(File aSrc, File aTarget) {
92         Assert.assertTrue(aSrc.isDirectory());
93         Assert.assertTrue(!aTarget.exists());
94
95         aTarget.mkdirs();
96
97         File[] files = aSrc.listFiles();
98         for (int i = 0; i < files.length; i++) {
99             File file = files[i];
100             if (file.isDirectory()) {
101                 if (!file.getName().equals(".svn")) {
102                     copyDir(new File(aSrc, file.getName()), new File(aTarget,
103                             file.getName()));
104                 }
105             } else {
106                 copyFile(file, new File(aTarget, file.getName()));
107             }
108         }
109     }
110
111     /**
112      * Copy a file. If copying fails then the testcase will fail.
113      * 
114      * @param aSrc
115      *            Source file.
116      * @param aTarget
117      *            Target file.
118      */
119     public static void copyFile(File aSrc, File aTarget) {
120
121         try {
122             FileInputStream fis = new FileInputStream(aSrc);
123             FileOutputStream fos = new FileOutputStream(aTarget);
124             FileChannel fcin = fis.getChannel();
125             FileChannel fcout = fos.getChannel();
126
127             // map input file
128
129             MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
130                     fcin.size());
131
132             // do the file copy
133             fcout.write(mbb);
134
135             // finish up
136
137             fcin.close();
138             fcout.close();
139             fis.close();
140             fos.close();
141         } catch (IOException e) {
142             Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
143                     + aTarget.getPath() + " failed.", false);
144         }
145     }
146
147     /**
148      * Remove a file or directory. The test case will fail if this does not
149      * succeed.
150      * 
151      * @param aFile
152      *            entry to remove.
153      */
154     public static void delete(File aFile) {
155         Assert
156                 .assertTrue("Could not delete " + aFile.getPath(), aFile
157                         .delete());
158     }
159
160     /**
161      * Remove all files within a given directory including the directory itself.
162      * This only attempts to remove regular files and not directories within the
163      * directory. If the directory contains a nested directory, the deletion
164      * will fail. The test case will fail if this fails.
165      * 
166      * @param aDir
167      *            Directory to remove.
168      */
169     public static void deleteDir(File aDir) {
170         cleanDir(aDir);
171         delete(aDir);
172     }
173
174     /**
175      * Remove all regular files within a given directory.
176      * 
177      * @param outputDirName
178      */
179     public static void cleanDir(File aDir) {
180         if (!aDir.exists()) {
181             return; // nothing to do.
182         }
183         File[] entries = aDir.listFiles();
184         for (int i = 0; i < entries.length; i++) {
185             File file = entries[i];
186             if (file.isFile()) {
187                 Assert.assertTrue("Could not delete " + entries[i].getPath(),
188                         entries[i].delete());
189             }
190         }
191     }
192
193     /**
194      * Creates directory if it does not already exist. The test case will fail
195      * if the directory cannot be created.
196      * 
197      * @param aDir
198      *            Directory to create.
199      */
200     public static void createDir(File aDir) {
201         if (aDir.isDirectory()) {
202             return; // nothing to do.
203         }
204         Assert.assertTrue("Could not create directory " + aDir.getPath(), aDir
205                 .mkdirs());
206     }
207 }