public void testLibertyAndImmigration() {
    Game game = ServerTestHelper.startServerGame(getTestMap(true));

    final int population = 3;
    Colony colony = getStandardColony(population);

    ServerBuilding townHall = (ServerBuilding) colony.getBuilding(townHallType);
    Unit statesman = colony.getUnitList().get(0);
    townHall.setWorkFor(statesman);
    assertEquals(bellsType, statesman.getWorkType());

    ServerBuilding church = (ServerBuilding) colony.getBuilding(chapelType);
    church.upgrade();
    Unit preacher = colony.getUnitList().get(1);
    church.setWorkFor(preacher);
    assertEquals(crossesType, preacher.getWorkType());

    assertEquals(0, colony.getGoodsCount(bellsType));
    ServerTestHelper.newTurn();

    int bells = 3;
    assertEquals(population, colony.getUnitCount());
    assertEquals(bells, colony.getNetProductionOf(bellsType));
    assertEquals(bells, colony.getGoodsCount(bellsType));

    colony.addGoods(bellsType, 7);
    bells += 7;
    assertEquals(bells, colony.getGoodsCount(bellsType));
    assertEquals(bells, colony.getLiberty());

    colony.removeGoods(bellsType, 5);
    bells -= 5;
    assertEquals(bells, colony.getGoodsCount(bellsType));
    assertEquals(bells, colony.getLiberty());

    int crosses = colony.getTotalProductionOf(crossesType) - colony.getConsumptionOf(crossesType);
    assertEquals(crosses, colony.getNetProductionOf(crossesType));
    assertEquals(crosses, colony.getGoodsCount(crossesType));
    assertEquals(crosses, colony.getImmigration());

    colony.addGoods(crossesType, 7);
    crosses += 7;
    assertEquals(crosses, colony.getGoodsCount(crossesType));
    assertEquals(crosses, colony.getImmigration());

    colony.removeGoods(crossesType, 5);
    crosses -= 5;
    assertEquals(crosses, colony.getGoodsCount(crossesType));
    assertEquals(crosses, colony.getImmigration());
  }
  public void testAvoidStarvation() {
    Game game = ServerTestHelper.startServerGame(getTestMap(marsh));

    int unitsBeforeNewTurn = 3;
    Colony colony = getStandardColony(unitsBeforeNewTurn);
    ServerPlayer player = (ServerPlayer) colony.getOwner();
    assertEquals("Wrong number of units in colony", unitsBeforeNewTurn, colony.getUnitCount());

    final Building townHall = colony.getBuilding(townHallType);
    for (Unit u : colony.getUnitList()) {
      u.setLocation(townHall);
    }
    colony.removeGoods(foodGoodsType);
    colony.invalidateCache();

    int consumption = colony.getFoodConsumption();
    int production = colony.getTile().getType().getPotentialProduction(grainType, null);
    assertEquals(6, consumption);
    assertEquals(3, production);
    assertEquals(-3, colony.getNetProductionOf(foodType));
    assertEquals(0, colony.getGoodsCount(foodType));
    assertEquals(0, colony.getTile().getUnitCount());

    colony.addGoods(foodType, 202);
    ServerTestHelper.newTurn();
    assertEquals(199, colony.getGoodsCount(foodType));
    assertEquals(0, colony.getTile().getUnitCount());
    assertEquals(3, colony.getUnitCount());

    colony.addGoods(foodType, 15);
    ServerTestHelper.newTurn();
    assertEquals(11, colony.getGoodsCount(foodType));
    assertEquals(1, colony.getTile().getUnitCount());
  }
 /**
  * Returns a list of all units in this colony of the given type.
  *
  * @param type The type of the units to include in the list. For instance Unit.EXPERT_FARMER.
  * @return A list of all the units of the given type in this colony.
  */
 private List<Unit> getUnitList(Colony colony, UnitType type) {
   List<Unit> units = new ArrayList<>();
   for (Unit unit : colony.getUnitList()) {
     if (type.equals(unit.getType())) {
       units.add(unit);
     }
   }
   return units;
 }
  public void testDeathByStarvation() {
    Game game = ServerTestHelper.startServerGame(getTestMap(marsh));

    int consumption, production, unitsBeforeNewTurn = 3;
    Colony colony = getStandardColony(unitsBeforeNewTurn);
    ServerPlayer player = (ServerPlayer) colony.getOwner();

    final Building townHall = colony.getBuilding(townHallType);
    for (Unit u : colony.getUnitList()) {
      u.setLocation(townHall);
    }
    colony.removeGoods(foodGoodsType);
    colony.invalidateCache();

    consumption = colony.getFoodConsumption();
    production = colony.getFoodProduction();
    assertTrue(
        "Food consumption ("
            + consumption
            + ") should be higher than production ("
            + production
            + ")",
        consumption > production);
    assertEquals("No food stored in colony", 0, colony.getGoodsCount(foodType));
    assertEquals("Wrong number of units in colony", unitsBeforeNewTurn, colony.getUnitCount());

    ServerTestHelper.newTurn();

    consumption = colony.getFoodConsumption();
    production = colony.getFoodProduction();
    assertTrue(
        "Food consumption ("
            + consumption
            + ") should be higher than production ("
            + production
            + ")",
        consumption > production);
    assertEquals("No food stored in colony", 0, colony.getGoodsCount(foodType));
    assertEquals("Wrong number of units in colony", unitsBeforeNewTurn - 1, colony.getUnitCount());
  }
 /** Creates a colony with a university and n elder statesmen */
 private Colony getSchoolColony(int n, SchoolLevel slevel) {
   Colony colony = getStandardColony(n);
   for (Unit u : colony.getUnitList()) u.setType(elderStatesmanType);
   BuildingType type = null;
   switch (slevel) {
     case SCHOOLHOUSE:
       type = schoolType;
       break;
     case COLLEGE:
       type = collegeType;
       break;
     case UNIVERSITY:
       type = universityType;
       break;
     default:
       fail("Setup error, cannot setup school");
   }
   Building school = new ServerBuilding(colony.getGame(), colony, type);
   colony.addBuilding(school);
   assertEquals(school.getUnitList().size(), 0);
   colony.addGoods(grainType, 150); // prevent starving during tests
   return colony;
 }
  public void testEqualFoodProductionConsumptionCase() {
    Game game = ServerTestHelper.startServerGame(getTestMap(desert));

    // Setting test colony
    Tile colonyTile = game.getMap().getTile(5, 8);
    Colony colony =
        FreeColTestUtils.getColonyBuilder().colonyTile(colonyTile).initialColonists(1).build();

    // Set the food production of the center tile of the colony to 2
    // This will be the only food production of the colony
    List<AbstractGoods> colonyTileProduction = colonyTile.getType().getPossibleProduction(true);
    for (int i = 0; i < colonyTileProduction.size(); i++) {
      AbstractGoods production = colonyTileProduction.get(i);
      if (production.getType() == foodGoodsType) {
        production.setAmount(2);
        break;
      }
    }
    Unit unit = colony.getUnitList().get(0);
    unit.setLocation(colony.getWorkLocationFor(unit, bellsType));

    // Verify that there is enough food stored
    colony.addGoods(foodGoodsType, colony.getFoodConsumption() * 2);

    assertEquals(
        "Production not equal to consumption",
        colony.getFoodConsumption(),
        colony.getFoodProduction());

    int colonists = colony.getUnitCount();
    assertEquals("Unexpected change of colonists in colony", colonists, colony.getUnitCount());

    assertEquals(
        "Unexpected change of production/consumption ratio",
        colony.getFoodProduction(),
        colony.getFoodConsumption());
  }