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 * @author Erik Brakkee
36 public final class FileSystemUtils {
38 private static final Log LOG = LogFactory.getLog(FileSystemUtils.class);
41 * Test output directory relative to the sub project.
43 private static final String TEST_OUTPUT_DIR = "../target/testoutput";
46 * Test input directory relative to the sub project.
48 private static final String TEST_INPUT_DIR = "../src/test/resources";
54 private FileSystemUtils() {
59 * Deletes a directory recursively. The test case will fail if the directory
60 * does not exist or if deletion fails.
63 * Directory to delete.
65 public static void deleteDirRecursively(String aDir) {
66 deleteDirRecursively(new File(aDir));
70 * Deletes a directory recursively. See {@link #deleteDirRecursively}.
75 public static void deleteDirRecursively(File aDir) {
76 TestCase.assertTrue(aDir.isDirectory());
78 for (File file : aDir.listFiles()) {
79 if (file.isDirectory()) {
80 deleteDirRecursively(file);
90 * Deletes a file or directory. The test case will fail if the file or
91 * directory does not exist or if deletion fails. Deletion of a non-empty
92 * directory will always fail.
95 * File or directory to delete.
97 public static void delete(File aFile) {
98 TestCase.assertTrue(aFile.delete());
102 * Gets a path relative to a sub project. This utility should be used to
103 * easily access file paths within a subproject without requiring any
104 * specific Eclipse configuration.
106 * @param aRelativePath
111 public static File getPath(String aRelativePath, Class aTestClass) {
112 CodeSource source = aTestClass.getProtectionDomain().getCodeSource();
113 if (source == null) {
114 LOG.warn("Could not obtain path for '" + aRelativePath
115 + "' for class " + aTestClass
116 + ", using relative path as is");
117 return new File(aRelativePath);
119 URL location = source.getLocation();
120 String protocol = location.getProtocol();
121 if (!protocol.equals("file")) {
122 LOG.warn("protocol is not 'file': " + location);
123 return new File(aRelativePath);
126 String path = location.getPath();
128 path = URLDecoder.decode(location.getPath(), "UTF-8");
129 } catch (UnsupportedEncodingException e) {
130 // ignore it.. just don't decode
131 LOG.warn("Decoding path failed: '" + location.getPath() + "'", e );
134 return new File(new File(path).getParentFile(), aRelativePath);
138 * Ensures that a directory hierarchy exists (recursively if needed). If it
139 * is not possible to create the directory, then the test case will fail.
142 * Directory to create.
144 public static void createDir(File aDir) {
145 if (aDir.exists() && !aDir.isDirectory()) {
146 TestCase.fail("'" + aDir
147 + "' already exists and is not a directory");
152 createDir(aDir.getParentFile());
153 TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
157 * Gets the test output directory for a specific test class.
161 * @return Test output directory.
163 public static File getTestOutputDir(Class aTestClass) {
164 File file = getPath(TEST_OUTPUT_DIR, aTestClass);
165 String packageName = aTestClass.getPackage().getName();
166 String packagePath = packageName.replaceAll("\\.", "/");
167 return new File(file, packagePath);
171 * Gets the test input directory for a specific test class.
175 * @return Test input directory.
177 public static File getTestInputDir(Class aTestClass) {
178 File file = getPath(TEST_INPUT_DIR, aTestClass);
179 String packageName = aTestClass.getPackage().getName();
180 String packagePath = packageName.replaceAll("\\.", "/");
181 return new File(file, packagePath);
185 * Creates a directory hierarchy for the output directory of a test class if
190 * @return Test directory.
192 public static File createTestOutputDir(Class aTestClass) {
193 File file = getTestOutputDir(aTestClass);
199 * Gets a test output file name. This returns a File object representing the
200 * output file and ensures that the directory where the file will be created
209 public static File getTestOutputFile(String aName, Class aTestClass) {
210 File file = new File(getTestOutputDir(aTestClass), aName);
211 createDir(file.getParentFile());
215 public static String read(InputStream aIs) throws IOException {
217 StringBuffer buffer = new StringBuffer();
219 while ((c = aIs.read()) != -1) {
220 buffer.append((char)c);
222 return buffer.toString();