X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=support%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fio%2FFileSystemUtils.java;h=ad3b4e208d27e51960b18c41ed93c939353da2a9;hb=a327761c480f8f127e3663bc5886aca39d1a0682;hp=3d0f3b3b164cb8744355b4b905791230d3c70355;hpb=f7c3218ce2a75ce5ce287191d2ea54e850eb7559;p=utils diff --git a/support/src/test/java/org/wamblee/io/FileSystemUtils.java b/support/src/test/java/org/wamblee/io/FileSystemUtils.java index 3d0f3b3b..ad3b4e20 100644 --- a/support/src/test/java/org/wamblee/io/FileSystemUtils.java +++ b/support/src/test/java/org/wamblee/io/FileSystemUtils.java @@ -36,205 +36,225 @@ import org.apache.commons.logging.LogFactory; /** * File system utilities. - * + * * @author Erik Brakkee */ public final class FileSystemUtils { - private static final Log LOG = LogFactory.getLog(FileSystemUtils.class); - - /** - * Test output directory relative to the sub project. - */ - private static final String TEST_OUTPUT_DIR = "../target/testoutput"; - - /** - * Test input directory relative to the sub project. - */ - private static final String TEST_INPUT_DIR = "../src/test/resources"; - - /* - * Disabled. - * - */ - private FileSystemUtils() { - // Empty - } - - /** - * Deletes a directory recursively. The test case will fail if the directory - * does not exist or if deletion fails. - * - * @param aDir - * Directory to delete. - */ - public static void deleteDirRecursively(String aDir) { - deleteDirRecursively(new File(aDir)); - } - - /** - * Deletes a directory recursively. See {@link #deleteDirRecursively}. - * - * @param aDir - * Directory. - */ - public static void deleteDirRecursively(File aDir) { - TestCase.assertTrue(aDir.isDirectory()); - - for (File file : aDir.listFiles()) { - if (file.isDirectory()) { - deleteDirRecursively(file); - } else { - delete(file); - } - } - - delete(aDir); - } - - /** - * Deletes a file or directory. The test case will fail if the file or - * directory does not exist or if deletion fails. Deletion of a non-empty - * directory will always fail. - * - * @param aFile - * File or directory to delete. - */ - public static void delete(File aFile) { - TestCase.assertTrue(aFile.delete()); - } - - /** - * Gets a path relative to a sub project. This utility should be used to - * easily access file paths within a subproject without requiring any - * specific Eclipse configuration. - * - * @param aRelativePath - * Relative path. - * @param aTestClass - * Test class. - */ - public static File getPath(String aRelativePath, Class aTestClass) { - CodeSource source = aTestClass.getProtectionDomain().getCodeSource(); - if (source == null) { - LOG.warn("Could not obtain path for '" + aRelativePath - + "' for class " + aTestClass - + ", using relative path as is"); - return new File(aRelativePath); - } - URL location = source.getLocation(); - String protocol = location.getProtocol(); - if (!protocol.equals("file")) { - LOG.warn("protocol is not 'file': " + location); - return new File(aRelativePath); - } - - String path = location.getPath(); - try { - path = URLDecoder.decode(location.getPath(), "UTF-8"); - } catch (UnsupportedEncodingException e) { - // ignore it.. just don't decode - LOG.warn("Decoding path failed: '" + location.getPath() + "'", e ); - } - - return new File(new File(path).getParentFile(), aRelativePath); - } - - /** - * Ensures that a directory hierarchy exists (recursively if needed). If it - * is not possible to create the directory, then the test case will fail. - * - * @param aDir - * Directory to create. - */ - public static void createDir(File aDir) { - if (aDir.exists() && !aDir.isDirectory()) { - TestCase.fail("'" + aDir - + "' already exists and is not a directory"); - } - if (aDir.exists()) { - return; - } - createDir(aDir.getParentFile()); - TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir()); - } - - /** - * Gets the test output directory for a specific test class. - * - * @param aTestClass - * Test class. - * @return Test output directory. - */ - public static File getTestOutputDir(Class aTestClass) { - File file = getPath(TEST_OUTPUT_DIR, aTestClass); - String packageName = aTestClass.getPackage().getName(); - String packagePath = packageName.replaceAll("\\.", "/"); - return new File(file, packagePath); - } - - /** - * Gets the test input directory for a specific test class. - * - * @param aTestClass - * Test class. - * @return Test input directory. - */ - public static File getTestInputDir(Class aTestClass) { - File file = getPath(TEST_INPUT_DIR, aTestClass); - String packageName = aTestClass.getPackage().getName(); - String packagePath = packageName.replaceAll("\\.", "/"); - return new File(file, packagePath); - } - - /** - * Creates a directory hierarchy for the output directory of a test class if - * needed. - * - * @param aTestClass - * Test class - * @return Test directory. - */ - public static File createTestOutputDir(Class aTestClass) { - File file = getTestOutputDir(aTestClass); - createDir(file); - return file; - } - - /** - * Gets a test output file name. This returns a File object representing the - * output file and ensures that the directory where the file will be created - * already exists. - * - * @param aName - * Name of the file. - * @param aTestClass - * Test class. - * @return File. - */ - public static File getTestOutputFile(String aName, Class aTestClass) { - File file = new File(getTestOutputDir(aTestClass), aName); - createDir(file.getParentFile()); - return file; - } - - public static String read(InputStream aIs) throws IOException { - try { - StringBuffer buffer = new StringBuffer(); - int c; - while ((c = aIs.read()) != -1) { - buffer.append((char)c); - } - return buffer.toString(); - } finally { - aIs.close(); - } - } - - /** - * Copies an input stream to an output stream. - * @param aIs Input stream. - * @param aOs Output stream. + private static final Log LOG = LogFactory.getLog(FileSystemUtils.class); + + /** + * Test output directory relative to the sub project. + */ + private static final String TEST_OUTPUT_DIR = "../target/testoutput"; + + /** + * Test input directory relative to the sub project. + */ + private static final String TEST_INPUT_DIR = "../src/test/resources"; + + /* + * Disabled. + * + */ + private FileSystemUtils() { + // Empty + } + + /** + * Deletes a directory recursively. The test case will fail if the directory + * does not exist or if deletion fails. + * + * @param aDir + * Directory to delete. + */ + public static void deleteDirRecursively(String aDir) { + deleteDirRecursively(new File(aDir)); + } + + /** + * Deletes a directory recursively. See {@link #deleteDirRecursively}. + * + * @param aDir + * Directory. + */ + public static void deleteDirRecursively(File aDir) { + TestCase.assertTrue(aDir.isDirectory()); + + for (File file : aDir.listFiles()) { + if (file.isDirectory()) { + deleteDirRecursively(file); + } else { + delete(file); + } + } + + delete(aDir); + } + + /** + * Deletes a file or directory. The test case will fail if the file or + * directory does not exist or if deletion fails. Deletion of a non-empty + * directory will always fail. + * + * @param aFile + * File or directory to delete. + */ + public static void delete(File aFile) { + TestCase.assertTrue(aFile.delete()); + } + + /** + * Gets a path relative to a sub project. This utility should be used to + * easily access file paths within a subproject without requiring any + * specific Eclipse configuration. + * + * @param aRelativePath + * Relative path. + * @param aTestClass + * Test class. + */ + public static File getPath(String aRelativePath, Class aTestClass) { + CodeSource source = aTestClass.getProtectionDomain().getCodeSource(); + if (source == null) { + LOG.warn("Could not obtain path for '" + aRelativePath + + "' for class " + aTestClass + + ", using relative path as is"); + return new File(aRelativePath); + } + URL location = source.getLocation(); + String protocol = location.getProtocol(); + if (!protocol.equals("file")) { + LOG.warn("protocol is not 'file': " + location); + return new File(aRelativePath); + } + + String path = location.getPath(); + try { + path = URLDecoder.decode(location.getPath(), "UTF-8"); + } catch (UnsupportedEncodingException e) { + // ignore it.. just don't decode + LOG.warn("Decoding path failed: '" + location.getPath() + "'", e); + } + + return new File(new File(path).getParentFile(), aRelativePath); + } + + /** + * Ensures that a directory hierarchy exists (recursively if needed). If it + * is not possible to create the directory, then the test case will fail. + * + * @param aDir + * Directory to create. + */ + public static void createDir(File aDir) { + if (aDir.exists() && !aDir.isDirectory()) { + TestCase.fail("'" + aDir + + "' already exists and is not a directory"); + } + if (aDir.exists()) { + return; + } + createDir(aDir.getParentFile()); + TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir()); + } + + /** + * Creates a file in a directory. + * @param aDir Directory path. Will be created if it does not exist. + * @param aName Filename. + * @param aContents Contents. + */ + public static void createFile(File aDir, String aName, InputStream aContents) { + createDir(aDir); + try { + OutputStream os = new FileOutputStream(new File(aDir, aName)); + copyStream(aContents, os); + } catch (IOException e) { + e.printStackTrace(); + TestCase.fail(e.getMessage()); + } + } + + /** + * Gets the test output directory for a specific test class. + * + * @param aTestClass + * Test class. + * @return Test output directory. + */ + public static File getTestOutputDir(Class aTestClass) { + File file = getPath(TEST_OUTPUT_DIR, aTestClass); + String className = aTestClass.getName(); + String classRelPath = className.replaceAll("\\.", "/"); + return new File(file, classRelPath); + } + + /** + * Gets the test input directory for a specific test class. + * + * @param aTestClass + * Test class. + * @return Test input directory. + */ + public static File getTestInputDir(Class aTestClass) { + File file = getPath(TEST_INPUT_DIR, aTestClass); + String packageName = aTestClass.getPackage().getName(); + String packagePath = packageName.replaceAll("\\.", "/"); + return new File(file, packagePath); + } + + /** + * Creates a directory hierarchy for the output directory of a test class if + * needed. + * + * @param aTestClass + * Test class + * @return Test directory. + */ + public static File createTestOutputDir(Class aTestClass) { + File file = getTestOutputDir(aTestClass); + createDir(file); + return file; + } + + /** + * Gets a test output file name. This returns a File object representing the + * output file and ensures that the directory where the file will be created + * already exists. + * + * @param aName + * Name of the file. + * @param aTestClass + * Test class. + * @return File. + */ + public static File getTestOutputFile(String aName, Class aTestClass) { + File file = new File(getTestOutputDir(aTestClass), aName); + createDir(file.getParentFile()); + return file; + } + + public static String read(InputStream aIs) throws IOException { + try { + StringBuffer buffer = new StringBuffer(); + int c; + while ((c = aIs.read()) != -1) { + buffer.append((char) c); + } + return buffer.toString(); + } finally { + 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 { @@ -243,7 +263,7 @@ public final class FileSystemUtils { aOs.write(c); } aIs.close(); - aOs.close(); + aOs.close(); } catch (IOException e) { e.printStackTrace(); Assert.fail(e.getMessage()); @@ -313,7 +333,7 @@ public final class FileSystemUtils { + 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 @@ -327,7 +347,7 @@ public final class FileSystemUtils { cleanDir(aDir); delete(aDir); } - + /** * Remove all regular files within a given directory. *