package org.scotlandyard.tests.engine;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.scotlandyard.engine.Game;
import org.scotlandyard.engine.GameException;
import org.scotlandyard.engine.User;
import org.scotlandyard.engine.boardmap.BoardMap;
import org.scotlandyard.engine.constants.GameStatus;
import org.scotlandyard.engine.player.Player;
import org.scotlandyard.impl.engine.GameEngine;
import org.scotlandyard.impl.engine.GameImpl;
import org.scotlandyard.impl.engine.UserImpl;
import org.scotlandyard.impl.engine.boardmap.BoardMapImpl;
import org.scotlandyard.impl.engine.player.Detective;
import org.scotlandyard.impl.engine.player.MrX;
/**
* TODO add description
*
* @author Hussain Al-Mutawa
* @version 1.0
* @since Sept 2011
*
*/
public class GameTest {
private static BoardMap boardMap;
private static final String GAME = "game";
private static final String OUTPUT = "Not yer implemented";
@Before
public void setUpBeforeClass() throws GameException {
boardMap = BoardMapImpl.getNewInstance("pnth");
}
@Test //TODO add description of what the test should do
public final void tearDown() throws Exception{
GameEngine.instance().getLobby().removeAllGames();
GameEngine.instance().getUsers().clear();
}
@Test //TODO add description of what the test should do
public final void testAddDetective() throws GameException{
final User creator = new UserImpl("user1", "user1@game.com");
Game game;
try{
game = GameImpl.getNewInstance("game1", creator, boardMap);
Assert.assertTrue(false);
}catch(GameException ex){
Assert.assertTrue(true);
}
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(),0);
GameEngine.instance().loginUser(creator);
Assert.assertNotNull(GameEngine.instance().getUser(creator.getEmail()));
Assert.assertEquals(GameEngine.instance().getUser(creator.getEmail()), creator);
game = GameImpl.getNewInstance("game1", creator, boardMap);
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(),1);
Assert.assertEquals(game.getDetectives().size(),0);
Assert.assertEquals(game.getPlayers().size(),0);
final Player player = Detective.getNewInstance(game, creator);
Assert.assertEquals(game.getDetectives().size(),1);
Assert.assertEquals(game.getPlayers().size(),1);
Assert.assertEquals(game.getPlayer(creator.getEmail()),player);
Assert.assertEquals(game.getPlayer(player.getEmail()),player);
Assert.assertEquals(creator.getEmail(),player.getEmail());
Assert.assertEquals(creator.getName(),player.getName());
Assert.assertNotNull(creator);
Assert.assertNotNull(creator.getEmail());
Assert.assertNull(game.getMrX());
GameEngine.instance().logoutUser(creator.getEmail());
try{
game.removePlayer(creator.getEmail());
Assert.assertFalse(true);
}catch(GameException ex){
Assert.assertTrue(true);
}
GameEngine.instance().getLobby().removeGame(game.getIdentifier());
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(),0);
GameEngine.instance().getBoardMaps().clear();
}
@Test //TODO add description of what the test should do
public final void testGetAvailablePositions() {
fail(OUTPUT); // TODO testGetAvailablePositions
}
@Test //TODO add description of what the test should do
public final void testGetBoardMap() throws GameException {
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 0);
Assert.assertEquals(GameEngine.instance().getBoardMaps().size(), 0);
final Game game = GameImpl.getNewInstance(GAME, null, null);
Assert.assertNull(game.getBoardMap());
final BoardMap boardMap = BoardMapImpl.getNewInstance("pnth");
game.setBoardMap(boardMap);
Assert.assertNotNull(game.getBoardMap());
Assert.assertSame(boardMap,game.getBoardMap());
Assert.assertEquals(game.getBoardMap().getName(), boardMap.getName());
game.setBoardMap(null);
Assert.assertNull(game.getBoardMap());
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 1);
Assert.assertEquals(GameEngine.instance().getBoardMaps().size(), 1);
GameEngine.instance().getBoardMaps().remove(boardMap.getName());
GameEngine.instance().getLobby().removeGame(game.getIdentifier());
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 0);
Assert.assertEquals(GameEngine.instance().getBoardMaps().size(), 0);
}
@Test //TODO add description of what the test should do
public final void testGetCreator() throws GameException{
Assert.assertEquals(GameEngine.instance().getUsers().size(),0);
Game game = GameImpl.getNewInstance("new", null, null);
Assert.assertNull(game.getCreator());
game = GameImpl.getNewInstance("another", UserImpl.getNewInstance("s", "s"), null);
Assert.assertNotNull(game.getCreator());
Assert.assertEquals(game.getCreator().getName(),"s");
GameEngine.instance().getLobby().removeAllGames();
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(),0);
Assert.assertEquals(GameEngine.instance().getUsers().size(),1);
GameEngine.instance().getUsers().clear();
Assert.assertEquals(GameEngine.instance().getUsers().size(),0);
}
@Test(expected=GameException.class)//TODO add description of what the test should do
public final void testGetDetectives() throws GameException{
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 0);
Assert.assertEquals(GameEngine.instance().getUsers().size(), 0);
final Game game = GameImpl.getNewInstance(GAME, null, null);
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 1);
Assert.assertEquals(game.getDetectives().size(), 0);
final Player player = new Detective(UserImpl.getNewInstance("s", "s"));
Assert.assertEquals(GameEngine.instance().getUsers().size(), 1);
game.addDetective((Detective)player);
Assert.assertEquals(game.getDetectives().size(), 1);
Assert.assertEquals(game.getPlayers().size(), 1);
Assert.assertEquals(game.getPlayer("s"), player);
Assert.assertTrue(game.removePlayer(player));
Assert.assertEquals(game.getPlayers().size(), 0);
Assert.assertEquals(game.getDetectives().size(), 0);
GameEngine.instance().getLobby().removeAllGames();
GameEngine.instance().getUsers().clear();
Assert.assertEquals(GameEngine.instance().getUsers().size(), 0);
game.removePlayer(player); // should throw an exception
}
@Test //TODO add description of what the test should do
public final void testGetGameStatus() {
fail(OUTPUT); // TODO testGetGameStatus
}
@Test //TODO add description of what the test should do
public final void testGetIdentifier() throws GameException{
final Game game = GameImpl.getNewInstance(GAME, null, null);
Assert.assertEquals(game.getIdentifier(),GAME);
GameEngine.instance().getLobby().removeAllGames();
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 0);
}
@Test(expected=GameException.class)
public final void testGetMrX() throws GameException{
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 0);
Assert.assertEquals(GameEngine.instance().getUsers().size(), 0);
final Game game = GameImpl.getNewInstance(GAME, null, null);
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(), 1);
Assert.assertNull(game.getMrX());
final Player player = new MrX(UserImpl.getNewInstance("s", "s"));
Assert.assertEquals(GameEngine.instance().getUsers().size(), 1);
game.setMrX((MrX)player);
Assert.assertEquals(game.getDetectives().size(), 0);
Assert.assertEquals(game.getPlayers().size(), 1);
Assert.assertEquals(game.getPlayer("s"), player);
Assert.assertTrue(game.removePlayer(player));
Assert.assertEquals(game.getPlayers().size(), 0);
GameEngine.instance().getLobby().removeAllGames();
GameEngine.instance().getUsers().clear();
Assert.assertEquals(GameEngine.instance().getUsers().size(), 0);
game.removePlayer(player); // should throw an exception
}
@Test //TODO add description of what the test should do
public final void testGetOccupiedPositions() {
fail(OUTPUT); // TODO testGetOccupiedPositions
}
@Test //TODO add description of what the test should do
public final void testGetPlayerPosition() {
fail(OUTPUT); // TODO testGetPlayerPosition
}
@Test //TODO add description of what the test should do
public final void testGetPosssibleMoves() {
fail(OUTPUT); // TODO testGetPosssibleMoves
}
@Test //TODO add description of what the test should do
public final void testGetTokens() {
fail(OUTPUT); // TODO testGetTokens
}
@Test //TODO add description of what the test should do
public final void testGetTurn() {
fail(OUTPUT); // TODO testGetTurn
}
@Test //TODO add description of what the test should do
public final void testRemove() throws GameException{
final GameEngine engine = GameEngine.instance();
User user1,user2;
user1 = new UserImpl("user1","user1");
user2 = new UserImpl("user2","user2");
Assert.assertEquals(engine.getUsers().size(), 0);
engine.loginUser(user1);
engine.loginUser(user2);
Assert.assertEquals(engine.getLobby().getAvailableGames().size(), 0);
final Game game = GameImpl.getNewInstance("game1", user1, boardMap);
Assert.assertEquals(engine.getLobby().getAvailableGames().size(), 1);
final Player player1 = new Detective(user1);
final Player player2 = new MrX(user2);
Assert.assertEquals(engine.getUsers().size(), 2);
Assert.assertEquals(game.getDetectives().size(), 0);
game.setMrX(player2);
try{
engine.startGame(game);
Assert.assertFalse(true);
}catch(Exception ex){
Assert.assertTrue(true);
}
try{
game.addDetective((Detective)player2);
Assert.assertFalse(true);
}catch(Exception ex){
Assert.assertTrue(true);
}
game.addDetective((Detective)player1);
Assert.assertNotNull(game.getPlayer(user1.getEmail()));
Assert.assertNotNull(game.getPlayer(user2.getEmail()));
Assert.assertEquals(game.getDetectives().size(), 1);
Assert.assertEquals(game.getPlayers().size(),2);
Assert.assertTrue(engine.logoutUser(user1.getEmail()));
Assert.assertFalse(engine.logoutUser(user1.getEmail()));
Assert.assertEquals(game.getPlayers().size(),1);
try{
engine.startGame(game);
Assert.assertFalse(true);
}catch(Exception ex){
Assert.assertTrue(true);
}
engine.getUsers().put(user1.getEmail(), user1);
try{
game.setMrX(player1); // should not allow to override existing mrX
Assert.assertFalse(true);
}catch(Exception ex){
Assert.assertTrue(true);
}
Assert.assertEquals(game.getDetectives().size(), 0);
game.addDetective((Detective)player1);
Assert.assertEquals(game.getDetectives().size(), 1);
Assert.assertEquals(game.getPlayers().size(), 2);
Assert.assertSame(game.getMrX(),player2);
Assert.assertTrue(game.getDetectives().contains(player1));
Assert.assertFalse(game.getDetectives().contains(player2));
try{
game.addDetective((Detective)player2); //should not allow player to be both mrx and detective in the same game
Assert.assertFalse(true);
}catch(Exception ex){
Assert.assertTrue(true);
}
try{
game.addDetective((Detective)player1); //should not allow player to be added as detective twice
Assert.assertFalse(true);
}catch(Exception ex){
Assert.assertTrue(true);
}
Assert.assertEquals(game.getDetectives().size(),1);
Assert.assertEquals(game.getPlayers().size(),2);
engine.startGame(game);
Assert.assertEquals(engine.startGame(game), "done");
Assert.assertEquals(game.getGameStatus().toString(),GameStatus.STARTED.toString());
Assert.assertTrue(game.removePlayer(user1.getEmail()));
Assert.assertTrue(game.removePlayer(user2.getEmail()));
try{
game.removePlayer(user2.getEmail());
Assert.assertFalse(true);
}catch(GameException ex){
Assert.assertTrue(true);
}
Assert.assertEquals(game.getDetectives().size(),0);
Assert.assertNull(game.getMrX());
}
@Test //TODO add description of what the test should do
public final void testSetGameStatus() throws GameException{
final Game game = GameImpl.getNewInstance("g", null, null);
Assert.assertEquals(game.getGameStatus().toString(),GameStatus.JUST_CREATED.toString());
game.setGameStatus(GameStatus.FINISHED);
Assert.assertEquals(game.getGameStatus().toString(),GameStatus.FINISHED.toString());
GameEngine.instance().getLobby().removeAllGames();
Assert.assertEquals(GameEngine.instance().getLobby().getAvailableGames().size(),0);
}
@Test //TODO add description of what the test should do
public final void testSetMrX() throws GameException{
final User creator = UserImpl.getNewInstance("c", "c");
final Game game = GameImpl.getNewInstance("d", creator, null);
Assert.assertNull(game.getMrX());
final Player mrX = MrX.getNewInstance(game, creator);
Assert.assertNotNull(game.getMrX());
Assert.assertSame(game.getMrX(),mrX);
}
@Test //TODO add description of what the test should do
public final void testSetTurn() {
fail(OUTPUT); // TODO testSetTurn
}
}