From dbaba09fb929ac8938bfb0ccfc04235ef2a75839 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sun, 2 Jul 2023 04:39:05 +0200 Subject: [PATCH] [file-is-empty] Check If a File is Empty in Java (#14319) * [file-is-empty] Check If a File is Empty in Java * [file-is-empty] adjust code format * [file-is-empty] rename test methods * [file-is-empty] Then->then in method names --- .../emptyfile/CheckFileIsEmptyUnitTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java diff --git a/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java b/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java new file mode 100644 index 0000000000..0603a298f5 --- /dev/null +++ b/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java @@ -0,0 +1,71 @@ +package com.baeldung.emptyfile; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class CheckFileIsEmptyUnitTest { + @Test + void whenTheFileIsEmpty_thenFileLengthIsZero(@TempDir Path tempDir) throws IOException { + File emptyFile = tempDir.resolve("an-empty-file.txt") + .toFile(); + emptyFile.createNewFile(); + assertTrue(emptyFile.exists()); + assertEquals(0, emptyFile.length()); + } + + @Test + void whenFileDoesNotExist_thenFileLengthIsZero(@TempDir Path tempDir) { + File aNewFile = tempDir.resolve("a-new-file.txt") + .toFile(); + assertFalse(aNewFile.exists()); + assertEquals(0, aNewFile.length()); + } + + boolean isFileEmpty(File file) { + if (!file.exists()) { + throw new IllegalArgumentException("Cannot check the file length. The file is not found: " + file.getAbsolutePath()); + } + return file.length() == 0; + } + + @Test + void whenTheFileDoesNotExist_thenIsFilesEmptyThrowsException(@TempDir Path tempDir) { + File aNewFile = tempDir.resolve("a-new-file.txt") + .toFile(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> isFileEmpty(aNewFile)); + assertEquals(ex.getMessage(), "Cannot check the file length. The file is not found: " + aNewFile.getAbsolutePath()); + } + + @Test + void whenTheFileIsEmpty_thenIsFilesEmptyReturnsTrue(@TempDir Path tempDir) throws IOException { + File emptyFile = tempDir.resolve("an-empty-file.txt") + .toFile(); + emptyFile.createNewFile(); + assertTrue(isFileEmpty(emptyFile)); + + } + + @Test + void whenTheFileIsEmpty_thenFilesSizeReturnsTrue(@TempDir Path tempDir) throws IOException { + Path emptyFilePath = tempDir.resolve("an-empty-file.txt"); + Files.createFile(emptyFilePath); + assertEquals(0, Files.size(emptyFilePath)); + } + + @Test + void whenTheFileDoesNotExist_thenFilesSizeThrowsException(@TempDir Path tempDir) { + Path aNewFilePath = tempDir.resolve("a-new-file.txt"); + assertThrows(NoSuchFileException.class, () -> Files.size(aNewFilePath)); + } +} \ No newline at end of file