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.UnsupportedEncodingException;
21 import java.net.URLDecoder;
22 import java.security.CodeSource;
24 import junit.framework.TestCase;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
30 * File system utilities.
32 public final class FileSystemUtils {
34 private static final Log LOG = LogFactory.getLog(FileSystemUtils.class);
37 * Test output directory relative to the sub project.
39 private static final String TEST_OUTPUT_DIR = "resources/testoutput";
45 private FileSystemUtils() {
50 * Deletes a directory recursively. The test case will fail if the directory
51 * does not exist or if deletion fails.
54 * Directory to delete.
56 public static void deleteDirRecursively(String aDir) {
57 deleteDirRecursively(new File(aDir));
61 * Deletes a directory recursively. See {@link #deleteDirRecursively}.
66 public static void deleteDirRecursively(File aDir) {
67 TestCase.assertTrue(aDir.isDirectory());
69 for (File file: aDir.listFiles()) {
70 if (file.isDirectory()) {
71 deleteDirRecursively(file);
81 * Deletes a file or directory. The test case will fail if the file or
82 * directory does not exist or if deletion fails. Deletion of a non-empty
83 * directory will always fail.
86 * File or directory to delete.
88 public static void delete(File aFile) {
89 TestCase.assertTrue(aFile.delete());
93 * Gets a path relative to a sub project. This utility should be used to
94 * easily access file paths within a subproject without requiring any
95 * specific Eclipse configuration.
97 * @param aRelativePath
102 public static File getPath(String aRelativePath, Class aTestClass) {
103 CodeSource source = aTestClass.getProtectionDomain().getCodeSource();
104 if (source == null) {
105 LOG.warn("Could not obtain path for '" + aRelativePath
106 + "' for class " + aTestClass
107 + ", using relative path as is");
108 return new File(aRelativePath);
110 URL location = source.getLocation();
111 String protocol = location.getProtocol();
112 if (!protocol.equals("file")) {
113 return new File(aRelativePath);
116 String path = location.getPath();
118 path = URLDecoder.decode(location.getPath(), "UTF-8");
119 } catch (UnsupportedEncodingException e) {
120 // ignore it.. just don't decode
123 return new File(new File(path).getParentFile(), aRelativePath);
127 * Ensures that a directory hierarchy exists (recursively if needed). If it
128 * is not possible to create the directory, then the test case will fail.
131 * Directory to create.
133 public static void createDir(File aDir) {
134 if (aDir.exists() && !aDir.isDirectory()) {
135 TestCase.fail("'" + aDir
136 + "' already exists and is not a directory");
141 createDir(aDir.getParentFile());
142 TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
146 * Gets the test output directory for a specific test class.
150 * @return Test output directory.
152 public static File getTestOutputDir(Class aTestClass) {
153 File file = getPath(TEST_OUTPUT_DIR, aTestClass);
154 String packageName = aTestClass.getPackage().getName();
155 String packagePath = packageName.replaceAll("\\.", "/");
156 return new File(file, packagePath);
160 * Creates a directory hierarchy for the output directory of a test class if
165 * @return Test directory.
167 public static File createTestOutputDir(Class aTestClass) {
168 File file = getTestOutputDir(aTestClass);
174 * Gets a test output file name. This returns a File object representing the
175 * output file and ensures that the directory where the file will be created
184 public static File getTestOutputFile(String aName, Class aTestClass) {
185 File file = new File(getTestOutputDir(aTestClass), aName);
186 createDir(file.getParentFile());