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  
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   * Tests {@link DeletingPathVisitor}.
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       * Tests an empty folder.
48       */
49      @ParameterizedTest
50      @MethodSource("deletingPathVisitors")
51      public void testDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException {
52          applyDeleteEmptyDirectory(visitor);
53          // This will throw if not empty.
54          Files.deleteIfExists(tempDir);
55      }
56  
57      /**
58       * Tests an empty folder.
59       */
60      @ParameterizedTest
61      @MethodSource("pathCounters")
62      public void testDeleteEmptyDirectoryNullCtorArg(final PathCounters pathCounters) throws IOException {
63          applyDeleteEmptyDirectory(new DeletingPathVisitor(pathCounters, (String[]) null));
64          // This will throw if not empty.
65          Files.deleteIfExists(tempDir);
66      }
67  
68      /**
69       * Tests a directory with one file of size 0.
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          // This will throw if not empty.
77          Files.deleteIfExists(tempDir);
78      }
79  
80      /**
81       * Tests a directory with one file of size 1.
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          // This will throw if not empty.
89          Files.deleteIfExists(tempDir);
90      }
91  
92      /**
93       * Tests a directory with one file of size 1 but skip that file.
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      * Tests a directory with two subdirectories, each containing one file of size 1.
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         // This will throw if not empty.
116         Files.deleteIfExists(tempDir);
117     }
118 }