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.Test;
33  import org.junit.jupiter.api.io.TempDir;
34  import org.junit.jupiter.params.ParameterizedTest;
35  import org.junit.jupiter.params.provider.MethodSource;
36  
37  /**
38   * Tests {@link DeletingPathVisitor}.
39   */
40  public class CleaningPathVisitorTest extends TestArguments {
41  
42      @TempDir
43      private Path tempDir;
44  
45      private void applyCleanEmptyDirectory(final CleaningPathVisitor visitor) throws IOException {
46          Files.walkFileTree(tempDir, visitor);
47          assertCounts(1, 0, 0, visitor);
48      }
49  
50      /**
51       * Tests an empty folder.
52       */
53      @ParameterizedTest
54      @MethodSource("cleaningPathVisitors")
55      public void testCleanEmptyDirectory(final CleaningPathVisitor visitor) throws IOException {
56          applyCleanEmptyDirectory(visitor);
57      }
58  
59      /**
60       * Tests an empty folder.
61       */
62      @ParameterizedTest
63      @MethodSource("pathCounters")
64      public void testCleanEmptyDirectoryNullCtorArg(final PathCounters pathCounters) throws IOException {
65          applyCleanEmptyDirectory(new CleaningPathVisitor(pathCounters, (String[]) null));
66      }
67  
68      /**
69       * Tests a directory with one file of size 0.
70       */
71      @ParameterizedTest
72      @MethodSource("cleaningPathVisitors")
73      public void testCleanFolders1FileSize0(final CleaningPathVisitor visitor) throws IOException {
74          PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDir);
75          final CleaningPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
76          assertCounts(1, 1, 0, visitFileTree);
77          assertSame(visitor, visitFileTree);
78          //
79          assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
80          assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
81          assertEquals(visitFileTree, visitFileTree);
82          assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
83      }
84  
85      /**
86       * Tests a directory with one file of size 1.
87       */
88      @ParameterizedTest
89      @MethodSource("cleaningPathVisitors")
90      public void testCleanFolders1FileSize1(final CleaningPathVisitor visitor) throws IOException {
91          PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir);
92          final CleaningPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
93          assertCounts(1, 1, 1, visitFileTree);
94          assertSame(visitor, visitFileTree);
95          //
96          assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
97          assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
98          assertEquals(visitFileTree, visitFileTree);
99          assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
100     }
101 
102     /**
103      * Tests a directory with one file of size 1 but skip that file.
104      */
105     @ParameterizedTest
106     @MethodSource("pathCounters")
107     public void testCleanFolders1FileSize1Skip(final PathCounters pathCounters) throws IOException {
108         PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir);
109         final String skipFileName = "file-size-1.bin";
110         final CountingPathVisitor visitor = new CleaningPathVisitor(pathCounters, skipFileName);
111         final CountingPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
112         assertCounts(1, 1, 1, visitFileTree);
113         assertSame(visitor, visitFileTree);
114         final Path skippedFile = tempDir.resolve(skipFileName);
115         Assertions.assertTrue(Files.exists(skippedFile));
116         Files.delete(skippedFile);
117         //
118         assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
119         assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
120         assertEquals(visitFileTree, visitFileTree);
121         assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
122     }
123 
124     /**
125      * Tests a directory with two subdirectories, each containing one file of size 1.
126      */
127     @ParameterizedTest
128     @MethodSource("cleaningPathVisitors")
129     public void testCleanFolders2FileSize2(final CleaningPathVisitor visitor) throws IOException {
130         PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDir);
131         final CleaningPathVisitor visitFileTree = PathUtils.visitFileTree(visitor, tempDir);
132         assertCounts(3, 2, 2, visitFileTree);
133         assertSame(visitor, visitFileTree);
134         //
135         assertNotEquals(visitFileTree, CleaningPathVisitor.withLongCounters());
136         assertNotEquals(visitFileTree.hashCode(), CleaningPathVisitor.withLongCounters().hashCode());
137         assertEquals(visitFileTree, visitFileTree);
138         assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
139     }
140 
141     @Test
142     public void testEqualsHashCode() {
143         final CountingPathVisitor visitor0 = CleaningPathVisitor.withLongCounters();
144         final CountingPathVisitor visitor1 = CleaningPathVisitor.withLongCounters();
145         assertEquals(visitor0, visitor0);
146         assertEquals(visitor0, visitor1);
147         assertEquals(visitor1, visitor0);
148         assertEquals(visitor0.hashCode(), visitor0.hashCode());
149         assertEquals(visitor0.hashCode(), visitor1.hashCode());
150         assertEquals(visitor1.hashCode(), visitor0.hashCode());
151         visitor0.getPathCounters().getByteCounter().increment();
152         assertEquals(visitor0, visitor0);
153         assertNotEquals(visitor0, visitor1);
154         assertNotEquals(visitor1, visitor0);
155         assertEquals(visitor0.hashCode(), visitor0.hashCode());
156         assertNotEquals(visitor0.hashCode(), visitor1.hashCode());
157         assertNotEquals(visitor1.hashCode(), visitor0.hashCode());
158     }
159 }