1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.file;
19
20 import static org.apache.commons.io.file.CounterAssertions.assertCounts;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotEquals;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31
32 import org.apache.commons.io.file.Counters.PathCounters;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.params.ParameterizedTest;
35 import org.junit.jupiter.params.provider.MethodSource;
36
37
38
39
40 class DeletingPathVisitorTest extends AbstractTempDirTest {
41
42 private static final String ARGS = "org.apache.commons.io.file.TestArguments#";
43
44 private void applyDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException {
45 Files.walkFileTree(tempDirPath, visitor);
46 assertCounts(1, 0, 0, visitor);
47 }
48
49
50
51
52 @ParameterizedTest
53 @MethodSource(ARGS + "deletingPathVisitors")
54 void testDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException {
55 applyDeleteEmptyDirectory(visitor);
56
57 Files.deleteIfExists(tempDirPath);
58 }
59
60
61
62
63 @ParameterizedTest
64 @MethodSource(ARGS + "pathCounters")
65 void testDeleteEmptyDirectoryNullCtorArg(final PathCounters pathCounters) throws IOException {
66 applyDeleteEmptyDirectory(new DeletingPathVisitor(pathCounters, (String[]) null));
67
68 Files.deleteIfExists(tempDirPath);
69 }
70
71
72
73
74 @ParameterizedTest
75 @MethodSource(ARGS + "deletingPathVisitors")
76 void testDeleteFolders1FileSize0(final DeletingPathVisitor visitor) throws IOException {
77 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDirPath);
78 assertCounts(1, 1, 0, PathUtils.visitFileTree(visitor, tempDirPath));
79
80 Files.deleteIfExists(tempDirPath);
81 }
82
83
84
85
86 @ParameterizedTest
87 @MethodSource(ARGS + "deletingPathVisitors")
88 void testDeleteFolders1FileSize1(final DeletingPathVisitor visitor) throws IOException {
89 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDirPath);
90 assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDirPath));
91
92 Files.deleteIfExists(tempDirPath);
93 }
94
95
96
97
98 @ParameterizedTest
99 @MethodSource(ARGS + "pathCounters")
100 void testDeleteFolders1FileSize1Skip(final PathCounters pathCounters) throws IOException {
101 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDirPath);
102 final String skipFileName = "file-size-1.bin";
103 final CountingPathVisitor visitor = new DeletingPathVisitor(pathCounters, skipFileName);
104 assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDirPath));
105 final Path skippedFile = tempDirPath.resolve(skipFileName);
106 assertTrue(Files.exists(skippedFile));
107 Files.delete(skippedFile);
108 }
109
110
111
112
113 @ParameterizedTest
114 @MethodSource(ARGS + "deletingPathVisitors")
115 void testDeleteFolders2FileSize2(final DeletingPathVisitor visitor) throws IOException {
116 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDirPath);
117 assertCounts(3, 2, 2, PathUtils.visitFileTree(visitor, tempDirPath));
118
119 Files.deleteIfExists(tempDirPath);
120 }
121
122 @Test
123 void testEqualsHashCode() {
124 final DeletingPathVisitor visitor0 = DeletingPathVisitor.withLongCounters();
125 final DeletingPathVisitor visitor1 = DeletingPathVisitor.withLongCounters();
126 assertEquals(visitor0, visitor0);
127 assertEquals(visitor0, visitor1);
128 assertEquals(visitor1, visitor0);
129 assertEquals(visitor0.hashCode(), visitor0.hashCode());
130 assertEquals(visitor0.hashCode(), visitor1.hashCode());
131 assertEquals(visitor1.hashCode(), visitor0.hashCode());
132 visitor0.getPathCounters().getByteCounter().increment();
133 assertEquals(visitor0, visitor0);
134 assertNotEquals(visitor0, visitor1);
135 assertNotEquals(visitor1, visitor0);
136 assertEquals(visitor0.hashCode(), visitor0.hashCode());
137 assertNotEquals(visitor0.hashCode(), visitor1.hashCode());
138 assertNotEquals(visitor1.hashCode(), visitor0.hashCode());
139 }
140
141
142
143
144 @Test
145 void testIO850DirectoriesAndFiles() throws IOException {
146 final Path rootDir = Files.createDirectory(managedTempDirPath.resolve("IO850"));
147 createTempSymbolicLinkedRelativeDir(rootDir);
148 final Path targetDir = rootDir.resolve(SUB_DIR);
149 final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
150 Files.write(targetDir.resolve("file0.txt"), "Hello".getBytes(StandardCharsets.UTF_8));
151 final Path subDir0 = Files.createDirectory(targetDir.resolve("subDir0"));
152 Files.write(subDir0.resolve("file1.txt"), "Hello".getBytes(StandardCharsets.UTF_8));
153 final DeletingPathVisitor visitor = DeletingPathVisitor.withLongCounters();
154 Files.walkFileTree(rootDir, visitor);
155 assertFalse(Files.exists(targetDir));
156 assertFalse(Files.exists(symlinkDir));
157 assertFalse(Files.exists(rootDir));
158 assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
159 assertTrue(visitor.getPathCounters().getFileCounter().get() > 0);
160 }
161
162
163
164
165 @Test
166 void testIO850DirectoriesOnly() throws IOException {
167 final Path rootDir = Files.createDirectory(managedTempDirPath.resolve("IO850"));
168 createTempSymbolicLinkedRelativeDir(rootDir);
169 final Path targetDir = rootDir.resolve(SUB_DIR);
170 final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
171 final DeletingPathVisitor visitor = DeletingPathVisitor.withLongCounters();
172 Files.walkFileTree(rootDir, visitor);
173 assertFalse(Files.exists(targetDir));
174 assertFalse(Files.exists(symlinkDir));
175 assertFalse(Files.exists(rootDir));
176 assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
177 }
178 }