Package es.mahulo.battleship

Source Code of es.mahulo.battleship.GameServerTest

package es.mahulo.battleship;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import es.mahulo.battleship.api.dao.GameConfigDao;
import es.mahulo.battleship.api.dao.GameDao;
import es.mahulo.battleship.api.dao.PlayerDao;
import es.mahulo.battleship.api.dao.UserDao;
import es.mahulo.battleship.mocks.GameConfigDaoMock;
import es.mahulo.battleship.mocks.GameDaoMock;
import es.mahulo.battleship.mocks.PlayerDaoMock;
import es.mahulo.battleship.mocks.UserDaoMock;
import es.mahulo.battleship.model.Cell;
import es.mahulo.battleship.model.Game;
import es.mahulo.battleship.model.GameStatus;
import es.mahulo.battleship.model.Hit;
import es.mahulo.battleship.model.Ship;
import es.mahulo.battleship.model.ShipConfig;
import es.mahulo.battleship.model.Shot;
import es.mahulo.battleship.service.ConfigImpl;
import es.mahulo.battleship.service.GameServiceImpl;

public class GameServerTest {
 
  private GameServiceImpl gameServer;
 
  private UserDao userDao;
  private GameDao gameDao;
  private GameConfigDao gameConfigDao;
  private PlayerDao playerDao;
 
 
  private int cellShip1[][]= {{9,1},{9,2},{9,3},{9,4},{9,5}};
  private int cellShip2[][]= {{7,4},{7,5},{7,6},{7,7}};
  private int cellShip3[][]= {{2,6},{3,6},{4,6}};
  private int cellShip4[][]= {{1,1},{1,2}};
  private int cellShip5[][]= {{3,4},{4,4}};
  private int cellShip6[][]= {{3,2}};
  private int cellShip7[][]= {{1,8}}
 
  private List<Ship> ships1 = new ArrayList<Ship>();
  private List<Ship> ships2 = new ArrayList<Ship>();
  private List<ShipConfig> shipConfigs = new ArrayList<ShipConfig>();
 
 

 
 
  @Before public void setUp() {
   
    playerDao = new PlayerDaoMock();
    userDao = new UserDaoMock();
    gameDao = new GameDaoMock();
    gameConfigDao = new GameConfigDaoMock();
   
   
   
    gameServer = new GameServiceImpl();
    gameServer.setGameDao(gameDao);
    gameServer.setUserDao(userDao);
    gameServer.setGameConfigDao(gameConfigDao);

    gameServer.setPlayerDao(playerDao);
    gameServer.setGameDao(gameDao);
    gameServer.setConfigService(new ConfigImpl());
   

    ShipConfig shipConfig1 = new ShipConfig();
    shipConfig1.setId(1L);
    shipConfig1.setName("aircraft carrier");
    shipConfig1.setNumber(1);
    shipConfig1.setSize(5);
    shipConfigs.add(shipConfig1);
   
    ShipConfig shipConfig2 = new ShipConfig();
    shipConfig2.setId(2L);
    shipConfig2.setName("battleship");
    shipConfig2.setNumber(1);
    shipConfig2.setSize(4);
    shipConfigs.add(shipConfig2);

    ShipConfig shipConfig3 = new ShipConfig();
    shipConfig3.setId(3L);
    shipConfig3.setName("cruiser");
    shipConfig3.setNumber(1);
    shipConfig3.setSize(3);
    shipConfigs.add(shipConfig3);
   
    ShipConfig shipConfig4 = new ShipConfig();
    shipConfig4.setId(4L);
    shipConfig4.setName("destroyer");
    shipConfig4.setNumber(2);
    shipConfig4.setSize(2);
    shipConfigs.add(shipConfig4);
   
   
    ShipConfig shipConfig5 = new ShipConfig();
    shipConfig5.setId(5L);
    shipConfig5.setName("submarine");
    shipConfig5.setNumber(2);
    shipConfig5.setSize(1);
    shipConfigs.add(shipConfig5);
   
   
    ships1.add(buildShip(shipConfig1,cellShip1));
    ships1.add(buildShip(shipConfig2,cellShip2));
    ships1.add(buildShip(shipConfig3,cellShip3));
    ships1.add(buildShip(shipConfig4,cellShip4));
    ships1.add(buildShip(shipConfig4,cellShip5));
    ships1.add(buildShip(shipConfig5,cellShip6));
    ships1.add(buildShip(shipConfig5,cellShip7));
   
   
    ships2.add(buildShip(shipConfig1,cellShip1));
    ships2.add(buildShip(shipConfig2,cellShip2));
    ships2.add(buildShip(shipConfig3,cellShip3));
    ships2.add(buildShip(shipConfig4,cellShip4));
    ships2.add(buildShip(shipConfig4,cellShip5));
    ships2.add(buildShip(shipConfig5,cellShip6));
    ships2.add(buildShip(shipConfig5,cellShip7));



  }
 
