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 public class TestSupport {
33 * Obtain root directory of JUnit tests.
35 * @return Directory name.
37 public static File getTestRootDir() {
38 return new File("testdata");
42 * Returns a temporary directory.
44 * @return Temporary directory.
46 public static File getTmpDir() {
47 return new File(getTestRootDir(), "tmpdir");
51 * Recursively remove a directory.
56 public static void removeDir(File aSrc) {
60 Assert.assertTrue(aSrc.getPath(), aSrc.isDirectory());
61 File[] files = aSrc.listFiles();
62 for (int i = 0; i < files.length; i++) {
64 if (file.isDirectory()) {
67 Assert.assertTrue(file.getPath(), file.delete());
70 Assert.assertTrue(aSrc.getPath(), aSrc.delete());
74 * Recursively copy a directory.
81 public static void copyDir(File aSrc, File aTarget) {
82 Assert.assertTrue(aSrc.isDirectory());
83 Assert.assertTrue(!aTarget.exists());
87 File[] files = aSrc.listFiles();
88 for (int i = 0; i < files.length; i++) {
90 if (file.isDirectory()) {
91 if (!file.getName().equals(".svn")) {
92 copyDir(new File(aSrc, file.getName()), new File(aTarget,
96 copyFile(file, new File(aTarget, file.getName()));
102 * Copy a file. If copying fails then the testcase will fail.
109 public static void copyFile(File aSrc, File aTarget) {
112 FileInputStream fis = new FileInputStream(aSrc);
113 FileOutputStream fos = new FileOutputStream(aTarget);
114 FileChannel fcin = fis.getChannel();
115 FileChannel fcout = fos.getChannel();
119 MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
131 } catch (IOException e) {
132 Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
133 + aTarget.getPath() + " failed.", false);
138 * Remove a file or directory. The test case will fail if this does not
144 public static void delete(File aFile) {
145 Assert.assertTrue("Could not delete " + aFile.getPath(), aFile.delete());
149 * Remove all files within a given directory including the directory itself.
150 * This only attempts to remove regular files and not directories within the
151 * directory. If the directory contains a nested directory, the deletion
152 * will fail. The test case will fail if this fails.
155 * Directory to remove.
157 public static void deleteDir(File aDir) {
163 * Remove all regular files within a given directory.
165 * @param outputDirName
167 public static void cleanDir(File aDir) {
168 if (!aDir.exists()) {
169 return; // nothing to do.
171 File[] entries = aDir.listFiles();
172 for (int i = 0; i < entries.length; i++) {
173 File file = entries[i];
175 Assert.assertTrue("Could not delete " + entries[i].getPath(),
176 entries[i].delete());
182 * Creates directory if it does not already exist. The test case will fail
183 * if the directory cannot be created.
186 * Directory to create.
188 public static void createDir(File aDir) {
189 if (aDir.isDirectory()) {
190 return; // nothing to do.
192 Assert.assertTrue("Could not create directory " + aDir.getPath(), aDir