|
26 | 26 | import static org.junit.jupiter.api.Assertions.assertThrows; |
27 | 27 | import static org.junit.jupiter.api.Assertions.assertTrue; |
28 | 28 | import static org.junit.jupiter.api.Assertions.fail; |
| 29 | +import static org.junit.jupiter.api.Assumptions.assumeFalse; |
| 30 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
29 | 31 |
|
30 | 32 | import java.io.BufferedOutputStream; |
31 | 33 | import java.io.ByteArrayOutputStream; |
|
43 | 45 | import java.nio.charset.StandardCharsets; |
44 | 46 | import java.nio.charset.UnsupportedCharsetException; |
45 | 47 | import java.nio.file.Files; |
| 48 | +import java.nio.file.LinkOption; |
46 | 49 | import java.nio.file.Path; |
47 | 50 | import java.nio.file.Paths; |
48 | 51 | import java.nio.file.StandardCopyOption; |
@@ -808,6 +811,38 @@ public void testToURLs3() { |
808 | 811 | "Can't convert null list"); |
809 | 812 | } |
810 | 813 |
|
| 814 | + // IO-807 |
| 815 | + @Test |
| 816 | + public void testCopyDirectory_brokenSymLink() throws IOException { |
| 817 | + // Make a file |
| 818 | + final File sourceDirectory = new File(tempDirFile, "source_directory"); |
| 819 | + sourceDirectory.mkdir(); |
| 820 | + final File targetFile = new File(sourceDirectory, "hello.txt"); |
| 821 | + FileUtils.writeStringToFile(targetFile, "HELLO WORLD", "UTF8"); |
| 822 | + |
| 823 | + // Make a symlink to the file |
| 824 | + final Path targetPath = targetFile.toPath(); |
| 825 | + final Path linkPath = sourceDirectory.toPath().resolve("linkfile"); |
| 826 | + Files.createSymbolicLink(linkPath, targetPath); |
| 827 | + assumeTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symlink here: " + linkPath); |
| 828 | + assumeTrue(Files.exists(linkPath)); |
| 829 | + assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS)); |
| 830 | + |
| 831 | + // Delete the file file to break the symlink |
| 832 | + assumeTrue(targetFile.delete()); |
| 833 | + assumeFalse(Files.exists(linkPath)); |
| 834 | + assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS)); |
| 835 | + |
| 836 | + // Now copy sourceDirectory, including the broken link, to another directory |
| 837 | + final File destination = new File(tempDirFile, "destination"); |
| 838 | + final FileNotFoundException thrown = assertThrows( |
| 839 | + FileNotFoundException.class, |
| 840 | + () -> FileUtils.copyDirectory(sourceDirectory, destination), |
| 841 | + "ignored broken link" |
| 842 | + ); |
| 843 | + assertTrue(thrown.getMessage().contains("linkfile' does not exist")); |
| 844 | + } |
| 845 | + |
811 | 846 | @Test |
812 | 847 | public void testCopyDirectoryPreserveDates() throws Exception { |
813 | 848 | final File source = new File(tempDirFile, "source"); |
|
0 commit comments