Skip to content

Commit 08b3ea4

Browse files
committed
Tests IO-850
DeletingPathVisitor always fails to delete a dir when symbolic link target is deleted before the link itself https://issues.apache.org/jira/browse/IO-850
1 parent e72d010 commit 08b3ea4

2 files changed

Lines changed: 56 additions & 12 deletions

File tree

src/test/java/org/apache/commons/io/file/AbstractTempDirTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@
3131
*/
3232
public abstract class AbstractTempDirTest {
3333

34+
protected static final String SUB_DIR = "subdir";
35+
protected static final String SYMLINKED_DIR = "symlinked-dir";
36+
3437
/**
35-
* Creates directory test fixtures.
38+
* Creates directory test fixtures in the given directory {@code rootDir}.
3639
* <ol>
37-
* <li>tempDirPath/subdir</li>
38-
* <li>tempDirPath/symlinked-dir -> tempDirPath/subdir</li>
40+
* <li>{@code rootDir/subdir}</li>
41+
* <li>{@code rootDir/symlinked-dir} -> {@code rootDir/subdir}</li>
3942
* </ol>
4043
* @param rootDir Root for directory entries.
41-
* @return Path to tempDirPath/subdir.
44+
* @return Path for {@code tempDirPath/subdir}.
4245
* @throws IOException if an I/O error occurs or the parent directory does not exist.
4346
*/
4447
protected static Path createTempSymlinkedRelativeDir(final Path rootDir) throws IOException {
45-
final Path targetDir = rootDir.resolve("subdir");
46-
final Path symlinkDir = rootDir.resolve("symlinked-dir");
48+
final Path targetDir = rootDir.resolve(SUB_DIR);
49+
final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
4750
Files.createDirectory(targetDir);
48-
Files.createSymbolicLink(symlinkDir, targetDir);
49-
return symlinkDir;
51+
return Files.createSymbolicLink(symlinkDir, targetDir);
5052
}
5153

5254
/**
@@ -56,14 +58,14 @@ protected static Path createTempSymlinkedRelativeDir(final Path rootDir) throws
5658
public Path managedTempDirPath;
5759

5860
/**
59-
* A temporary directory managed by each test so we can optionally fiddle with its permissions independently.
61+
* A File version of this test's Path object.
6062
*/
61-
public Path tempDirPath;
63+
public File tempDirFile;
6264

6365
/**
64-
* A File version of this test's Path object.
66+
* A temporary directory managed by each test so we can optionally fiddle with its permissions independently.
6567
*/
66-
public File tempDirFile;
68+
public Path tempDirPath;
6769

6870
@BeforeEach
6971
public void beforeEachCreateTempDirs() throws IOException {

src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
package org.apache.commons.io.file;
1919

2020
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
2123

2224
import java.io.IOException;
25+
import java.nio.charset.StandardCharsets;
2326
import java.nio.file.Files;
2427
import java.nio.file.Path;
2528
import java.nio.file.Paths;
2629

2730
import org.apache.commons.io.file.Counters.PathCounters;
2831
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.Test;
2933
import org.junit.jupiter.params.ParameterizedTest;
3034
import org.junit.jupiter.params.provider.MethodSource;
3135

@@ -113,4 +117,42 @@ public void testDeleteFolders2FileSize2(final DeletingPathVisitor visitor) throw
113117
// This will throw if not empty.
114118
Files.deleteIfExists(tempDirPath);
115119
}
120+
121+
/**
122+
* Tests https://issues.apache.org/jira/browse/IO-850
123+
*/
124+
@Test
125+
public void testIO850DirectoriesOnly() throws IOException {
126+
final Path rootDir = Files.createDirectory(managedTempDirPath.resolve("IO850"));
127+
createTempSymlinkedRelativeDir(rootDir);
128+
final Path targetDir = rootDir.resolve(SUB_DIR);
129+
final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
130+
final DeletingPathVisitor visitor = DeletingPathVisitor.withLongCounters();
131+
Files.walkFileTree(rootDir, visitor);
132+
assertFalse(Files.exists(targetDir));
133+
assertFalse(Files.exists(symlinkDir));
134+
assertFalse(Files.exists(rootDir));
135+
assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
136+
}
137+
138+
/**
139+
* Tests https://issues.apache.org/jira/browse/IO-850
140+
*/
141+
@Test
142+
public void testIO850DirectoriesAndFiles() throws IOException {
143+
final Path rootDir = Files.createDirectory(managedTempDirPath.resolve("IO850"));
144+
createTempSymlinkedRelativeDir(rootDir);
145+
final Path targetDir = rootDir.resolve(SUB_DIR);
146+
final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
147+
Files.write(targetDir.resolve("file0.txt"), "Hello".getBytes(StandardCharsets.UTF_8));
148+
final Path subDir0 = Files.createDirectory(targetDir.resolve("subDir0"));
149+
Files.write(subDir0.resolve("file1.txt"), "Hello".getBytes(StandardCharsets.UTF_8));
150+
final DeletingPathVisitor visitor = DeletingPathVisitor.withLongCounters();
151+
Files.walkFileTree(rootDir, visitor);
152+
assertFalse(Files.exists(targetDir));
153+
assertFalse(Files.exists(symlinkDir));
154+
assertFalse(Files.exists(rootDir));
155+
assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
156+
assertTrue(visitor.getPathCounters().getFileCounter().get() > 0);
157+
}
116158
}

0 commit comments

Comments
 (0)