commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r609622 - in /commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/FileUtils.java src/test/org/apache/commons/io/FileUtilsTestCase.java
Date Mon, 07 Jan 2008 14:54:10 GMT
Author: niallp
Date: Mon Jan  7 06:54:07 2008
New Revision: 609622

URL: http://svn.apache.org/viewvc?rev=609622&view=rev
Log:
IO-77 add moveFile() and moveDirectory() methods to FileUtils - thanks to Nicolas de Loof
for the patch

Modified:
    commons/proper/io/trunk/RELEASE-NOTES.txt
    commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java

Modified: commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=609622&r1=609621&r2=609622&view=diff
==============================================================================
--- commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/io/trunk/RELEASE-NOTES.txt Mon Jan  7 06:54:07 2008
@@ -37,6 +37,7 @@
   - Infinite loop on FileUtils.copyDirectory when the destination directory is within
     the source directory [IO-141]
   - Add a copyDirectory() method that makes use of FileFilter [IO-105]
+  - Add moveDirectory() and moveFile() methods [IO-77]
 
 - HexDump
   - HexDump's use of static StringBuffers isn't thread-safe [IO-136]

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=609622&r1=609621&r2=609622&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Mon Jan  7 06:54:07
2008
@@ -1710,4 +1710,179 @@
         return checksum;
     }
 
+    /**
+     * Move a directory.
+     * <p>
+     * When the destination directory is on another file system, do a "copy and delete".
+     *
+     * @param srcDir the directory to be moved
+     * @param destDir the destination directory
+     * @throws NullPointerException if source or destination is <code>null</code>
+     * @throws IOException if source or destination is invalid
+     * @throws IOException if an IO error occurs moving the file
+     * @since Commons IO 1.4
+     */
+    public static void moveDirectory(File srcDir, File destDir) throws IOException {
+        if (srcDir == null) {
+            throw new NullPointerException("Source must not be null");
+        }
+        if (destDir == null) {
+            throw new NullPointerException("Destination must not be null");
+        }
+        if (!srcDir.exists()) {
+            throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
+        }
+        if (!srcDir.isDirectory()) {
+            throw new IOException("Source '" + srcDir + "' is not a directory");
+        }
+        if (destDir.exists()) {
+            throw new IOException("Destination '" + destDir + "' allready exists");
+        }
+        boolean rename = srcDir.renameTo(destDir);
+        if (!rename) {
+            copyDirectory( srcDir, destDir );
+            deleteDirectory( srcDir );
+            if (srcDir.exists()) {
+                throw new IOException("Failed to delete original directory '" + srcDir +
+                        "' after copy to '" + destDir + "'");
+            }
+        }
+    }
+
+    /**
+     * Move a directory to a directory.
+     *
+     * @param src the file to be moved
+     * @param destDir the destination file
+     * @param createDestDir If <code>true</code> create the destination directory,
+     * otherwise if <code>false</code> throw an IOException
+     * @throws NullPointerException if source or destination is <code>null</code>
+     * @throws IOException if source or destination is invalid
+     * @throws IOException if an IO error occurs moving the file
+     * @since Commons IO 1.4
+     */
+    public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir)
throws IOException {
+        if (src == null) {
+            throw new NullPointerException( "Source must not be null" );
+        }
+        if (destDir == null) {
+            throw new NullPointerException("Destination directory must not be null");
+        }
+        if (!destDir.exists() && createDestDir) {
+            destDir.mkdirs();
+        }
+        if (!destDir.exists()) {
+            throw new IOException("Destination directory does not exist");
+        }
+        if (!destDir.isDirectory()) {
+            throw new IOException("Destination '" + destDir + "' is not a directory");
+        }
+        moveDirectory(src, new File(destDir, src.getName()));
+    
+    }
+
+    /**
+     * Move a file.
+     * <p>
+     * When the destination file is on another file system, do a "copy and delete".
+     *
+     * @param srcFile the file to be moved
+     * @param destFile the destination file
+     * @throws NullPointerException if source or destination is <code>null</code>
+     * @throws IOException if source or destination is invalid
+     * @throws IOException if an IO error occurs moving the file
+     * @since Commons IO 1.4
+     */
+    public static void moveFile(File srcFile, File destFile) throws IOException {
+        if (srcFile == null) {
+            throw new NullPointerException( "Source must not be null" );
+        }
+        if (destFile == null) {
+            throw new NullPointerException("Destination must not be null");
+        }
+        if (!srcFile.exists()) {
+            throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
+        }
+        if (srcFile.isDirectory()) {
+            throw new IOException("Source '" + srcFile + "' is a directory");
+        }
+        if (destFile.exists()) {
+            throw new IOException("Destination '" + destFile + "' allready exists");
+        }
+        if (destFile.isDirectory()) {
+            throw new IOException("Destination '" + destFile + "' is a directory");
+        }
+        boolean rename = srcFile.renameTo(destFile);
+        if (!rename) {
+            copyFile( srcFile, destFile );
+            if (!srcFile.delete()) {
+                FileUtils.deleteQuietly(destFile);
+                throw new IOException("Failed to delete original file '" + srcFile +
+                        "' after copy to '" + destFile + "'");
+            }
+        }
+    }
+
+    /**
+     * Move a file to a directory.
+     *
+     * @param srcFile the file to be moved
+     * @param destDir the destination file
+     * @param createDestDir If <code>true</code> create the destination directory,
+     * otherwise if <code>false</code> throw an IOException
+     * @throws NullPointerException if source or destination is <code>null</code>
+     * @throws IOException if source or destination is invalid
+     * @throws IOException if an IO error occurs moving the file
+     * @since Commons IO 1.4
+     */
+    public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir)
throws IOException {
+        if (srcFile == null) {
+            throw new NullPointerException( "Source must not be null" );
+        }
+        if (destDir == null) {
+            throw new NullPointerException("Destination directory must not be null");
+        }
+        if (!destDir.exists() && createDestDir) {
+            destDir.mkdirs();
+        }
+        if (!destDir.exists()) {
+            throw new IOException("Destination directory does not exist");
+        }
+        if (!destDir.isDirectory()) {
+            throw new IOException("Destination '" + destDir + "' is not a directory");
+        }
+        moveFile(srcFile, new File(destDir, srcFile.getName()));
+    }
+
+    /**
+     * Move a file or directory to the destination directory.
+     * <p>
+     * When the destination is on another file system, do a "copy and delete".
+     *
+     * @param src the file or directory to be moved
+     * @param destDir the destination directory 
+     * @param createDestDir If <code>true</code> create the destination directory,
+     * otherwise if <code>false</code> throw an IOException
+     * @throws NullPointerException if source or destination is <code>null</code>
+     * @throws IOException if source or destination is invalid
+     * @throws IOException if an IO error occurs moving the file
+     * @since Commons IO 1.4
+     */
+    public static void moveToDirectory(File src, File destDir, boolean createDestDir) throws
IOException {
+        if (src == null) {
+            throw new NullPointerException( "Source must not be null" );
+        }
+        if (destDir == null) {
+            throw new NullPointerException("Destination must not be null");
+        }
+        if (!src.exists()) {
+            throw new FileNotFoundException("Source '" + src + "' does not exist");
+        }
+        if (src.isDirectory()) {
+            moveDirectoryToDirectory(src, destDir, createDestDir);
+        } else {
+            moveFileToDirectory(src, destDir, createDestDir);
+        }
+    }
+
 }

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=609622&r1=609621&r2=609622&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Mon Jan
 7 06:54:07 2008
