X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fio%2FFileSystemUtils.java;h=bf8019808691dcaae46463cb9628a60d89fa40a2;hb=4a575582a5c2999bd816b197d9cf274b4b3ddcd7;hp=fd507caf41084334ed746ed6fa55a98dfcbe971e;hpb=ddd261f331280640c5b53c7128230b629ebcd268;p=utils diff --git a/support/general/src/test/java/org/wamblee/io/FileSystemUtils.java b/support/general/src/test/java/org/wamblee/io/FileSystemUtils.java index fd507caf..bf801980 100644 --- a/support/general/src/test/java/org/wamblee/io/FileSystemUtils.java +++ b/support/general/src/test/java/org/wamblee/io/FileSystemUtils.java @@ -1,12 +1,12 @@ /* - * Copyright 2006 the original author or authors. - * + * Copyright 2005-2010 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. @@ -15,12 +15,6 @@ */ package org.wamblee.io; -import junit.framework.Assert; -import junit.framework.TestCase; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -28,23 +22,25 @@ 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 java.util.logging.Level; +import java.util.logging.Logger; +import junit.framework.Assert; +import junit.framework.TestCase; /** * File system utilities. - * + * * @author Erik Brakkee */ public final class FileSystemUtils { - private static final Log LOG = LogFactory.getLog(FileSystemUtils.class); + private static final Logger LOG = Logger.getLogger(FileSystemUtils.class + .getName()); /** * Test output directory relative to the sub project. @@ -58,7 +54,6 @@ public final class FileSystemUtils { /* * Disabled. - * */ private FileSystemUtils() { // Empty @@ -67,7 +62,7 @@ public final class FileSystemUtils { /** * Deletes a directory recursively. The test case will fail if the directory * does not exist or if deletion fails. - * + * * @param aDir * Directory to delete. */ @@ -77,7 +72,7 @@ public final class FileSystemUtils { /** * Deletes a directory recursively. See {@link #deleteDirRecursively}. - * + * * @param aDir * Directory. */ @@ -99,7 +94,7 @@ public final class FileSystemUtils { * 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. */ @@ -111,7 +106,7 @@ public final class FileSystemUtils { * 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 @@ -121,7 +116,7 @@ public final class FileSystemUtils { CodeSource source = aTestClass.getProtectionDomain().getCodeSource(); if (source == null) { - LOG.warn("Could not obtain path for '" + aRelativePath + + LOG.warning("Could not obtain path for '" + aRelativePath + "' for class " + aTestClass + ", using relative path as is"); return new File(aRelativePath); @@ -131,7 +126,7 @@ public final class FileSystemUtils { String protocol = location.getProtocol(); if (!protocol.equals("file")) { - LOG.warn("protocol is not 'file': " + location); + LOG.warning("protocol is not 'file': " + location); return new File(aRelativePath); } @@ -142,7 +137,8 @@ public final class FileSystemUtils { path = URLDecoder.decode(location.getPath(), "UTF-8"); } catch (UnsupportedEncodingException e) { // ignore it.. just don't decode - LOG.warn("Decoding path failed: '" + location.getPath() + "'", e); + LOG.log(Level.WARNING, "Decoding path failed: '" + + location.getPath() + "'", e); } return new File(new File(path).getParentFile(), aRelativePath); @@ -151,7 +147,7 @@ public final class FileSystemUtils { /** * 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. */ @@ -171,9 +167,13 @@ public final class FileSystemUtils { /** * Creates a file in a directory. - * @param aDir Directory path. Will be created if it does not exist. - * @param aName Filename. - * @param aContents Contents. + * + * @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); @@ -189,7 +189,7 @@ public final class FileSystemUtils { /** * Gets the test output directory for a specific test class. - * + * * @param aTestClass * Test class. * @return Test output directory. @@ -204,7 +204,7 @@ public final class FileSystemUtils { /** * Gets the test input directory for a specific test class. - * + * * @param aTestClass * Test class. * @return Test input directory. @@ -220,7 +220,7 @@ public final class FileSystemUtils { /** * Creates a directory hierarchy for the output directory of a test class if * needed. - * + * * @param aTestClass * Test class * @return Test directory. @@ -236,7 +236,7 @@ public final class FileSystemUtils { * 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 @@ -267,7 +267,7 @@ public final class FileSystemUtils { /** * Copies an input stream to an output stream. - * + * * @param aIs * Input stream. * @param aOs @@ -291,7 +291,7 @@ public final class FileSystemUtils { /** * Recursively copy a directory. - * + * * @param aSrc * Source directory * @param aTarget @@ -301,7 +301,9 @@ public final class FileSystemUtils { Assert.assertTrue(aSrc.isDirectory()); Assert.assertTrue(!aTarget.exists()); - aTarget.mkdirs(); + if (!aTarget.mkdirs()) { + Assert.fail("Could not create target directory '" + aTarget + "'"); + } File[] files = aSrc.listFiles(); @@ -310,8 +312,8 @@ public final class FileSystemUtils { if (file.isDirectory()) { if (!file.getName().equals(".svn")) { - copyDir(new File(aSrc, file.getName()), - new File(aTarget, file.getName())); + copyDir(new File(aSrc, file.getName()), new File(aTarget, + file.getName())); } } else { copyFile(file, new File(aTarget, file.getName())); @@ -321,7 +323,7 @@ public final class FileSystemUtils { /** * Copy a file. If copying fails then the testcase will fail. - * + * * @param aSrc * Source file. * @param aTarget @@ -336,7 +338,7 @@ public final class FileSystemUtils { // map input file MappedByteBuffer mbb = fcin.map(FileChannel.MapMode.READ_ONLY, 0, - fcin.size()); + fcin.size()); // do the file copy fcout.write(mbb); @@ -357,7 +359,7 @@ public final class FileSystemUtils { * 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. */ @@ -368,7 +370,7 @@ public final class FileSystemUtils { /** * Remove all regular files within a given directory. - * + * * @param outputDirName */ public static void cleanDir(File aDir) {