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 2021/01/16 00:08:14 UTC

[commons-io] branch master updated (8588c9c -> cc9af31)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git.


    from 8588c9c  [IO-701] Make PathUtils.setReadOnly deal with LinuxDosFileAttributeView #186.
     new 3a9aae6  Javadoc.
     new 5b065d7  FIx test method name.
     new 471e4b3  Sort methods.
     new cc9af31  Add FileUtils.isEmptyDirectory(File).

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |   3 +
 src/main/java/org/apache/commons/io/FileUtils.java |  12 ++
 .../java/org/apache/commons/io/file/PathUtils.java |   6 +-
 .../org/apache/commons/io/FileUtilsTestCase.java   | 164 +++++++++++----------
 .../commons/io/file/PathUtilsIsEmptyTest.java      |   4 +-
 5 files changed, 110 insertions(+), 79 deletions(-)


[commons-io] 04/04: Add FileUtils.isEmptyDirectory(File).

Posted by gg...@apache.org.
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

commit cc9af31a0a10f5ab1df753c54607d35778ba617d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jan 15 19:08:08 2021 -0500

    Add FileUtils.isEmptyDirectory(File).
---
 src/changes/changes.xml                            |  3 +
 src/main/java/org/apache/commons/io/FileUtils.java | 12 +++
 .../org/apache/commons/io/FileUtilsTestCase.java   | 98 +++++++++++++---------
 .../commons/io/file/PathUtilsIsEmptyTest.java      |  2 +-
 4 files changed, 73 insertions(+), 42 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8fad977..92fb248 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -130,6 +130,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="IO-700" dev="ggregory" type="add" due-to="Gary Gregory">
         Add FileUtils.moveFile(File, File, CopyOption...) #185.
       </action>
