X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Ftest%2Forg%2Fwamblee%2Ftest%2FTestSupport.java;h=c10c24b33296faf95df85187e6b41a7991fb7ec7;hb=5b6b49fbe01397461e67238481c84a62c8ed4bef;hp=4cb23e3b3f3b4897e7c971c28517731d43576b02;hpb=a9a399fb3921ce5dca15cf85cd06f523724a7db1;p=utils diff --git a/support/test/org/wamblee/test/TestSupport.java b/support/test/org/wamblee/test/TestSupport.java index 4cb23e3b..c10c24b3 100644 --- a/support/test/org/wamblee/test/TestSupport.java +++ b/support/test/org/wamblee/test/TestSupport.java @@ -12,7 +12,7 @@ * 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; @@ -27,169 +27,179 @@ import junit.framework.Assert; /** * @author Erik Test support utility. */ -public class TestSupport { - - /** - * 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()); - } +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()); + } }