forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
core-java/src/test/java/org/baeldung/java/io/JavaFolderSizeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.baeldung.java.io; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Files; | ||
//import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.text.DecimalFormat; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.junit.Test; | ||
|
||
public class JavaFolderSizeTest { | ||
|
||
@Test | ||
public void whenGetFolderSizeRecursive_thenCorrect() { | ||
final long expectedSize = 12607; | ||
|
||
final File folder = new File("src/test/resources"); | ||
final long size = getFolderSize(folder); | ||
|
||
assertEquals(expectedSize, size); | ||
} | ||
|
||
@Test | ||
public void whenGetFolderSizeUsingJava7_thenCorrect() throws IOException { | ||
final long expectedSize = 12607; | ||
|
||
final AtomicLong size = new AtomicLong(0); | ||
final Path folder = Paths.get("src/test/resources"); | ||
|
||
Files.walkFileTree(folder, new SimpleFileVisitor<Path>() { | ||
@Override | ||
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { | ||
size.addAndGet(attrs.size()); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
}); | ||
|
||
assertEquals(expectedSize, size.longValue()); | ||
} | ||
|
||
@Test | ||
public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException { | ||
final long expectedSize = 12607; | ||
|
||
final Path folder = Paths.get("src/test/resources"); | ||
final long size = Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum(); | ||
|
||
assertEquals(expectedSize, size); | ||
} | ||
|
||
@Test | ||
public void whenGetFolderSizeUsingApacheCommonsIO_thenCorrect() { | ||
final long expectedSize = 12607; | ||
|
||
final File folder = new File("src/test/resources"); | ||
final long size = FileUtils.sizeOfDirectory(folder); | ||
|
||
assertEquals(expectedSize, size); | ||
} | ||
|
||
@Test | ||
public void whenGetFolderSizeUsingGuava_thenCorrect() { | ||
final long expectedSize = 12607; | ||
|
||
final File folder = new File("src/test/resources"); | ||
|
||
final Iterable<File> files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder); | ||
final long size = StreamSupport.stream(files.spliterator(), false).filter(f -> f.isFile()).mapToLong(File::length).sum(); | ||
|
||
assertEquals(expectedSize, size); | ||
} | ||
|
||
@Test | ||
public void whenGetReadableSize_thenCorrect() { | ||
final File folder = new File("src/test/resources"); | ||
final long size = getFolderSize(folder); | ||
|
||
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; | ||
final int unitIndex = (int) (Math.log10(size) / 3); | ||
final double unitValue = 1 << (unitIndex * 10); | ||
|
||
final String readableSize = new DecimalFormat("#,##0.#").format(size / unitValue) + " " + units[unitIndex]; | ||
assertEquals("12.3 KB", readableSize); | ||
} | ||
|
||
private long getFolderSize(final File folder) { | ||
long length = 0; | ||
final File[] files = folder.listFiles(); | ||
|
||
final int count = files.length; | ||
|
||
for (int i = 0; i < count; i++) { | ||
if (files[i].isFile()) | ||
length += files[i].length(); | ||
else | ||
length += getFolderSize(files[i]); | ||
} | ||
return length; | ||
} | ||
} |