Skip to content

Commit 5bcc9a8

Browse files
authored
[IO-875] CopyDirectoryVisitor ignores fileFilter (#743)
* [IO-875] CopyDirectoryVisitor ignores fileFilter * 100% test coverage for CopyDirectoryVisitor * Fix javadoc typo
1 parent 039230a commit 5bcc9a8

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

src/main/java/org/apache/commons/io/file/CopyDirectoryVisitor.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static CopyOption[] toCopyOption(final CopyOption... copyOptions) {
4545
private final Path targetDirectory;
4646

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

6262
/**
63-
* Constructs a instance that deletes files except for the files and directories explicitly given.
63+
* Constructs an instance that copies files matching the given file and directory filters.
6464
*
6565
* @param pathCounter How to count visits.
6666
* @param fileFilter How to filter file paths.
@@ -171,8 +171,11 @@ private Path resolveRelativeAsString(final Path directory) {
171171
@Override
172172
public FileVisitResult visitFile(final Path sourceFile, final BasicFileAttributes attributes) throws IOException {
173173
final Path targetFile = resolveRelativeAsString(sourceFile);
174-
copy(sourceFile, targetFile);
175-
return super.visitFile(targetFile, attributes);
174+
if (accept(sourceFile, attributes)) {
175+
copy(sourceFile, targetFile);
176+
updateFileCounters(targetFile, attributes);
177+
}
178+
return FileVisitResult.CONTINUE;
176179
}
177180

178181
}

src/main/java/org/apache/commons/io/file/CountingPathVisitor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,18 @@ protected void updateDirCounter(final Path dir, final IOException exc) {
290290
pathCounters.getDirectoryCounter().increment();
291291
}
292292

293+
/**
294+
* Returns true to copy the given file, false if not.
295+
*
296+
* @param file the visited file.
297+
* @param attributes the visited file attributes.
298+
* @return true to copy the given file, false if not.
299+
*/
300+
protected boolean accept(final Path file, final BasicFileAttributes attributes) {
301+
// Note: A file can be a symbolic link to a directory.
302+
return Files.exists(file) && fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE;
303+
}
304+
293305
/**
294306
* Updates the counters for visiting the given file.
295307
*
@@ -303,10 +315,10 @@ protected void updateFileCounters(final Path file, final BasicFileAttributes att
303315

304316
@Override
305317
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
306-
// Note: A file can be a symbolic link to a directory.
307-
if (Files.exists(file) && fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE) {
318+
if (accept(file, attributes)) {
308319
updateFileCounters(file, attributes);
309320
}
310321
return FileVisitResult.CONTINUE;
311322
}
323+
312324
}

src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@
2020
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
2121
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
2324
import static org.junit.jupiter.api.Assertions.assertNotEquals;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
2426

2527
import java.io.IOException;
2628
import java.nio.file.CopyOption;
29+
import java.nio.file.Files;
2730
import java.nio.file.Path;
2831
import java.nio.file.Paths;
2932
import java.nio.file.StandardCopyOption;
3033
import java.util.function.Supplier;
3134

3235
import org.apache.commons.io.file.Counters.PathCounters;
36+
import org.apache.commons.io.filefilter.NameFileFilter;
3337
import org.apache.commons.io.filefilter.TrueFileFilter;
3438
import org.junit.jupiter.api.io.TempDir;
3539
import org.junit.jupiter.params.ParameterizedTest;
3640
import org.junit.jupiter.params.provider.MethodSource;
3741

3842
/**
39-
* Tests {@link CountingPathVisitor}.
43+
* Tests {@link CopyDirectoryVisitor}.
4044
*/
4145
public class CopyDirectoryVisitorTest extends TestArguments {
4246

@@ -59,19 +63,22 @@ public void testCopyDirectoryEmptyFolder(final PathCounters pathCounters) throws
5963
assertEquals(sourceDir.get(), ((AbstractPathWrapper) visitFileTree.getSourceDirectory()).get());
6064
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
6165
assertEquals(targetDir, visitFileTree.getTargetDirectory());
62-
assertEquals(targetDir, visitFileTree.getTargetDirectory());
63-
//
66+
// Tests equals and hashCode
6467
assertEquals(visitFileTree, supplier.get());
6568
assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
6669
assertEquals(visitFileTree, visitFileTree);
6770
assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
6871
assertNotEquals(visitFileTree, "not");
72+
assertNotEquals(visitFileTree, new DeletingPathVisitor(pathCounters));
73+
assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir));
74+
assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, sourceDir, sourceDir, EXPECTED_COPY_OPTIONS));
75+
assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, targetDir, sourceDir, EXPECTED_COPY_OPTIONS));
6976
assertNotEquals(visitFileTree, CountingPathVisitor.withLongCounters());
7077
}
7178
}
7279

7380
/**
74-
* Tests an empty folder.
81+
* Tests an empty folder with filters.
7582
*/
7683
@ParameterizedTest
7784
@MethodSource("pathCounters")
@@ -84,14 +91,34 @@ public void testCopyDirectoryEmptyFolderFilters(final PathCounters pathCounters)
8491
assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
8592
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
8693
assertEquals(targetDir, visitFileTree.getTargetDirectory());
87-
//
94+
// Tests equals and hashCode
8895
assertEquals(visitFileTree, supplier.get());
8996
assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
9097
assertEquals(visitFileTree, visitFileTree);
9198
assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
9299
}
93100
}
94101

102+
/**
103+
* Tests filters.
104+
*/
105+
@ParameterizedTest
106+
@MethodSource("pathCounters")
107+
public void testCopyDirectoryFilters(final PathCounters pathCounters) throws IOException {
108+
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-4");
109+
final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(new CopyDirectoryVisitor(pathCounters, new NameFileFilter("file-size-1.bin"),
110+
new NameFileFilter("dirs-2-file-size-4", "dirs-a-file-size-1"), sourceDir, targetDir, null),
111+
sourceDir);
112+
assertCounts(2, 1, 2, visitFileTree);
113+
assertArrayEquals(PathUtils.EMPTY_COPY_OPTIONS, visitFileTree.getCopyOptions());
114+
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
115+
assertEquals(targetDir, visitFileTree.getTargetDirectory());
116+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-1.bin")));
117+
assertFalse(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-2.bin")));
118+
assertFalse(Files.exists(targetDir.resolve("dirs-a-file-size-2")));
119+
}
120+
121+
95122
/**
96123
* Tests a directory with one file of size 0.
97124
*/
@@ -105,10 +132,10 @@ public void testCopyDirectoryFolders1FileSize0(final PathCounters pathCounters)
105132
assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
106133
assertEquals(sourceDir, visitFileTree.getSourceDirectory());
107134
assertEquals(targetDir, visitFileTree.getTargetDirectory());
108-
//
135+
assertTrue(Files.exists(targetDir.resolve("file-size-0.bin")));
136+
// Tests equals and hashCode
109137
assertEquals(visitFileTree, supplier.get());
110138
assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
111-
assertEquals(visitFileTree, visitFileTree);
112139
assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
113140
}
114141

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

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

145175
}

0 commit comments

Comments
 (0)