You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/03/24 13:34:30 UTC
(commons-io) branch master updated: Add FileUtilsTest.testForceDeleteUnwritableFile()
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 e168e7d6f Add FileUtilsTest.testForceDeleteUnwritableFile()
e168e7d6f is described below
commit e168e7d6fd99bc92b7bfc64e68fdada820ac0aa6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 24 09:34:25 2024 -0400
Add FileUtilsTest.testForceDeleteUnwritableFile()
---
.../java/org/apache/commons/io/FileUtilsTest.java | 48 ++++++++++++++++------
1 file changed, 36 insertions(+), 12 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java
index efd139228..c2bddc330 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -1242,18 +1242,6 @@ public class FileUtilsTest extends AbstractTempDirTest {
"Should not be able to copy a file into the same directory as itself");
}
- @Test
- public void testCopyFileToReadOnlyDirectory() throws Exception {
- final File directory = new File(tempDirFile, "readonly");
- if (!directory.exists()) {
- assumeTrue(directory.mkdirs());
- }
- assumeTrue(directory.setWritable(false));
-
- assertThrows(IOException.class, () -> FileUtils.copyFileToDirectory(testFile1, directory),
- "Should not be able to copy a file into a readonly directory");
- }
-
@Test
public void testCopyFile2() throws Exception {
final File destination = new File(tempDirFile, "copy2.txt");
@@ -1334,6 +1322,18 @@ public class FileUtilsTest extends AbstractTempDirTest {
assertArrayEquals(expected, destination.toByteArray(), "Check Full copy");
}
+ @Test
+ public void testCopyFileToReadOnlyDirectory() throws Exception {
+ final File directory = new File(tempDirFile, "readonly");
+ if (!directory.exists()) {
+ assumeTrue(directory.mkdirs());
+ }
+ assumeTrue(directory.setWritable(false));
+
+ assertThrows(IOException.class, () -> FileUtils.copyFileToDirectory(testFile1, directory),
+ "Should not be able to copy a file into a readonly directory");
+ }
+
@Test
public void testCopyToDirectoryWithDirectory() throws IOException {
final File destDirectory = new File(tempDirFile, "destination");
@@ -1684,6 +1684,30 @@ public class FileUtilsTest extends AbstractTempDirTest {
}
}
+ @Test
+ public void testForceDeleteUnwritableFile() throws Exception {
+ try (TempFile destination = TempFile.create("test-", ".txt")) {
+ final File file = destination.toFile();
+ assertTrue(file.canWrite());
+ assertTrue(file.setWritable(false));
+ assertFalse(file.canWrite());
+ assertTrue(file.canRead());
+ // sanity check that File.delete() deletes read-only files.
+ assertTrue(file.delete());
+ }
+ try (TempFile destination = TempFile.create("test-", ".txt")) {
+ final File file = destination.toFile();
+ // real test
+ assertTrue(file.canWrite());
+ assertTrue(file.setWritable(false));
+ assertFalse(file.canWrite());
+ assertTrue(file.canRead());
+ assertTrue(file.exists(), "File doesn't exist to delete");
+ FileUtils.forceDelete(file);
+ assertFalse(file.exists(), "Check deletion");
+ }
+ }
+
@Test
public void testForceMkdir() throws Exception {
// Tests with existing directory