Package es.mahulo.battleship

Source Code of es.mahulo.battleship.ConfigTest

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.service.Config;
import es.mahulo.battleship.model.Cell;
import es.mahulo.battleship.model.Game;
import es.mahulo.battleship.model.GameConfig;
import es.mahulo.battleship.model.GameStatus;
import es.mahulo.battleship.model.Player;
import es.mahulo.battleship.model.Ship;
import es.mahulo.battleship.model.ShipConfig;
import es.mahulo.battleship.model.ShipExecption;
import es.mahulo.battleship.service.ConfigImpl;

public class ConfigTest {
  private Game game;
  private GameConfig  gameConfig;
  private List<ShipConfig> shipConfigs = new ArrayList<ShipConfig>();
  private List<Ship> ships = new ArrayList<Ship>();

 
  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}}
 
 
  @Before public void setUp() {
    shipConfigs = new ArrayList<ShipConfig>();

    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);
   
   
   
    gameConfig = new GameConfig();
    gameConfig.setId(1L);
    gameConfig.setDimensionX(10);
    gameConfig.setDimensionY(10);
    gameConfig.setShipConfigs(shipConfigs);
   
    game = new Game();
    game.setId(1L);
    game.setStatus(GameStatus.Configuring);
    game.setGameConfig(gameConfig);
   
   
    ships.add(buildShip(shipConfig1,cellShip1));
    ships.add(buildShip(shipConfig2,cellShip2));
    ships.add(buildShip(shipConfig3,cellShip3));
    ships.add(buildShip(shipConfig4,cellShip4));
    ships.add(buildShip(shipConfig4,cellShip5));
    ships.add(buildShip(shipConfig5,cellShip6));
    ships.add(buildShip(shipConfig5,cellShip7));
  }
 
  @Test(expected=ShipExecption.class) public void isConfigOkNoCellsTest() throws ShipExecption {
    Config configService = new ConfigImpl();

    List<Cell> cells = new ArrayList<Cell>();
    
    Ship ship = new Ship();
    ship.setShipConfig(shipConfigs.get(0));
    ship.setCells(cells);
   
    Player player = new Player();
    player.setShips(new ArrayList<Ship>());
   
    configService.isValid(game, player,ship);
  }
 
  @Test(expected=ShipExecption.class) public void isConfigOkShipSizeTest() throws ShipExecption {
    Config configService = new ConfigImpl();
   

    List<Cell> cells = new ArrayList<Cell>();
    Cell cell1 = new Cell();
    cell1.setX(9);
    cell1.setY(1);
    cells.add(cell1);
   

    Ship ship = new Ship();
    ship.setId(1L);
    ship.setShipConfig(shipConfigs.get(0));
    ship.setCells(cells);
   
    Player player = new Player();
    player.setShips(new ArrayList<Ship>());
   
    configService.isValid(game, player,ship);
  }
 
  @Test(expected=ShipExecption.class) public void isConfigOkNumberOfShipTest() throws ShipExecption {
    Config configService = new ConfigImpl();
   
    List<Ship> playerShips = new ArrayList<Ship>();
    playerShips.add(buildShip(shipConfigs.get(0), cellShip1));
   
    List<Player>  players = new ArrayList<Player>();
    Player player = new Player();
    player.setShips(playerShips);
    players.add(player);

   
    Game game = new Game();
    game.setStatus(GameStatus.Configuring);
    game.setGameConfig(gameConfig);
    game.setPlayers(players);
   
    Ship ship = buildShip(shipConfigs.get(0), cellShip1);
    
   
    configService.isValid(game, player,ship);
  }
 
  @Test(expected=ShipExecption.class) public void isConfigOkOutSideBoardTest() throws ShipExecption {
    Config configService = new ConfigImpl();
   
    List<Cell> cells = new ArrayList<Cell>();
    Cell cell = new Cell();
    cell.setX(11);
    cell.setY(1);
    cells.add(cell);

    List<Ship> ships = new ArrayList<Ship>();
    Ship ship = new Ship();
    ship.setShipConfig(shipConfigs.get(0));
    ship.setCells(cells);
    ships.add(ship);
   
    Player player = new Player();
    player.setShips(new ArrayList<Ship>());
   
    configService.isValid(game, player,ship);
  }
 
  @Test(expected=ShipExecption.class) public void isConfigOkCellIsUsedTest() throws ShipExecption {
    Config configService = new ConfigImpl();
   
    List<Cell> cells = new ArrayList<Cell>();
    Cell cell1 = new Cell();
    cell1.setX(9);
    cell1.setY(1);
    cells.add(cell1);
   
    Ship playerShip = new Ship();
    playerShip.setShipConfig(shipConfigs.get(4));
    playerShip.setCells(cells);
   
    List<Ship> ships = new ArrayList<Ship>();
    ships.add(playerShip);
   
    Player player = new Player();
    player.setId(1L);
    player.setShips(ships);
   
   
    cells = new ArrayList<Cell>();
    Cell cell2 = new Cell();
    cell1.setX(10);
    cell1.setY(2);
    cells.add(cell2);
   
    Ship ship = new Ship();
   
    configService.isValid(game, player,ship);
  }


 
  @Test public void isConfigValidTest() throws ShipExecption {
    Config configService = new ConfigImpl();
   
    List<Ship> playerShips = new ArrayList<Ship>();
    playerShips.add(buildShip(shipConfigs.get(0),cellShip1));
    playerShips.add(buildShip(shipConfigs.get(1),cellShip2));
    playerShips.add(buildShip(shipConfigs.get(2),cellShip3));
    playerShips.add(buildShip(shipConfigs.get(3),cellShip4));
    playerShips.add(buildShip(shipConfigs.get(3),cellShip5));
    playerShips.add(buildShip(shipConfigs.get(4),cellShip6));


   
    Player player = new Player();
    player.setId(1L);
    player.setShips(playerShips);
   
    Ship ship = buildShip(shipConfigs.get(4),cellShip7);
   
    configService.isValid(game, player,ship);
  }
 
  @Test public void isConfigComplete() throws ShipExecption {
    Config configService = new ConfigImpl();
   
   
    List<Ship> playerShips = new ArrayList<Ship>();
    playerShips.add(buildShip(shipConfigs.get(0),cellShip1));
    playerShips.add(buildShip(shipConfigs.get(1),cellShip2));
    playerShips.add(buildShip(shipConfigs.get(2),cellShip3));
    playerShips.add(buildShip(shipConfigs.get(3),cellShip4));
    playerShips.add(buildShip(shipConfigs.get(3),cellShip5));
    playerShips.add(buildShip(shipConfigs.get(4),cellShip6));
    playerShips.add(buildShip(shipConfigs.get(4),cellShip7));


   
    Player player = new Player();
    player.setId(1L);
    player.setShips(ships);
   
   
   
    boolean isConfigComple = configService.isComplete(gameConfig,player);
   
    Assert.assertTrue(isConfigComple);

   
  }

  @Test public void isConfigNotComplete() throws ShipExecption {
    Config configService = new ConfigImpl();
   
   
    List<Ship> playerShips = new ArrayList<Ship>();
    playerShips.add(buildShip(shipConfigs.get(0),cellShip1));
    playerShips.add(buildShip(shipConfigs.get(1),cellShip2));
    playerShips.add(buildShip(shipConfigs.get(2),cellShip3));
    playerShips.add(buildShip(shipConfigs.get(3),cellShip4));
    playerShips.add(buildShip(shipConfigs.get(3),cellShip5));
    playerShips.add(buildShip(shipConfigs.get(4),cellShip6));
   


   
    Player player = new Player();
    player.setId(1L);
    player.setShips(playerShips);
   
   
   
    boolean isConfigComple = configService.isComplete(gameConfig,player);
   
    Assert.assertFalse(isConfigComple);

   
  }
 
 
  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;
  }
 
}



TOP

Related Classes of es.mahulo.battleship.ConfigTest

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.