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  package org.apache.commons.io.filefilter;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.nio.file.Paths;
29  import java.util.Collections;
30  
31  import org.apache.commons.io.FileUtils;
32  import org.apache.commons.io.file.AccumulatorPathVisitor;
33  import org.apache.commons.io.file.CounterAssertions;
34  import org.apache.commons.io.file.Counters;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Tests {@link DirectoryFileFilter}.
39   */
40  public class DirectoryFileFilterTest {
41  
42      /**
43       * Javadoc example.
44       *
45       * System.out calls are commented out here but not in the Javadoc.
46       */
47      @Test
48      public void testJavadocExampleUsingIo() {
49          final File dir = FileUtils.current();
50          final String[] files = dir.list(DirectoryFileFilter.INSTANCE);
51          // End of Javadoc example
52          assertTrue(files.length > 0);
53      }
54  
55      /**
56       * Javadoc example.
57       *
58       * System.out calls are commented out here but not in the Javadoc.
59       */
60      @Test
61      public void testJavadocExampleUsingNio() throws IOException {
62          final Path dir = Paths.get("");
63          final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(DirectoryFileFilter.INSTANCE,
64              TrueFileFilter.INSTANCE);
65          //
66          // Walk one dir
67          Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
68          // System.out.println(visitor.getPathCounters());
69          // System.out.println(visitor.getFileList());
70          //
71          visitor.getPathCounters().reset();
72          //
73          // Walk dir tree
74          Files.walkFileTree(dir, visitor);
75          // System.out.println(visitor.getPathCounters());
76          // System.out.println(visitor.getDirList());
77          // System.out.println(visitor.getFileList());
78          //
79          // End of Javadoc example
80          assertEquals(0, visitor.getPathCounters().getFileCounter().get());
81          assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
82          assertEquals(0, visitor.getPathCounters().getByteCounter().get());
83          // We counted and accumulated
84          assertFalse(visitor.getDirList().isEmpty());
85          assertFalse(visitor.getFileList().isEmpty());
86          //
87          assertNotEquals(Counters.noopPathCounters(), visitor.getPathCounters());
88          visitor.getPathCounters().reset();
89          CounterAssertions.assertZeroCounters(visitor.getPathCounters());
90      }
91  
92  }