Skip to content

Commit d463fa0

Browse files
mikkomaunugarydgregory
authored andcommitted
[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
1 parent 58324c1 commit d463fa0

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ The <action> type attribute can be add,update,fix,remove.
131131
<action issue="IO-617" dev="ggregory" type="add" due-to="Rob Spoor">
132132
Add classes Added TaggedReader, ClosedReader and BrokenReader. #85.
133133
</action>
134+
<action issue="IO-625" dev="ggregory" type="fix" due-to="Mikko Maunu">
135+
Corrected misleading exception message for FileUtils.copyDirectoryToDirectory.
136+
</action>
134137
</release>
135138

136139
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ private static void checkEqualSizes(final File srcFile, final File destFile, fin
12071207
* @param destDir the directory to place the copy in, must not be {@code null}
12081208
*
12091209
* @throws NullPointerException if source or destination is {@code null}
1210+
* @throws IllegalArgumentException if {@code srcDir} or {@code destDir} is not a directory
12101211
* @throws IOException if source or destination is invalid
12111212
* @throws IOException if an IO error occurs during copying
12121213
* @since 1.2
@@ -1216,7 +1217,7 @@ public static void copyDirectoryToDirectory(final File srcDir, final File destDi
12161217
throw new NullPointerException("Source must not be null");
12171218
}
12181219
if (srcDir.exists() && srcDir.isDirectory() == false) {
1219-
throw new IllegalArgumentException("Source '" + destDir + "' is not a directory");
1220+
throw new IllegalArgumentException("Source '" + srcDir + "' is not a directory");
12201221
}
12211222
if (destDir == null) {
12221223
throw new NullPointerException("Destination must not be null");
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.junit.rules.TemporaryFolder;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.fail;
28+
29+
/**
30+
* This class ensure the correctness of {@link FileUtils#copyDirectoryToDirectory(File, File)}.
31+
* TODO: currently does not cover happy cases
32+
*
33+
* @see FileUtils#copyDirectoryToDirectory(File, File)
34+
*/
35+
public class FileUtilsCopyDirectoryToDirectoryTestCase {
36+
37+
@Rule
38+
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
39+
40+
@Test
41+
public void copyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory() throws IOException {
42+
final File srcDir = temporaryFolder.newFile("notadirectory");
43+
final File destDir = temporaryFolder.newFolder("destinationDirectory");
44+
final String expectedMessage = String.format("Source '%s' is not a directory", srcDir);
45+
assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage);
46+
}
47+
48+
@Test
49+
public void copyDirectoryToDirectoryThrowsIllegalArgumentExceptionWithCorrectMessageWhenDstDirIsNotDirectory() throws IOException {
50+
final File srcDir = temporaryFolder.newFolder("sourceDirectory");
51+
final File destDir = temporaryFolder.newFile("notadirectory");
52+
String expectedMessage = String.format("Destination '%s' is not a directory", destDir);
53+
assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage);
54+
}
55+
56+
@Test
57+
public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenSrcDirIsNull() throws IOException {
58+
final File srcDir = null;
59+
final File destinationDirectory = temporaryFolder.newFolder("destinationDirectory");
60+
final String expectedMessage = "Source must not be null";
61+
assertExceptionTypeAndMessage(srcDir, destinationDirectory, NullPointerException.class, expectedMessage);
62+
}
63+
64+
@Test
65+
public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenDstDirIsNull() throws IOException {
66+
final File srcDir = temporaryFolder.newFolder("sourceDirectory");
67+
final File destDir = null;
68+
final String expectedMessage = "Destination must not be null";
69+
assertExceptionTypeAndMessage(srcDir, destDir, NullPointerException.class, expectedMessage);
70+
}
71+
72+
private static void assertExceptionTypeAndMessage(final File srcDir, final File destDir, final Class expectedExceptionType, final String expectedMessage) {
73+
try {
74+
FileUtils.copyDirectoryToDirectory(srcDir, destDir);
75+
} catch (final Exception e) {
76+
final String msg = e.getMessage();
77+
assertEquals(expectedExceptionType, e.getClass());
78+
assertEquals(expectedMessage, msg);
79+
return;
80+
}
81+
fail();
82+
}
83+
}

0 commit comments

Comments
 (0)