|
| 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 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 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 | + |
| 30 | +import org.apache.commons.io.filefilter.AndFileFilter; |
| 31 | +import org.apache.commons.io.filefilter.DirectoryFileFilter; |
| 32 | +import org.apache.commons.io.filefilter.EmptyFileFilter; |
| 33 | +import org.apache.commons.io.filefilter.PathVisitorFileFilter; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.io.TempDir; |
| 36 | + |
| 37 | +public class AccumulatorPathVisitorTest { |
| 38 | + |
| 39 | + @TempDir |
| 40 | + File tempDirFile; |
| 41 | + |
| 42 | + /** |
| 43 | + * Tests an empty folder. |
| 44 | + */ |
| 45 | + @Test |
| 46 | + public void testEmptyFolder() throws IOException { |
| 47 | + final Path tempDir = tempDirFile.toPath(); |
| 48 | + final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters(); |
| 49 | + final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor); |
| 50 | + Files.walkFileTree(tempDir, |
| 51 | + new AndFileFilter(countingFileFilter, DirectoryFileFilter.INSTANCE, EmptyFileFilter.EMPTY)); |
| 52 | + assertCounts(1, 0, 0, accPathVisitor.getPathCounters()); |
| 53 | + assertEquals(1, accPathVisitor.getDirList().size()); |
| 54 | + assertTrue(accPathVisitor.getFileList().isEmpty()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Tests a directory with one file of size 0. |
| 59 | + */ |
| 60 | + @Test |
| 61 | + public void testFolders1FileSize0() throws IOException { |
| 62 | + final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters(); |
| 63 | + final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor); |
| 64 | + Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), |
| 65 | + countingFileFilter); |
| 66 | + assertCounts(1, 1, 0, accPathVisitor.getPathCounters()); |
| 67 | + assertEquals(1, accPathVisitor.getDirList().size()); |
| 68 | + assertEquals(1, accPathVisitor.getFileList().size()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Tests a directory with one file of size 1. |
| 73 | + */ |
| 74 | + @Test |
| 75 | + public void testFolders1FileSize1() throws IOException { |
| 76 | + final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters(); |
| 77 | + final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor); |
| 78 | + Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), |
| 79 | + countingFileFilter); |
| 80 | + assertCounts(1, 1, 1, accPathVisitor.getPathCounters()); |
| 81 | + assertEquals(1, accPathVisitor.getDirList().size()); |
| 82 | + assertEquals(1, accPathVisitor.getFileList().size()); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tests a directory with two subdirectories, each containing one file of size 1. |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void testFolders2FileSize2() throws IOException { |
| 90 | + final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters(); |
| 91 | + final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor); |
| 92 | + Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), |
| 93 | + countingFileFilter); |
| 94 | + assertCounts(3, 2, 2, accPathVisitor.getPathCounters()); |
| 95 | + assertEquals(3, accPathVisitor.getDirList().size()); |
| 96 | + assertEquals(2, accPathVisitor.getFileList().size()); |
| 97 | + |
| 98 | + } |
| 99 | +} |
0 commit comments