BAEL-3143 - made the required changes

This commit is contained in:
vatsalgosar
2019-10-23 00:17:07 +05:30
parent 9946fa53a1
commit a87822229a

View File

@@ -1,54 +1,34 @@
package com.baeldung.scanner;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException;
import java.util.Scanner;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class JavaScannerUnitTest {
@Test
public void whenReadingLines_thenCorrect() {
String input = "Scanner\nTest\n";
byte[] byteArray = input.getBytes(StandardCharsets.UTF_8);
//@formatter:off
try (InputStream is = new ByteArrayInputStream(byteArray);
Scanner scanner = new Scanner(is)) {
//@formatter:on
String result = scanner.nextLine() + " " + scanner.nextLine();
String expected = input.replace("\n", " ")
.trim();
assertEquals(expected, result);
} catch (IOException e) {
fail(e.getMessage());
try (Scanner scanner = new Scanner(input)) {
assertEquals("Scanner", scanner.nextLine());
assertEquals("Test", scanner.nextLine());
}
}
@Test(expected = NoSuchElementException.class)
public void whenReadingLinesFromStringContainingNoLines_thenThrowNoSuchElementException() {
String input = "";
Scanner scanner = new Scanner(input);
String result = scanner.nextLine();
scanner.close();
public void whenReadingLines_thenThrowNoSuchElementException() {
try (Scanner scanner = new Scanner("")) {
String result = scanner.nextLine();
}
}
@Test(expected = IllegalStateException.class)
public void whenReadingLinesUsingClosedScanner_thenThrowIllegalStateException() {
String input = "";
Scanner scanner = new Scanner(input);
public void whenReadingLines_thenThrowIllegalStateException() {
Scanner scanner = new Scanner("");
scanner.close();
String result = scanner.nextLine();
}
}