3778a97077889ee42dcf63190a1661fc41adaaef
[utils] / support / test / org / wamblee / test / FileSystemUtils.java
1 /*
2  * Copyright 2006 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.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URL;
23 import java.net.URLDecoder;
24 import java.security.CodeSource;
25
26 import junit.framework.TestCase;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import sun.security.krb5.internal.crypto.b;
32
33 /**
34  * File system utilities.
35  */
36 public final class FileSystemUtils {
37
38     private static final Log LOG = LogFactory.getLog(FileSystemUtils.class);
39
40     /**
41      * Test output directory relative to the sub project.
42      */
43     private static final String TEST_OUTPUT_DIR = "resources/testoutput";
44
45     /*
46      * Disabled.
47      * 
48      */
49     private FileSystemUtils() {
50         // Empty
51     }
52
53     /**
54      * Deletes a directory recursively. The test case will fail if the directory
55      * does not exist or if deletion fails.
56      * 
57      * @param aDir
58      *            Directory to delete.
59      */
60     public static void deleteDirRecursively(String aDir) {
61         deleteDirRecursively(new File(aDir));
62     }
63
64     /**
65      * Deletes a directory recursively. See {@link #deleteDirRecursively}.
66      * 
67      * @param aDir
68      *            Directory.
69      */
70     public static void deleteDirRecursively(File aDir) {
71         TestCase.assertTrue(aDir.isDirectory());
72
73         for (File file : aDir.listFiles()) {
74             if (file.isDirectory()) {
75                 deleteDirRecursively(file);
76             } else {
77                 delete(file);
78             }
79         }
80
81         delete(aDir);
82     }
83
84     /**
85      * Deletes a file or directory. The test case will fail if the file or
86      * directory does not exist or if deletion fails. Deletion of a non-empty
87      * directory will always fail.
88      * 
89      * @param aFile
90      *            File or directory to delete.
91      */
92     public static void delete(File aFile) {
93         TestCase.assertTrue(aFile.delete());
94     }
95
96     /**
97      * Gets a path relative to a sub project. This utility should be used to
98      * easily access file paths within a subproject without requiring any
99      * specific Eclipse configuration.
100      * 
101      * @param aRelativePath
102      *            Relative path.
103      * @param aTestClass
104      *            Test class.
105      */
106     public static File getPath(String aRelativePath, Class aTestClass) {
107         CodeSource source = aTestClass.getProtectionDomain().getCodeSource();
108         if (source == null) {
109             LOG.warn("Could not obtain path for '" + aRelativePath
110                     + "' for class " + aTestClass
111                     + ", using relative path as is");
112             return new File(aRelativePath);
113         }
114         URL location = source.getLocation();
115         String protocol = location.getProtocol();
116         if (!protocol.equals("file")) {
117             return new File(aRelativePath);
118         }
119
120         String path = location.getPath();
121         try {
122             path = URLDecoder.decode(location.getPath(), "UTF-8");
123         } catch (UnsupportedEncodingException e) {
124             // ignore it.. just don't decode
125         }
126
127         return new File(new File(path).getParentFile(), aRelativePath);
128     }
129
130     /**
131      * Ensures that a directory hierarchy exists (recursively if needed). If it
132      * is not possible to create the directory, then the test case will fail.
133      * 
134      * @param aDir
135      *            Directory to create.
136      */
137     public static void createDir(File aDir) {
138         if (aDir.exists() && !aDir.isDirectory()) {
139             TestCase.fail("'" + aDir
140                     + "' already exists and is not a directory");
141         }
142         if (aDir.exists()) {
143             return;
144         }
145         createDir(aDir.getParentFile());
146         TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
147     }
148
149     /**
150      * Gets the test output directory for a specific test class.
151      * 
152      * @param aTestClass
153      *            Test class.
154      * @return Test output directory.
155      */
156     public static File getTestOutputDir(Class aTestClass) {
157         File file = getPath(TEST_OUTPUT_DIR, aTestClass);
158         String packageName = aTestClass.getPackage().getName();
159         String packagePath = packageName.replaceAll("\\.", "/");
160         return new File(file, packagePath);
161     }
162
163     /**
164      * Creates a directory hierarchy for the output directory of a test class if
165      * needed.
166      * 
167      * @param aTestClass
168      *            Test class
169      * @return Test directory.
170      */
171     public static File createTestOutputDir(Class aTestClass) {
172         File file = getTestOutputDir(aTestClass);
173         createDir(file);
174         return file;
175     }
176
177     /**
178      * Gets a test output file name. This returns a File object representing the
179      * output file and ensures that the directory where the file will be created
180      * already exists.
181      * 
182      * @param aName
183      *            Name of the file.
184      * @param aTestClass
185      *            Test class.
186      * @return File.
187      */
188     public static File getTestOutputFile(String aName, Class aTestClass) {
189         File file = new File(getTestOutputDir(aTestClass), aName);
190         createDir(file.getParentFile());
191         return file;
192     }
193
194     public static String read(InputStream aIs) throws IOException {
195         try {
196             StringBuffer buffer = new StringBuffer();
197             int c;
198             while ((c = aIs.read()) != -1) {
199                 buffer.append((char)c);
200             }
201             return buffer.toString();
202         } finally {
203             aIs.close();
204         }
205     }
206 }