Skip to content

[IO-875] CopyDirectoryVisitor ignores fileFilter #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static CopyOption[] toCopyOption(final CopyOption... copyOptions) {
private final Path targetDirectory;

/**
* Constructs a instance that deletes files except for the files and directories explicitly given.
* Constructs an instance that copies all files.
*
* @param pathCounter How to count visits.
* @param sourceDirectory The source directory
Expand All @@ -60,7 +60,7 @@ public CopyDirectoryVisitor(final PathCounters pathCounter, final Path sourceDir
}

/**
* Constructs a instance that deletes files except for the files and directories explicitly given.
* Constructs an instance that copies files matching the given file and directory filters.
*
* @param pathCounter How to count visits.
* @param fileFilter How to filter file paths.
Expand Down Expand Up @@ -171,8 +171,11 @@ private Path resolveRelativeAsString(final Path directory) {
@Override
public FileVisitResult visitFile(final Path sourceFile, final BasicFileAttributes attributes) throws IOException {
final Path targetFile = resolveRelativeAsString(sourceFile);
copy(sourceFile, targetFile);
return super.visitFile(targetFile, attributes);
if (accept(sourceFile, attributes)) {
copy(sourceFile, targetFile);
updateFileCounters(targetFile, attributes);
}
return FileVisitResult.CONTINUE;
}

}
16 changes: 14 additions & 2 deletions src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ protected void updateDirCounter(final Path dir, final IOException exc) {
pathCounters.getDirectoryCounter().increment();
}

/**
* Returns true to copy the given file, false if not.
*
* @param file the visited file.
* @param attributes the visited file attributes.
* @return true to copy the given file, false if not.
*/
protected boolean accept(final Path file, final BasicFileAttributes attributes) {
// Note: A file can be a symbolic link to a directory.
return Files.exists(file) && fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE;
}

/**
* Updates the counters for visiting the given file.
*
Expand All @@ -303,10 +315,10 @@ protected void updateFileCounters(final Path file, final BasicFileAttributes att

@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
// Note: A file can be a symbolic link to a directory.
if (Files.exists(file) && fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE) {
if (accept(file, attributes)) {
updateFileCounters(file, attributes);
}
return FileVisitResult.CONTINUE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.function.Supplier;

import org.apache.commons.io.file.Counters.PathCounters;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Tests {@link CountingPathVisitor}.
* Tests {@link CopyDirectoryVisitor}.
*/
public class CopyDirectoryVisitorTest extends TestArguments {

Expand All @@ -59,19 +63,22 @@ public void testCopyDirectoryEmptyFolder(final PathCounters pathCounters) throws
assertEquals(sourceDir.get(), ((AbstractPathWrapper) visitFileTree.getSourceDirectory()).get());
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
assertEquals(targetDir, visitFileTree.getTargetDirectory());
assertEquals(targetDir, visitFileTree.getTargetDirectory());
//
// Tests equals and hashCode
assertEquals(visitFileTree, supplier.get());
assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
assertEquals(visitFileTree, visitFileTree);
assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
assertNotEquals(visitFileTree, "not");
assertNotEquals(visitFileTree, new DeletingPathVisitor(pathCounters));
assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir));
assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, sourceDir, sourceDir, EXPECTED_COPY_OPTIONS));
assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, targetDir, sourceDir, EXPECTED_COPY_OPTIONS));
assertNotEquals(visitFileTree, CountingPathVisitor.withLongCounters());
}
}

/**
* Tests an empty folder.
* Tests an empty folder with filters.
*/
@ParameterizedTest
@MethodSource("pathCounters")
Expand All @@ -84,14 +91,34 @@ public void testCopyDirectoryEmptyFolderFilters(final PathCounters pathCounters)
assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
assertEquals(targetDir, visitFileTree.getTargetDirectory());
//
// Tests equals and hashCode
assertEquals(visitFileTree, supplier.get());
assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
assertEquals(visitFileTree, visitFileTree);
assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
}
}

/**
* Tests filters.
*/
@ParameterizedTest
@MethodSource("pathCounters")
public void testCopyDirectoryFilters(final PathCounters pathCounters) throws IOException {
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-4");
final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(new CopyDirectoryVisitor(pathCounters, new NameFileFilter("file-size-1.bin"),
new NameFileFilter("dirs-2-file-size-4", "dirs-a-file-size-1"), sourceDir, targetDir, null),
sourceDir);
assertCounts(2, 1, 2, visitFileTree);
assertArrayEquals(PathUtils.EMPTY_COPY_OPTIONS, visitFileTree.getCopyOptions());
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
assertEquals(targetDir, visitFileTree.getTargetDirectory());
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-1.bin")));
assertFalse(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-2.bin")));
assertFalse(Files.exists(targetDir.resolve("dirs-a-file-size-2")));
}


/**
* Tests a directory with one file of size 0.
*/
Expand All @@ -105,10 +132,10 @@ public void testCopyDirectoryFolders1FileSize0(final PathCounters pathCounters)
assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
assertEquals(targetDir, visitFileTree.getTargetDirectory());
//
assertTrue(Files.exists(targetDir.resolve("file-size-0.bin")));
// Tests equals and hashCode
assertEquals(visitFileTree, supplier.get());
assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
assertEquals(visitFileTree, visitFileTree);
assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
}

Expand All @@ -125,6 +152,7 @@ public void testCopyDirectoryFolders1FileSize1(final PathCounters pathCounters)
assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
assertEquals(targetDir, visitFileTree.getTargetDirectory());
assertTrue(Files.exists(targetDir.resolve("file-size-1.bin")));
}

/**
Expand All @@ -140,6 +168,8 @@ public void testCopyDirectoryFolders2FileSize2(final PathCounters pathCounters)
assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
assertEquals(targetDir, visitFileTree.getTargetDirectory());
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-1.bin")));
assertTrue(Files.exists(targetDir.resolve("dirs-b-file-size-1/file-size-1.bin")));
}

}
Loading