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 final class TestSupport {
33 * Disabled constructor.
36 private TestSupport() {
41 * Obtain root directory of JUnit tests.
43 * @return Directory name.
45 public static File getTestRootDir() {
46 return new File("testdata");
50 * Returns a temporary directory.
52 * @return Temporary directory.
54 public static File getTmpDir() {
55 return new File(getTestRootDir(), "tmpdir");
59 * Recursively remove a directory.
64 public static void removeDir(File aSrc) {
68 Assert.assertTrue(aSrc.getPath(), aSrc.isDirectory());
69 File[] files = aSrc.listFiles();
70 for (int i = 0; i < files.length; i++) {
72 if (file.isDirectory()) {
75 Assert.assertTrue(file.getPath(), file.delete());
78 Assert.assertTrue(aSrc.getPath(), aSrc.delete());
82 * Recursively copy a directory.
89 public static void copyDir(File aSrc, File aTarget) {
90 Assert.assertTrue(aSrc.isDirectory());
91 Assert.assertTrue(!aTarget.exists());
95 File[] files = aSrc.listFiles();
96 for (int i = 0; i < files.length; i++) {
98 if (file.isDirectory()) {
99 if (!file.getName().equals(".svn")) {
100 copyDir(new File(aSrc, file.getName()), new File(aTarget,
104 copyFile(file, new File(aTarget, file.getName()));
110 * Copy a file. If copying fails then the testcase will fail.
117 public static void copyFile(File aSrc, File aTarget) {
120 FileInputStream fis = new FileInputStream(aSrc);
121 FileOutputStream fos = new FileOutputStream(aTarget);
122 FileChannel fcin = fis.getChannel();
123 FileChannel fcout = fos.getChannel();
127 MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0,
139 } catch (IOException e) {
140 Assert.assertTrue("Copying file " + aSrc.getPath() + " to "
141 + aTarget.getPath() + " failed.", false);
146 * Remove a file or directory. The test case will fail if this does not
152 public static void delete(File aFile) {
154 .assertTrue("Could not delete " + aFile.getPath(), aFile
159 * Remove all files within a given directory including the directory itself.
160 * This only attempts to remove regular files and not directories within the
161 * directory. If the directory contains a nested directory, the deletion
162 * will fail. The test case will fail if this fails.
165 * Directory to remove.
167 public static void deleteDir(File aDir) {
173 * Remove all regular files within a given directory.
175 * @param outputDirName
177 public static void cleanDir(File aDir) {
178 if (!aDir.exists()) {
179 return; // nothing to do.
181 File[] entries = aDir.listFiles();
182 for (int i = 0; i < entries.length; i++) {
183 File file = entries[i];
185 Assert.assertTrue("Could not delete " + entries[i].getPath(),
186 entries[i].delete());
192 * Creates directory if it does not already exist. The test case will fail
193 * if the directory cannot be created.
196 * Directory to create.
198 public static void createDir(File aDir) {
199 if (aDir.isDirectory()) {
200 return; // nothing to do.
202 Assert.assertTrue("Could not create directory " + aDir.getPath(), aDir