package games.war;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import game.Card;
import game.Deck;
import games.war.behaviors.Cheating;
import org.junit.Before;
import org.junit.Test;
/**
* Makes sure PlayerController works as it should
* @author Chris Hersh
*
*/
public class TestPlayerController
{
private ByteArrayOutputStream outContent;
/**
* Resets the deck and re-initializes the output stream
*/
@Before
public void destroyDeck()
{
Deck.getInstance().reset();
outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));
}
/**
* Makes sure the initialization works correctly
*/
@Test
public void testInit()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
assertEquals(2, pc.players.size());
assertTrue(pc.players.get(0) != null);
assertTrue(pc.players.get(1) != null);
assertEquals(null, pc.player1Card);
assertEquals(null, pc.player2Card);
assertTrue(pc.cardsOnScreen != null);
}
/**
* Makes sure the normal draw works as it should
*/
@Test
public void testNormalDraw()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
pc.playerNormalDraw(0);
assertTrue(pc.player1Card != null);
assertTrue(pc.player2Card == null);
pc.playerNormalDraw(1);
assertTrue(pc.player2Card != null);
}
/**
* makes sure cards can be placed correctly
*/
@Test
public void testPlaceCard()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
String properOutPlayer1 = "0-c";
String properOutPlayer2 = "0-c \t\t1-c\n";
pc.placeCard(0, new Card(0, 'c'));
assertEquals(properOutPlayer1, outContent.toString());
pc.placeCard(1, new Card(1, 'c'));
assertEquals(properOutPlayer2, outContent.toString());
}
/**
* Makes sure null cards can be placed
*/
@Test
public void testPlaceCardWithNullCard()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
String properOutPlayer1 = "";
String properOutPlayer2 = " \t\t\n";
pc.placeCard(0, null);
assertEquals(properOutPlayer1, outContent.toString());
pc.placeCard(1, null);
assertEquals(properOutPlayer2, outContent.toString());
}
/**
* Makes sure cards can be killed and that the header is output correctly
*/
@Test
public void testKillCards()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
String dashes = "--------------------------------------------------------------------------------";
String properOut = "\n" + dashes + "\nPlayer One\tPlayer Two\nCards: 26\tCards: 26\n";
pc.killCardsOnScreen();
assertEquals(properOut, outContent.toString());
}
/**
* Makes sure winners can be found
*/
@Test
public void testFindWinner()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
pc.player1Card = new Card(10, 'c');
pc.player2Card = new Card(1, 'c');
assertEquals(0, pc.findWinner());
pc.player1Card = new Card(1, 'c');
pc.player2Card = new Card(10, 'c');
assertEquals(1, pc.findWinner());
}
/**
* Makes sure the players can go to war
*/
@Test
public void testGoToWar()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
assertTrue(pc.goToWar(pc.players.get(0)) != null);
}
/**
* Makes sure checkForWar works as it should
*/
@Test
public void testCheckForWar()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
assertTrue(pc.checkForWar(new Card(10, 'c'), new Card(10, 'd')));
assertFalse(pc.checkForWar(new Card(10, 'c'), new Card(9, 'd')));
}
/**
* Makes sure checkForWar works will null cards
*/
@Test
public void testCheckForWarNullCards()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
assertFalse(pc.checkForWar(null, new Card(10, 'd')));
assertFalse(pc.checkForWar(new Card(10, 'c'), null));
assertFalse(pc.checkForWar(null, null));
}
/**
* Makes sure warActions works as it should
*/
@Test
public void testWarActions()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
pc.warActions();
assertTrue(pc.warCards1 != null);
assertTrue(pc.warCards2 != null);
assertTrue(outContent.toString() != "" && outContent.toString() != null);
assertTrue(pc.player1Card != null);
assertTrue(pc.player2Card != null);
}
/**
* makes sure update works as it should if one player has no hand left
*/
@Test
public void testUpdatePlayerOneEmptyHand()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
String dashes = "--------------------------------------------------------------------------------";
String killOutput = "\n" + dashes + "\nPlayer One\tPlayer Two\nCards: 0\tCards: 26\n";
String properOut = killOutput + "Player 2 won in ";
pc.players.get(0).clearHand();
pc.update();
assertEquals(properOut, outContent.toString());
}
/**
* Makes sure update works if the other player has no hand left
*/
@Test
public void testUpdatePlayerTwoEmptyHand()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
String dashes = "--------------------------------------------------------------------------------";
String killOutput = "\n" + dashes + "\nPlayer One\tPlayer Two\nCards: 26\tCards: 0\n";
String properOut = killOutput + "Player 1 won in ";
pc.players.get(1).clearHand();
pc.update();
assertEquals(properOut, outContent.toString());
}
/**
* Makes sure the update method works
*/
@Test
public void testUpdate()
{
PlayerController pc = new PlayerController(new Cheating(), new Cheating());
String dashes = "--------------------------------------------------------------------------------";
String killOutput = "\n" + dashes + "\nPlayer One\tPlayer Two\nCards: 26\tCards: 26\n";
pc.update();
assertTrue(pc.player1Card != null);
assertTrue(pc.player2Card != null);
assertTrue(pc.players.get(0).getHandSize() > 26 || pc.players.get(1).getHandSize() > 26);
}
}