From 53d269ca68a7389290396189a5cee0ce521159a7 Mon Sep 17 00:00:00 2001 From: Bahaa El-Din Helmy Date: Tue, 9 May 2023 00:11:33 +0300 Subject: [PATCH] Add files via upload (#13978) --- core-java-modules/core-java-io-apis-2/pom.xml | 159 ++++++++++----- .../com/baeldung/multinput/MultiInputs.java | 36 ++++ .../AbsoluteToRelativeUnitTest.java | 185 +++++++++--------- .../BufferedReaderUnitTest.java | 62 +++--- .../FileReaderUnitTest.java | 60 +++--- .../multinput/TestMultipleInputsUnitTest.java | 47 +++++ .../baeldung/path/DesktopPathUnitTest.java | 56 +++--- .../scanner/NextLineVsNextIntUnitTest.java | 168 ++++++++-------- .../scanner/ScanACharacterUnitTest.java | 76 +++---- .../src/test/resources/sampleText1.txt | 6 +- 10 files changed, 502 insertions(+), 353 deletions(-) create mode 100644 core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/multinput/MultiInputs.java create mode 100644 core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java 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 5ad5528bad..22c04cdc58 100644 --- a/core-java-modules/core-java-io-apis-2/pom.xml +++ b/core-java-modules/core-java-io-apis-2/pom.xml @@ -1,46 +1,115 @@ - - - 4.0.0 - core-java-io-apis-2 - core-java-io-apis-2 - 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 - - - - - core-java-io-apis-2 - - - src/main/resources - true - - - - + + + 4.0.0 + core-java-io-apis-2 + core-java-io-apis-2 + 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 + + + org.junit.jupiter + junit-jupiter-engine + 5.7.2 + test + + + org.junit.jupiter + junit-jupiter + + + org.junit.jupiter + junit-jupiter + + + org.junit.jupiter + junit-jupiter + + + org.junit.jupiter + junit-jupiter + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter-version} + test + + + + org.junit.jupiter + junit-jupiter-params + ${junit-jupiter-version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter-version} + test + + + org.junit.jupiter + junit-jupiter + + + org.junit.jupiter + junit-jupiter + + + org.junit.jupiter + junit-jupiter + + + org.testng + testng + 7.1.0 + test + + + org.testng + testng + 7.5 + compile + + + + 5.2.0 + + + core-java-io-apis-2 + + + src/main/resources + true + + + + \ No newline at end of file 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-2/src/main/java/com/baeldung/multinput/MultiInputs.java new file mode 100644 index 0000000000..df799b2511 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/multinput/MultiInputs.java @@ -0,0 +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."); } + + } +} + diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/absolutetorelative/AbsoluteToRelativeUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/absolutetorelative/AbsoluteToRelativeUnitTest.java index 0830b0808a..9b3f424a30 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/absolutetorelative/AbsoluteToRelativeUnitTest.java +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/absolutetorelative/AbsoluteToRelativeUnitTest.java @@ -1,93 +1,92 @@ -package com.baeldung.absolutetorelative; - -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.net.URI; -import java.nio.file.Path; -import java.nio.file.Paths; - -public class AbsoluteToRelativeUnitTest { - - // given - until using Paths, no need to create physical files - private final Path pathOne = Paths.get("/baeldung/bar/one.txt"); - private final Path pathTwo = Paths.get("/baeldung/bar/two.txt"); - private final Path pathThree = Paths.get("/baeldung/foo/three.txt"); - - private final URI uriOne = pathOne.toUri(); - private final URI uriTwo = pathTwo.toUri(); - private final URI uriThree = pathThree.toUri(); - - @Test - public void givenAbsolutePaths_whenRelativizePathOneToPathTwo_thenRelativeIsReturned() { - Path result = pathOne.relativize(pathTwo); - - Assertions.assertThat(result) - .isRelative() - .isEqualTo(Paths.get("../two.txt")); - } - - @Test - public void givenAbsolutePaths_whenRelativizePathTwoToPathOne_thenRelativeIsReturned() { - Path result = pathTwo.relativize(pathOne); - - Assertions.assertThat(result) - .isRelative() - .isEqualTo(Paths.get("../one.txt")); - } - - @Test - public void givenAbsolutePaths_whenRelativizePathOneParentToPathTwo_thenRelativeIsReturned() { - Path result = pathOne.getParent().relativize(pathTwo); - - Assertions.assertThat(result) - .isRelative() - .isEqualTo(Paths.get("two.txt")); - } - - @Test - public void givenAbsolutePaths_whenRelativizePathOneToPathThree_thenRelativeIsReturned() { - Path result = pathOne.relativize(pathThree); - - Assertions.assertThat(result) - .isRelative() - .isEqualTo(Paths.get("../../foo/three.txt")); - } - - @Test - public void givenAbsolutePaths_whenRelativizePathThreeToPathOne_thenRelativeIsReturned() { - Path result = pathThree.relativize(pathOne); - - Assertions.assertThat(result) - .isRelative() - .isEqualTo(Paths.get("../../bar/one.txt")); - } - - @Test - public void givenAbsoluteURIs_whenRelativizeUriOneToUriTwo_thenAbsoluteIsReturned() { - URI result = uriOne.relativize(uriTwo); - - Assertions.assertThat(result) - .asString() - .contains("/baeldung/bar/two.txt"); - } - - @Test - public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriTwo_thenRelativeIsReturned() { - URI result = pathOne.getParent().toUri().relativize(uriTwo); - - Assertions.assertThat(result) - .asString() - .contains("two.txt"); - } - - @Test - public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriThree_thenAbsoluteIsReturned() { - URI result = pathOne.getParent().toUri().relativize(uriThree); - - Assertions.assertThat(result) - .asString() - .contains("/baeldung/foo/three.txt"); - } - -} +package com.baeldung.absolutetorelative; + +import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class AbsoluteToRelativeUnitTest { + + // given - until using Paths, no need to create physical files + private final Path pathOne = Paths.get("/baeldung/bar/one.txt"); + private final Path pathTwo = Paths.get("/baeldung/bar/two.txt"); + private final Path pathThree = Paths.get("/baeldung/foo/three.txt"); + + private final URI uriOne = pathOne.toUri(); + private final URI uriTwo = pathTwo.toUri(); + private final URI uriThree = pathThree.toUri(); + + @Test + public void givenAbsolutePaths_whenRelativizePathOneToPathTwo_thenRelativeIsReturned() { + Path result = pathOne.relativize(pathTwo); + + org.assertj.core.api.Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../two.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathTwoToPathOne_thenRelativeIsReturned() { + Path result = pathTwo.relativize(pathOne); + + org.assertj.core.api.Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../one.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathOneParentToPathTwo_thenRelativeIsReturned() { + Path result = pathOne.getParent().relativize(pathTwo); + + org.assertj.core.api.Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("two.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathOneToPathThree_thenRelativeIsReturned() { + Path result = pathOne.relativize(pathThree); + + org.assertj.core.api.Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../../foo/three.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathThreeToPathOne_thenRelativeIsReturned() { + Path result = pathThree.relativize(pathOne); + + org.assertj.core.api.Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../../bar/one.txt")); + } + + @Test + public void givenAbsoluteURIs_whenRelativizeUriOneToUriTwo_thenAbsoluteIsReturned() { + URI result = uriOne.relativize(uriTwo); + + org.assertj.core.api.Assertions.assertThat(result) + .asString() + .contains("/baeldung/bar/two.txt"); + } + + @Test + public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriTwo_thenRelativeIsReturned() { + URI result = pathOne.getParent().toUri().relativize(uriTwo); + + org.assertj.core.api.Assertions.assertThat(result) + .asString() + .contains("two.txt"); + } + + @Test + public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriThree_thenAbsoluteIsReturned() { + URI result = pathOne.getParent().toUri().relativize(uriThree); + + org.assertj.core.api.Assertions.assertThat(result) + .asString() + .contains("/baeldung/foo/three.txt"); + } + +} 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-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java index a2aeca56ad..06b7d38821 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-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java @@ -1,31 +1,31 @@ -package com.baeldung.bufferedreadervsfilereader; - -import org.junit.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; - -public class BufferedReaderUnitTest { - - @Test - public void whenReadingAFile_thenReadsLineByLine() { - StringBuilder result = new StringBuilder(); - - try (BufferedReader br = new BufferedReader(new FileReader("src/test/resources/sampleText1.txt"))) { - 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.FileReader; +import java.io.IOException; + +import org.junit.jupiter.api.Assertions; +import org.testng.annotations.Test; +public class BufferedReaderUnitTest { + + @Test + public void whenReadingAFile_thenReadsLineByLine() { + StringBuilder result = new StringBuilder(); + + try (BufferedReader br = new BufferedReader(new FileReader("src/test/resources/sampleText1.txt"))) { + 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-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java index f046f313d4..5df870e7b5 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-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java @@ -1,30 +1,30 @@ -package com.baeldung.bufferedreadervsfilereader; - -import org.junit.jupiter.api.Test; - -import java.io.FileReader; -import java.io.IOException; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class FileReaderUnitTest { - - @Test - public 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; + +public class FileReaderUnitTest { + + @Test + public 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-2/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java new file mode 100644 index 0000000000..317d9e817e --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/multinput/TestMultipleInputsUnitTest.java @@ -0,0 +1,47 @@ +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); + } +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/path/DesktopPathUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/path/DesktopPathUnitTest.java index fe21d0a72f..1b71512c5c 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/path/DesktopPathUnitTest.java +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/path/DesktopPathUnitTest.java @@ -1,29 +1,27 @@ -package com.baeldung.path; - -import org.junit.jupiter.api.Test; - -import java.io.File; - -import static org.junit.Assert.assertEquals; - -import javax.swing.filechooser.FileSystemView; - -public class DesktopPathUnitTest { - // Adapt DESKTOP_PATH variable to your own system path - // private static final String DESKTOP_PATH = "C:\\Users\\HRAF\\Desktop"; - - @Test - public void whenUsingGetUserHomeProperty_thenShouldEqualDesktopPath() { - String desktopPath = System.getProperty("user.home") + File.separator + "Desktop"; - // assertEquals(DESKTOP_PATH, desktopPath); - } - - @Test - public void whenUsingFileSystemViewGetHomeDirectory_thenShouldEqualDesktopPath() { - FileSystemView view = FileSystemView.getFileSystemView(); - File file = view.getHomeDirectory(); - String path = file.getPath(); - // assertEquals(DESKTOP_PATH, path); - } - -} +package com.baeldung.path; + +import java.io.File; + +import javax.swing.filechooser.FileSystemView; + +import org.junit.jupiter.api.Test; + +public class DesktopPathUnitTest { + // Adapt DESKTOP_PATH variable to your own system path + // private static final String DESKTOP_PATH = "C:\\Users\\HRAF\\Desktop"; + + @Test + public void whenUsingGetUserHomeProperty_thenShouldEqualDesktopPath() { + String desktopPath = System.getProperty("user.home") + File.separator + "Desktop"; + // assertEquals(DESKTOP_PATH, desktopPath); + } + + @Test + public void whenUsingFileSystemViewGetHomeDirectory_thenShouldEqualDesktopPath() { + FileSystemView view = FileSystemView.getFileSystemView(); + File file = view.getHomeDirectory(); + String path = file.getPath(); + // assertEquals(DESKTOP_PATH, path); + } + +} 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-io-apis-2/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java index 8fab7c62e9..3aae0469d0 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/NextLineVsNextIntUnitTest.java +++ b/core-java-modules/core-java-io-apis-2/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/ScanACharacterUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java index 1a70c6e3af..340b58bbf6 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScanACharacterUnitTest.java +++ b/core-java-modules/core-java-io-apis-2/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/resources/sampleText1.txt b/core-java-modules/core-java-io-apis-2/src/test/resources/sampleText1.txt index 20aeba2bad..ea0584cfe0 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/resources/sampleText1.txt +++ b/core-java-modules/core-java-io-apis-2/src/test/resources/sampleText1.txt @@ -1,3 +1,3 @@ -first line -second line -third line +first line +second line +third line