From 046403eb8fae0d713210aa6023e6a993cb474b3a Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 27 Jan 2022 18:54:25 +0100 Subject: [PATCH] Added getScanner() method for Cli class with test for testing stream integration --- src/main/java/Application/Cli.java | 12 +++++++++++- src/main/java/Main.java | 4 ++-- src/test/java/Application/CliTest.java | 15 ++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/Application/Cli.java b/src/main/java/Application/Cli.java index a2d787d..b786871 100644 --- a/src/main/java/Application/Cli.java +++ b/src/main/java/Application/Cli.java @@ -1,16 +1,26 @@ package Application; +import java.io.InputStream; import java.io.PrintStream; +import java.util.Scanner; public class Cli { private PrintStream printStream; + private InputStream inputStream; + private Scanner scanner; - public Cli(PrintStream printStream) { + public Cli(PrintStream printStream, InputStream inputStream) { this.printStream = printStream; + this.inputStream = inputStream; + this.scanner = new Scanner(inputStream); } public PrintStream getPrintStream() { return this.printStream; } + + public Scanner getScanner(){ + return this.scanner; + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index cf9689e..d286686 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,8 +7,8 @@ public class Main { public static void main(String[] args) { Tictactoe ttt = new Tictactoe(); - Cli cli = new Cli(System.out); - Scanner scan = new Scanner(System.in); + Cli cli = new Cli(System.out, System.in); + Scanner scan = cli.getScanner(); ttt.print(cli); while (scan.hasNext()) { ttt.update(scan.next()); diff --git a/src/test/java/Application/CliTest.java b/src/test/java/Application/CliTest.java index 747cf61..bc81251 100644 --- a/src/test/java/Application/CliTest.java +++ b/src/test/java/Application/CliTest.java @@ -4,7 +4,9 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.io.PrintStream; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -13,23 +15,34 @@ class CliTest { private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; + private final ByteArrayInputStream inContent = new ByteArrayInputStream("Test".getBytes()); + private final InputStream originalIn = System.in; Cli cli; @BeforeEach void setUp() { System.setOut(new PrintStream(outContent)); - cli = new Cli(System.out); + System.setIn(inContent); + cli = new Cli(System.out, System.in); } @AfterEach void tearDown() { System.setOut(originalOut); + System.setIn(originalIn); } + //Only tests correct stream integration, other methods are provided @Test void getPrintStream() { cli.getPrintStream().println("Hello World!"); assertEquals("Hello World!\n", outContent.toString().replaceAll("\r", "")); } + + //Only tests correct stream integration, other methods are provided + @Test + void getScanner() { + assertEquals(cli.getScanner().next(), "Test"); + } } \ No newline at end of file