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