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.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.UnsupportedEncodingException;
26 import java.net.URLDecoder;
27 import java.nio.MappedByteBuffer;
28 import java.nio.channels.FileChannel;
29 import java.security.CodeSource;
31 import junit.framework.Assert;
32 import junit.framework.TestCase;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
38 * File system utilities.
40 * @author Erik Brakkee
42 public final class FileSystemUtils {
44 private static final Log LOG = LogFactory.getLog(FileSystemUtils.class);
47 * Test output directory relative to the sub project.
49 private static final String TEST_OUTPUT_DIR = "../target/testoutput";
52 * Test input directory relative to the sub project.
54 private static final String TEST_INPUT_DIR = "../src/test/resources";
60 private FileSystemUtils() {
65 * Deletes a directory recursively. The test case will fail if the directory
66 * does not exist or if deletion fails.
69 * Directory to delete.
71 public static void deleteDirRecursively(String aDir) {
72 deleteDirRecursively(new File(aDir));
76 * Deletes a directory recursively. See {@link #deleteDirRecursively}.
81 public static void deleteDirRecursively(File aDir) {
82 TestCase.assertTrue(aDir.isDirectory());
84 for (File file : aDir.listFiles()) {
85 if (file.isDirectory()) {
86 deleteDirRecursively(file);
96 * Deletes a file or directory. The test case will fail if the file or
97 * directory does not exist or if deletion fails. Deletion of a non-empty
98 * directory will always fail.
101 * File or directory to delete.
103 public static void delete(File aFile) {
104 TestCase.assertTrue(aFile.delete());
108 * Gets a path relative to a sub project. This utility should be used to
109 * easily access file paths within a subproject without requiring any
110 * specific Eclipse configuration.
112 * @param aRelativePath
117 public static File getPath(String aRelativePath, Class aTestClass) {
118 CodeSource source = aTestClass.getProtectionDomain().getCodeSource();
119 if (source == null) {
120 LOG.warn("Could not obtain path for '" + aRelativePath
121 + "' for class " + aTestClass
122 + ", using relative path as is");
123 return new File(aRelativePath);
125 URL location = source.getLocation();
126 String protocol = location.getProtocol();
127 if (!protocol.equals("file")) {
128 LOG.warn("protocol is not 'file': " + location);
129 return new File(aRelativePath);
132 String path = location.getPath();
134 path = URLDecoder.decode(location.getPath(), "UTF-8");
135 } catch (UnsupportedEncodingException e) {
136 // ignore it.. just don't decode
137 LOG.warn("Decoding path failed: '" + location.getPath() + "'", e);
140 return new File(new File(path).getParentFile(), aRelativePath);
144 * Ensures that a directory hierarchy exists (recursively if needed). If it
145 * is not possible to create the directory, then the test case will fail.
148 * Directory to create.
150 public static void createDir(File aDir) {
151 if (aDir.exists() && !aDir.isDirectory()) {
152 TestCase.fail("'" + aDir
153 + "' already exists and is not a directory");
158 createDir(aDir.getParentFile());
159 TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
163 * Creates a file in a directory.
164 * @param aDir Directory path. Will be created if it does not exist.
165 * @param aName Filename.
166 * @param aContents Contents.
168 public static void createFile(File aDir, String aName, InputStream aContents) {
171 OutputStream os = new FileOutputStream(new File(aDir, aName));
172 copyStream(aContents, os);
173 } catch (IOException e) {
175 TestCase.fail(e.getMessage());
180 * Gets the test output directory for a specific test class.
184 * @return Test output directory.
186 public static File getTestOutputDir(Class aTestClass) {
187 File file = getPath(TEST_OUTPUT_DIR, aTestClass);
188 String className = aTestClass.getName();
189 String classRelPath = className.replaceAll("\\.", "/");
190 return new File(file, classRelPath);
194 * Gets the test input directory for a specific test class.
198 * @return Test input directory.
200 public static File getTestInputDir(Class aTestClass) {
201 File file = getPath(TEST_INPUT_DIR, aTestClass);
202 String packageName = aTestClass.getPackage().getName();
203 String packagePath = packageName.replaceAll("\\.", "/");
204 return new File(file, packagePath);
208 * Creates a directory hierarchy for the output directory of a test class if
213 * @return Test directory.
215 public static File createTestOutputDir(Class aTestClass) {
216 File file = getTestOutputDir(aTestClass);
222 * Gets a test output file name. This returns a File object representing the
223 * output file and ensures that the directory where the file will be created
232 public static File getTestOutputFile(String aName, Class aTestClass) {
233 File file = new File(getTestOutputDir(aTestClass), aName);
234 createDir(file.getParentFile());
238 public static String read(InputStream aIs) throws IOException {
240 StringBuffer buffer = new StringBuffer();
242 while ((c = aIs.read()) != -1) {
243 buffer.append((char) c);
245 return buffer.toString();
252 * Copies an input stream to an output stream.
259 public static void copyStream(InputStream aIs, OutputStream aOs) {
262 while ((c = aIs.read()) != -1) {
267 } catch (IOException e) {
269 Assert.fail(e.getMessage());
274 * Recursively copy a directory.
281 public static void copyDir(File aSrc, File aTarget) {
282 Assert.assertTrue(aSrc.isDirectory());
283 Assert.assertTrue(!aTarget.exists());
287 File[] files = aSrc.listFiles();
288 for (int i = 0; i < files.length; i++) {
289 File file = files[i];
290 if (file.isDirectory()) {
291 if (!file.getName().equals(".svn")) {
292 copyDir(new File(aSrc, file.getName()), new File(aTarget,
296 copyFile(file, new File(aTarget, file.getName()));
302 * Copy a file. If copying fails then the testcase will fail.
309 public static void copyFile(File aSrc, File aTarget) {
312 FileInputStream fis = new FileInputStream(aSrc);
313 FileOutputStream fos = new FileOutputStream(aTarget);
314 FileChannel fcin = fis.getChannel();
315 FileChannel fcout = fos.getChannel();
319 MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
331 } catch (IOException e) {
332 Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
333 + aTarget.getPath() + " failed.", false);
338 * Remove all files within a given directory including the directory itself.
339 * This only attempts to remove regular files and not directories within the
340 * directory. If the directory contains a nested directory, the deletion
341 * will fail. The test case will fail if this fails.
344 * Directory to remove.
346 public static void deleteDir(File aDir) {
352 * Remove all regular files within a given directory.
354 * @param outputDirName
356 public static void cleanDir(File aDir) {
357 if (!aDir.exists()) {
358 return; // nothing to do.
360 File[] entries = aDir.listFiles();
361 for (int i = 0; i < entries.length; i++) {
362 File file = entries[i];
364 Assert.assertTrue("Could not delete " + entries[i].getPath(),
365 entries[i].delete());