+      <action issue="IO-700" dev="ggregory" type="add" due-to="Gary Gregory">
+        Add FileUtils.isEmptyDirectory(File).
+      </action>
       <!-- UPDATES -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Update junit-jupiter from 5.6.2 to 5.7.0 #153.
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 4aa0756..098c14b 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1488,6 +1488,18 @@ public class FileUtils {
     }
 
     /**
+     * Tests whether the directory is empty.
+     *
+     * @param directory the directory to query.
+     * @return whether the directory is empty.
+     * @throws IOException if an I/O error occurs.
+     * @since 2.9.0
+     */
+    public static boolean isEmptyDirectory(final File directory) throws IOException {
+        return PathUtils.isEmptyDirectory(directory.toPath());
+    }
+    
+    /**
      * Tests if the specified {@code File} is newer than the specified {@code ChronoLocalDate}
      * at the current time.
      *
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
index 94ae6c6..b41e483 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -40,6 +40,8 @@ import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.CopyOption;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.time.Instant;
 import java.time.LocalDate;
@@ -61,11 +63,13 @@ import java.util.zip.CRC32;
 import java.util.zip.Checksum;
 
 import org.apache.commons.io.file.PathUtils;
+import org.apache.commons.io.file.PathUtilsIsEmptyTest;
 import org.apache.commons.io.filefilter.IOFileFilter;
 import org.apache.commons.io.filefilter.NameFileFilter;
 import org.apache.commons.io.filefilter.WildcardFileFilter;
 import org.apache.commons.io.test.TestUtils;
 import org.apache.commons.lang3.SystemUtils;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
@@ -479,7 +483,7 @@ public class FileUtilsTestCase {
         assertSame(testChecksum, resultChecksum);
         assertEquals(expectedValue, resultValue);
     }
-
+    
     //-----------------------------------------------------------------------
     @Test
     public void testChecksumCRC32() throws Exception {
@@ -571,8 +575,6 @@ public class FileUtilsTestCase {
         assertEquals(sizeLong1, sizeBig.longValue(), "longSize should equal BigSize");
     }
 
-    // toFiles
-
     @Test
     public void testContentEquals() throws Exception {
         // Non-existent files
@@ -627,6 +629,8 @@ public class FileUtilsTestCase {
         assertTrue(FileUtils.contentEquals(file, file2));
     }
 
+    // toFiles
+
     @Test
     public void testContentEqualsIgnoreEOL() throws Exception {
         // Non-existent files
@@ -797,8 +801,6 @@ public class FileUtilsTestCase {
         FileUtils.deleteDirectory(target);
     }
 
-    // toURLs
-
     /* Test for IO-141 */
     @Test
     public void testCopyDirectoryToChild() throws Exception {
@@ -818,25 +820,7 @@ public class FileUtilsTestCase {
         assertTrue(expectedSize > 0, "Size > 0");
     }
 
-//   @Test public void testToURLs2() throws Exception {
-//        File[] files = new File[] {
-//            new File(temporaryFolder, "file1.txt"),
-//            null,
-//        };
-//        URL[] urls = FileUtils.toURLs(files);
-//
-//        assertEquals(files.length, urls.length);
-//        assertTrue(urls[0].toExternalForm().startsWith("file:"));
-//        assertTrue(urls[0].toExternalForm().indexOf("file1.txt") > 0);
-//        assertEquals(null, urls[1]);
-//    }
-//
-//   @Test public void testToURLs3() throws Exception {
-//        File[] files = null;
-//        URL[] urls = FileUtils.toURLs(files);
-//
-//        assertEquals(0, urls.length);
-//    }
+    // toURLs
 
     @Test
     public void testCopyDirectoryToDirectory_NonExistingDest() throws Exception {
@@ -882,7 +866,25 @@ public class FileUtilsTestCase {
         FileUtils.deleteDirectory(destDir);
     }
 
-    // contentEquals
+//   @Test public void testToURLs2() throws Exception {
+//        File[] files = new File[] {
+//            new File(temporaryFolder, "file1.txt"),
+//            null,
+//        };
+//        URL[] urls = FileUtils.toURLs(files);
+//
+//        assertEquals(files.length, urls.length);
+//        assertTrue(urls[0].toExternalForm().startsWith("file:"));
+//        assertTrue(urls[0].toExternalForm().indexOf("file1.txt") > 0);
+//        assertEquals(null, urls[1]);
+//    }
+//
+//   @Test public void testToURLs3() throws Exception {
+//        File[] files = null;
+//        URL[] urls = FileUtils.toURLs(files);
+//
+//        assertEquals(0, urls.length);
+//    }
 
     @Test
     public void testCopyDirectoryToExistingDest() throws Exception {
@@ -925,6 +927,8 @@ public class FileUtilsTestCase {
         assertTrue(new File(destDir, "sub/A.txt").exists());
     }
 
+    // contentEquals
+
     /* Test for IO-141 */
     @Test
     public void testCopyDirectoryToGrandChild() throws Exception {
@@ -941,8 +945,6 @@ public class FileUtilsTestCase {
         assertTrue(expectedSize > 0, "Size > 0");
     }
 
-    // copyURLToFile
-
     /* Test for IO-217 FileUtils.copyDirectoryToDirectory makes infinite loops */
     @Test
     public void testCopyDirectoryToItself() throws Exception {
@@ -952,6 +954,8 @@ public class FileUtilsTestCase {
         assertEquals(1, LIST_WALKER.list(dir).size());
     }
 
+    // copyURLToFile
+
     @Test
     public void testCopyDirectoryToNonExistingDest() throws Exception {
         if (!testFile1.getParentFile().exists()) {
@@ -994,8 +998,6 @@ public class FileUtilsTestCase {
         FileUtils.deleteDirectory(destDir);
     }
 
-    // forceMkdir
-
     @Test
     public void testCopyFile1() throws Exception {
         final File destination = new File(temporaryFolder, "copy1.txt");
@@ -1008,6 +1010,8 @@ public class FileUtilsTestCase {
         assertEquals(getLastModifiedMillis(testFile1), getLastModifiedMillis(destination), "Check last modified date preserved");
     }
 
+    // forceMkdir
+
     @Test
     public void testCopyFile1ToDir() throws Exception {
         final File directory = new File(temporaryFolder, "subdir");
@@ -1027,8 +1031,6 @@ public class FileUtilsTestCase {
             "Should not be able to copy a file into the same directory as itself");
     }
 
-    // sizeOfDirectory
-
     @Test
     public void testCopyFile2() throws Exception {
         final File destination = new File(temporaryFolder, "copy2.txt");
@@ -1041,6 +1043,8 @@ public class FileUtilsTestCase {
         assertEquals(getLastModifiedMillis(testFile1) , getLastModifiedMillis(destination), "Check last modified date preserved");
     }
 
+    // sizeOfDirectory
+
     @Test
     public void testCopyFile2ToDir() throws Exception {
         final File directory = new File(temporaryFolder, "subdir");
@@ -1158,14 +1162,14 @@ public class FileUtilsTestCase {
         assertEquals(testFile1Size, destination.length(), "Check Full Copy");
     }
 
-    // copyFile
-
     @Test
     public void testCopyToDirectoryWithFileSourceDoesNotExist() {
         assertThrows(IOException.class,
                 () -> FileUtils.copyToDirectory(new File(temporaryFolder, "doesNotExists"), temporaryFolder));
     }
 
+    // copyFile
+
     @Test
     public void testCopyToDirectoryWithFileSourceIsNull() {
         assertThrows(NullPointerException.class, () -> FileUtils.copyToDirectory((File) null, temporaryFolder));
@@ -1361,8 +1365,6 @@ public class FileUtilsTestCase {
         }
     }
 
-    // copyToDirectory
-
     /*
      *  Test the FileUtils implementation.
      */
@@ -1393,6 +1395,8 @@ public class FileUtilsTestCase {
 
     }
 
+    // copyToDirectory
+
     @Test
     public void testForceDeleteAFile1() throws Exception {
         final File destination = new File(temporaryFolder, "copy1.txt");
@@ -1489,8 +1493,6 @@ public class FileUtilsTestCase {
         assertFalse(testFile.exists());
     }
 
-    // forceDelete
-
     //-----------------------------------------------------------------------
     @Test
     public void testGetFile() {
@@ -1508,6 +1510,8 @@ public class FileUtilsTestCase {
         }
     }
 
+    // forceDelete
+
     @Test
     public void testGetFile_Parent() {
         final File parent = new File("parent");
@@ -1537,28 +1541,28 @@ public class FileUtilsTestCase {
         assertEquals(tempDirectory, FileUtils.getTempDirectory());
     }
 
-    // copyFileToDirectory
-
     @Test
     public void testGetTempDirectoryPath() {
         assertEquals(System.getProperty("java.io.tmpdir"),
                 FileUtils.getTempDirectoryPath());
     }
 
+    // copyFileToDirectory
+
     @Test
     public void testGetUserDirectory() {
         final File userDirectory = new File(System.getProperty("user.home"));
         assertEquals(userDirectory, FileUtils.getUserDirectory());
     }
 
-    // forceDelete
-
     @Test
     public void testGetUserDirectoryPath() {
         assertEquals(System.getProperty("user.home"),
                 FileUtils.getUserDirectoryPath());
     }
 
+    // forceDelete
+
     // This test relies on FileUtils.copyFile using File.length to check the output size
     @Test
     public void testIncorrectOutputSize() throws Exception {
@@ -1601,6 +1605,18 @@ public class FileUtilsTestCase {
         assertTrue(src.exists());
     }
 
+    @Test
+    public void testIsEmptyDirectory() throws IOException {
+        final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
+        final File tempDirAsFile = tempDir.toFile();
+        try {
+            Assertions.assertTrue(FileUtils.isEmptyDirectory(tempDirAsFile));
+        } finally {
+            Files.delete(tempDir);
+        }
+        Assertions.assertFalse(FileUtils.isEmptyDirectory(PathUtilsIsEmptyTest.DIR_SIZE_1.toFile()));
+    }
+
     // isFileNewer / isFileOlder
     @Test
     public void testIsFileNewerOlder() throws Exception {
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java
index d8d95b5..3cb31cd 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java
@@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test;
  */
 public class PathUtilsIsEmptyTest {
 
-    private static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");
+    public static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");
 
     private static final Path FILE_SIZE_0 = Paths
             .get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/file-size-0.bin");


[commons-io] 01/04: Javadoc.

Posted by gg...@apache.org.
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

commit 3a9aae6cbef6614d4bb3c00a7c5abecd0c6a995a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jan 15 18:25:05 2021 -0500

    Javadoc.
---
 src/main/java/org/apache/commons/io/file/PathUtils.java | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 06474b2..0303d15 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -723,7 +723,7 @@ public final class PathUtils {
     }
 
     /**
-     * Returns whether the given file or directory is empty.
+     * Tests whether the given file or directory is empty.
      *
      * @param path the file or directory to query.
      * @return whether the file or directory is empty.
@@ -734,7 +734,7 @@ public final class PathUtils {
     }
 
     /**
-     * Returns whether the directory is empty.
+     * Tests whether the directory is empty.
      *
      * @param directory the directory to query.
      * @return whether the directory is empty.
@@ -747,7 +747,7 @@ public final class PathUtils {
     }
 
     /**
-     * Returns whether the given file is empty.
+     * Tests whether the given file is empty.
      *
      * @param file the file to query.
      * @return whether the file is empty.


[commons-io] 02/04: FIx test method name.

Posted by gg...@apache.org.
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

commit 5b065d799912dbbf98bf71f10f861ab57dca8727
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jan 15 18:32:12 2021 -0500

    FIx test method name.
---
 src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java
index 647a7ef..d8d95b5 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java
@@ -52,7 +52,7 @@ public class PathUtilsIsEmptyTest {
     }
 
     @Test
-    public void testisEmptyDirectory() throws IOException {
+    public void testIsEmptyDirectory() throws IOException {
         final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
         try {
             Assertions.assertTrue(PathUtils.isEmptyDirectory(tempDir));


[commons-io] 03/04: Sort methods.

Posted by gg...@apache.org.
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

commit 471e4b3697ec80b58c307132a0b41ecff4bf4aff
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jan 15 18:33:27 2021 -0500

    Sort methods.
---
 .../org/apache/commons/io/FileUtilsTestCase.java   | 66 +++++++++++-----------
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
index efffbda..94ae6c6 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -1281,6 +1281,12 @@ public class FileUtilsTestCase {
     }
 
     @Test
+    public void testDelete() throws Exception {
+        assertEquals(testFile1, FileUtils.delete(testFile1));
+        assertThrows(IOException.class, () -> FileUtils.delete(new File("does not exist.nope")));
+    }
+
+    @Test
     public void testDeleteDirectoryWithNonDirectory() throws Exception {
         try {
             FileUtils.deleteDirectory(testFile1);
@@ -1291,12 +1297,6 @@ public class FileUtilsTestCase {
     }
 
     @Test
-    public void testDelete() throws Exception {
-        assertEquals(testFile1, FileUtils.delete(testFile1));
-        assertThrows(IOException.class, () -> FileUtils.delete(new File("does not exist.nope")));
-    }
-
-    @Test
     public void testDeleteQuietlyDir() throws IOException {
         final File testDirectory = new File(temporaryFolder, "testDeleteQuietlyDir");
         final File testFile = new File(testDirectory, "testDeleteQuietlyFile");
@@ -2169,6 +2169,32 @@ public class FileUtilsTestCase {
     }
     
     @Test
+    public void testMoveFile_CopyDelete_Failed() throws Exception {
+        final File destination = new File(temporaryFolder, "move3.txt");
+        final File src = new File(testFile1.getAbsolutePath()) {
+            private static final long serialVersionUID = 1L;
+
+            // Force delete failure
+            @Override
+            public boolean delete() {
+                return false;
+            }
+
+            // Force renameTo to fail, as if destination is on another
+            // filesystem
+            @Override
+            public boolean renameTo(final File f) {
+                return false;
+            }
+
+        };
+        assertThrows(IOException.class, () -> FileUtils.moveFile(src, destination));
+        // expected
+        assertTrue(!destination.exists(), "Check Rollback");
+        assertTrue(src.exists(), "Original exists");
+    }
+    
+    @Test
     public void testMoveFile_CopyDelete_WithFileDatePreservation() throws Exception {
         final File destination = new File(temporaryFolder, "move2.txt");
         
@@ -2194,7 +2220,7 @@ public class FileUtilsTestCase {
         final long delta = destLastMod - expected;
         assertEquals(expected, destLastMod, "Check last modified date same as input, delta " + delta);
     }
-    
+
     @Test
     public void testMoveFile_CopyDelete_WithoutFileDatePreservation() throws Exception {
         final File destination = new File(temporaryFolder, "move2.txt");
@@ -2230,32 +2256,6 @@ public class FileUtilsTestCase {
     }
 
     @Test
-    public void testMoveFile_CopyDelete_Failed() throws Exception {
-        final File destination = new File(temporaryFolder, "move3.txt");
-        final File src = new File(testFile1.getAbsolutePath()) {
-            private static final long serialVersionUID = 1L;
-
-            // Force delete failure
-            @Override
-            public boolean delete() {
-                return false;
-            }
-
-            // Force renameTo to fail, as if destination is on another
-            // filesystem
-            @Override
-            public boolean renameTo(final File f) {
-                return false;
-            }
-
-        };
-        assertThrows(IOException.class, () -> FileUtils.moveFile(src, destination));
-        // expected
-        assertTrue(!destination.exists(), "Check Rollback");
-        assertTrue(src.exists(), "Original exists");
-    }
-
-    @Test
     public void testMoveFile_Errors() throws Exception {
         assertThrows(NullPointerException.class, () -> FileUtils.moveFile(null, new File("foo")));
         assertThrows(NullPointerException.class, () -> FileUtils.moveFile(new File("foo"), null));