View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link DeletingPathVisitor}.
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       * Tests an empty folder.
51       */
52      @ParameterizedTest
53      @MethodSource("cleaningPathVisitors")
54      public void testCleanEmptyDirectory(final CleaningPathVisitor visitor) throws IOException {
55          applyCleanEmptyDirectory(visitor);
56      }
57  
58      /**
59       * Tests an empty folder.
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       * Tests a directory with one file of size 0.
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       * Tests a directory with one file of size 1.
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      * Tests a directory with one file of size 1 but skip that file.
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      * Tests a directory with two subdirectories, each containing one file of size 1.
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 }