Skip to content

Commit 6573ce7

Browse files
committed
[IO-326] Add new FileUtils.sizeOf[Directory] APIs to return BigInteger. Thanks to Bruno P. Kinoshita for the fix and test case.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1349488 13f79535-47bb-0310-9956-ffa450edef68
1 parent bd952dd commit 6573ce7

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<modelVersion>4.0.0</modelVersion>
2525
<groupId>commons-io</groupId>
2626
<artifactId>commons-io</artifactId>
27-
<version>2.4-SNAPSHOT</version>
27+
<version>2.4</version>
2828
<name>Commons IO</name>
2929

3030
<inceptionYear>2002</inceptionYear>

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2511,7 +2511,7 @@ public static BigInteger sizeOfDirectoryAsBigInteger(File directory) {
25112511
for (final File file : files) {
25122512
try {
25132513
if (!isSymlink(file)) {
2514-
size.add(BigInteger.valueOf(sizeOf(file)));
2514+
size = size.add(BigInteger.valueOf(sizeOf(file)));
25152515
}
25162516
} catch (IOException ioe) {
25172517
// Ignore exceptions caught when asking if a File is a symlink.

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class FileUtilsTestCase extends FileBasedTestCase {
6363
*/
6464
private static final BigInteger TEST_DIRECTORY_SIZE_BI = BigInteger.ZERO;
6565

66+
/**
67+
* Size (greater of zero) of test file.
68+
*/
69+
private static final BigInteger TEST_DIRECTORY_SIZE_GT_ZERO_BI = BigInteger.valueOf(100);
70+
6671
/**
6772
* List files recursively
6873
*/
@@ -796,7 +801,8 @@ public void testSizeOfDirectoryAsBigInteger() throws Exception {
796801
try {
797802
FileUtils.sizeOfDirectoryAsBigInteger(file);
798803
fail("Exception expected.");
799-
} catch (IllegalArgumentException ex) {}
804+
} catch (IllegalArgumentException ex) {
805+
}
800806

801807
// Creates file
802808
file.createNewFile();
@@ -806,18 +812,30 @@ public void testSizeOfDirectoryAsBigInteger() throws Exception {
806812
try {
807813
FileUtils.sizeOfDirectoryAsBigInteger(file);
808814
fail("Exception expected.");
809-
} catch (IllegalArgumentException ex) {}
815+
} catch (IllegalArgumentException ex) {
816+
}
810817

811818
// Existing directory
812819
file.delete();
813820
file.mkdir();
814821

815822
this.createCircularSymLink(file);
816823

817-
assertEquals(
818-
"Unexpected directory size",
819-
TEST_DIRECTORY_SIZE_BI,
820-
FileUtils.sizeOfDirectoryAsBigInteger(file));
824+
assertEquals("Unexpected directory size", TEST_DIRECTORY_SIZE_BI, FileUtils.sizeOfDirectoryAsBigInteger(file));
825+
826+
// Existing directory which size is greater than zero
827+
file.delete();
828+
file.mkdir();
829+
830+
File nonEmptyFile = new File(file, "nonEmptyFile" + System.nanoTime());
831+
this.createFile(nonEmptyFile, TEST_DIRECTORY_SIZE_GT_ZERO_BI.longValue());
832+
nonEmptyFile.deleteOnExit();
833+
834+
assertEquals("Unexpected directory size", TEST_DIRECTORY_SIZE_GT_ZERO_BI,
835+
FileUtils.sizeOfDirectoryAsBigInteger(file));
836+
837+
nonEmptyFile.delete();
838+
file.delete();
821839
}
822840

823841
/**

0 commit comments

Comments
 (0)