diff --git a/core-java-modules/core-java-io-apis-2/README.md b/core-java-modules/core-java-io-apis-2/README.md index d62fd3dbd1..5b9421152a 100644 --- a/core-java-modules/core-java-io-apis-2/README.md +++ b/core-java-modules/core-java-io-apis-2/README.md @@ -4,13 +4,5 @@ This module contains articles about core Java input/output(IO) APIs. ### Relevant Articles: - [Constructing a Relative Path From Two Absolute Paths in Java](https://www.baeldung.com/java-relative-path-absolute) -- [Java Scanner Taking a Character Input](https://www.baeldung.com/java-scanner-character-input) - [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path) -- [Integer.parseInt(scanner.nextLine()) and scanner.nextInt() in Java](https://www.baeldung.com/java-scanner-integer) -- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader) -- [Java: Read Multiple Inputs on Same Line](https://www.baeldung.com/java-read-multiple-inputs-same-line) -- [Storing Java Scanner Input in an Array](https://www.baeldung.com/java-store-scanner-input-in-array) -- [How to Take Input as String With Spaces in Java Using Scanner?](https://www.baeldung.com/java-scanner-input-with-spaces) -- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file) -- [What’s the difference between Scanner next() and nextLine() methods?](https://www.baeldung.com/java-scanner-next-vs-nextline) -- [Handle NoSuchElementException When Reading a File Through Scanner](https://www.baeldung.com/java-scanner-nosuchelementexception-reading-file) +- [Check if a File Is Empty in Java](https://www.baeldung.com/java-check-file-empty) diff --git a/core-java-modules/core-java-io-apis-2/pom.xml b/core-java-modules/core-java-io-apis-2/pom.xml index e828b730d2..bf7af998e3 100644 --- a/core-java-modules/core-java-io-apis-2/pom.xml +++ b/core-java-modules/core-java-io-apis-2/pom.xml @@ -92,12 +92,6 @@ 7.1.0 test - - org.testng - testng - 7.5 - compile - core-java-io-apis-2 diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java new file mode 100644 index 0000000000..02b4d758b3 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/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; + +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 diff --git a/core-java-modules/core-java-io-apis-3/README.md b/core-java-modules/core-java-io-apis-3/README.md index c540d1aa31..e69de29bb2 100644 --- a/core-java-modules/core-java-io-apis-3/README.md +++ b/core-java-modules/core-java-io-apis-3/README.md @@ -1,7 +0,0 @@ -## Core Java IO APIs - -This module contains articles about core Java input/output(IO) APIs. - -### Relevant Articles: -- [Read Date in Java Using Scanner](https://www.baeldung.com/java-scanner-read-date) -- [Check if a File Is Empty in Java](https://www.baeldung.com/java-check-file-empty) diff --git a/core-java-modules/core-java-io-apis-3/pom.xml b/core-java-modules/core-java-io-apis-3/pom.xml deleted file mode 100644 index 8b2431397b..0000000000 --- a/core-java-modules/core-java-io-apis-3/pom.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - 4.0.0 - core-java-io-apis-3 - core-java-io-apis-3 - jar - - - com.baeldung.core-java-modules - core-java-modules - 0.0.1-SNAPSHOT - - \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis/README.md b/core-java-modules/core-java-io-apis/README.md index 9399443ebd..b00b0a6022 100644 --- a/core-java-modules/core-java-io-apis/README.md +++ b/core-java-modules/core-java-io-apis/README.md @@ -10,6 +10,6 @@ This module contains articles about core Java input/output(IO) APIs. - [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](https://www.baeldung.com/java-path) - [Quick Use of FilenameFilter](https://www.baeldung.com/java-filename-filter) - [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader) -- [Java Scanner](https://www.baeldung.com/java-scanner) -- [Scanner nextLine() Method](https://www.baeldung.com/java-scanner-nextline) -- [Java Scanner hasNext() vs. hasNextLine()](https://www.baeldung.com/java-scanner-hasnext-vs-hasnextline) \ No newline at end of file +- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader) +- [Java: Read Multiple Inputs on Same Line](https://www.baeldung.com/java-read-multiple-inputs-same-line) +- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file) \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis/pom.xml b/core-java-modules/core-java-io-apis/pom.xml index f9d404cd5b..8889fd5f50 100644 --- a/core-java-modules/core-java-io-apis/pom.xml +++ b/core-java-modules/core-java-io-apis/pom.xml @@ -31,6 +31,12 @@ ${lombok.version} provided + + org.testng + testng + 7.5 + compile + diff --git a/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/multinput/MultiInputs.java b/core-java-modules/core-java-io-apis/src/main/java/com/baeldung/multinput/MultiInputs.java similarity index 97% rename from core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/multinput/MultiInputs.java rename to core-java-modules/core-java-io-apis/src/main/java/com/baeldung/multinput/MultiInputs.java index df799b2511..bc14c4275b 100644 --- a/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/multinput/MultiInputs.java +++ b/core-java-modules/core-java-io-apis/src/main/java/com/baeldung/multinput/MultiInputs.java @@ -1,36 +1,36 @@ -package com.baeldung.multinput; - -import java.util.InputMismatchException; -import java.util.Scanner; - -public class MultiInputs { - public void UsingSpaceDelimiter(){ - Scanner scanner = new Scanner(System.in); - System.out.print("Enter two numbers: "); - int num1 = scanner.nextInt(); - int num2 = scanner.nextInt(); - System.out.println("You entered " + num1 + " and " + num2); - - } - public void UsingREDelimiter(){ - Scanner scanner = new Scanner(System.in); - scanner.useDelimiter("[\\s,]+"); - System.out.print("Enter two numbers separated by a space or a comma: "); - int num1 = scanner.nextInt(); - int num2 = scanner.nextInt(); - System.out.println("You entered " + num1 + " and " + num2); - - } - public void UsingCustomDelimiter(){ - Scanner scanner = new Scanner(System.in); - scanner.useDelimiter(";"); - System.out.print("Enter two numbers separated by a semicolon: "); - try { int num1 = scanner.nextInt(); - int num2 = scanner.nextInt(); - System.out.println("You entered " + num1 + " and " + num2); } - catch (InputMismatchException e) - { System.out.println("Invalid input. Please enter two integers separated by a semicolon."); } - - } -} - +package com.baeldung.multinput; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class MultiInputs { + public void UsingSpaceDelimiter(){ + Scanner scanner = new Scanner(System.in); + System.out.print("Enter two numbers: "); + int num1 = scanner.nextInt(); + int num2 = scanner.nextInt(); + System.out.println("You entered " + num1 + " and " + num2); + + } + public void UsingREDelimiter(){ + Scanner scanner = new Scanner(System.in); + scanner.useDelimiter("[\\s,]+"); + System.out.print("Enter two numbers separated by a space or a comma: "); + int num1 = scanner.nextInt(); + int num2 = scanner.nextInt(); + System.out.println("You entered " + num1 + " and " + num2); + + } + public void UsingCustomDelimiter(){ + Scanner scanner = new Scanner(System.in); + scanner.useDelimiter(";"); + System.out.print("Enter two numbers separated by a semicolon: "); + try { int num1 = scanner.nextInt(); + int num2 = scanner.nextInt(); + System.out.println("You entered " + num1 + " and " + num2); } + catch (InputMismatchException e) + { System.out.println("Invalid input. Please enter two integers separated by a semicolon."); } + + } +} + diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java similarity index 96% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java rename to core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java index 62f0d9da22..6892ef33a9 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java +++ b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java @@ -1,36 +1,36 @@ -package com.baeldung.bufferedreadervsfilereader; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - -import org.junit.jupiter.api.Test; - -class BufferedReaderUnitTest { - - @Test - void whenReadingAFile_thenReadsLineByLine() { - StringBuilder result = new StringBuilder(); - - final Path filePath = new File("src/test/resources/sampleText1.txt").toPath(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath), StandardCharsets.UTF_8))) { - String line; - - while((line = br.readLine()) != null) { - result.append(line); - result.append('\n'); - } - } catch (IOException e) { - e.printStackTrace(); - } - - assertEquals("first line\nsecond line\nthird line\n", result.toString()); - } - -} +package com.baeldung.bufferedreadervsfilereader; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.junit.jupiter.api.Test; + +class BufferedReaderUnitTest { + + @Test + void whenReadingAFile_thenReadsLineByLine() { + StringBuilder result = new StringBuilder(); + + final Path filePath = new File("src/test/resources/sampleText1.txt").toPath(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath), StandardCharsets.UTF_8))) { + String line; + + while((line = br.readLine()) != null) { + result.append(line); + result.append('\n'); + } + } catch (IOException e) { + e.printStackTrace(); + } + + assertEquals("first line\nsecond line\nthird line\n", result.toString()); + } + +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java similarity index 95% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java rename to core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java index da724d32e8..2c09d2a084 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java +++ b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java @@ -1,30 +1,30 @@ -package com.baeldung.bufferedreadervsfilereader; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.jupiter.api.Test; - -class FileReaderUnitTest { - - @Test - void whenReadingAFile_thenReadsCharByChar() { - StringBuilder result = new StringBuilder(); - - try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) { - int i = fr.read(); - - while(i != -1) { - result.append((char)i); - - i = fr.read(); - } - } catch (IOException e) { - e.printStackTrace(); - } - - assertEquals("qwerty", result.toString()); - } -} +package com.baeldung.bufferedreadervsfilereader; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.FileReader; +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +class FileReaderUnitTest { + + @Test + void whenReadingAFile_thenReadsCharByChar() { + StringBuilder result = new StringBuilder(); + + try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) { + int i = fr.read(); + + while(i != -1) { + result.append((char)i); + + i = fr.read(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + assertEquals("qwerty", result.toString()); + } +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java similarity index 84% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java rename to core-java-modules/core-java-io-apis/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java index 317d9e817e..0fbcd9bbb2 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java +++ b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java @@ -1,47 +1,49 @@ -package com.baeldung.multinput; - import java.io.ByteArrayInputStream; - import java.io.InputStream; - import java.util.InputMismatchException; - import org.junit.jupiter.api.Assertions; - import org.testng.annotations.Test; -import com.baeldung.multinput.MultiInputs; -public class TestMultipleInputsUnitTest { - @Test - public void givenMultipleInputs_whenUsingSpaceDelimiter_thenExpectPrintingOutputs() { - String input = "10 20\n"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); - MultiInputs mi = new MultiInputs(); - mi.UsingSpaceDelimiter(); - // You can add assertions here to verify the behavior of the method - } - - @Test - public void givenMultipleInputs_whenUsingREDelimiter_thenExpectPrintingOutputs() { - String input = "30, 40\n"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); - MultiInputs mi = new MultiInputs(); - mi.UsingREDelimiter(); - // You can add assertions here to verify the behavior of the method - } - - @Test - public void givenMultipleInputs_whenUsingCustomDelimiter_thenExpectPrintingOutputs() { - String input = "50; 60\n"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); - MultiInputs mi = new MultiInputs(); - mi.UsingCustomDelimiter(); - // You can add assertions here to verify the behavior of the method - } - - @Test - public void givenInvalidInput_whenUsingSpaceDelimiter_thenExpectInputMismatchException() { - String input = "abc\n"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); - MultiInputs mi = new MultiInputs(); - Assertions.assertThrows(InputMismatchException.class, mi::UsingSpaceDelimiter); - } -} +package com.baeldung.multinput; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.InputMismatchException; + +import org.junit.jupiter.api.Assertions; +import org.testng.annotations.Test; + +public class TestMultipleInputsUnitTest { + @Test + public void givenMultipleInputs_whenUsingSpaceDelimiter_thenExpectPrintingOutputs() { + String input = "10 20\n"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + MultiInputs mi = new MultiInputs(); + mi.UsingSpaceDelimiter(); + // You can add assertions here to verify the behavior of the method + } + + @Test + public void givenMultipleInputs_whenUsingREDelimiter_thenExpectPrintingOutputs() { + String input = "30, 40\n"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + MultiInputs mi = new MultiInputs(); + mi.UsingREDelimiter(); + // You can add assertions here to verify the behavior of the method + } + + @Test + public void givenMultipleInputs_whenUsingCustomDelimiter_thenExpectPrintingOutputs() { + String input = "50; 60\n"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + MultiInputs mi = new MultiInputs(); + mi.UsingCustomDelimiter(); + // You can add assertions here to verify the behavior of the method + } + + @Test + public void givenInvalidInput_whenUsingSpaceDelimiter_thenExpectInputMismatchException() { + String input = "abc\n"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + MultiInputs mi = new MultiInputs(); + Assertions.assertThrows(InputMismatchException.class, mi::UsingSpaceDelimiter); + } +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/outputtofile/ConsoleOutputToFileUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/outputtofile/ConsoleOutputToFileUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/outputtofile/ConsoleOutputToFileUnitTest.java rename to core-java-modules/core-java-io-apis/src/test/java/com/baeldung/outputtofile/ConsoleOutputToFileUnitTest.java diff --git a/core-java-modules/core-java-scanner/README.md b/core-java-modules/core-java-scanner/README.md new file mode 100644 index 0000000000..58f7a7095b --- /dev/null +++ b/core-java-modules/core-java-scanner/README.md @@ -0,0 +1,15 @@ +## Core Java IO APIs + +This module contains articles about core Java input/output(IO) APIs. + +### Relevant Articles: +- [Read Date in Java Using Scanner](https://www.baeldung.com/java-scanner-read-date) +- [Java Scanner Taking a Character Input](https://www.baeldung.com/java-scanner-character-input) +- [Integer.parseInt(scanner.nextLine()) and scanner.nextInt() in Java](https://www.baeldung.com/java-scanner-integer) +- [Storing Java Scanner Input in an Array](https://www.baeldung.com/java-store-scanner-input-in-array) +- [How to Take Input as String With Spaces in Java Using Scanner?](https://www.baeldung.com/java-scanner-input-with-spaces) +- [What’s the difference between Scanner next() and nextLine() methods?](https://www.baeldung.com/java-scanner-next-vs-nextline) +- [Handle NoSuchElementException When Reading a File Through Scanner](https://www.baeldung.com/java-scanner-nosuchelementexception-reading-file) +- [Java Scanner](https://www.baeldung.com/java-scanner) +- [Scanner nextLine() Method](https://www.baeldung.com/java-scanner-nextline) +- [Java Scanner hasNext() vs. hasNextLine()](https://www.baeldung.com/java-scanner-hasnext-vs-hasnextline) \ No newline at end of file diff --git a/core-java-modules/core-java-scanner/pom.xml b/core-java-modules/core-java-scanner/pom.xml new file mode 100644 index 0000000000..f149f51955 --- /dev/null +++ b/core-java-modules/core-java-scanner/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + core-java-scanner + core-java-scanner + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis-3/src/main/java/com/baeldung/scanner/DateScanner.java b/core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/DateScanner.java similarity index 96% rename from core-java-modules/core-java-io-apis-3/src/main/java/com/baeldung/scanner/DateScanner.java rename to core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/DateScanner.java index a9f3ba820a..79fda8dc35 100644 --- a/core-java-modules/core-java-io-apis-3/src/main/java/com/baeldung/scanner/DateScanner.java +++ b/core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/DateScanner.java @@ -1,29 +1,29 @@ -package com.baeldung.scanner; - -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.util.Date; -import java.util.Scanner; - -public class DateScanner { - - LocalDate scanToLocalDate(String input) { - try (Scanner scanner = new Scanner(input)) { - String dateString = scanner.next(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - return LocalDate.parse(dateString, formatter); - } - } - - Date scanToDate(String input) throws ParseException { - try (Scanner scanner = new Scanner(input)) { - String dateString = scanner.next(); - DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - return formatter.parse(dateString); - } - } - -} +package com.baeldung.scanner; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.Scanner; + +public class DateScanner { + + LocalDate scanToLocalDate(String input) { + try (Scanner scanner = new Scanner(input)) { + String dateString = scanner.next(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return LocalDate.parse(dateString, formatter); + } + } + + Date scanToDate(String input) throws ParseException { + try (Scanner scanner = new Scanner(input)) { + String dateString = scanner.next(); + DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + return formatter.parse(dateString); + } + } + +} diff --git a/core-java-modules/core-java-io-apis/src/main/java/com/baeldung/scanner/HasNextVsHasNextLineDemo.java b/core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/HasNextVsHasNextLineDemo.java similarity index 100% rename from core-java-modules/core-java-io-apis/src/main/java/com/baeldung/scanner/HasNextVsHasNextLineDemo.java rename to core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/HasNextVsHasNextLineDemo.java diff --git a/core-java-modules/core-java-io-apis/src/main/java/com/baeldung/scanner/NextLineAfterNextMethods.java b/core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/NextLineAfterNextMethods.java similarity index 100% rename from core-java-modules/core-java-io-apis/src/main/java/com/baeldung/scanner/NextLineAfterNextMethods.java rename to core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/NextLineAfterNextMethods.java diff --git a/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.java b/core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.java similarity index 100% rename from core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.java rename to core-java-modules/core-java-scanner/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.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-scanner/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/emptyfile/CheckFileIsEmptyUnitTest.java diff --git a/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java similarity index 96% rename from core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java index 1ecd440d95..0a02db12f3 100644 --- a/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java +++ b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java @@ -1,26 +1,26 @@ -package com.baeldung.scanner; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -import org.junit.jupiter.api.Test; - -class DateScannerUnitTest { - - @Test - void whenScanToLocalDate_ThenCorrectLocalDate() { - String dateString = "2018-09-09"; - assertEquals(LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd")), new DateScanner().scanToLocalDate(dateString)); - } - - @Test - void whenScanToDate_ThenCorrectDate() throws ParseException { - String dateString = "2018-09-09"; - assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse(dateString), new DateScanner().scanToDate(dateString)); - } - -} +package com.baeldung.scanner; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import org.junit.jupiter.api.Test; + +class DateScannerUnitTest { + + @Test + void whenScanToLocalDate_ThenCorrectLocalDate() { + String dateString = "2018-09-09"; + assertEquals(LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd")), new DateScanner().scanToLocalDate(dateString)); + } + + @Test + void whenScanToDate_ThenCorrectDate() throws ParseException { + String dateString = "2018-09-09"; + assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse(dateString), new DateScanner().scanToDate(dateString)); + } + +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java diff --git a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/scanner/JavaScannerUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/JavaScannerUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis/src/test/java/com/baeldung/scanner/JavaScannerUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/JavaScannerUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java similarity index 96% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java index 3aae0469d0..8fab7c62e9 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java +++ b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java @@ -1,85 +1,85 @@ -package com.baeldung.scanner; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.InputMismatchException; -import java.util.Scanner; - -import org.junit.jupiter.api.Test; - -public class NextLineVsNextIntUnitTest { - - @Test - void whenInputLineIsNumber_thenNextLineAndNextIntBothWork() { - String input = "42\n"; - - //nextLine() - Scanner sc1 = new Scanner(input); - int num1 = Integer.parseInt(sc1.nextLine()); - assertEquals(42, num1); - - //nextInt() - Scanner sc2 = new Scanner(input); - int num2 = sc2.nextInt(); - assertEquals(42, num2); - - } - - @Test - void whenInputIsNotValidNumber_thenNextLineAndNextIntThrowDifferentException() { - String input = "Nan\n"; - - //nextLine() -> NumberFormatException - Scanner sc1 = new Scanner(input); - assertThrows(NumberFormatException.class, () -> Integer.parseInt(sc1.nextLine())); - - //nextInt() -> InputMismatchException - Scanner sc2 = new Scanner(input); - assertThrows(InputMismatchException.class, sc2::nextInt); - } - - @Test - void whenUsingNextInt_thenTheNextTokenAfterItFailsToParseIsNotConsumed() { - String input = "42 is a magic number\n"; - - //nextInt() to read '42' - Scanner sc2 = new Scanner(input); - int num2 = sc2.nextInt(); - assertEquals(42, num2); - - // call nextInt() again on "is" - assertThrows(InputMismatchException.class, sc2::nextInt); - - String theNextToken = sc2.next(); - assertEquals("is", theNextToken); - - theNextToken = sc2.next(); - assertEquals("a", theNextToken); - } - - @Test - void whenReadingTwoInputLines_thenNextLineAndNextIntBehaveDifferently() { - - String input = new StringBuilder().append("42\n") - .append("It is a magic number.\n") - .toString(); - - //nextLine() - Scanner sc1 = new Scanner(input); - int num1 = Integer.parseInt(sc1.nextLine()); - String nextLineText1 = sc1.nextLine(); - assertEquals(42, num1); - assertEquals("It is a magic number.", nextLineText1); - - //nextInt() - Scanner sc2 = new Scanner(input); - int num2 = sc2.nextInt(); - assertEquals(42, num2); - - // nextInt() leaves the newline character (\n) behind - String nextLineText2 = sc2.nextLine(); - assertEquals("", nextLineText2); - } - +package com.baeldung.scanner; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.InputMismatchException; +import java.util.Scanner; + +import org.junit.jupiter.api.Test; + +public class NextLineVsNextIntUnitTest { + + @Test + void whenInputLineIsNumber_thenNextLineAndNextIntBothWork() { + String input = "42\n"; + + //nextLine() + Scanner sc1 = new Scanner(input); + int num1 = Integer.parseInt(sc1.nextLine()); + assertEquals(42, num1); + + //nextInt() + Scanner sc2 = new Scanner(input); + int num2 = sc2.nextInt(); + assertEquals(42, num2); + + } + + @Test + void whenInputIsNotValidNumber_thenNextLineAndNextIntThrowDifferentException() { + String input = "Nan\n"; + + //nextLine() -> NumberFormatException + Scanner sc1 = new Scanner(input); + assertThrows(NumberFormatException.class, () -> Integer.parseInt(sc1.nextLine())); + + //nextInt() -> InputMismatchException + Scanner sc2 = new Scanner(input); + assertThrows(InputMismatchException.class, sc2::nextInt); + } + + @Test + void whenUsingNextInt_thenTheNextTokenAfterItFailsToParseIsNotConsumed() { + String input = "42 is a magic number\n"; + + //nextInt() to read '42' + Scanner sc2 = new Scanner(input); + int num2 = sc2.nextInt(); + assertEquals(42, num2); + + // call nextInt() again on "is" + assertThrows(InputMismatchException.class, sc2::nextInt); + + String theNextToken = sc2.next(); + assertEquals("is", theNextToken); + + theNextToken = sc2.next(); + assertEquals("a", theNextToken); + } + + @Test + void whenReadingTwoInputLines_thenNextLineAndNextIntBehaveDifferently() { + + String input = new StringBuilder().append("42\n") + .append("It is a magic number.\n") + .toString(); + + //nextLine() + Scanner sc1 = new Scanner(input); + int num1 = Integer.parseInt(sc1.nextLine()); + String nextLineText1 = sc1.nextLine(); + assertEquals(42, num1); + assertEquals("It is a magic number.", nextLineText1); + + //nextInt() + Scanner sc2 = new Scanner(input); + int num2 = sc2.nextInt(); + assertEquals(42, num2); + + // nextInt() leaves the newline character (\n) behind + String nextLineText2 = sc2.nextLine(); + assertEquals("", nextLineText2); + } + } \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/NextVsNextLineUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/NextVsNextLineUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/NextVsNextLineUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/NextVsNextLineUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java similarity index 96% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java index 340b58bbf6..1a70c6e3af 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java +++ b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java @@ -1,38 +1,38 @@ -package com.baeldung.scanner; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Scanner; - -import org.junit.jupiter.api.Test; - -public class ScanACharacterUnitTest { - - // given - input scanner source, no need to scan from console - String input = new StringBuilder().append("abc\n") - .append("mno\n") - .append("xyz\n") - .toString(); - - @Test - public void givenInputSource_whenScanCharUsingNext_thenOneCharIsRead() { - Scanner sc = new Scanner(input); - char c = sc.next().charAt(0); - assertEquals('a', c); - } - - @Test - public void givenInputSource_whenScanCharUsingFindInLine_thenOneCharIsRead() { - Scanner sc = new Scanner(input); - char c = sc.findInLine(".").charAt(0); - assertEquals('a', c); - } - - @Test - public void givenInputSource_whenScanCharUsingUseDelimiter_thenOneCharIsRead() { - Scanner sc = new Scanner(input); - char c = sc.useDelimiter("").next().charAt(0); - assertEquals('a', c); - } - -} +package com.baeldung.scanner; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Scanner; + +import org.junit.jupiter.api.Test; + +public class ScanACharacterUnitTest { + + // given - input scanner source, no need to scan from console + String input = new StringBuilder().append("abc\n") + .append("mno\n") + .append("xyz\n") + .toString(); + + @Test + public void givenInputSource_whenScanCharUsingNext_thenOneCharIsRead() { + Scanner sc = new Scanner(input); + char c = sc.next().charAt(0); + assertEquals('a', c); + } + + @Test + public void givenInputSource_whenScanCharUsingFindInLine_thenOneCharIsRead() { + Scanner sc = new Scanner(input); + char c = sc.findInLine(".").charAt(0); + assertEquals('a', c); + } + + @Test + public void givenInputSource_whenScanCharUsingUseDelimiter_thenOneCharIsRead() { + Scanner sc = new Scanner(input); + char c = sc.useDelimiter("").next().charAt(0); + assertEquals('a', c); + } + +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerToArrayUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/ScannerToArrayUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerToArrayUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scanner/ScannerToArrayUnitTest.java diff --git a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/scannernextline/ScannerNextLineUnitTest.java b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scannernextline/ScannerNextLineUnitTest.java similarity index 100% rename from core-java-modules/core-java-io-apis/src/test/java/com/baeldung/scannernextline/ScannerNextLineUnitTest.java rename to core-java-modules/core-java-scanner/src/test/java/com/baeldung/scannernextline/ScannerNextLineUnitTest.java index f3e76229da..5ea29e63e0 100644 --- a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/scannernextline/ScannerNextLineUnitTest.java +++ b/core-java-modules/core-java-scanner/src/test/java/com/baeldung/scannernextline/ScannerNextLineUnitTest.java @@ -1,11 +1,11 @@ package com.baeldung.scannernextline; -import org.junit.Test; +import static org.junit.Assert.assertEquals; import java.util.NoSuchElementException; import java.util.Scanner; -import static org.junit.Assert.assertEquals; +import org.junit.Test; public class ScannerNextLineUnitTest { diff --git a/core-java-modules/core-java-io-apis-2/src/test/resources/emptyFile.txt b/core-java-modules/core-java-scanner/src/test/resources/emptyFile.txt similarity index 100% rename from core-java-modules/core-java-io-apis-2/src/test/resources/emptyFile.txt rename to core-java-modules/core-java-scanner/src/test/resources/emptyFile.txt diff --git a/core-java-modules/core-java-io-apis/src/test/resources/test_read.in b/core-java-modules/core-java-scanner/src/test/resources/test_read.in similarity index 100% rename from core-java-modules/core-java-io-apis/src/test/resources/test_read.in rename to core-java-modules/core-java-scanner/src/test/resources/test_read.in diff --git a/core-java-modules/core-java-io-apis/src/test/resources/test_read_d.in b/core-java-modules/core-java-scanner/src/test/resources/test_read_d.in similarity index 100% rename from core-java-modules/core-java-io-apis/src/test/resources/test_read_d.in rename to core-java-modules/core-java-scanner/src/test/resources/test_read_d.in diff --git a/core-java-modules/core-java-io-apis/src/test/resources/test_read_multiple.in b/core-java-modules/core-java-scanner/src/test/resources/test_read_multiple.in similarity index 100% rename from core-java-modules/core-java-io-apis/src/test/resources/test_read_multiple.in rename to core-java-modules/core-java-scanner/src/test/resources/test_read_multiple.in diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index f85189570f..bfa48893e9 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -81,7 +81,6 @@ core-java-io-4 core-java-io-apis core-java-io-apis-2 - core-java-io-apis-3 core-java-io-conversions core-java-jar core-java-jndi @@ -125,6 +124,7 @@ core-java-properties core-java-reflection core-java-reflection-2 + core-java-scanner core-java-security-2 core-java-security-3 core-java-security-algorithms