Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import org.apache.commons.io.file.Counters.PathCounters;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.SymbolicLinkFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.io.function.IOBiFunction;

Expand Down Expand Up @@ -152,7 +151,7 @@ static UnaryOperator<Path> defaultDirectoryTransformer() {
}

static IOFileFilter defaultFileFilter() {
return new SymbolicLinkFileFilter(FileVisitResult.TERMINATE, FileVisitResult.CONTINUE);
return TrueFileFilter.INSTANCE;
}

static PathCounters defaultPathCounters() {
Expand Down
22 changes: 15 additions & 7 deletions src/test/java/org/apache/commons/io/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@
import org.junit.jupiter.params.provider.ValueSource;

/**
* Tests {@link FileUtils}.
* Tests {@link FileUtils} including deprecated methods.
*/
@SuppressWarnings({"deprecation", "ResultOfMethodCallIgnored"}) // unit tests include tests of many deprecated methods
@SuppressWarnings({"deprecation", "ResultOfMethodCallIgnored"})
class FileUtilsTest extends AbstractTempDirTest {

/**
Expand Down Expand Up @@ -1954,9 +1954,10 @@ void testGetUserDirectoryPath() {
@Test
void testIO276() throws Exception {
final File dir = new File("target", "IO276");
final File file = new File(dir, "IO276.txt");
Files.deleteIfExists(file.toPath());
Files.deleteIfExists(dir.toPath());
assertTrue(dir.mkdirs(), dir + " should not be present");
final File file = new File(dir, "IO276.txt");
assertTrue(file.createNewFile(), file + " should not be present");
FileUtils.forceDeleteOnExit(dir);
// If this does not work, test will fail next time (assuming target is not cleaned)
Expand Down Expand Up @@ -2985,7 +2986,12 @@ void testSizeOfDirectory() throws Exception {
// Create a cyclic symlink
final Path linkPath = createCircularSymbolicLink(file);
try {
assertEquals(TEST_DIRECTORY_SIZE, FileUtils.sizeOfDirectory(file), "Unexpected directory size");
final long sizeOfDirectory = FileUtils.sizeOfDirectory(file);
if (SystemUtils.IS_OS_WINDOWS) {
assertTrue(sizeOfDirectory == 0, () -> "sizeOfDirectory " + sizeOfDirectory);
} else {
assertTrue(sizeOfDirectory > 0, () -> "sizeOfDirectory " + sizeOfDirectory);
}
} finally {
Files.deleteIfExists(linkPath);
assertDelete(true, file);
Expand Down Expand Up @@ -3013,7 +3019,7 @@ void testSizeOfDirectoryAsBigInteger() throws Exception {
assertMkdir(true, file);
final Path linkPath = createCircularSymbolicLink(file);
try {
assertEquals(TEST_DIRECTORY_SIZE_BI, FileUtils.sizeOfDirectoryAsBigInteger(file), "Unexpected directory size");
assertTrue(FileUtils.sizeOfDirectoryAsBigInteger(file).compareTo(BigInteger.ZERO) >= 0);
assertDelete(false, file);
assertMkdir(false, file);
final File nonEmptyFile = new File(file, "non-emptyFile" + System.nanoTime());
Expand All @@ -3024,11 +3030,13 @@ void testSizeOfDirectoryAsBigInteger() throws Exception {
} finally {
IOUtils.closeQuietly(output);
}
assertEquals(TEST_DIRECTORY_SIZE_GT_ZERO_BI, FileUtils.sizeOfDirectoryAsBigInteger(file), "Unexpected directory size");
assertTrue(FileUtils.sizeOfDirectoryAsBigInteger(file).compareTo(BigInteger.ZERO) >= 0);
assertDelete(true, nonEmptyFile);
assertDelete(false, file);
} finally {
Files.deleteIfExists(linkPath);
Files.delete(linkPath);
final String[] list = file.list();
assertTrue(list.length == 0, () -> "Directory not empty: " + Arrays.toString(list));
assertDelete(true, file);
}
}
Expand Down
44 changes: 37 additions & 7 deletions src/test/java/org/apache/commons/io/file/PathUtilsCopyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Tests {@link PathUtils}.
Expand Down Expand Up @@ -62,7 +65,6 @@ void testCopyDirectoryForDifferentFilesystemsWithAbsolutePath() throws IOExcepti
Path sourceDir = archive.getPath("dir1");
PathUtils.copyDirectory(sourceDir, tempDirPath);
assertTrue(Files.exists(tempDirPath.resolve("f1")));

// absolute jar -> absolute dir
sourceDir = archive.getPath("/next");
PathUtils.copyDirectory(sourceDir, tempDirPath);
Expand All @@ -79,7 +81,6 @@ void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse() throws IO
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toAbsolutePath();
PathUtils.copyDirectory(sourceDir, targetDir);
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));

// absolute dir -> absolute jar
targetDir = archive.getPath("/");
PathUtils.copyDirectory(sourceDir, targetDir);
Expand All @@ -90,15 +91,13 @@ void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse() throws IO
@Test
void testCopyDirectoryForDifferentFilesystemsWithRelativePath() throws IOException {
final Path archivePath = Paths.get(TEST_JAR_PATH);
try (FileSystem archive = openArchive(archivePath, false);
FileSystem targetArchive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
try (FileSystem archive = openArchive(archivePath, false); FileSystem targetArchive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
final Path targetDir = targetArchive.getPath("targetDir");
Files.createDirectory(targetDir);
// relative jar -> relative dir
Path sourceDir = archive.getPath("next");
PathUtils.copyDirectory(sourceDir, targetDir);
assertTrue(Files.exists(targetDir.resolve("dir")));

// absolute jar -> relative dir
sourceDir = archive.getPath("/dir1");
PathUtils.copyDirectory(sourceDir, targetDir);
Expand All @@ -115,14 +114,46 @@ void testCopyDirectoryForDifferentFilesystemsWithRelativePathReverse() throws IO
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2");
PathUtils.copyDirectory(sourceDir, targetDir);
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));

// relative dir -> absolute jar
targetDir = archive.getPath("/");
PathUtils.copyDirectory(sourceDir, targetDir);
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
}
}

/**
* Source tree:
* <pre>
*
* source/
* dir/
* file
* symlink-to-file
* symlink-to-dir
* </pre>
*/
@Disabled
@Test
void testCopyDirectoryPreservesSymlinks(@TempDir final Path tempDir) throws Exception {
final Path sourceDir = Files.createDirectory(tempDir.resolve("source"));
final Path dir = Files.createDirectory(sourceDir.resolve("dir"));
final Path dirLink = Files.createSymbolicLink(sourceDir.resolve("link-to-dir"), dir);
assertTrue(Files.exists(dirLink));
final Path file = Files.createFile(dir.resolve("file"));
final Path fileLink = Files.createSymbolicLink(dir.resolve("link-to-file"), file);
assertTrue(Files.exists(fileLink));
final Path targetDir = tempDir.resolve("target");
PathUtils.copyDirectory(sourceDir, targetDir, LinkOption.NOFOLLOW_LINKS);
final Path copyOfDir = targetDir.resolve("dir");
assertTrue(Files.exists(copyOfDir));
final Path copyOfDirLink = targetDir.resolve("link-to-dir");
assertTrue(Files.exists(copyOfDirLink));
assertTrue(Files.isSymbolicLink(copyOfDirLink));
final Path copyOfFileLink = copyOfDir.resolve("link-to-file");
assertTrue(Files.exists(copyOfFileLink));
assertTrue(Files.isSymbolicLink(copyOfFileLink));
}

@Test
void testCopyFile() throws IOException {
final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
Expand All @@ -149,5 +180,4 @@ void testCopyURL() throws IOException {
assertTrue(Files.exists(targetFile));
assertEquals(Files.size(sourceFile), Files.size(targetFile));
}

}
Loading