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.assertNotEquals;
23 import static org.junit.jupiter.api.Assertions.assertSame;
24
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29
30 import org.apache.commons.io.file.Counters.PathCounters;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.io.TempDir;
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.MethodSource;
35
36
37
38
39 public class CleaningPathVisitorTest extends TestArguments {
40
41 @TempDir
42 private Path tempDir;
43
44 private void applyCleanEmptyDirectory(final CleaningPathVisitor visitor) throws IOException {
45 Files.walkFileTree(tempDir, visitor);
46 assertCounts(1, 0, 0, visitor);
47 }
48
49
50
51
52 @ParameterizedTest
53 @MethodSource("cleaningPathVisitors")
54 public void testCleanEmptyDirectory(final CleaningPathVisitor visitor) throws IOException {
55 applyCleanEmptyDirectory(visitor);
56 }
57
58
59
60
61 @ParameterizedTest
62 @MethodSource("pathCounters")
63 public void testCleanEmptyDirectoryNullCtorArg(final PathCounters pathCounters) throws IOException {
64 applyCleanEmptyDirectory(new CleaningPathVisitor(pathCounters, (String[]) null));
65 }
66
67
68
69
70 @ParameterizedTest
71 @MethodSource("cleaningPathVisitors")
72 public void testCleanFolders1FileSize0(final CleaningPathVisitor visitor) throws IOException {
73 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDir);
74 final CleaningPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
75 assertCounts(1, 1, 0, visitFileTree);
76 assertSame(visitor, visitFileTree);
77
78 assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
79 assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
80 assertEquals(visitFileTree, visitFileTree);
81 assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
82 }
83
84
85
86
87 @ParameterizedTest
88 @MethodSource("cleaningPathVisitors")
89 public void testCleanFolders1FileSize1(final CleaningPathVisitor visitor) throws IOException {
90 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir);
91 final CleaningPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
92 assertCounts(1, 1, 1, visitFileTree);
93 assertSame(visitor, visitFileTree);
94
95 assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
96 assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
97 assertEquals(visitFileTree, visitFileTree);
98 assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
99 }
100
101
102
103
104 @ParameterizedTest
105 @MethodSource("pathCounters")
106 public void testCleanFolders1FileSize1Skip(final PathCounters pathCounters) throws IOException {
107 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir);
108 final String skipFileName = "file-size-1.bin";
109 final CountingPathVisitor visitor = new CleaningPathVisitor(pathCounters, skipFileName);
110 final CountingPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
111 assertCounts(1, 1, 1, visitFileTree);
112 assertSame(visitor, visitFileTree);
113 final Path skippedFile = tempDir.resolve(skipFileName);
114 Assertions.assertTrue(Files.exists(skippedFile));
115 Files.delete(skippedFile);
116
117 assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
118 assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
119 assertEquals(visitFileTree, visitFileTree);
120 assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
121 }
122
123
124
125
126 @ParameterizedTest
127 @MethodSource("cleaningPathVisitors")
128 public void testCleanFolders2FileSize2(final CleaningPathVisitor visitor) throws IOException {
129 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDir);
130 final CleaningPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
131 assertCounts(3, 2, 2, visitFileTree);
132 assertSame(visitor, visitFileTree);
133
134 assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
135 assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
136 assertEquals(visitFileTree, visitFileTree);
137 assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
138 }
139 }