minor fix
This commit is contained in:
parent
fcb4a33e3a
commit
d426ea75b4
|
@ -1,21 +1,25 @@
|
|||
package com.baeldung.java8;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.FileVisitResult;
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaFolderSizeUnitTest {
|
||||
|
||||
private final long EXPECTED_SIZE = 260;
|
||||
private String path;
|
||||
|
||||
@Before
|
||||
|
@ -26,18 +30,14 @@ public class JavaFolderSizeUnitTest {
|
|||
|
||||
@Test
|
||||
public void whenGetFolderSizeRecursive_thenCorrect() {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final File folder = new File(path);
|
||||
final long size = getFolderSize(folder);
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
assertEquals(EXPECTED_SIZE, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFolderSizeUsingJava7_thenCorrect() throws IOException {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final AtomicLong size = new AtomicLong(0);
|
||||
final Path folder = Paths.get(path);
|
||||
|
||||
|
@ -49,41 +49,33 @@ public class JavaFolderSizeUnitTest {
|
|||
}
|
||||
});
|
||||
|
||||
assertEquals(expectedSize, size.longValue());
|
||||
assertEquals(EXPECTED_SIZE, size.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final Path folder = Paths.get(path);
|
||||
final long size = Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum();
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
assertEquals(EXPECTED_SIZE, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFolderSizeUsingApacheCommonsIO_thenCorrect() {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final File folder = new File(path);
|
||||
final long size = FileUtils.sizeOfDirectory(folder);
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
assertEquals(EXPECTED_SIZE, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFolderSizeUsingGuava_thenCorrect() {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final File folder = new File(path);
|
||||
|
||||
final Iterable<File> files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder);
|
||||
final long size = StreamSupport.stream(files.spliterator(), false)
|
||||
.filter(File::isFile)
|
||||
.mapToLong(File::length).sum();
|
||||
final long size = StreamSupport.stream(files.spliterator(), false).filter(File::isFile).mapToLong(File::length).sum();
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
assertEquals(EXPECTED_SIZE, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -91,19 +83,19 @@ public class JavaFolderSizeUnitTest {
|
|||
final File folder = new File(path);
|
||||
final long size = getFolderSize(folder);
|
||||
|
||||
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
|
||||
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("136 B", readableSize);
|
||||
assertEquals(EXPECTED_SIZE + " B", readableSize);
|
||||
}
|
||||
|
||||
private long getFolderSize(final File folder) {
|
||||
long length = 0;
|
||||
final File[] files = folder.listFiles();
|
||||
|
||||
for (File file : files) {
|
||||
for (final File file : files) {
|
||||
if (file.isFile()) {
|
||||
length += file.length();
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue