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 NameFileFilter}.
39   */
40  public class NameFileFilterTest {
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(new NameFileFilter("NOTICE.txt"));
51          // End of Javadoc example
52          assertEquals(1, files.length);
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          // We are interested in files older than one day
64          final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new NameFileFilter("NOTICE.txt"),
65              TrueFileFilter.INSTANCE);
66          //
67          // Walk one dir
68          Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
69          // System.out.println(visitor.getPathCounters());
70          // System.out.println(visitor.getFileList());
71          // System.out.println(visitor.getDirList());
72          assertEquals(1, visitor.getPathCounters().getFileCounter().get());
73          assertEquals(1, visitor.getPathCounters().getDirectoryCounter().get());
74          assertTrue(visitor.getPathCounters().getByteCounter().get() > 0);
75          assertFalse(visitor.getDirList().isEmpty());
76          assertFalse(visitor.getFileList().isEmpty());
77          assertEquals(1, visitor.getFileList().size());
78          //
79          visitor.getPathCounters().reset();
80          //
81          // Walk dir tree
82          Files.walkFileTree(dir, visitor);
83          // System.out.println(visitor.getPathCounters());
84          // System.out.println(visitor.getDirList());
85          // System.out.println(visitor.getFileList());
86          //
87          // End of Javadoc example
88          assertTrue(visitor.getPathCounters().getFileCounter().get() > 0);
89          assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
90          assertTrue(visitor.getPathCounters().getByteCounter().get() > 0);
91          // We counted and accumulated
92          assertFalse(visitor.getDirList().isEmpty());
93          assertFalse(visitor.getFileList().isEmpty());
94          //
95          assertNotEquals(Counters.noopPathCounters(), visitor.getPathCounters());
96          visitor.getPathCounters().reset();
97          CounterAssertions.assertZeroCounters(visitor.getPathCounters());
98      }
99  
100     @Test
101     public void testNoCounting() throws IOException {
102         final Path dir = Paths.get("");
103         final AccumulatorPathVisitor visitor = new AccumulatorPathVisitor(Counters.noopPathCounters(),
104             new NameFileFilter("NOTICE.txt"), TrueFileFilter.INSTANCE);
105         Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
106         //
107         CounterAssertions.assertZeroCounters(visitor.getPathCounters());
108         // We did not count, but we still accumulated
109         assertFalse(visitor.getDirList().isEmpty());
110         assertFalse(visitor.getFileList().isEmpty());
111     }
112 }