  @Test public void startUpTest() throws Exception {
    Game game = gameServer.startUp("manuel");

    Assert.assertNotNull(game);
    Assert.assertEquals(10,game.getGameConfig().getDimensionX());
    Assert.assertEquals(10,game.getGameConfig().getDimensionY());
    Assert.assertEquals(5,game.getGameConfig().getShipConfigs().size());
    Assert.assertEquals(GameStatus.StartUp,game.getStatus());
    Assert.assertEquals(1,game.getPlayers().size());
    Assert.assertEquals("manuel",game.getPlayers().get(0).getUser().getName());
  }
 
 
  @Test public void joinPlayerTest() throws Exception {
    Game game = gameServer.startUp("manuel");
    game = gameServer.joinPlayer(game.getId(), "maeve");
   
    Assert.assertEquals(GameStatus.Configuring,game.getStatus());
    Assert.assertEquals(2,game.getPlayers().size());
    Assert.assertEquals("manuel",game.getPlayers().get(0).getUser().getName());
    Assert.assertEquals("maeve",game.getPlayers().get(1).getUser().getName());
  }
 
 
  @Test public void addShipTest() throws Exception {
    Game game = gameServer.startUp("manuel");
    game = gameServer.joinPlayer(game.getId(), "maeve");

   
    gameServer.addShip(1L,"manuel", ships1);
   
  }
 
  @Test public void shotHitTest() throws Exception {
    Game game = gameServer.startUp("manuel");
    game = gameServer.joinPlayer(game.getId(), "maeve");

    gameServer.addShip(1L,"manuel", ships1);
    gameServer.addShip(1L,"maeve", ships2);
   
    Shot shot = new Shot();
    shot.setX(1);
    shot.setY(1);
   
    Boolean hit = gameServer.shot(1L,"maeve", shot);
   
    Assert.assertTrue(hit);
  }
 
  @Test public void shotNoHitTest() throws Exception {
    Game game = gameServer.startUp("manuel");
    game = gameServer.joinPlayer(game.getId(), "maeve");

    gameServer.addShip(1L,"manuel", ships1);
    gameServer.addShip(1L,"maeve", ships2);
   
    Shot shot = new Shot();
    shot.setX(3);
    shot.setY(3);
   
    Boolean hit = gameServer.shot(1L,"maeve", shot);
   
    Assert.assertFalse(hit);
  }
 
  @Test public void wind1Test() throws Exception {
    Game game = gameServer.startUp("manuel");
    game = gameServer.joinPlayer(game.getId(), "maeve");

    gameServer.addShip(1L,"manuel", ships1);
    gameServer.addShip(1L,"maeve", ships2);
   
    shotShip1(gameServer,cellShip1);
    shotShip1(gameServer,cellShip2);
    shotShip1(gameServer,cellShip3);
    shotShip1(gameServer,cellShip4);
    shotShip1(gameServer,cellShip5);
    shotShip1(gameServer,cellShip6);
    shotShip1(gameServer,cellShip7);
   

    Assert.assertEquals(GameStatus.Wind1, game.getStatus());
   
  }
 
