AccumulatorPathVisitor does not track directories properly.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -63,6 +63,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Michiel Kalkman">
FileUtils#copyDirectory(File, File, FileFilter, preserveFileDate) clean up #163.
</action>
+ <action dev="ggregory" type="fix" due-to="Michiel Kalkman">
+ AccumulatorPathVisitor does not track directories properly.
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add FileSystemProviders class.

MODIFY
src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java
src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java
import java.io.IOException;
import java.nio.file.FileVisitResult;
-import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
@@ -70,6 +69,10 @@ public AccumulatorPathVisitor(final PathCounters pathCounter) {
super(pathCounter);
}
+ private void add(final List<Path> list, final Path dir) {
+ list.add(dir.normalize());
+ }
+
@Override
public boolean equals(final Object obj) {
if (this == obj) {
@@ -111,6 +114,12 @@ public int hashCode() {
return result;
}
+ @Override
+ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
+ add(dirList, dir);
+ return super.postVisitDirectory(dir, exc);
+ }
+
/**
* Relativizes each directory path with {@link Path#relativize(Path)} against the given {@code parent}, optionally
* sorting the result.
@@ -120,7 +129,8 @@ public int hashCode() {
* @param comparator How to sort, null uses default sorting.
* @return A new list
*/
- public List<Path> relativizeDirectories(final Path parent, final boolean sort, final Comparator<? super Path> comparator) {
+ public List<Path> relativizeDirectories(final Path parent, final boolean sort,
+ final Comparator<? super Path> comparator) {
return PathUtils.relativize(getDirList(), parent, sort, comparator);
}
@@ -133,13 +143,14 @@ public int hashCode() {
* @param comparator How to sort, null uses default sorting.
* @return A new list
*/
- public List<Path> relativizeFiles(final Path parent, final boolean sort, final Comparator<? super Path> comparator) {
+ public List<Path> relativizeFiles(final Path parent, final boolean sort,
+ final Comparator<? super Path> comparator) {
return PathUtils.relativize(getFileList(), parent, sort, comparator);
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
- ((Files.isDirectory(file)) ? dirList : fileList).add(file.normalize());
+ add(fileList, file);
return super.visitFile(file, attributes);
}

ADD
src/test/java/org/apache/commons/io/file/AccumulatorPathVisitorTest.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.file;
+
+import static org.apache.commons.io.file.CounterAssertions.assertCounts;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.commons.io.filefilter.AndFileFilter;
+import org.apache.commons.io.filefilter.DirectoryFileFilter;
+import org.apache.commons.io.filefilter.EmptyFileFilter;
+import org.apache.commons.io.filefilter.PathVisitorFileFilter;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+public class AccumulatorPathVisitorTest {
+
+ @TempDir
+ File tempDirFile;
+
+ /**
+ * Tests an empty folder.
+ */
+ @Test
+ public void testEmptyFolder() throws IOException {
+ final Path tempDir = tempDirFile.toPath();
+ final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters();
+ final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor);
+ Files.walkFileTree(tempDir,
+ new AndFileFilter(countingFileFilter, DirectoryFileFilter.INSTANCE, EmptyFileFilter.EMPTY));
+ assertCounts(1, 0, 0, accPathVisitor.getPathCounters());
+ assertEquals(1, accPathVisitor.getDirList().size());
+ assertTrue(accPathVisitor.getFileList().isEmpty());
+ }
+
+ /**
+ * Tests a directory with one file of size 0.
+ */
+ @Test
+ public void testFolders1FileSize0() throws IOException {
+ final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters();
+ final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor);
+ Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"),
+ countingFileFilter);
+ assertCounts(1, 1, 0, accPathVisitor.getPathCounters());
+ assertEquals(1, accPathVisitor.getDirList().size());
+ assertEquals(1, accPathVisitor.getFileList().size());
+ }
+
+ /**
+ * Tests a directory with one file of size 1.
+ */
+ @Test
+ public void testFolders1FileSize1() throws IOException {
+ final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters();
+ final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor);
+ Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"),
+ countingFileFilter);
+ assertCounts(1, 1, 1, accPathVisitor.getPathCounters());
+ assertEquals(1, accPathVisitor.getDirList().size());
+ assertEquals(1, accPathVisitor.getFileList().size());
+ }
+
+ /**
+ * Tests a directory with two subdirectories, each containing one file of size 1.
+ */
+ @Test
+ public void testFolders2FileSize2() throws IOException {
+ final AccumulatorPathVisitor accPathVisitor = AccumulatorPathVisitor.withLongCounters();
+ final PathVisitorFileFilter countingFileFilter = new PathVisitorFileFilter(accPathVisitor);
+ Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"),
+ countingFileFilter);
+ assertCounts(3, 2, 2, accPathVisitor.getPathCounters());
+ assertEquals(3, accPathVisitor.getDirList().size());
+ assertEquals(2, accPathVisitor.getFileList().size());
+
+ }
+}

AccumulatorPathVisitor does not track directories properly.