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.File;
23  import java.io.IOException;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.function.Supplier;
28  import java.util.stream.Stream;
29  
30  import org.apache.commons.io.filefilter.AndFileFilter;
31  import org.apache.commons.io.filefilter.DirectoryFileFilter;
32  import org.apache.commons.io.filefilter.EmptyFileFilter;
33  import org.apache.commons.io.filefilter.PathVisitorFileFilter;
34  import org.junit.jupiter.api.io.TempDir;
35  import org.junit.jupiter.params.ParameterizedTest;
36  import org.junit.jupiter.params.provider.Arguments;
37  import org.junit.jupiter.params.provider.MethodSource;
38  
39  /**
40   * Tests {@link PathUtils}.
41   */
42  public class PathUtilsVisitorTest {
43  
44      static Stream<Arguments> testParameters() {
45          return AccumulatorPathVisitorTest.testParameters();
46      }
47  
48      @TempDir
49      File tempDirFile;
50  
51      /**
52       * Tests an empty folder.
53       */
54      @ParameterizedTest
55      @MethodSource("testParameters")
56      public void testCountEmptyFolder(final Supplier<AccumulatorPathVisitor> supplier) throws IOException {
57          final Path tempDir = tempDirFile.toPath();
58          final CountingPathVisitor countingPathVisitor = CountingPathVisitor.withLongCounters();
59          final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(countingPathVisitor);
60          Files.walkFileTree(tempDir,
61              new AndFileFilter(countingFileFilter, DirectoryFileFilter.INSTANCE, EmptyFileFilter.EMPTY));
62          assertCounts(1, 0, 0, countingPathVisitor.getPathCounters());
63      }
64  
65      /**
66       * Tests a directory with one file of size 0.
67       */
68      @ParameterizedTest
69      @MethodSource("testParameters")
70      public void testCountFolders1FileSize0(final Supplier<AccumulatorPathVisitor> supplier) throws IOException {
71          final CountingPathVisitor countingPathVisitor = CountingPathVisitor.withLongCounters();
72          final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(countingPathVisitor);
73          Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"),
74              countingFileFilter);
75          assertCounts(1, 1, 0, countingPathVisitor.getPathCounters());
76      }
77  
78      /**
79       * Tests a directory with one file of size 1.
80       */
81      @ParameterizedTest
82      @MethodSource("testParameters")
83      public void testCountFolders1FileSize1(final Supplier<AccumulatorPathVisitor> supplier) throws IOException {
84          final CountingPathVisitor countingPathVisitor = CountingPathVisitor.withLongCounters();
85          final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(countingPathVisitor);
86          Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"),
87              countingFileFilter);
88          assertCounts(1, 1, 1, countingPathVisitor.getPathCounters());
89      }
90  
91      /**
92       * Tests a directory with two subdirectories, each containing one file of size 1.
93       */
94      @ParameterizedTest
95      @MethodSource("testParameters")
96      public void testCountFolders2FileSize2(final Supplier<AccumulatorPathVisitor> supplier) throws IOException {
97          final CountingPathVisitor countingPathVisitor = CountingPathVisitor.withLongCounters();
98          final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(countingPathVisitor);
99          Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"),
100             countingFileFilter);
101         assertCounts(3, 2, 2, countingPathVisitor.getPathCounters());
102     }
103 }