Add missing CopyDirectoryVisitor tests
MODIFY
src/main/java/org/apache/commons/io/file/CopyDirectoryVisitor.java
src/main/java/org/apache/commons/io/file/CopyDirectoryVisitor.java
*/
public class CopyDirectoryVisitor extends CountingPathVisitor {
+ private static CopyOption[] toCopyOption(final CopyOption... copyOptions) {
+ return copyOptions == null ? PathUtils.EMPTY_COPY_OPTIONS : copyOptions.clone();
+ }
+
private final CopyOption[] copyOptions;
private final Path sourceDirectory;
private final Path targetDirectory;
@@ -52,7 +56,7 @@ public CopyDirectoryVisitor(final PathCounters pathCounter, final Path sourceDir
super(pathCounter);
this.sourceDirectory = sourceDirectory;
this.targetDirectory = targetDirectory;
- this.copyOptions = copyOptions == null ? PathUtils.EMPTY_COPY_OPTIONS : copyOptions.clone();
+ this.copyOptions = toCopyOption(copyOptions);
}
/**
@@ -71,7 +75,7 @@ public CopyDirectoryVisitor(final PathCounters pathCounter, final PathFilter fil
super(pathCounter, fileFilter, dirFilter);
this.sourceDirectory = sourceDirectory;
this.targetDirectory = targetDirectory;
- this.copyOptions = copyOptions == null ? PathUtils.EMPTY_COPY_OPTIONS : copyOptions.clone();
+ this.copyOptions = toCopyOption(copyOptions);
}
/**

MODIFY
src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java
src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java
package org.apache.commons.io.file;
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.assertNotEquals;
import java.io.IOException;
+import java.nio.file.CopyOption;
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.TrueFileFilter;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
*/
public class CopyDirectoryVisitorTest extends TestArguments {
+ private static final CopyOption[] EXPECTED_COPY_OPTIONS = {StandardCopyOption.REPLACE_EXISTING};
+
@TempDir
private Path targetDir;
@MethodSource("pathCounters")
public void testCopyDirectoryEmptyFolder(final PathCounters pathCounters) throws IOException {
try (TempDirectory sourceDir = TempDirectory.create(getClass().getSimpleName())) {
- assertCounts(1, 0, 0, PathUtils
- .visitFileTree(new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, StandardCopyOption.REPLACE_EXISTING), sourceDir.get()));
+ final Supplier<CopyDirectoryVisitor> supplier = () -> new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS);
+ final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(supplier.get(), sourceDir.get());
+ assertCounts(1, 0, 0, visitFileTree);
+ assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
+ assertEquals(sourceDir.get(), ((PathWrapper) visitFileTree.getSourceDirectory()).get());
+ assertEquals(sourceDir, visitFileTree.getSourceDirectory());
+ assertEquals(targetDir, visitFileTree.getTargetDirectory());
+ assertEquals(targetDir, visitFileTree.getTargetDirectory());
+ //
+ assertEquals(visitFileTree, supplier.get());
+ assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
+ assertEquals(visitFileTree, visitFileTree);
+ assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
+ assertNotEquals(visitFileTree, "not");
+ assertNotEquals(visitFileTree, CountingPathVisitor.withLongCounters());
+ }
+ }
+
+ /**
+ * Tests an empty folder.
+ */
+ @ParameterizedTest
+ @MethodSource("pathCounters")
+ public void testCopyDirectoryEmptyFolderFilters(final PathCounters pathCounters) throws IOException {
+ try (TempDirectory sourceDir = TempDirectory.create(getClass().getSimpleName())) {
+ final Supplier<CopyDirectoryVisitor> supplier = () -> new CopyDirectoryVisitor(pathCounters, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE,
+ sourceDir, targetDir, EXPECTED_COPY_OPTIONS);
+ final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(supplier.get(), sourceDir.get());
+ assertCounts(1, 0, 0, visitFileTree);
+ assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
+ assertEquals(sourceDir, visitFileTree.getSourceDirectory());
+ assertEquals(targetDir, visitFileTree.getTargetDirectory());
+ //
+ assertEquals(visitFileTree, supplier.get());
+ assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
+ assertEquals(visitFileTree, visitFileTree);
+ assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
}
}
@@ -56,9 +98,17 @@ public void testCopyDirectoryEmptyFolder(final PathCounters pathCounters) throws
@MethodSource("pathCounters")
public void testCopyDirectoryFolders1FileSize0(final PathCounters pathCounters) throws IOException {
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0");
- assertCounts(1, 1, 0, PathUtils.visitFileTree(
- new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, StandardCopyOption.REPLACE_EXISTING),
- sourceDir));
+ final Supplier<CopyDirectoryVisitor> supplier = () -> new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS);
+ final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(supplier.get(), sourceDir);
+ assertCounts(1, 1, 0, visitFileTree);
+ assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
+ assertEquals(sourceDir, visitFileTree.getSourceDirectory());
+ assertEquals(targetDir, visitFileTree.getTargetDirectory());
+ //
+ assertEquals(visitFileTree, supplier.get());
+ assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
+ assertEquals(visitFileTree, visitFileTree);
+ assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
}
/**
@@ -68,9 +118,12 @@ public void testCopyDirectoryFolders1FileSize0(final PathCounters pathCounters)
@MethodSource("pathCounters")
public void testCopyDirectoryFolders1FileSize1(final PathCounters pathCounters) throws IOException {
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");
- assertCounts(1, 1, 1, PathUtils.visitFileTree(
- new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, StandardCopyOption.REPLACE_EXISTING),
- sourceDir));
+ final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS),
+ sourceDir);
+ assertCounts(1, 1, 1, visitFileTree);
+ assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
+ assertEquals(sourceDir, visitFileTree.getSourceDirectory());
+ assertEquals(targetDir, visitFileTree.getTargetDirectory());
}
/**
@@ -80,9 +133,12 @@ public void testCopyDirectoryFolders1FileSize1(final PathCounters pathCounters)
@MethodSource("pathCounters")
public void testCopyDirectoryFolders2FileSize2(final PathCounters pathCounters) throws IOException {
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2");
- assertCounts(3, 2, 2, PathUtils.visitFileTree(
- new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, StandardCopyOption.REPLACE_EXISTING),
- sourceDir));
+ final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS),
+ sourceDir);
+ assertCounts(3, 2, 2, visitFileTree);
+ assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
+ assertEquals(sourceDir, visitFileTree.getSourceDirectory());
+ assertEquals(targetDir, visitFileTree.getTargetDirectory());
}
}

MODIFY
src/test/java/org/apache/commons/io/file/PathWrapper.java
src/test/java/org/apache/commons/io/file/PathWrapper.java
@@ -69,8 +69,15 @@ public boolean endsWith(final String other) {
}
@Override
- public boolean equals(final Object other) {
- return path.equals(other);
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof PathWrapper)) {
+ return false;
+ }
+ final PathWrapper other = (PathWrapper) obj;
+ return Objects.equals(path, other.path);
}
@Override
@@ -119,7 +126,7 @@ public Path getRoot() {
@Override
public int hashCode() {
- return path.hashCode();
+ return Objects.hash(path);
}
@Override

Add missing CopyDirectoryVisitor tests