|
| 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.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 NameFileFilter}. |
| 38 | + */ |
| 39 | +public class NameFileFilterTest { |
| 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 | + File dir = new File("."); |
| 49 | + String[] files = dir.list(new NameFileFilter("NOTICE.txt")); |
| 50 | + for (String file : files) { |
| 51 | + // System.out.println(file); |
| 52 | + } |
| 53 | + // End of Javadoc example |
| 54 | + assertEquals(1, files.length); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Javadoc example. |
| 59 | + * |
| 60 | + * System.out calls are commented out here but not in the Javadoc. |
| 61 | + */ |
| 62 | + @Test |
| 63 | + public void testJavadocExampleUsingNio() throws IOException { |
| 64 | + final Path dir = Paths.get("."); |
| 65 | + // We are interested in files older than one day |
| 66 | + final AccumulatorPathVisitor visitor = AccumulatorPathVisitor |
| 67 | + .withLongCounters(new NameFileFilter("NOTICE.txt")); |
| 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 | + // System.out.println(visitor.getDirList()); |
| 74 | + assertEquals(1, visitor.getPathCounters().getFileCounter().get()); |
| 75 | + assertEquals(1, visitor.getPathCounters().getDirectoryCounter().get()); |
| 76 | + assertTrue(visitor.getPathCounters().getByteCounter().get() > 0); |
| 77 | + assertFalse(visitor.getDirList().isEmpty()); |
| 78 | + assertFalse(visitor.getFileList().isEmpty()); |
| 79 | + assertEquals(1, visitor.getFileList().size()); |
| 80 | + // |
| 81 | + visitor.getPathCounters().reset(); |
| 82 | + // |
| 83 | + // Walk dir tree |
| 84 | + Files.walkFileTree(dir, visitor); |
| 85 | + // System.out.println(visitor.getPathCounters()); |
| 86 | + // System.out.println(visitor.getDirList()); |
| 87 | + // System.out.println(visitor.getFileList()); |
| 88 | + // |
| 89 | + // End of Javadoc example |
| 90 | + assertTrue(visitor.getPathCounters().getFileCounter().get() > 0); |
| 91 | + assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0); |
| 92 | + assertTrue(visitor.getPathCounters().getByteCounter().get() > 0); |
| 93 | + // We counted and accumulated |
| 94 | + assertFalse(visitor.getDirList().isEmpty()); |
| 95 | + assertFalse(visitor.getFileList().isEmpty()); |
| 96 | + // |
| 97 | + assertNotEquals(Counters.noopPathCounters(), visitor.getPathCounters()); |
| 98 | + visitor.getPathCounters().reset(); |
| 99 | + CounterAssertions.assertZeroCounters(visitor.getPathCounters()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testNoCounting() throws IOException { |
| 104 | + final Path dir = Paths.get("."); |
| 105 | + final AccumulatorPathVisitor visitor = new AccumulatorPathVisitor(Counters.noopPathCounters(), |
| 106 | + new NameFileFilter("NOTICE.txt")); |
| 107 | + Files.walkFileTree(dir, Collections.emptySet(), 1, visitor); |
| 108 | + // |
| 109 | + CounterAssertions.assertZeroCounters(visitor.getPathCounters()); |
| 110 | + // We did not count, but we still accumulated |
| 111 | + assertFalse(visitor.getDirList().isEmpty()); |
| 112 | + assertFalse(visitor.getFileList().isEmpty()); |
| 113 | + } |
| 114 | +} |
0 commit comments