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;
31 import sun.security.krb5.internal.crypto.b;
34 * File system utilities.
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 = "resources/testoutput";
49 private FileSystemUtils() {
54 * Deletes a directory recursively. The test case will fail if the directory
55 * does not exist or if deletion fails.
58 * Directory to delete.
60 public static void deleteDirRecursively(String aDir) {
61 deleteDirRecursively(new File(aDir));
65 * Deletes a directory recursively. See {@link #deleteDirRecursively}.
70 public static void deleteDirRecursively(File aDir) {
71 TestCase.assertTrue(aDir.isDirectory());
73 for (File file : aDir.listFiles()) {
74 if (file.isDirectory()) {
75 deleteDirRecursively(file);
85 * Deletes a file or directory. The test case will fail if the file or
86 * directory does not exist or if deletion fails. Deletion of a non-empty
87 * directory will always fail.
90 * File or directory to delete.
92 public static void delete(File aFile) {
93 TestCase.assertTrue(aFile.delete());
97 * Gets a path relative to a sub project. This utility should be used to
98 * easily access file paths within a subproject without requiring any
99 * specific Eclipse configuration.
101 * @param aRelativePath
106 public static File getPath(String aRelativePath, Class aTestClass) {
107 CodeSource source = aTestClass.getProtectionDomain().getCodeSource();
108 if (source == null) {
109 LOG.warn("Could not obtain path for '" + aRelativePath
110 + "' for class " + aTestClass
111 + ", using relative path as is");
112 return new File(aRelativePath);
114 URL location = source.getLocation();
115 String protocol = location.getProtocol();
116 if (!protocol.equals("file")) {
117 return new File(aRelativePath);
120 String path = location.getPath();
122 path = URLDecoder.decode(location.getPath(), "UTF-8");
123 } catch (UnsupportedEncodingException e) {
124 // ignore it.. just don't decode
127 return new File(new File(path).getParentFile(), aRelativePath);
131 * Ensures that a directory hierarchy exists (recursively if needed). If it
132 * is not possible to create the directory, then the test case will fail.
135 * Directory to create.
137 public static void createDir(File aDir) {
138 if (aDir.exists() && !aDir.isDirectory()) {
139 TestCase.fail("'" + aDir
140 + "' already exists and is not a directory");
145 createDir(aDir.getParentFile());
146 TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
150 * Gets the test output directory for a specific test class.
154 * @return Test output directory.
156 public static File getTestOutputDir(Class aTestClass) {
157 File file = getPath(TEST_OUTPUT_DIR, aTestClass);
158 String packageName = aTestClass.getPackage().getName();
159 String packagePath = packageName.replaceAll("\\.", "/");
160 return new File(file, packagePath);
164 * Creates a directory hierarchy for the output directory of a test class if
169 * @return Test directory.
171 public static File createTestOutputDir(Class aTestClass) {
172 File file = getTestOutputDir(aTestClass);
178 * Gets a test output file name. This returns a File object representing the
179 * output file and ensures that the directory where the file will be created
188 public static File getTestOutputFile(String aName, Class aTestClass) {
189 File file = new File(getTestOutputDir(aTestClass), aName);
190 createDir(file.getParentFile());
194 public static String read(InputStream aIs) throws IOException {
196 StringBuffer buffer = new StringBuffer();
198 while ((c = aIs.read()) != -1) {
199 buffer.append((char)c);
201 return buffer.toString();