2 * Copyright 2005 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.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.nio.MappedByteBuffer;
23 import java.nio.channels.FileChannel;
25 import junit.framework.Assert;
28 * @author Erik Test support utility.
30 * @author Erik Brakkee
32 public final class TestSupport {
35 * Disabled constructor.
38 private TestSupport() {
43 * Obtain root directory of JUnit tests.
45 * @return Directory name.
47 public static File getTestRootDir() {
48 return new File("testdata");
52 * Returns a temporary directory.
54 * @return Temporary directory.
56 public static File getTmpDir() {
57 return new File(getTestRootDir(), "tmpdir");
61 * Recursively remove a directory.
66 public static void removeDir(File aSrc) {
70 Assert.assertTrue(aSrc.getPath(), aSrc.isDirectory());
71 File[] files = aSrc.listFiles();
72 for (int i = 0; i < files.length; i++) {
74 if (file.isDirectory()) {
77 Assert.assertTrue(file.getPath(), file.delete());
80 Assert.assertTrue(aSrc.getPath(), aSrc.delete());
84 * Recursively copy a directory.
91 public static void copyDir(File aSrc, File aTarget) {
92 Assert.assertTrue(aSrc.isDirectory());
93 Assert.assertTrue(!aTarget.exists());
97 File[] files = aSrc.listFiles();
98 for (int i = 0; i < files.length; i++) {
100 if (file.isDirectory()) {
101 if (!file.getName().equals(".svn")) {
102 copyDir(new File(aSrc, file.getName()), new File(aTarget,
106 copyFile(file, new File(aTarget, file.getName()));
112 * Copy a file. If copying fails then the testcase will fail.
119 public static void copyFile(File aSrc, File aTarget) {
122 FileInputStream fis = new FileInputStream(aSrc);
123 FileOutputStream fos = new FileOutputStream(aTarget);
124 FileChannel fcin = fis.getChannel();
125 FileChannel fcout = fos.getChannel();
129 MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
141 } catch (IOException e) {
142 Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
143 + aTarget.getPath() + " failed.", false);
148 * Remove a file or directory. The test case will fail if this does not
154 public static void delete(File aFile) {
156 .assertTrue("Could not delete " + aFile.getPath(), aFile
161 * Remove all files within a given directory including the directory itself.
162 * This only attempts to remove regular files and not directories within the
163 * directory. If the directory contains a nested directory, the deletion
164 * will fail. The test case will fail if this fails.
167 * Directory to remove.
169 public static void deleteDir(File aDir) {
175 * Remove all regular files within a given directory.
177 * @param outputDirName
179 public static void cleanDir(File aDir) {
180 if (!aDir.exists()) {
181 return; // nothing to do.
183 File[] entries = aDir.listFiles();
184 for (int i = 0; i < entries.length; i++) {
185 File file = entries[i];
187 Assert.assertTrue("Could not delete " + entries[i].getPath(),
188 entries[i].delete());
194 * Creates directory if it does not already exist. The test case will fail
195 * if the directory cannot be created.
198 * Directory to create.
200 public static void createDir(File aDir) {
201 if (aDir.isDirectory()) {
202 return; // nothing to do.
204 Assert.assertTrue("Could not create directory " + aDir.getPath(), aDir