Browse Source

Added Cli class for centralized I/O management and getPrintStream() method with test.

Updated other classes for Cli class integration.
feature-cli
Nick Stolbov 3 years ago
parent
commit
2ab0579f3c
  1. 16
      src/main/java/Application/Cli.java
  2. 6
      src/main/java/Game/Game.java
  3. 2
      src/main/java/Game/TicTacToe/Board.java
  4. 7
      src/main/java/Main.java
  5. 35
      src/test/java/Application/CliTest.java
  6. 2
      src/test/java/Application/MenuTest.java
  7. 29
      src/test/java/Game/GameTest.java
  8. 2
      src/test/java/Game/TicTacToe/BoardTest.java
  9. 7
      src/test/java/Game/TictactoeTest.java

16
src/main/java/Application/Cli.java

@ -0,0 +1,16 @@
package Application;
import java.io.PrintStream;
public class Cli {
private PrintStream printStream;
public Cli(PrintStream printStream) {
this.printStream = printStream;
}
public PrintStream getPrintStream() {
return this.printStream;
}
}

6
src/main/java/Game/Game.java

@ -1,5 +1,7 @@
package Game; package Game;
import Application.Cli;
import java.util.ArrayList; import java.util.ArrayList;
public abstract class Game { public abstract class Game {
@ -9,9 +11,9 @@ public abstract class Game {
public abstract void update(String input); public abstract void update(String input);
public void print() {
public void print(Cli cli) {
for (String s : outputBuffer) { for (String s : outputBuffer) {
System.out.println(s);
cli.getPrintStream().println(s);
} }
} }

2
src/main/java/Game/TicTacToe/Board.java

@ -1,7 +1,6 @@
package Game.TicTacToe; package Game.TicTacToe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class Board { public class Board {
@ -39,7 +38,6 @@ public class Board {
} }
public static char getStatedChar(State state) { public static char getStatedChar(State state) {
switch (state) { switch (state) {
case CIRCLE: case CIRCLE:

7
src/main/java/Main.java

@ -1,3 +1,4 @@
import Application.Cli;
import Game.Tictactoe; import Game.Tictactoe;
import java.util.Scanner; import java.util.Scanner;
@ -5,13 +6,13 @@ import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello world!");
Tictactoe ttt = new Tictactoe(); Tictactoe ttt = new Tictactoe();
Cli cli = new Cli(System.out);
Scanner scan = new Scanner(System.in); Scanner scan = new Scanner(System.in);
ttt.print();
ttt.print(cli);
while (scan.hasNext()) { while (scan.hasNext()) {
ttt.update(scan.next()); ttt.update(scan.next());
ttt.print();
ttt.print(cli);
} }
} }
} }

35
src/test/java/Application/CliTest.java

@ -0,0 +1,35 @@
package Application;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CliTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Cli cli;
@BeforeEach
void setUp() {
System.setOut(new PrintStream(outContent));
cli = new Cli(System.out);
}
@AfterEach
void tearDown() {
System.setOut(originalOut);
}
@Test
void getPrintStream() {
cli.getPrintStream().println("Hello World!");
assertEquals("Hello World!\n", outContent.toString().replaceAll("\r", ""));
}
}

2
src/test/java/Application/MenuTest.java

@ -4,8 +4,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;

29
src/test/java/Game/GameTest.java

@ -2,42 +2,17 @@ package Game;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class GameTest { class GameTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Game game;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
System.setOut(new PrintStream(outContent));
game = mock(Game.class, Mockito.CALLS_REAL_METHODS);
} }
@AfterEach @AfterEach
void tearDown() { void tearDown() {
System.setOut(originalOut);
}
@Test
public void print() {
ArrayList<String> testOB = new ArrayList<>();
testOB.add("Hello");
testOB.add("World");
testOB.add("!!!");
game.setOutputBuffer(testOB);
game.print();
assertEquals("Hello\nWorld\n!!!\n", outContent.toString().replaceAll("\r", ""));
} }
} }

2
src/test/java/Game/TicTacToe/BoardTest.java

@ -1,6 +1,5 @@
package Game.TicTacToe; package Game.TicTacToe;
import Game.Tictactoe;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -9,7 +8,6 @@ import org.mockito.internal.util.reflection.FieldSetter;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class BoardTest { class BoardTest {

7
src/test/java/Game/TictactoeTest.java

@ -2,13 +2,6 @@ package Game;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
class TictactoeTest { class TictactoeTest {

Loading…
Cancel
Save