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
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26
27 import org.apache.commons.io.file.Counters.PathCounters;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.io.TempDir;
30 import org.junit.jupiter.params.ParameterizedTest;
31 import org.junit.jupiter.params.provider.MethodSource;
32
33
34
35
36 public class DeletingPathVisitorTest extends TestArguments {
37
38 @TempDir
39 private Path tempDir;
40
41 private void applyDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException {
42 Files.walkFileTree(tempDir, visitor);
43 assertCounts(1, 0, 0, visitor);
44 }
45
46
47
48
49 @ParameterizedTest
50 @MethodSource("deletingPathVisitors")
51 public void testDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException {
52 applyDeleteEmptyDirectory(visitor);
53
54 Files.deleteIfExists(tempDir);
55 }
56
57
58
59
60 @ParameterizedTest
61 @MethodSource("pathCounters")
62 public void testDeleteEmptyDirectoryNullCtorArg(final PathCounters pathCounters) throws IOException {
63 applyDeleteEmptyDirectory(new DeletingPathVisitor(pathCounters, (String[]) null));
64
65 Files.deleteIfExists(tempDir);
66 }
67
68
69
70
71 @ParameterizedTest
72 @MethodSource("deletingPathVisitors")
73 public void testDeleteFolders1FileSize0(final DeletingPathVisitor visitor) throws IOException {
74 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDir);
75 assertCounts(1, 1, 0, PathUtils.visitFileTree(visitor, tempDir));
76
77 Files.deleteIfExists(tempDir);
78 }
79
80
81
82
83 @ParameterizedTest
84 @MethodSource("deletingPathVisitors")
85 public void testDeleteFolders1FileSize1(final DeletingPathVisitor visitor) throws IOException {
86 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir);
87 assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDir));
88
89 Files.deleteIfExists(tempDir);
90 }
91
92
93
94
95 @ParameterizedTest
96 @MethodSource("pathCounters")
97 public void testDeleteFolders1FileSize1Skip(final PathCounters pathCounters) throws IOException {
98 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir);
99 final String skipFileName = "file-size-1.bin";
100 final CountingPathVisitor visitor = new DeletingPathVisitor(pathCounters, skipFileName);
101 assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDir));
102 final Path skippedFile = tempDir.resolve(skipFileName);
103 Assertions.assertTrue(Files.exists(skippedFile));
104 Files.delete(skippedFile);
105 }
106
107
108
109
110 @ParameterizedTest
111 @MethodSource("deletingPathVisitors")
112 public void testDeleteFolders2FileSize2(final DeletingPathVisitor visitor) throws IOException {
113 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDir);
114 assertCounts(3, 2, 2, PathUtils.visitFileTree(visitor, tempDir));
115
116 Files.deleteIfExists(tempDir);
117 }
118 }