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