  @Test public void wind2Test() throws Exception {
    Game game = gameServer.startUp("manuel");
    game = gameServer.joinPlayer(game.getId(), "maeve");

    gameServer.addShip(1L,"manuel", ships1);
    gameServer.addShip(1L,"maeve", ships2);
   
    shotShip2(gameServer,cellShip1);
    shotShip2(gameServer,cellShip2);
    shotShip2(gameServer,cellShip3);
    shotShip2(gameServer,cellShip4);
    shotShip2(gameServer,cellShip5);
    shotShip2(gameServer,cellShip6);
    shotShip2(gameServer,cellShip7);
   

    Assert.assertEquals(GameStatus.Wind2, game.getStatus());
   

   
  }


  @Test public void drawTest() throws Exception {
    Game game = gameServer.startUp("manuel");
    game = gameServer.joinPlayer(game.getId(), "maeve");

    gameServer.addShip(1L,"manuel", ships1);
    gameServer.addShip(1L,"maeve", ships2);
   
    shotShipDraw(gameServer,cellShip1);
    shotShipDraw(gameServer,cellShip1);
    shotShipDraw(gameServer,cellShip2);
    shotShipDraw(gameServer,cellShip3);
    shotShipDraw(gameServer,cellShip4);
    shotShipDraw(gameServer,cellShip5);
    shotShipDraw(gameServer,cellShip6);
    shotShipDraw(gameServer,cellShip7);

    Assert.assertEquals(GameStatus.Draw, game.getStatus());
   
  }


 
  private Ship buildShip(ShipConfig shipConfig,int [][] positions) {
    List<Cell> cells = new ArrayList<Cell>();
    for (int i=0; i< positions.length ;i++) {
      Cell cell = new Cell();
     
      cell.setX(positions[i][0]);
      cell.setY(positions[i][1]);
      cells.add(cell);
     
    }
    
    Ship ship = new Ship();
   
    ship.setShipConfig(shipConfig);
    ship.setCells(cells);

   
    return ship;
  }
 
  private void shotShip1(GameServiceImpl gameServer,int [][] positions) throws Exception {
    Shot shot2 = new Shot();
    shot2.setX(3);
    shot2.setY(3);

   
    for (int i=0; i< positions.length ;i++) {
      Shot shot1 = new Shot();
     
      shot1.setX(positions[i][0]);
      shot1.setY(positions[i][1]);
     
      gameServer.shot(1L,"maeve", shot1);
      gameServer.shot(1L,"manuel", shot2);
    }
  }
 
  private void shotShip2(GameServiceImpl gameServer,int [][] positions) throws Exception {
    Shot shot1 = new Shot();
    shot1.setX(3);
    shot1.setY(3);

   
    for (int i=0; i< positions.length ;i++) {
      Shot shot2 = new Shot();
     
      shot2.setX(positions[i][0]);
      shot2.setY(positions[i][1]);
     
      gameServer.shot(1L,"maeve", shot1);
      gameServer.shot(1L,"manuel", shot2);
    }
  }
 
 
  private void shotShipDraw(GameServiceImpl gameServer,int [][] positions) throws Exception {

   
    for (int i=0; i< positions.length ;i++) {
      Shot shot1 = new Shot();
     
      shot1.setX(positions[i][0]);
      shot1.setY(positions[i][1]);
     
      Shot shot2 = new Shot();
     
      shot2.setX(positions[i][0]);
      shot2.setY(positions[i][1]);

     
      gameServer.shot(1L,"maeve", shot1);
      gameServer.shot(1L,"manuel", shot2);
    }
  }



}



TOP

Related Classes of es.mahulo.battleship.GameServerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.