This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new e02c47887 Add FileUtilsTest.testForceDeleteReadOnlyDirectory() e02c47887 is described below commit e02c47887d81e342ed83e23548d0bb0a47faab5c Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Mar 24 10:06:48 2024 -0400 Add FileUtilsTest.testForceDeleteReadOnlyDirectory() --- .../java/org/apache/commons/io/FileUtilsTest.java | 31 ++++++++++++++++++++++ .../commons/io/file/AbstractPathWrapper.java | 11 ++++++++ 2 files changed, 42 insertions(+) diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java index c2bddc330..ccad1b10d 100644 --- a/src/test/java/org/apache/commons/io/FileUtilsTest.java +++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java @@ -79,6 +79,7 @@ import java.util.zip.CRC32; import java.util.zip.Checksum; import org.apache.commons.io.file.AbstractTempDirTest; +import org.apache.commons.io.file.Counters.PathCounters; import org.apache.commons.io.file.PathUtils; import org.apache.commons.io.file.TempDirectory; import org.apache.commons.io.file.TempFile; @@ -1662,6 +1663,36 @@ public class FileUtilsTest extends AbstractTempDirTest { assertFalse(testDirectory.exists(), "TestDirectory must not exist"); } + @Test + public void testForceDeleteReadOnlyDirectory() throws Exception { + try (TempDirectory destDir = TempDirectory.create("dir-"); + TempFile destination = TempFile.create(destDir, "test-", ".txt")) { + final File file = destination.toFile(); + assertTrue(file.setReadOnly()); + assertTrue(file.canRead()); + assertFalse(file.canWrite()); + // sanity check that File.delete() deletes a read-only directory. + final PathCounters delete = destDir.delete(); + assertEquals(1, delete.getDirectoryCounter().get()); + assertEquals(1, delete.getFileCounter().get()); + assertFalse(file.exists()); + assertFalse(destDir.exists()); + } + try (TempDirectory destDir = TempDirectory.create("dir-"); + TempFile destination = TempFile.create(destDir, "test-", ".txt")) { + final File dir = destDir.toFile(); + // real test + assertTrue(dir.setReadOnly()); + assertTrue(dir.canRead()); + assertFalse(dir.canWrite()); + assertTrue(dir.exists(), "File doesn't exist to delete"); + FileUtils.forceDelete(dir); + assertFalse(destination.exists(), "Check deletion"); + assertFalse(dir.exists(), "Check deletion"); + assertFalse(destDir.exists(), "Check deletion"); + } + } + @Test public void testForceDeleteReadOnlyFile() throws Exception { try (TempFile destination = TempFile.create("test-", ".txt")) { diff --git a/src/test/java/org/apache/commons/io/file/AbstractPathWrapper.java b/src/test/java/org/apache/commons/io/file/AbstractPathWrapper.java index 674ee3dee..4c5857c49 100644 --- a/src/test/java/org/apache/commons/io/file/AbstractPathWrapper.java +++ b/src/test/java/org/apache/commons/io/file/AbstractPathWrapper.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.FileSystem; +import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.WatchEvent.Kind; @@ -85,6 +86,16 @@ public abstract class AbstractPathWrapper implements Path { path.forEach(action); } + /** + * Delegates to {@link Files#exists(Path, LinkOption...)}. + * + * @param options See {@link Files#exists(Path, LinkOption...)}. + * @return See {@link Files#exists(Path, LinkOption...)}. + */ + public boolean exists(final LinkOption... options) { + return Files.exists(path, options); + } + /** * Gets the delegate Path. *