Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.junit.Assert.*;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- /**
- * The test class PositionTest.
- *
- * @author Rasmus Dahlgaard & Christian Norre
- * @version CG2
- */
- public class PositionTest
- {
- private Game game;
- private Country country1, country2;
- private City cityA, cityB, cityC, cityD, cityE, cityF, cityG;
- private Position pos1, pos2;
- /**
- * Default constructor for test class RoadTest
- */
- public PositionTest()
- {
- }
- /**
- * Sets up the test fixture.
- *
- * Called before every test case method.
- */
- @Before
- public void setUp()
- {
- // Create countries
- country1 = new Country("Country 1");
- country2 = new Country("Country 2");
- // Create cities
- cityA = new City("City A", 80, country1);
- cityB = new City("City B", 60, country1);
- cityC = new City("City C", 40, country1);
- cityD = new City("City D", 100, country1);
- cityE = new City("City E", 50, country2);
- cityF = new City("City F", 90, country2);
- cityG = new City("City G", 70, country2);
- // Connect cities to countries
- country1.addCity(cityA);
- country1.addCity(cityB);
- country1.addCity(cityC);
- country1.addCity(cityD);
- country2.addCity(cityE);
- country2.addCity(cityF);
- country2.addCity(cityG);
- // Create roads
- country1.addRoads(cityA, cityB, 4);
- country1.addRoads(cityA, cityC, 3);
- country1.addRoads(cityA, cityD, 5);
- country1.addRoads(cityB, cityD, 2);
- country1.addRoads(cityC, cityD, 2);
- country1.addRoads(cityC, cityE, 4);
- country1.addRoads(cityD, cityF, 3);
- country2.addRoads(cityE, cityC, 4);
- country2.addRoads(cityE, cityF, 2);
- country2.addRoads(cityE, cityG, 5);
- country2.addRoads(cityF, cityD, 3);
- country2.addRoads(cityF, cityG, 6);
- // Create positions
- pos1 = new Position(cityA, cityB, 4);
- pos2 = new Position(cityC, cityD, 2);
- }
- /**
- * Tets the Position class constructor and indirectly if the acessor methods work
- */
- @Test
- public void constructor() {
- // pos1 is from A to B with total distance 4 and remaining distance 4
- assertEquals(cityA, pos1.getFrom());
- assertEquals(cityB, pos1.getTo());
- assertEquals(4, pos1.getDistance());
- assertEquals(4, pos1.getTotal());
- // pos2 is from C to D with total distance 2 and remaining distance 2
- assertEquals(cityC, pos2.getFrom());
- assertEquals(cityD, pos2.getTo());
- assertEquals(2, pos2.getDistance());
- assertEquals(2, pos2.getTotal());
- }
- @Test
- public void move() {
- // Pos2 has a total distance of 2 and remaining distance 2
- assertEquals(true, pos2.move());;
- assertEquals(1, pos2.getDistance());
- assertEquals(true, pos2.move());;
- assertEquals(0, pos2.getDistance()); // Distance should now be 0
- assertEquals(false, pos2.move());; // therefore move should return false
- assertEquals(0, pos2.getDistance());
- }
- /**
- * Tests the turnAround method
- */
- @Test
- public void turnAround() {
- // pos1 is from A to B with total distance 4 and remaining distance 4
- // After moving 1 and then turning it should be from B to A with a remaining distance of 1
- pos1.move();
- pos1.turnAround();
- assertEquals(cityB, pos1.getFrom());
- assertEquals(cityA, pos1.getTo());
- assertEquals(1, pos1.getDistance());
- pos1.turnAround();
- assertEquals(cityA, pos1.getFrom());
- assertEquals(cityB, pos1.getTo());
- assertEquals(3, pos1.getDistance());
- // pos2 is from C to D with total distance 2 and remaining distance 2
- // Is used to check the outer cases where the player has arrived
- pos2.turnAround();
- assertEquals(cityD, pos2.getFrom());
- assertEquals(cityC, pos2.getTo());
- assertEquals(0, pos2.getDistance());
- pos2.turnAround();
- assertEquals(cityC, pos2.getFrom());
- assertEquals(cityD, pos2.getTo());
- assertEquals(2, pos2.getDistance());
- }
- /**
- * Tests the hasArrived method
- * Expected to arrive after moving twice
- */
- @Test
- public void hasArrived()
- {
- // pos2 is from C to D with total distance 2 and remaining distance 2
- assertEquals(false, pos2.hasArrived());
- pos2.move();
- assertEquals(false, pos2.hasArrived());
- pos2.move();
- assertEquals(true, pos2.hasArrived());
- }
- /**
- * Tests the toString method
- */
- @Test
- public void testToString() {
- // City A has the value of 80, city B has the value of 60
- // pos1 is from A to B with total distance 4 and remaining distance 4
- assertEquals("City A (80) -> City B (60) : 4/4", pos1.toString());
- // City C has the vaue of 40, city B has the value of 100
- // pos2 is from C to D with total distance 2 and remaining distance 2
- assertEquals("City C (40) -> City D (100) : 2/2", pos2.toString());
- pos2.move();
- assertEquals("City C (40) -> City D (100) : 1/2", pos2.toString());
- pos2.move();
- assertEquals("City C (40) -> City D (100) : 0/2", pos2.toString());
- // Note that the value of the city is not changed by any of the methods in the position class
- }
- /**
- * Tears down the test fixture.
- *
- * Called after every test case method.
- */
- @After
- public void tearDown()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement