Skip to content

Commit db158e5

Browse files
author
Gary Gregory
committed
Add cleaned up test related to IO-141 and PR apache#371 by PJ Fanning
See comment apache#371 (comment)
1 parent a92fa38 commit db158e5

1 file changed

Lines changed: 66 additions & 26 deletions

File tree

src/test/java/org/apache/commons/io/FileUtilsTest.java

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
import java.util.Iterator;
6565
import java.util.List;
6666
import java.util.Map;
67+
import java.util.Set;
6768
import java.util.concurrent.TimeUnit;
69+
import java.util.stream.Collectors;
6870
import java.util.zip.CRC32;
6971
import java.util.zip.Checksum;
7072

@@ -230,7 +232,7 @@ private void createCircularSymLink(final File file) throws IOException {
230232
assertTrue(Files.isSymbolicLink(linkPath), () -> "Expected a sym link here: " + linkName);
231233
}
232234

233-
private void createFilesForTestCopyDirectory(final File grandParentDir, final File parentDir, final File childDir) throws Exception {
235+
private void createFilesForTestCopyDirectory(final File grandParentDir, final File parentDir, final File childDir) throws IOException {
234236
final File childDir2 = new File(parentDir, "child2");
235237
final File grandChildDir = new File(childDir, "grandChild");
236238
final File grandChild2Dir = new File(childDir2, "grandChild2");
@@ -259,6 +261,16 @@ private Path createTempSymlinkedRelativeDir() throws IOException {
259261
return symlinkDir;
260262
}
261263

264+
private Set<String> getFilePathSet(List<File> files) {
265+
return files.stream().map(f -> {
266+
try {
267+
return f.getCanonicalPath();
268+
} catch (IOException e) {
269+
throw new RuntimeException(e);
270+
}
271+
}).collect(Collectors.toSet());
272+
}
273+
262274
private long getLastModifiedMillis(final File file) throws IOException {
263275
return FileUtils.lastModified(file);
264276
}
@@ -770,7 +782,7 @@ public void testCopyDirectoryExceptions() {
770782
}
771783

772784
@Test
773-
public void testCopyDirectoryFiltered() throws Exception {
785+
public void testCopyDirectoryFiltered() throws IOException {
774786
final File grandParentDir = new File(tempDirFile, "grandparent");
775787
final File parentDir = new File(grandParentDir, "parent");
776788
final File childDir = new File(parentDir, "child");
@@ -787,6 +799,26 @@ public void testCopyDirectoryFiltered() throws Exception {
787799
assertEquals("file3.txt", files.get(2).getName());
788800
}
789801

802+
// @Test public void testToURLs2() throws Exception {
803+
// File[] files = new File[] {
804+
// new File(temporaryFolder, "file1.txt"),
805+
// null,
806+
// };
807+
// URL[] urls = FileUtils.toURLs(files);
808+
//
809+
// assertEquals(files.length, urls.length);
810+
// assertTrue(urls[0].toExternalForm().startsWith("file:"));
811+
// assertTrue(urls[0].toExternalForm().indexOf("file1.txt") > 0);
812+
// assertEquals(null, urls[1]);
813+
// }
814+
//
815+
// @Test public void testToURLs3() throws Exception {
816+
// File[] files = null;
817+
// URL[] urls = FileUtils.toURLs(files);
818+
//
819+
// assertEquals(0, urls.length);
820+
// }
821+
790822
@Test
791823
public void testCopyDirectoryPreserveDates() throws Exception {
792824
final File source = new File(tempDirFile, "source");
@@ -842,29 +874,9 @@ public void testCopyDirectoryPreserveDates() throws Exception {
842874
FileUtils.deleteDirectory(target);
843875
}
844876

845-
// @Test public void testToURLs2() throws Exception {
846-
// File[] files = new File[] {
847-
// new File(temporaryFolder, "file1.txt"),
848-
// null,
849-
// };
850-
// URL[] urls = FileUtils.toURLs(files);
851-
//
852-
// assertEquals(files.length, urls.length);
853-
// assertTrue(urls[0].toExternalForm().startsWith("file:"));
854-
// assertTrue(urls[0].toExternalForm().indexOf("file1.txt") > 0);
855-
// assertEquals(null, urls[1]);
856-
// }
857-
//
858-
// @Test public void testToURLs3() throws Exception {
859-
// File[] files = null;
860-
// URL[] urls = FileUtils.toURLs(files);
861-
//
862-
// assertEquals(0, urls.length);
863-
// }
864-
865877
/** Tests IO-141 */
866878
@Test
867-
public void testCopyDirectoryToChild() throws Exception {
879+
public void testCopyDirectoryToChild() throws IOException {
868880
final File grandParentDir = new File(tempDirFile, "grandparent");
869881
final File parentDir = new File(grandParentDir, "parent");
870882
final File childDir = new File(parentDir, "child");
@@ -950,7 +962,7 @@ public void testCopyDirectoryToExistingDest() throws Exception {
950962

951963
/** Test IO-141 */
952964
@Test
953-
public void testCopyDirectoryToGrandChild() throws Exception {
965+
public void testCopyDirectoryToGrandChild() throws IOException {
954966
final File grandParentDir = new File(tempDirFile, "grandparent");
955967
final File parentDir = new File(grandParentDir, "parent");
956968
final File childDir = new File(parentDir, "child");
@@ -1007,6 +1019,34 @@ public void testCopyDirectoryToNonExistingDest() throws Exception {
10071019
FileUtils.deleteDirectory(destDir);
10081020
}
10091021

1022+
/**
1023+
* Test for https://github.com/apache/commons-io/pull/371. The dir name 'par' is a substring of
1024+
* the dir name 'parent' which is the parent of the 'parent/child' dir.
1025+
*/
1026+
@Test
1027+
public void testCopyDirectoryWithPotentialFalsePartialMatch() throws IOException {
1028+
final File grandParentDir = new File(tempDirFile, "grandparent");
1029+
final File parentDir = new File(grandParentDir, "parent");
1030+
final File parDir = new File(grandParentDir, "par");
1031+
final File childDir = new File(parentDir, "child");
1032+
createFilesForTestCopyDirectory(grandParentDir, parDir, childDir);
1033+
1034+
final List<File> initFiles = LIST_WALKER.list(grandParentDir);
1035+
final List<File> parFiles = LIST_WALKER.list(parDir);
1036+
final long expectedCount = initFiles.size() + parFiles.size();
1037+
final long expectedSize = FileUtils.sizeOfDirectory(grandParentDir) + FileUtils.sizeOfDirectory(parDir);
1038+
FileUtils.copyDirectory(parDir, childDir);
1039+
final List<File> latestFiles = LIST_WALKER.list(grandParentDir);
1040+
assertEquals(expectedCount, latestFiles.size());
1041+
assertEquals(expectedSize, FileUtils.sizeOfDirectory(grandParentDir));
1042+
assertTrue(expectedCount > 0, "Count > 0");
1043+
assertTrue(expectedSize > 0, "Size > 0");
1044+
Set<String> initFilePaths = getFilePathSet(initFiles);
1045+
Set<String> newFilePaths = getFilePathSet(latestFiles);
1046+
newFilePaths.removeAll(initFilePaths);
1047+
assertEquals(parFiles.size(), newFilePaths.size());
1048+
}
1049+
10101050
@Test
10111051
public void testCopyFile1() throws Exception {
10121052
final File destination = new File(tempDirFile, "copy1.txt");
@@ -2757,6 +2797,7 @@ public void testWriteCharSequence2() throws Exception {
27572797
TestUtils.assertEqualContent(text, file);
27582798
}
27592799

2800+
27602801
@Test
27612802
public void testWriteLines_3arg_nullSeparator() throws Exception {
27622803
final Object[] data = {
@@ -2788,7 +2829,6 @@ public void testWriteLines_3argsWithAppendOptionFalse_ShouldDeletePreviousFileLi
27882829
assertEquals(expected, actual);
27892830
}
27902831

2791-
27922832
@Test
27932833
public void testWriteLines_3argsWithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception {
27942834
final File file = TestUtils.newFile(tempDirFile, "lines.txt");
@@ -3028,7 +3068,7 @@ public void testWriteStringToFileWithNullStringCharset() throws Exception {
30283068
final byte[] text = "Hello /u1234".getBytes();
30293069
TestUtils.assertEqualContent(text, file);
30303070
}
3031-
3071+
30323072
@Test
30333073
public void testWriteWithEncoding_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception {
30343074
final File file = TestUtils.newFile(tempDirFile, "lines.txt");

0 commit comments

Comments
 (0)