@@ -1301,6 +1301,329 @@
         }
     }
 
+    public void testMoveFile_Rename() throws Exception {
+        File destination = new File( getTestDirectory(), "move1.txt" );
+
+        FileUtils.moveFile( testFile1, destination );
+        assertTrue( "Check Exist", destination.exists() );
+        assertTrue( "Original deleted", ! testFile1.exists() );
+    }
+
+    public void testMoveFile_CopyDelete() throws Exception {
+        File destination = new File( getTestDirectory(), "move2.txt" );
+        File src = new File( testFile1.getAbsolutePath() ) {
+            // Force renameTo to fail, as if destination is on another
+            // filesystem
+            public boolean renameTo( File f ) {
+                return false;
+            }
+        };
+        FileUtils.moveFile( src, destination );
+        assertTrue( "Check Exist", destination.exists() );
+        assertTrue( "Original deleted", ! src.exists() );
+    }
+
+
+    public void testMoveFile_CopyDelete_Failed() throws Exception {
+        File destination = new File( getTestDirectory(), "move3.txt" );
+        File src = new File( testFile1.getAbsolutePath() ) {
+            // Force renameTo to fail, as if destination is on another
+            // filesystem
+            public boolean renameTo( File f ) {
+                return false;
+            }
+
+            // Force delete failure
+            public boolean delete() {
+                return false;
+            }
+
+        };
+        try {
+            FileUtils.moveFile( src, destination );
+            fail( "move should have failed as src has not been deleted" );
+        } catch (IOException e) {
+           // exepected
+            assertTrue( "Check Rollback", !destination.exists() );
+            assertTrue( "Original exists", src.exists() );
+        }
+    }
+    public void testMoveFile_Errors() throws Exception {
+        try {
+            FileUtils.moveFile(null, new File("foo"));
+            fail("Expected NullPointerException when source is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveFile(new File("foo"), null);
+            fail("Expected NullPointerException when destination is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveFile(new File("nonexistant"), new File("foo"));
+            fail("Expected FileNotFoundException for source");
+        } catch (FileNotFoundException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveFile(getTestDirectory(), new File("foo"));
+            fail("Expected IOException when source is a directory");
+        } catch (IOException e) {
+            // expected
+        }
+        File testSourceFile = new File(getTestDirectory(), "testMoveFileSource");
+        File testDestFile = new File(getTestDirectory(), "testMoveFileSource");
+        createFile(testSourceFile, 0);
+        createFile(testDestFile, 0);
+        try {
+            FileUtils.moveFile(testSourceFile, testDestFile);
+            fail("Expected IOException when dest already exists");
+        } catch (IOException e) {
+            // expected
+        }
+
+    }
+
+    public void testMoveFileToDirectory() throws Exception {
+        File destDir = new File( getTestDirectory(), "moveFileDestDir");
+        File movedFile = new File(destDir, testFile1.getName());
+        assertFalse("Check Exist before", destDir.exists());
+        assertFalse("Check Exist before", movedFile.exists());
+
+        FileUtils.moveFileToDirectory(testFile1, destDir, true);
+        assertTrue( "Check Exist after", movedFile.exists() );
+        assertTrue( "Original deleted", ! testFile1.exists() );
+    }
+
+    public void testMoveFileToDirectory_Errors() throws Exception {
+        try {
+            FileUtils.moveFileToDirectory(null, new File("foo"), true);
+            fail("Expected NullPointerException when source is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveFileToDirectory(new File("foo"), null, true);
+            fail("Expected NullPointerException when destination is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        File testFile1    = new File(getTestDirectory(), "testMoveFileFile1");
+        File testFile2    = new File(getTestDirectory(), "testMoveFileFile2");
+        createFile(testFile1, 0);
+        createFile(testFile2, 0);
+        try {
+            FileUtils.moveFileToDirectory(testFile1, testFile2, true);
+            fail("Expected IOException when dest not a directory");
+        } catch (IOException e) {
+            // expected
+        }
+
+        File nonexistant = new File(getTestDirectory(), "testMoveFileNonExistant");
+        try {
+            FileUtils.moveFileToDirectory(testFile1, nonexistant, false);
+            fail("Expected IOException when dest does not exist and create=false");
+        } catch (IOException e) {
+            // expected
+        }
+    }
+
+
+    public void testMoveDirectory_Rename() throws Exception {
+        File dir = getTestDirectory();
+        File src = new File(dir, "testMoveDirectory1Source");
+        File testDir = new File(src, "foo");
+        File testFile = new File(testDir, "bar");
+        testDir.mkdirs();
+        createFile(testFile, 0);
+        File destination = new File(dir, "testMoveDirectory1Dest");
+        FileUtils.deleteDirectory( destination );
+
+        // Move the directory
+        FileUtils.moveDirectory( src, destination );
+
+        // Check results
+        assertTrue( "Check Exist", destination.exists() );
+        assertTrue( "Original deleted", ! src.exists() );
+        File movedDir = new File(destination, testDir.getName());
+        File movedFile = new File(movedDir, testFile.getName());
+        assertTrue( "Check dir moved", movedDir.exists());
+        assertTrue( "Check file moved", movedFile.exists());
+    }
+
+    public void testMoveDirectory_CopyDelete() throws Exception {
+
+        File dir = getTestDirectory();
+        File src = new File(dir, "testMoveDirectory2Source") {
+
+            // Force renameTo to fail
+            public boolean renameTo( File dest ) {
+                return false;
+            }
+        };
+        File testDir = new File(src, "foo");
+        File testFile = new File(testDir, "bar");
+        testDir.mkdirs();
+        createFile(testFile, 0);
+        File destination = new File(dir, "testMoveDirectory1Dest");
+        FileUtils.deleteDirectory( destination );
+
+        // Move the directory
+        FileUtils.moveDirectory( src, destination );
+
+        // Check results
+        assertTrue( "Check Exist", destination.exists() );
+        assertTrue( "Original deleted", ! src.exists() );
+        File movedDir = new File(destination, testDir.getName());
+        File movedFile = new File(movedDir, testFile.getName());
+        assertTrue( "Check dir moved", movedDir.exists());
+        assertTrue( "Check file moved", movedFile.exists());
+    }
+
+    public void testMoveDirectory_Errors() throws Exception {
+        try {
+            FileUtils.moveDirectory(null, new File("foo"));
+            fail("Expected NullPointerException when source is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveDirectory(new File("foo"), null);
+            fail("Expected NullPointerException when destination is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveDirectory(new File("nonexistant"), new File("foo"));
+            fail("Expected FileNotFoundException for source");
+        } catch (FileNotFoundException e) {
+            // expected
+        }
+        File testFile = new File(getTestDirectory(), "testMoveDirectoryFile");
+        createFile(testFile, 0);
+        try {
+            FileUtils.moveDirectory(testFile, new File("foo"));
+            fail("Expected IOException when source is not a directory");
+        } catch (IOException e) {
+            // expected
+        }
+        File testSrcFile = new File(getTestDirectory(), "testMoveDirectorySource");
+        File testDestFile = new File(getTestDirectory(), "testMoveDirectoryDest");
+        testDestFile.mkdir();
+        try {
+            FileUtils.moveDirectory(testSrcFile, testDestFile);
+            fail("Expected IOException when dest already exists");
+        } catch (IOException e) {
+            // expected
+        }
+
+    }
+
+    public void testMoveDirectoryToDirectory() throws Exception {
+        File dir = getTestDirectory();
+        File src = new File(dir, "testMoveDirectory1Source");
+        File testChildDir = new File(src, "foo");
+        File testFile = new File(testChildDir, "bar");
+        testChildDir.mkdirs();
+        createFile(testFile, 0);
+        File destDir = new File(dir, "testMoveDirectory1Dest");
+        FileUtils.deleteDirectory( destDir );
+        assertFalse( "Check Exist before", destDir.exists() );
+
+        // Move the directory
+        FileUtils.moveDirectoryToDirectory(src, destDir, true);
+
+        // Check results
+        assertTrue( "Check Exist after", destDir.exists() );
+        assertTrue( "Original deleted", ! src.exists() );
+        File movedDir = new File(destDir, src.getName());
+        File movedChildDir = new File(movedDir, testChildDir.getName());
+        File movedFile = new File(movedChildDir, testFile.getName());
+        assertTrue( "Check dir moved", movedDir.exists());
+        assertTrue( "Check child dir moved", movedChildDir.exists());
+        assertTrue( "Check file moved", movedFile.exists());
+    }
+
+    public void testMoveDirectoryToDirectory_Errors() throws Exception {
+        try {
+            FileUtils.moveDirectoryToDirectory(null, new File("foo"), true);
+            fail("Expected NullPointerException when source is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveDirectoryToDirectory(new File("foo"), null, true);
+            fail("Expected NullPointerException when destination is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        File testFile1    = new File(getTestDirectory(), "testMoveFileFile1");
+        File testFile2    = new File(getTestDirectory(), "testMoveFileFile2");
+        createFile(testFile1, 0);
+        createFile(testFile2, 0);
+        try {
+            FileUtils.moveDirectoryToDirectory(testFile1, testFile2, true);
+            fail("Expected IOException when dest not a directory");
+        } catch (IOException e) {
+            // expected
+        }
+
+        File nonexistant = new File(getTestDirectory(), "testMoveFileNonExistant");
+        try {
+            FileUtils.moveDirectoryToDirectory(testFile1, nonexistant, false);
+            fail("Expected IOException when dest does not exist and create=false");
+        } catch (IOException e) {
+            // expected
+        }
+    }
+    public void testMoveToDirectory() throws Exception {
+        File destDir     = new File(getTestDirectory(), "testMoveToDirectoryDestDir");
+        File testDir     = new File(getTestDirectory(), "testMoveToDirectoryTestDir");
+        File testFile    = new File(getTestDirectory(), "testMoveToDirectoryTestFile");
+        testDir.mkdirs();
+        createFile(testFile, 0);
+        File movedFile = new File(destDir, testFile.getName());
+        File movedDir = new File(destDir, testFile.getName());
+        
+        assertFalse( "Check File Doesnt exist", movedFile.exists() );
+        assertFalse( "Check Dir Doesnt exist", movedDir.exists() );
+        
+        // Test moving a file
+        FileUtils.moveToDirectory(testFile, destDir, true);
+        assertTrue( "Check File exists", movedFile.exists() );
+        assertFalse( "Check Original File doesn't exist", testFile.exists() );
+        
+        // Test moving a directory
+        FileUtils.moveToDirectory(testDir, destDir, true);
+        assertTrue( "Check Dir exists", movedDir.exists() );
+        assertFalse( "Check Original Dir doesn't exist", testDir.exists());
+    }
+
+    public void testMoveToDirectory_Errors() throws Exception {
+        try {
+            FileUtils.moveDirectoryToDirectory(null, new File("foo"), true);
+            fail("Expected NullPointerException when source is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.moveDirectoryToDirectory(new File("foo"), null, true);
+            fail("Expected NullPointerException when destination is null");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        File nonexistant    = new File(getTestDirectory(), "nonexistant");
+        File destDir    = new File(getTestDirectory(), "MoveToDirectoryDestDir");
+        try {
+            FileUtils.moveToDirectory(nonexistant, destDir, true);
+            fail("Expected IOException when source does not exist");
+        } catch (IOException e) {
+            // expected
+        }
+    }
+
     /**
      * DirectoryWalker implementation that recursively lists all files and directories.
      */



Mime
View raw message