|
17 | 17 | package org.apache.commons.io; |
18 | 18 |
|
19 | 19 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
21 | 22 | import static org.junit.jupiter.api.Assertions.assertFalse; |
22 | 23 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
23 | 24 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
24 | 25 | import static org.junit.jupiter.api.Assertions.assertNull; |
25 | 26 | import static org.junit.jupiter.api.Assertions.assertSame; |
26 | | -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
27 | 27 | import static org.junit.jupiter.api.Assertions.assertThrows; |
28 | 28 | import static org.junit.jupiter.api.Assertions.assertThrowsExactly; |
29 | 29 | import static org.junit.jupiter.api.Assertions.assertTrue; |
|
91 | 91 | import org.apache.commons.io.test.TestUtils; |
92 | 92 | import org.apache.commons.lang3.SystemProperties; |
93 | 93 | import org.apache.commons.lang3.SystemUtils; |
| 94 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
94 | 95 | import org.junit.jupiter.api.Assertions; |
95 | 96 | import org.junit.jupiter.api.BeforeEach; |
96 | 97 | import org.junit.jupiter.api.Disabled; |
@@ -245,12 +246,12 @@ private void createFilesForTestCopyDirectory(final File grandParentDir, final Fi |
245 | 246 | FileUtils.writeStringToFile(file6, "File 6 in grandChild2", "UTF8"); |
246 | 247 | } |
247 | 248 |
|
248 | | - private Path createTempSymbolicLinkedRelativeDir() throws IOException { |
| 249 | + private ImmutablePair<Path, Path> createTempSymbolicLinkedRelativeDir() throws IOException { |
249 | 250 | final Path targetDir = tempDirPath.resolve("subdir"); |
250 | 251 | final Path symlinkDir = tempDirPath.resolve("symlinked-dir"); |
251 | 252 | Files.createDirectory(targetDir); |
252 | 253 | Files.createSymbolicLink(symlinkDir, targetDir); |
253 | | - return symlinkDir; |
| 254 | + return ImmutablePair.of(symlinkDir, targetDir); |
254 | 255 | } |
255 | 256 |
|
256 | 257 | private Set<String> getFilePathSet(final List<File> files) { |
@@ -404,8 +405,7 @@ public void test_openOutputStream_existsButIsDirectory() { |
404 | 405 | */ |
405 | 406 | @Test |
406 | 407 | public void test_openOutputStream_intoExistingSymlinkedDir() throws Exception { |
407 | | - final Path symlinkedDir = createTempSymbolicLinkedRelativeDir(); |
408 | | - |
| 408 | + final Path symlinkedDir = createTempSymbolicLinkedRelativeDir().getLeft(); |
409 | 409 | final File file = symlinkedDir.resolve("test.txt").toFile(); |
410 | 410 | try (FileOutputStream out = FileUtils.openOutputStream(file)) { |
411 | 411 | out.write(0); |
@@ -1549,21 +1549,49 @@ public void testDelete() throws Exception { |
1549 | 1549 |
|
1550 | 1550 | @Test |
1551 | 1551 | public void testDeleteDirectoryFailsOnFile() { |
1552 | | - // Fail request for a File |
| 1552 | + // Fail request to delete a directory for a file |
1553 | 1553 | assertThrows(IllegalArgumentException.class, () -> FileUtils.deleteDirectory(testFile1)); |
1554 | 1554 | } |
1555 | 1555 |
|
1556 | 1556 | @Test |
1557 | | - public void testDeleteDirectoryIsSymLink() throws IOException { |
1558 | | - final Path symlinkedDir = createTempSymbolicLinkedRelativeDir(); |
| 1557 | + public void testDeleteDirectoryNoopIfAbsent() { |
| 1558 | + // Noop on non-existent entry |
| 1559 | + assertDoesNotThrow(() -> FileUtils.deleteDirectory(new File("does not exist.nope"))); |
| 1560 | + } |
| 1561 | + |
| 1562 | + @Test |
| 1563 | + public void testDeleteDirectorySymbolicLink() throws IOException { |
| 1564 | + final Path symlinkedDir = createTempSymbolicLinkedRelativeDir().getLeft(); |
1559 | 1565 | FileUtils.deleteDirectory(symlinkedDir.toFile()); |
1560 | 1566 | assertFalse(Files.exists(symlinkedDir)); |
1561 | 1567 | } |
1562 | 1568 |
|
1563 | 1569 | @Test |
1564 | | - public void testDeleteDirectoryNoopIfAbsent() { |
1565 | | - // Noop on non-existent entry |
1566 | | - assertDoesNotThrow(() -> FileUtils.deleteDirectory(new File("does not exist.nope"))); |
| 1570 | + public void testDeleteDirectorySymbolicLinkAbsent() throws IOException { |
| 1571 | + final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir(); |
| 1572 | + final Path symlinkedDir = pair.getLeft(); |
| 1573 | + final Path targetDir = pair.getRight(); |
| 1574 | + assertTrue(Files.exists(symlinkedDir), symlinkedDir::toString); |
| 1575 | + Files.delete(symlinkedDir); |
| 1576 | + assertTrue(Files.exists(targetDir), targetDir::toString); |
| 1577 | + assertFalse(Files.exists(symlinkedDir), symlinkedDir::toString); |
| 1578 | + // actual test |
| 1579 | + FileUtils.deleteDirectory(symlinkedDir.toFile()); |
| 1580 | + assertFalse(Files.exists(symlinkedDir), symlinkedDir::toString); |
| 1581 | + } |
| 1582 | + |
| 1583 | + @Test |
| 1584 | + public void testDeleteDirectorySymbolicLinkAbsentTarget() throws IOException { |
| 1585 | + final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir(); |
| 1586 | + final Path symlinkedDir = pair.getLeft(); |
| 1587 | + final Path targetDir = pair.getRight(); |
| 1588 | + assertTrue(Files.exists(symlinkedDir), symlinkedDir::toString); |
| 1589 | + Files.delete(targetDir); |
| 1590 | + assertFalse(Files.exists(targetDir), targetDir::toString); |
| 1591 | + assertFalse(Files.exists(symlinkedDir), symlinkedDir::toString); |
| 1592 | + // actual test |
| 1593 | + FileUtils.deleteDirectory(symlinkedDir.toFile()); |
| 1594 | + assertFalse(Files.exists(symlinkedDir), symlinkedDir::toString); |
1567 | 1595 | } |
1568 | 1596 |
|
1569 | 1597 | @Test |
@@ -3453,11 +3481,9 @@ public void testWriteStringToFileIntoNonExistentSubdir() throws Exception { |
3453 | 3481 | */ |
3454 | 3482 | @Test |
3455 | 3483 | public void testWriteStringToFileIntoSymlinkedDir() throws Exception { |
3456 | | - final Path symlinkDir = createTempSymbolicLinkedRelativeDir(); |
3457 | | - |
| 3484 | + final Path symlinkDir = createTempSymbolicLinkedRelativeDir().getLeft(); |
3458 | 3485 | final File file = symlinkDir.resolve("file").toFile(); |
3459 | 3486 | FileUtils.writeStringToFile(file, "Hello /u1234", StandardCharsets.UTF_8); |
3460 | | - |
3461 | 3487 | final byte[] text = "Hello /u1234".getBytes(); |
3462 | 3488 | TestUtils.assertEqualContent(text, file); |
3463 | 3489 | } |
|
0 commit comments