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 db158e5a Add cleaned up test related to IO-141 and PR #371 by PJ Fanning db158e5a is described below commit db158e5aac3405ce19cf41335a791f0d9043878a Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Mon Aug 29 09:51:20 2022 -0400 Add cleaned up test related to IO-141 and PR #371 by PJ Fanning See comment https://github.com/apache/commons-io/pull/371#issuecomment-1228927498 --- .../java/org/apache/commons/io/FileUtilsTest.java | 92 ++++++++++++++++------ 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java index c26605bb..456007f4 100644 --- a/src/test/java/org/apache/commons/io/FileUtilsTest.java +++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java @@ -64,7 +64,9 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import java.util.zip.CRC32; import java.util.zip.Checksum; @@ -230,7 +232,7 @@ public class FileUtilsTest extends AbstractTempDirTest { assertTrue(Files.isSymbolicLink(linkPath), () -> "Expected a sym link here: " + linkName); } - private void createFilesForTestCopyDirectory(final File grandParentDir, final File parentDir, final File childDir) throws Exception { + private void createFilesForTestCopyDirectory(final File grandParentDir, final File parentDir, final File childDir) throws IOException { final File childDir2 = new File(parentDir, "child2"); final File grandChildDir = new File(childDir, "grandChild"); final File grandChild2Dir = new File(childDir2, "grandChild2"); @@ -259,6 +261,16 @@ public class FileUtilsTest extends AbstractTempDirTest { return symlinkDir; } + private Set<String> getFilePathSet(List<File> files) { + return files.stream().map(f -> { + try { + return f.getCanonicalPath(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }).collect(Collectors.toSet()); + } + private long getLastModifiedMillis(final File file) throws IOException { return FileUtils.lastModified(file); } @@ -770,7 +782,7 @@ public class FileUtilsTest extends AbstractTempDirTest { } @Test - public void testCopyDirectoryFiltered() throws Exception { + public void testCopyDirectoryFiltered() throws IOException { final File grandParentDir = new File(tempDirFile, "grandparent"); final File parentDir = new File(grandParentDir, "parent"); final File childDir = new File(parentDir, "child"); @@ -787,6 +799,26 @@ public class FileUtilsTest extends AbstractTempDirTest { assertEquals("file3.txt", files.get(2).getName()); } +// @Test public void testToURLs2() throws Exception { +// File[] files = new File[] { +// new File(temporaryFolder, "file1.txt"), +// null, +// }; +// URL[] urls = FileUtils.toURLs(files); +// +// assertEquals(files.length, urls.length); +// assertTrue(urls[0].toExternalForm().startsWith("file:")); +// assertTrue(urls[0].toExternalForm().indexOf("file1.txt") > 0); +// assertEquals(null, urls[1]); +// } +// +// @Test public void testToURLs3() throws Exception { +// File[] files = null; +// URL[] urls = FileUtils.toURLs(files); +// +// assertEquals(0, urls.length); +// } + @Test public void testCopyDirectoryPreserveDates() throws Exception { final File source = new File(tempDirFile, "source"); @@ -842,29 +874,9 @@ public class FileUtilsTest extends AbstractTempDirTest { FileUtils.deleteDirectory(target); } -// @Test public void testToURLs2() throws Exception { -// File[] files = new File[] { -// new File(temporaryFolder, "file1.txt"), -// null, -// }; -// URL[] urls = FileUtils.toURLs(files); -// -// assertEquals(files.length, urls.length); -// assertTrue(urls[0].toExternalForm().startsWith("file:")); -// assertTrue(urls[0].toExternalForm().indexOf("file1.txt") > 0); -// assertEquals(null, urls[1]); -// } -// -// @Test public void testToURLs3() throws Exception { -// File[] files = null; -// URL[] urls = FileUtils.toURLs(files); -// -// assertEquals(0, urls.length); -// } - /** Tests IO-141 */ @Test - public void testCopyDirectoryToChild() throws Exception { + public void testCopyDirectoryToChild() throws IOException { final File grandParentDir = new File(tempDirFile, "grandparent"); final File parentDir = new File(grandParentDir, "parent"); final File childDir = new File(parentDir, "child"); @@ -950,7 +962,7 @@ public class FileUtilsTest extends AbstractTempDirTest { /** Test IO-141 */ @Test - public void testCopyDirectoryToGrandChild() throws Exception { + public void testCopyDirectoryToGrandChild() throws IOException { final File grandParentDir = new File(tempDirFile, "grandparent"); final File parentDir = new File(grandParentDir, "parent"); final File childDir = new File(parentDir, "child"); @@ -1007,6 +1019,34 @@ public class FileUtilsTest extends AbstractTempDirTest { FileUtils.deleteDirectory(destDir); } + /** + * Test for https://github.com/apache/commons-io/pull/371. The dir name 'par' is a substring of + * the dir name 'parent' which is the parent of the 'parent/child' dir. + */ + @Test + public void testCopyDirectoryWithPotentialFalsePartialMatch() throws IOException { + final File grandParentDir = new File(tempDirFile, "grandparent"); + final File parentDir = new File(grandParentDir, "parent"); + final File parDir = new File(grandParentDir, "par"); + final File childDir = new File(parentDir, "child"); + createFilesForTestCopyDirectory(grandParentDir, parDir, childDir); + + final List<File> initFiles = LIST_WALKER.list(grandParentDir); + final List<File> parFiles = LIST_WALKER.list(parDir); + final long expectedCount = initFiles.size() + parFiles.size(); + final long expectedSize = FileUtils.sizeOfDirectory(grandParentDir) + FileUtils.sizeOfDirectory(parDir); + FileUtils.copyDirectory(parDir, childDir); + final List<File> latestFiles = LIST_WALKER.list(grandParentDir); + assertEquals(expectedCount, latestFiles.size()); + assertEquals(expectedSize, FileUtils.sizeOfDirectory(grandParentDir)); + assertTrue(expectedCount > 0, "Count > 0"); + assertTrue(expectedSize > 0, "Size > 0"); + Set<String> initFilePaths = getFilePathSet(initFiles); + Set<String> newFilePaths = getFilePathSet(latestFiles); + newFilePaths.removeAll(initFilePaths); + assertEquals(parFiles.size(), newFilePaths.size()); + } + @Test public void testCopyFile1() throws Exception { final File destination = new File(tempDirFile, "copy1.txt"); @@ -2757,6 +2797,7 @@ public class FileUtilsTest extends AbstractTempDirTest { TestUtils.assertEqualContent(text, file); } + @Test public void testWriteLines_3arg_nullSeparator() throws Exception { final Object[] data = { @@ -2788,7 +2829,6 @@ public class FileUtilsTest extends AbstractTempDirTest { assertEquals(expected, actual); } - @Test public void testWriteLines_3argsWithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(tempDirFile, "lines.txt"); @@ -3028,7 +3068,7 @@ public class FileUtilsTest extends AbstractTempDirTest { final byte[] text = "Hello /u1234".getBytes(); TestUtils.assertEqualContent(text, file); } - + @Test public void testWriteWithEncoding_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(tempDirFile, "lines.txt");