diff --git a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
index f1947827..4612b37f 100644
--- a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
+++ b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
@@ -19,6 +19,7 @@
* under the License.
*/
+import org.apache.commons.io.IOUtils;
import org.apache.maven.shared.utils.Os;
import org.apache.maven.shared.utils.StringUtils;
@@ -52,13 +53,14 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Objects;
import java.util.Random;
/**
* This class provides basic facilities for manipulating files and file paths.
- *
+ *
* Path-related methods
- *
+ *
* Methods exist to retrieve the components of a typical file path. For example
* /www/hosted/mysite/index.html
, can be broken into:
*
@@ -68,13 +70,12 @@
*
*
* File-related methods
- *
+ *
* There are methods to create a {@link #toFile File from a URL}, copy a
* {@link #copyFile File to another File},
* copy a {@link #copyURLToFile URL's contents to a File},
* as well as methods to {@link #deleteDirectory(File) delete} and {@link #cleanDirectory(File)
* clean} a directory.
- *
*
* Common {@link java.io.File} manipulation routines.
*
@@ -89,7 +90,6 @@
* @author Christoph.Reck
* @author Peter Donald
* @author Jeff Turner
- *
*/
public class FileUtils
{
@@ -633,7 +633,7 @@ public static boolean contentEquals( @Nonnull final File file1, @Nonnull final F
try ( InputStream input1 = new FileInputStream( file1 );
InputStream input2 = new FileInputStream( file2 ) )
{
- return IOUtil.contentEquals( input1, input2 );
+ return IOUtils.contentEquals( input1, input2 );
}
}
@@ -750,7 +750,7 @@ public static void copyFileToDirectory( @Nonnull final File source, @Nonnull fin
{
if ( destinationDirectory.exists() && !destinationDirectory.isDirectory() )
{
- throw new IllegalArgumentException( "Destination is not a directory" );
+ throw new IOException( "Destination is not a directory" );
}
copyFile( source, new File( destinationDirectory, source.getName() ) );
@@ -1022,9 +1022,9 @@ private static void copyStreamToFile( @Nonnull @WillClose final InputStream sour
}
/**
- * Resolve a file filename
to it's canonical form. If filename
is
- * relative (doesn't start with /
), it will be resolved relative to
- * baseFile
, otherwise it is treated as a normal root-relative path.
+ * Resolve a file filename
to its canonical form. If filename
is
+ * relative (doesn't start with /
), it is resolved relative to
+ * baseFile
. Otherwise it is treated as a normal root-relative path.
*
* @param baseFile where to resolve filename
from, if filename
is relative
* @param filename absolute or relative file path to resolve
@@ -1605,14 +1605,30 @@ public static List getFiles( @Nonnull File directory, @Nullable String inc
/**
* Copy the contents of a directory into another one.
*
- * @param sourceDirectory the source directory
- * @param destinationDirectory the target directory
+ * @param sourceDirectory the source directory. If the source does not exist,
+ * the method simply returns.
+ * @param destinationDirectory the target directory; will be created if it doesn't exist
* @throws IOException if any
+ * @deprecated use {@code org.apache.commons.io.FileUtils.copyDirectory()}
*/
+ @Deprecated
public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory )
throws IOException
{
- copyDirectory( sourceDirectory, destinationDirectory, "**", null );
+ Objects.requireNonNull( sourceDirectory );
+ Objects.requireNonNull( destinationDirectory );
+ if ( destinationDirectory.equals( sourceDirectory ) )
+ {
+ throw new IOException( "Can't copy directory " + sourceDirectory + " to itself." );
+ }
+ else if ( !destinationDirectory.exists() )
+ {
+ if ( !destinationDirectory.mkdirs() )
+ {
+ throw new IOException( "Can't create directory " + destinationDirectory );
+ }
+ }
+ copyDirectoryStructure( sourceDirectory, destinationDirectory );
}
/**
@@ -1622,9 +1638,11 @@ public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File d
* @param destinationDirectory the target directory
* @param includes Ant include pattern
* @param excludes Ant exclude pattern
- * @throws IOException if any
+ * @throws IOException if the source is a file or cannot be copied
* @see #getFiles(File, String, String)
+ * @deprecated use {@code org.apache.commons.io.FileUtils.copyDirectory()}
*/
+ @Deprecated
public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory,
@Nullable String includes, @Nullable String excludes )
throws IOException
@@ -1633,6 +1651,10 @@ public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File d
{
return;
}
+ else if ( !sourceDirectory.isDirectory() )
+ {
+ throw new IOException( sourceDirectory + " is not a directory." );
+ }
List files = getFiles( sourceDirectory, includes, excludes );
@@ -1651,8 +1673,8 @@ public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File d
* - The
sourceDirectory
must exist.
*
*
- * @param sourceDirectory the source dir
- * @param destinationDirectory the target dir
+ * @param sourceDirectory the existing directory to be copied
+ * @param destinationDirectory the new directory to be created
* @throws IOException if any
* @deprecated use {@code org.apache.commons.io.FileUtils.copyDirectory()}
*/
@@ -1686,7 +1708,7 @@ private static void copyDirectoryStructure( @Nonnull File sourceDirectory, @Nonn
if ( !sourceDirectory.exists() )
{
- throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
+ throw new IOException( "Source directory doesn't exist (" + sourceDirectory.getAbsolutePath() + ")." );
}
File[] files = sourceDirectory.listFiles();
@@ -2123,7 +2145,7 @@ public static boolean isSymbolicLinkForSure( @Nonnull final File file )
* @return the linked file
* @throws IOException in case of an error
* @see {@code java.nio.file.Files.createSymbolicLink(Path)} which creates a new
- * symbolic link but does not replace exsiting symbolic links
+ * symbolic link but does not replace existing symbolic links
*/
@Nonnull public static File createSymbolicLink( @Nonnull File symlink, @Nonnull File target )
throws IOException
diff --git a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
index c692a38a..4dfa4fa4 100644
--- a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
@@ -122,7 +122,7 @@ public void setUp()
createFile( testFile2, testFile2Size );
}
- void createFile( File file, long size )
+ private static void createFile( File file, long size )
throws IOException
{
if ( !file.getParentFile().exists() )
@@ -140,7 +140,7 @@ void createFile( File file, long size )
/**
* Assert that the content of a file is equal to that in a byte[].
*/
- void assertEqualContent( byte[] b0, File file )
+ private void assertEqualContent( byte[] b0, File file )
throws IOException
{
int count = 0, numRead = 0;
@@ -155,16 +155,16 @@ void assertEqualContent( byte[] b0, File file )
assertThat( "Different number of bytes: ", count, is( b0.length ) );
for ( int i = 0; i < count; i++ )
{
- assertThat( "byte " + i + " differs", b1[i], is( b0[i] ) );
+ assertEquals( "byte " + i + " differs", b1[i], b0[i] );
}
}
}
- void deleteFile( File file )
+ private void deleteFile( File file )
{
if ( file.exists() )
{
- assertThat( "Couldn't delete file: " + file, file.delete(), is( true ) );
+ assertTrue( "Couldn't delete file: " + file, file.delete() );
}
}
@@ -784,9 +784,7 @@ public void copyFile2()
}
@Test
- @Ignore( "Commons test case that is failing for plexus" )
- public void copyToSelf()
- throws Exception
+ public void copyToSelf() throws IOException
{
File destination = new File( tempFolder.getRoot(), "copy3.txt" );
//Prepare a test file
@@ -796,33 +794,6 @@ public void copyToSelf()
}
@Test
- @Ignore( "Commons test case that is failing for plexus" )
- public void copyDirectoryToDirectory_NonExistingDest()
- throws Exception
- {
- createFile( testFile1, 1234 );
- createFile( testFile2, 4321 );
- File srcDir = tempFolder.getRoot();
- File subDir = new File( srcDir, "sub" );
- subDir.mkdir();
- File subFile = new File( subDir, "A.txt" );
- FileUtils.fileWrite( subFile, "UTF8", "HELLO WORLD" );
- File destDir = new File( System.getProperty( "java.io.tmpdir" ), "tmp-FileUtilsTestCase" );
- FileUtils.deleteDirectory( destDir );
- File actualDestDir = new File( destDir, srcDir.getName() );
-
- FileUtils.copyDirectory( srcDir, destDir );
-
- assertThat( "Check exists", destDir.exists(), is( true ) );
- assertThat( "Check exists", actualDestDir.exists(), is( true ) );
- assertThat( "Check size", FileUtils.sizeOfDirectory( actualDestDir ),
- is( FileUtils.sizeOfDirectory( srcDir ) ) );
- assertThat( new File( actualDestDir, "sub/A.txt" ).exists(), is( true ) );
- FileUtils.deleteDirectory( destDir );
- }
-
- @Test
- @Ignore( "Commons test case that is failing for plexus" )
public void copyDirectoryToNonExistingDest()
throws Exception
{
@@ -838,130 +809,34 @@ public void copyDirectoryToNonExistingDest()
FileUtils.copyDirectory( srcDir, destDir );
- assertThat( "Check exists", destDir.exists(), is( true ) );
- assertThat( "Check size", FileUtils.sizeOfDirectory( destDir ), is( FileUtils.sizeOfDirectory( srcDir ) ) );
- assertThat( new File( destDir, "sub/A.txt" ).exists(), is( true ) );
+ assertTrue( destDir.exists() );
+ assertEquals( FileUtils.sizeOfDirectory( destDir ), FileUtils.sizeOfDirectory( srcDir ) );
+ assertTrue( new File( destDir, "sub/A.txt" ).exists() );
FileUtils.deleteDirectory( destDir );
}
@Test
- @Ignore( "Commons test case that is failing for plexus" )
- public void copyDirectoryToExistingDest()
- throws Exception
+ public void copyDirectoryToExistingDest() throws IOException
{
createFile( testFile1, 1234 );
createFile( testFile2, 4321 );
File srcDir = tempFolder.getRoot();
File subDir = new File( srcDir, "sub" );
- subDir.mkdir();
+ assertTrue( subDir.mkdir() );
File subFile = new File( subDir, "A.txt" );
FileUtils.fileWrite( subFile, "UTF8", "HELLO WORLD" );
File destDir = new File( System.getProperty( "java.io.tmpdir" ), "tmp-FileUtilsTestCase" );
FileUtils.deleteDirectory( destDir );
- destDir.mkdirs();
+ assertTrue ( destDir.mkdirs() );
FileUtils.copyDirectory( srcDir, destDir );
- assertThat( FileUtils.sizeOfDirectory( destDir ), is( FileUtils.sizeOfDirectory( srcDir ) ) );
- assertThat( new File( destDir, "sub/A.txt" ).exists(), is( true ) );
- }
-
- /**
- * Test for IO-141
- */
- @Test
- @Ignore( "Commons test case that is failing for plexus" )
- public void copyDirectoryToChild()
- throws Exception
- {
- File grandParentDir = new File( tempFolder.getRoot(), "grandparent" );
- File parentDir = new File( grandParentDir, "parent" );
- File childDir = new File( parentDir, "child" );
- createFilesForTestCopyDirectory( grandParentDir, parentDir, childDir );
-
- long expectedCount =
- FileUtils.getFileAndDirectoryNames( grandParentDir, null, null, true, true, true, true ).size()
- + FileUtils.getFileAndDirectoryNames( parentDir, null, null, true, true, true, true ).size();
- long expectedSize = FileUtils.sizeOfDirectory( grandParentDir ) + FileUtils.sizeOfDirectory( parentDir );
- FileUtils.copyDirectory( parentDir, childDir );
- assertThat(
- 1L * FileUtils.getFileAndDirectoryNames( grandParentDir, null, null, true, true, true, true ).size(),
- is( expectedCount ) );
- assertThat( FileUtils.sizeOfDirectory( grandParentDir ), is( expectedSize ) );
- }
-
- /**
- * Test for IO-141
- */
- @Test
- @Ignore( "Commons test case that is failing for plexus" )
- public void copyDirectoryToGrandChild()
- throws Exception
- {
- File grandParentDir = new File( tempFolder.getRoot(), "grandparent" );
- File parentDir = new File( grandParentDir, "parent" );
- File childDir = new File( parentDir, "child" );
- createFilesForTestCopyDirectory( grandParentDir, parentDir, childDir );
-
- long expectedCount =
- ( FileUtils.getFileAndDirectoryNames( grandParentDir, null, null, true, true, true, true ).size() * 2 );
- long expectedSize = ( FileUtils.sizeOfDirectory( grandParentDir ) * 2 );
- FileUtils.copyDirectory( grandParentDir, childDir );
- assertThat(
- 1L * FileUtils.getFileAndDirectoryNames( grandParentDir, null, null, true, true, true, true ).size(),
- is( expectedCount ) );
- assertThat( FileUtils.sizeOfDirectory( grandParentDir ), is( expectedSize ) );
- }
-
- /**
- * Test for IO-217 FileUtils.copyDirectoryToDirectory makes infinite loops
- */
- @Test
- public void copyDirectoryToItself()
- throws Exception
- {
- File dir = new File( tempFolder.getRoot(), "itself" );
- dir.mkdirs();
- FileUtils.copyDirectory( dir, dir );
- assertThat( FileUtils.getFileAndDirectoryNames( dir, null, null, true, true, true, true ).size(), is( 1 ) );
- }
-
- private void createFilesForTestCopyDirectory( File grandParentDir, File parentDir, File childDir )
- throws Exception
- {
- File childDir2 = new File( parentDir, "child2" );
- File grandChildDir = new File( childDir, "grandChild" );
- File grandChild2Dir = new File( childDir2, "grandChild2" );
- File file1 = new File( grandParentDir, "file1.txt" );
- File file2 = new File( parentDir, "file2.txt" );
- File file3 = new File( childDir, "file3.txt" );
- File file4 = new File( childDir2, "file4.txt" );
- File file5 = new File( grandChildDir, "file5.txt" );
- File file6 = new File( grandChild2Dir, "file6.txt" );
- FileUtils.deleteDirectory( grandParentDir );
- grandChildDir.mkdirs();
- grandChild2Dir.mkdirs();
- FileUtils.fileWrite( file1, "UTF8", "File 1 in grandparent" );
- FileUtils.fileWrite( file2, "UTF8", "File 2 in parent" );
- FileUtils.fileWrite( file3, "UTF8", "File 3 in child" );
- FileUtils.fileWrite( file4, "UTF8", "File 4 in child2" );
- FileUtils.fileWrite( file5, "UTF8", "File 5 in grandChild" );
- FileUtils.fileWrite( file6, "UTF8", "File 6 in grandChild2" );
+ assertEquals( FileUtils.sizeOfDirectory( destDir ), FileUtils.sizeOfDirectory( srcDir ) );
+ assertTrue( new File( destDir, "sub/A.txt" ).exists() );
}
@Test
- @Ignore( "Commons test case that is failing for plexus" )
- public void copyDirectoryErrors()
- throws Exception
- {
- try
- {
- FileUtils.copyDirectory( null, null );
- fail();
- }
- catch ( NullPointerException ex )
- {
- }
+ public void copyDirectoryErrors_nullDestination() throws IOException {
try
{
FileUtils.copyDirectory( new File( "a" ), null );
@@ -970,41 +845,43 @@ public void copyDirectoryErrors()
catch ( NullPointerException ex )
{
}
+ }
+
+ @Test
+ public void copyDirectoryErrors_copyToSelf() {
try
{
- FileUtils.copyDirectory( null, new File( "a" ) );
- fail();
- }
- catch ( NullPointerException ex )
- {
- }
- try
- {
- FileUtils.copyDirectory( new File( "doesnt-exist" ), new File( "a" ) );
+ FileUtils.copyDirectory( tempFolder.getRoot(), tempFolder.getRoot() );
fail();
}
catch ( IOException ex )
{
}
+ }
+
+ @Test
+ public void copyDirectoryErrors()
+ throws IOException
+ {
try
{
- FileUtils.copyDirectory( testFile1, new File( "a" ) );
+ FileUtils.copyDirectory( null, null );
fail();
}
- catch ( IOException ex )
+ catch ( NullPointerException ex )
{
}
try
{
- FileUtils.copyDirectory( tempFolder.getRoot(), testFile1 );
+ FileUtils.copyDirectory( null, new File( "a" ) );
fail();
}
- catch ( IOException ex )
+ catch ( NullPointerException ex )
{
}
try
{
- FileUtils.copyDirectory( tempFolder.getRoot(), tempFolder.getRoot() );
+ FileUtils.copyDirectory( tempFolder.getRoot(), testFile1 );
fail();
}
catch ( IOException ex )
@@ -1020,9 +897,9 @@ public void forceDeleteAFile1()
{
File destination = new File( tempFolder.getRoot(), "copy1.txt" );
destination.createNewFile();
- assertThat( "Copy1.txt doesn't exist to delete", destination.exists(), is( true ) );
+ assertTrue( "Copy1.txt doesn't exist to delete", destination.exists() );
FileUtils.forceDelete( destination );
- assertThat( "Check No Exist", !destination.exists(), is( true ) );
+ assertFalse( destination.exists() );
}
@Test