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