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  
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.MethodSource;
27  
28  /**
29   * Tests {@link CountingPathVisitor}.
30   */
31  public class CountingPathVisitorTest extends TestArguments {
32  
33      private void checkZeroCounts(final CountingPathVisitor visitor) {
34          Assertions.assertEquals(CountingPathVisitor.withLongCounters(), visitor);
35          Assertions.assertEquals(CountingPathVisitor.withBigIntegerCounters(), visitor);
36      }
37  
38      /**
39       * Tests an empty folder.
40       */
41      @ParameterizedTest
42      @MethodSource("countingPathVisitors")
43      public void testCountEmptyFolder(final CountingPathVisitor visitor) throws IOException {
44          checkZeroCounts(visitor);
45          try (TempDirectory tempDir = TempDirectory.create(getClass().getCanonicalName())) {
46              assertCounts(1, 0, 0, PathUtils.visitFileTree(visitor, tempDir.get()));
47          }
48      }
49  
50      /**
51       * Tests a directory with one file of size 0.
52       */
53      @ParameterizedTest
54      @MethodSource("countingPathVisitors")
55      public void testCountFolders1FileSize0(final CountingPathVisitor visitor) throws IOException {
56          checkZeroCounts(visitor);
57          assertCounts(1, 1, 0, PathUtils.visitFileTree(visitor,
58                  "src/test/resources/org/apache/commons/io/dirs-1-file-size-0"));
59      }
60  
61      /**
62       * Tests a directory with one file of size 1.
63       */
64      @ParameterizedTest
65      @MethodSource("countingPathVisitors")
66      public void testCountFolders1FileSize1(final CountingPathVisitor visitor) throws IOException {
67          checkZeroCounts(visitor);
68          assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor,
69                  "src/test/resources/org/apache/commons/io/dirs-1-file-size-1"));
70      }
71  
72      /**
73       * Tests a directory with two subdirectories, each containing one file of size 1.
74       */
75      @ParameterizedTest
76      @MethodSource("countingPathVisitors")
77      public void testCountFolders2FileSize2(final CountingPathVisitor visitor) throws IOException {
78          checkZeroCounts(visitor);
79          assertCounts(3, 2, 2, PathUtils.visitFileTree(visitor,
80                  "src/test/resources/org/apache/commons/io/dirs-2-file-size-2"));
81      }
82  
83      @ParameterizedTest
84      @MethodSource("countingPathVisitors")
85      void testToString(final CountingPathVisitor visitor) {
86          // Make sure it does not blow up
87          visitor.toString();
88      }
89  }