You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/09/08 17:41:33 UTC

[commons-io] branch master updated: [IO-625] FileUtils.copyDirectoryToDirectory does not reflect srcDir in exception message when srcDir is not a directory (#95)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new d463fa0  [IO-625] FileUtils.copyDirectoryToDirectory does not reflect srcDir in exception message when srcDir is not a directory (#95)
d463fa0 is described below

commit d463fa0120fc1041729b7a564b2b5f96de9d6ab6
Author: Mikko Maunu <mi...@maunu.org>
AuthorDate: Sun Sep 8 19:41:28 2019 +0200

    [IO-625] FileUtils.copyDirectoryToDirectory does not reflect srcDir in exception message when srcDir is not a directory (#95)
    
    * [IO-625] FileUtils.copyDirectoryToDirectory does not reflect srcDir in exception message when srcDir is not a directory
    
    * [IO-625] final variables, removed empty line, fixed link
    
    * [IO-625] added entry to changes.xml
---
 src/changes/changes.xml                            |  3 +
 src/main/java/org/apache/commons/io/FileUtils.java |  3 +-
 .../FileUtilsCopyDirectoryToDirectoryTestCase.java | 83 ++++++++++++++++++++++
 3 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 165f2a3..cb053d5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -131,6 +131,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="IO-617" dev="ggregory" type="add" due-to="Rob Spoor">
         Add classes Added TaggedReader, ClosedReader and BrokenReader. #85.
       </action>
+      <action issue="IO-625" dev="ggregory" type="fix" due-to="Mikko Maunu">
+        Corrected misleading exception message for FileUtils.copyDirectoryToDirectory.
+      </action>
     </release>
 
     <release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 6503eb3..72d8f2a 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1207,6 +1207,7 @@ public class FileUtils {
      * @param destDir the directory to place the copy in, must not be {@code null}
      *
      * @throws NullPointerException if source or destination is {@code null}
+     * @throws IllegalArgumentException if {@code srcDir} or {@code destDir} is not a directory
      * @throws IOException          if source or destination is invalid
      * @throws IOException          if an IO error occurs during copying
      * @since 1.2
@@ -1216,7 +1217,7 @@ public class FileUtils {
             throw new NullPointerException("Source must not be null");
         }
         if (srcDir.exists() && srcDir.isDirectory() == false) {
-            throw new IllegalArgumentException("Source '" + destDir + "' is not a directory");
+            throw new IllegalArgumentException("Source '" + srcDir + "' is not a directory");
         }
         if (destDir == null) {
             throw new NullPointerException("Destination must not be null");
diff --git a/src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java
new file mode 100644
index 0000000..a6b0c5c
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * This class ensure the correctness of {@link FileUtils#copyDirectoryToDirectory(File, File)}.
+ * TODO: currently does not cover happy cases
+ *
+ * @see FileUtils#copyDirectoryToDirectory(File, File)
+ */
+public class FileUtilsCopyDirectoryToDirectoryTestCase {
+
+    @Rule
+    public final TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    @Test
+    public void copyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory() throws IOException {
+        final File srcDir = temporaryFolder.newFile("notadirectory");
+        final File destDir = temporaryFolder.newFolder("destinationDirectory");
+        final String expectedMessage = String.format("Source '%s' is not a directory", srcDir);
+        assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage);
+    }
+
+    @Test
+    public void copyDirectoryToDirectoryThrowsIllegalArgumentExceptionWithCorrectMessageWhenDstDirIsNotDirectory() throws IOException {
+        final File srcDir = temporaryFolder.newFolder("sourceDirectory");
+        final File destDir =  temporaryFolder.newFile("notadirectory");
+        String expectedMessage = String.format("Destination '%s' is not a directory", destDir);
+        assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage);
+    }
+
+    @Test
+    public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenSrcDirIsNull() throws IOException {
+        final File srcDir = null;
+        final File destinationDirectory =  temporaryFolder.newFolder("destinationDirectory");
+        final String expectedMessage = "Source must not be null";
+        assertExceptionTypeAndMessage(srcDir, destinationDirectory, NullPointerException.class,  expectedMessage);
+    }
+
+    @Test
+    public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenDstDirIsNull() throws IOException {
+        final File srcDir = temporaryFolder.newFolder("sourceDirectory");
+        final File destDir =  null;
+        final String expectedMessage = "Destination must not be null";
+        assertExceptionTypeAndMessage(srcDir, destDir, NullPointerException.class, expectedMessage);
+    }
+
+    private static void assertExceptionTypeAndMessage(final File srcDir, final File destDir, final Class expectedExceptionType, final String expectedMessage) {
+        try {
+            FileUtils.copyDirectoryToDirectory(srcDir, destDir);
+        } catch (final Exception e) {
+            final String msg = e.getMessage();
+            assertEquals(expectedExceptionType, e.getClass());
+            assertEquals(expectedMessage, msg);
+            return;
+        }
+        fail();
+    }
+}