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