2 * Copyright 2006 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.io;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
23 import java.net.URLDecoder;
24 import java.security.CodeSource;
26 import junit.framework.TestCase;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
32 * File system utilities.
34 public final class FileSystemUtils {
36 private static final Log LOG = LogFactory.getLog(FileSystemUtils.class);
39 * Test output directory relative to the sub project.
41 private static final String TEST_OUTPUT_DIR = "resources/testoutput";
44 * Test input directory relative to the sub project.
46 private static final String TEST_INPUT_DIR = "../resources/test";
52 private FileSystemUtils() {
57 * Deletes a directory recursively. The test case will fail if the directory
58 * does not exist or if deletion fails.
61 * Directory to delete.
63 public static void deleteDirRecursively(String aDir) {
64 deleteDirRecursively(new File(aDir));
68 * Deletes a directory recursively. See {@link #deleteDirRecursively}.
73 public static void deleteDirRecursively(File aDir) {
74 TestCase.assertTrue(aDir.isDirectory());
76 for (File file : aDir.listFiles()) {
77 if (file.isDirectory()) {
78 deleteDirRecursively(file);
88 * Deletes a file or directory. The test case will fail if the file or
89 * directory does not exist or if deletion fails. Deletion of a non-empty
90 * directory will always fail.
93 * File or directory to delete.
95 public static void delete(File aFile) {
96 TestCase.assertTrue(aFile.delete());
100 * Gets a path relative to a sub project. This utility should be used to
101 * easily access file paths within a subproject without requiring any
102 * specific Eclipse configuration.
104 * @param aRelativePath
109 public static File getPath(String aRelativePath, Class aTestClass) {
110 CodeSource source = aTestClass.getProtectionDomain().getCodeSource();
111 if (source == null) {
112 LOG.warn("Could not obtain path for '" + aRelativePath
113 + "' for class " + aTestClass
114 + ", using relative path as is");
115 return new File(aRelativePath);
117 URL location = source.getLocation();
118 String protocol = location.getProtocol();
119 if (!protocol.equals("file")) {
120 LOG.warn("protocol is not 'file': " + location);
121 return new File(aRelativePath);
124 String path = location.getPath();
126 path = URLDecoder.decode(location.getPath(), "UTF-8");
127 } catch (UnsupportedEncodingException e) {
128 // ignore it.. just don't decode
129 LOG.warn("Decoding path failed: '" + location.getPath() + "'", e );
132 return new File(new File(path).getParentFile(), aRelativePath);
136 * Ensures that a directory hierarchy exists (recursively if needed). If it
137 * is not possible to create the directory, then the test case will fail.
140 * Directory to create.
142 public static void createDir(File aDir) {
143 if (aDir.exists() && !aDir.isDirectory()) {
144 TestCase.fail("'" + aDir
145 + "' already exists and is not a directory");
150 createDir(aDir.getParentFile());
151 TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
155 * Gets the test output directory for a specific test class.
159 * @return Test output directory.
161 public static File getTestOutputDir(Class aTestClass) {
162 File file = getPath(TEST_OUTPUT_DIR, aTestClass);
163 String packageName = aTestClass.getPackage().getName();
164 String packagePath = packageName.replaceAll("\\.", "/");
165 return new File(file, packagePath);
169 * Gets the test input directory for a specific test class.
173 * @return Test input directory.
175 public static File getTestInputDir(Class aTestClass) {
176 File file = getPath(TEST_INPUT_DIR, aTestClass);
177 String packageName = aTestClass.getPackage().getName();
178 String packagePath = packageName.replaceAll("\\.", "/");
179 return new File(file, packagePath);
183 * Creates a directory hierarchy for the output directory of a test class if
188 * @return Test directory.
190 public static File createTestOutputDir(Class aTestClass) {
191 File file = getTestOutputDir(aTestClass);
197 * Gets a test output file name. This returns a File object representing the
198 * output file and ensures that the directory where the file will be created
207 public static File getTestOutputFile(String aName, Class aTestClass) {
208 File file = new File(getTestOutputDir(aTestClass), aName);
209 createDir(file.getParentFile());
213 public static String read(InputStream aIs) throws IOException {
215 StringBuffer buffer = new StringBuffer();
217 while ((c = aIs.read()) != -1) {
218 buffer.append((char)c);
220 return buffer.toString();