You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2006/01/14 19:15:23 UTC

svn commit: r369070 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/FileUtils.java src/test/org/apache/commons/io/FileUtilsTestCase.java

Author: scolebourne
Date: Sat Jan 14 10:15:16 2006
New Revision: 369070

URL: http://svn.apache.org/viewcvs?rev=369070&view=rev
Log:
New FileUtils method to copy a directory to within another directory
rfe 36315

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

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=369070&r1=369069&r2=369070&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Sat Jan 14 10:15:16 2006
@@ -50,15 +50,18 @@
 
 - FileUtils.lineIterator
   IOUtils.lineIterator
-  New methods to provide an iterator over the lines in a file
+  New methods to provide an iterator over the lines in a file [38083]
+
+- FileUtils.copyDirectoryToDirectory
+  New method to copy a directory to within another directory [36315]
 
 
 Feedback
 --------
-Open source works best when you give feedback.
+Open source works best when you give feedback:
 http://jakarta.apache.org/commons/io/
 
-Please direct all bug reports to Bugzilla.
+Please direct all bug reports to Bugzilla (prefix bug reports by [io])
 http://issues.apache.org/bugzilla/buglist.cgi?product=Commons&component=IO
 
 Or subscribe to the commons-user mailing list (prefix emails by [io])

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=369070&r1=369069&r2=369070&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Sat Jan 14 10:15:16 2006
@@ -579,12 +579,48 @@
 
     //-----------------------------------------------------------------------
     /**
+     * Copies a directory to within another directory preserving the file dates.
+     * <p>
+     * This method copies the source directory and all its contents to a
+     * directory of the same name in the specified destination directory.
+     * <p>
+     * The destination directory is created if it does not exist.
+     * If the destination directory did exist, then this method merges
+     * the source with the destination, with the source taking precedence.
+     *
+     * @param srcDir  an existing directory to copy, must not be null
+     * @param destDir  the directory to place the copy in, must not be null
+     *
+     * @throws NullPointerException if source or destination is null
+     * @throws IOException if source or destination is invalid
+     * @throws IOException if an IO error occurs during copying
+     */
+    public static void copyDirectoryToDirectory(File srcDir, File destDir) throws IOException {
+        if (srcDir == null) {
+            throw new NullPointerException("Source must not be null");
+        }
+        if (srcDir.exists() && srcDir.isDirectory() == false) {
+            throw new IllegalArgumentException("Source '" + destDir + "' is not a directory");
+        }
+        if (destDir == null) {
+            throw new NullPointerException("Destination must not be null");
+        }
+        if (destDir.exists() && destDir.isDirectory() == false) {
+            throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
+        }
+        copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);
+    }
+
+    /**
      * Copies a whole directory to a new location preserving the file dates.
      * <p>
      * This method copies the specified directory and all its child
      * directories and files to the specified destination.
      * The destination is the new location and name of the directory.
-     * If it already exists, the contents will be overwritten.
+     * <p>
+     * The destination directory is created if it does not exist.
+     * If the destination directory did exist, then this method merges
+     * the source with the destination, with the source taking precedence.
      *
      * @param srcDir  an existing directory to copy, must not be null
      * @param destDir  the new directory, must not be null
@@ -603,6 +639,7 @@
      * <p>
      * This method copies the contents of the specified source directory
      * to within the specified destination directory.
+     * <p>
      * The destination directory is created if it does not exist.
      * If the destination directory did exist, then this method merges
      * the source with the destination, with the source taking precedence.

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=369070&r1=369069&r2=369070&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Sat Jan 14 10:15:16 2006
@@ -431,6 +431,27 @@
             testFile1.lastModified() != destination.lastModified());*/    
     }
 
+    public void testCopyDirectoryToDirectory_NonExistingDest() throws Exception {
+        createFile(testFile1, 1234);
+        createFile(testFile2, 4321);
+        File srcDir = getTestDirectory();
+        File subDir = new File(srcDir, "sub");
+        subDir.mkdir();
+        File subFile = new File(subDir, "A.txt");
+        FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
+        File destDir = new File(System.getProperty("java.io.tmpdir"), "tmp-FileUtilsTestCase");
+        FileUtils.deleteDirectory(destDir);
+        File actualDestDir = new File(destDir, srcDir.getName());
+        
+        FileUtils.copyDirectoryToDirectory(srcDir, destDir);
+        
+        assertTrue("Check exists", destDir.exists());
+        assertTrue("Check exists", actualDestDir.exists());
+        assertEquals("Check size", FileUtils.sizeOfDirectory(srcDir), FileUtils.sizeOfDirectory(actualDestDir));
+        assertEquals(true, new File(actualDestDir, "sub/A.txt").exists());
+        FileUtils.deleteDirectory(destDir);
+    }
+
     public void testCopyDirectoryToNonExistingDest() throws Exception {
         createFile(testFile1, 1234);
         createFile(testFile2, 4321);



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org