From: erik Date: Sun, 30 Mar 2008 11:20:29 +0000 (+0000) Subject: Created a TestData test support class for managing test output files. X-Git-Tag: wamblee-utils-0.2@603~217 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=28f1acbe7616a2b35f785b35ef6392c1705bb3c7;p=utils Created a TestData test support class for managing test output files. --- diff --git a/trunk/support/src/test/java/org/wamblee/io/FileSystemUtils.java b/trunk/support/src/test/java/org/wamblee/io/FileSystemUtils.java index 3e36219d..3d0f3b3b 100644 --- a/trunk/support/src/test/java/org/wamblee/io/FileSystemUtils.java +++ b/trunk/support/src/test/java/org/wamblee/io/FileSystemUtils.java @@ -16,13 +16,19 @@ package org.wamblee.io; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; import java.security.CodeSource; +import junit.framework.Assert; import junit.framework.TestCase; import org.apache.commons.logging.Log; @@ -224,4 +230,120 @@ public final class FileSystemUtils { aIs.close(); } } + + /** + * Copies an input stream to an output stream. + * @param aIs Input stream. + * @param aOs Output stream. + */ + public static void copyStream(InputStream aIs, OutputStream aOs) { + try { + int c; + while ((c = aIs.read()) != -1) { + aOs.write(c); + } + aIs.close(); + aOs.close(); + } catch (IOException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + } + + /** + * Recursively copy a directory. + * + * @param aSrc + * Source directory + * @param aTarget + * Target directory. + */ + public static void copyDir(File aSrc, File aTarget) { + Assert.assertTrue(aSrc.isDirectory()); + Assert.assertTrue(!aTarget.exists()); + + aTarget.mkdirs(); + + File[] files = aSrc.listFiles(); + for (int i = 0; i < files.length; i++) { + File file = files[i]; + if (file.isDirectory()) { + if (!file.getName().equals(".svn")) { + copyDir(new File(aSrc, file.getName()), new File(aTarget, + file.getName())); + } + } else { + copyFile(file, new File(aTarget, file.getName())); + } + } + } + + /** + * Copy a file. If copying fails then the testcase will fail. + * + * @param aSrc + * Source file. + * @param aTarget + * Target file. + */ + public static void copyFile(File aSrc, File aTarget) { + + try { + FileInputStream fis = new FileInputStream(aSrc); + FileOutputStream fos = new FileOutputStream(aTarget); + FileChannel fcin = fis.getChannel(); + FileChannel fcout = fos.getChannel(); + + // map input file + + MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0, + fcin.size()); + + // do the file copy + fcout.write(mbb); + + // finish up + + fcin.close(); + fcout.close(); + fis.close(); + fos.close(); + } catch (IOException e) { + Assert.assertTrue("Copying file " + aSrc.getPath() + " to " + + aTarget.getPath() + " failed.", false); + } + } + + /** + * Remove all files within a given directory including the directory itself. + * This only attempts to remove regular files and not directories within the + * directory. If the directory contains a nested directory, the deletion + * will fail. The test case will fail if this fails. + * + * @param aDir + * Directory to remove. + */ + public static void deleteDir(File aDir) { + cleanDir(aDir); + delete(aDir); + } + + /** + * Remove all regular files within a given directory. + * + * @param outputDirName + */ + public static void cleanDir(File aDir) { + if (!aDir.exists()) { + return; // nothing to do. + } + File[] entries = aDir.listFiles(); + for (int i = 0; i < entries.length; i++) { + File file = entries[i]; + if (file.isFile()) { + Assert.assertTrue("Could not delete " + entries[i].getPath(), + entries[i].delete()); + } + } + } } diff --git a/trunk/support/src/test/java/org/wamblee/io/TestData.java b/trunk/support/src/test/java/org/wamblee/io/TestData.java new file mode 100644 index 00000000..cc815c1b --- /dev/null +++ b/trunk/support/src/test/java/org/wamblee/io/TestData.java @@ -0,0 +1,147 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.io; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; + +import junit.framework.Assert; + +/** + * TestData provides a convenient interface for managing test output files. + * + * @author Erik Brakkee + */ +public final class TestData { + + private Object _testcase; + private File _root; + + /** + * Test data to be constructed in the setUp of a test. + * {@link #clean()} must be called to make sure that this + * directory is empty before executing a test. + */ + public TestData(Object aTestcase) { + _testcase = aTestcase; + _root = getTestRootDir(aTestcase); + } + + /** + * Obtain root directory of JUnit tests. + * + * @return Directory name. + */ + private static File getTestRootDir(Object aTestcase) { + return FileSystemUtils.getTestOutputDir(aTestcase.getClass()); + } + + /** + * Returns a temporary directory. + * + * @return Temporary directory. + */ + public File getTmpDir() { + return new File(_root, "tmpdir"); + } + + /** + * Cleans up the test output directory. + */ + public void clean() { + FileSystemUtils.deleteDirRecursively(_root); + } + + /** + * Recursively copy a directory contents to the test output directory. + * + * @param sSrc + * Source directory to copy. + */ + public void copyDir(File aSrc) { + FileSystemUtils.copyDir(aSrc, _root); + } + + /** + * Copies a classpath resource to a relative path in the test output + * directory. + * + * @param aResource + * Resource to copy. + * @param aRelativePath + * Relative path. + */ + public void copyResource(String aResource, String aRelativePath) { + try { + InputStream is = new ClassPathResource(aResource).getInputStream(); + FileOutputStream fos = new FileOutputStream(new File(_root, + aRelativePath)); + FileSystemUtils.copyStream(is, fos); + } catch (IOException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + } + + /** + * Copies a resource to the root directory of the test output. + * @param aResource Resource. + */ + public void copyResource(String aResource) { + String basename = new File(aResource).getName(); + copyResource(aResource, basename); + } + + /** + * Deletes a file or directory relative to the test output root. + * + * @param aRelative + * Relative path. The testcase will fail if the file or directory + * cannot be removed. + */ + public void delete(String aRelative) { + FileSystemUtils.delete(new File(_root, aRelative)); + } + + /** + * Deletes a directory including its contents. + * @param aRelative Relative path. + */ + public void deleteDir(String aRelative) { + FileSystemUtils.deleteDir(new File(_root, aRelative)); + } + + /** + * Deletes a directory recursively. + * @param aRelative Relative path. + */ + public void deleteDirRecursively(String aRelative) { + FileSystemUtils.deleteDir(new File(_root, aRelative)); + } + + /** + * Gets a file object for a relative path. + */ + public File getFile(String aRelative) { + return new File(_root, aRelative); + } +} diff --git a/trunk/support/src/test/java/org/wamblee/test/TestSupport.java b/trunk/support/src/test/java/org/wamblee/test/TestSupport.java deleted file mode 100644 index 4ab9afe4..00000000 --- a/trunk/support/src/test/java/org/wamblee/test/TestSupport.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright 2005 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.test; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; - -import junit.framework.Assert; - -/** - * @author Erik Test support utility. - * - * @author Erik Brakkee - */ -public final class TestSupport { - - /** - * Disabled constructor. - * - */ - private TestSupport() { - // Empty - } - - /** - * Obtain root directory of JUnit tests. - * - * @return Directory name. - */ - public static File getTestRootDir() { - return new File("testdata"); - } - - /** - * Returns a temporary directory. - * - * @return Temporary directory. - */ - public static File getTmpDir() { - return new File(getTestRootDir(), "tmpdir"); - } - - /** - * Recursively remove a directory. - * - * @param aSrc - * Directoryto remove. - */ - public static void removeDir(File aSrc) { - if (!aSrc.exists()) { - return; - } - Assert.assertTrue(aSrc.getPath(), aSrc.isDirectory()); - File[] files = aSrc.listFiles(); - for (int i = 0; i < files.length; i++) { - File file = files[i]; - if (file.isDirectory()) { - removeDir(file); - } else { - Assert.assertTrue(file.getPath(), file.delete()); - } - } - Assert.assertTrue(aSrc.getPath(), aSrc.delete()); - } - - /** - * Recursively copy a directory. - * - * @param aSrc - * Source directory - * @param aTarget - * Target directory. - */ - public static void copyDir(File aSrc, File aTarget) { - Assert.assertTrue(aSrc.isDirectory()); - Assert.assertTrue(!aTarget.exists()); - - aTarget.mkdirs(); - - File[] files = aSrc.listFiles(); - for (int i = 0; i < files.length; i++) { - File file = files[i]; - if (file.isDirectory()) { - if (!file.getName().equals(".svn")) { - copyDir(new File(aSrc, file.getName()), new File(aTarget, - file.getName())); - } - } else { - copyFile(file, new File(aTarget, file.getName())); - } - } - } - - /** - * Copy a file. If copying fails then the testcase will fail. - * - * @param aSrc - * Source file. - * @param aTarget - * Target file. - */ - public static void copyFile(File aSrc, File aTarget) { - - try { - FileInputStream fis = new FileInputStream(aSrc); - FileOutputStream fos = new FileOutputStream(aTarget); - FileChannel fcin = fis.getChannel(); - FileChannel fcout = fos.getChannel(); - - // map input file - - MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0, - fcin.size()); - - // do the file copy - fcout.write(mbb); - - // finish up - - fcin.close(); - fcout.close(); - fis.close(); - fos.close(); - } catch (IOException e) { - Assert.assertTrue("Copying file " + aSrc.getPath() + " to " - + aTarget.getPath() + " failed.", false); - } - } - - /** - * Remove a file or directory. The test case will fail if this does not - * succeed. - * - * @param aFile - * entry to remove. - */ - public static void delete(File aFile) { - Assert - .assertTrue("Could not delete " + aFile.getPath(), aFile - .delete()); - } - - /** - * Remove all files within a given directory including the directory itself. - * This only attempts to remove regular files and not directories within the - * directory. If the directory contains a nested directory, the deletion - * will fail. The test case will fail if this fails. - * - * @param aDir - * Directory to remove. - */ - public static void deleteDir(File aDir) { - cleanDir(aDir); - delete(aDir); - } - - /** - * Remove all regular files within a given directory. - * - * @param outputDirName - */ - public static void cleanDir(File aDir) { - if (!aDir.exists()) { - return; // nothing to do. - } - File[] entries = aDir.listFiles(); - for (int i = 0; i < entries.length; i++) { - File file = entries[i]; - if (file.isFile()) { - Assert.assertTrue("Could not delete " + entries[i].getPath(), - entries[i].delete()); - } - } - } - - /** - * Creates directory if it does not already exist. The test case will fail - * if the directory cannot be created. - * - * @param aDir - * Directory to create. - */ - public static void createDir(File aDir) { - if (aDir.isDirectory()) { - return; // nothing to do. - } - Assert.assertTrue("Could not create directory " + aDir.getPath(), aDir - .mkdirs()); - } -}