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.test;
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";
47 private FileSystemUtils() {
52 * Deletes a directory recursively. The test case will fail if the directory
53 * does not exist or if deletion fails.
56 * Directory to delete.
58 public static void deleteDirRecursively(String aDir) {
59 deleteDirRecursively(new File(aDir));
63 * Deletes a directory recursively. See {@link #deleteDirRecursively}.
68 public static void deleteDirRecursively(File aDir) {
69 TestCase.assertTrue(aDir.isDirectory());
71 for (File file : aDir.listFiles()) {
72 if (file.isDirectory()) {
73 deleteDirRecursively(file);
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.
88 * File or directory to delete.
90 public static void delete(File aFile) {
91 TestCase.assertTrue(aFile.delete());
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.
99 * @param aRelativePath
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);
112 URL location = source.getLocation();
113 String protocol = location.getProtocol();
114 if (!protocol.equals("file")) {
115 return new File(aRelativePath);
118 String path = location.getPath();
120 path = URLDecoder.decode(location.getPath(), "UTF-8");
121 } catch (UnsupportedEncodingException e) {
122 // ignore it.. just don't decode
125 return new File(new File(path).getParentFile(), aRelativePath);
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.
133 * Directory to create.
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");
143 createDir(aDir.getParentFile());
144 TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
148 * Gets the test output directory for a specific test class.
152 * @return Test output directory.
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);
162 * Creates a directory hierarchy for the output directory of a test class if
167 * @return Test directory.
169 public static File createTestOutputDir(Class aTestClass) {
170 File file = getTestOutputDir(aTestClass);
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
186 public static File getTestOutputFile(String aName, Class aTestClass) {
187 File file = new File(getTestOutputDir(aTestClass), aName);
188 createDir(file.getParentFile());
192 public static String read(InputStream aIs) throws IOException {
194 StringBuffer buffer = new StringBuffer();
196 while ((c = aIs.read()) != -1) {
197 buffer.append((char)c);
199 return buffer.toString();