Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1.  
  2. import static org.junit.Assert.*;
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6.  
  7. /**
  8. * The test class PositionTest.
  9. *
  10. * @author Rasmus Dahlgaard & Christian Norre
  11. * @version CG2
  12. */
  13. public class PositionTest
  14. {
  15. private Game game;
  16. private Country country1, country2;
  17. private City cityA, cityB, cityC, cityD, cityE, cityF, cityG;
  18. private Position pos1, pos2;
  19. /**
  20. * Default constructor for test class RoadTest
  21. */
  22. public PositionTest()
  23. {
  24. }
  25.  
  26. /**
  27. * Sets up the test fixture.
  28. *
  29. * Called before every test case method.
  30. */
  31. @Before
  32. public void setUp()
  33. {
  34. // Create countries
  35. country1 = new Country("Country 1");
  36. country2 = new Country("Country 2");
  37.  
  38. // Create cities
  39. cityA = new City("City A", 80, country1);
  40. cityB = new City("City B", 60, country1);
  41. cityC = new City("City C", 40, country1);
  42. cityD = new City("City D", 100, country1);
  43. cityE = new City("City E", 50, country2);
  44. cityF = new City("City F", 90, country2);
  45. cityG = new City("City G", 70, country2);
  46.  
  47. // Connect cities to countries
  48. country1.addCity(cityA);
  49. country1.addCity(cityB);
  50. country1.addCity(cityC);
  51. country1.addCity(cityD);
  52.  
  53. country2.addCity(cityE);
  54. country2.addCity(cityF);
  55. country2.addCity(cityG);
  56.  
  57. // Create roads
  58. country1.addRoads(cityA, cityB, 4);
  59. country1.addRoads(cityA, cityC, 3);
  60. country1.addRoads(cityA, cityD, 5);
  61. country1.addRoads(cityB, cityD, 2);
  62. country1.addRoads(cityC, cityD, 2);
  63. country1.addRoads(cityC, cityE, 4);
  64. country1.addRoads(cityD, cityF, 3);
  65. country2.addRoads(cityE, cityC, 4);
  66. country2.addRoads(cityE, cityF, 2);
  67. country2.addRoads(cityE, cityG, 5);
  68. country2.addRoads(cityF, cityD, 3);
  69. country2.addRoads(cityF, cityG, 6);
  70.  
  71. // Create positions
  72. pos1 = new Position(cityA, cityB, 4);
  73. pos2 = new Position(cityC, cityD, 2);
  74. }
  75.  
  76. /**
  77. * Tets the Position class constructor and indirectly if the acessor methods work
  78. */
  79. @Test
  80. public void constructor() {
  81. // pos1 is from A to B with total distance 4 and remaining distance 4
  82. assertEquals(cityA, pos1.getFrom());
  83. assertEquals(cityB, pos1.getTo());
  84. assertEquals(4, pos1.getDistance());
  85. assertEquals(4, pos1.getTotal());
  86.  
  87. // pos2 is from C to D with total distance 2 and remaining distance 2
  88. assertEquals(cityC, pos2.getFrom());
  89. assertEquals(cityD, pos2.getTo());
  90. assertEquals(2, pos2.getDistance());
  91. assertEquals(2, pos2.getTotal());
  92.  
  93. }
  94.  
  95. @Test
  96. public void move() {
  97.  
  98. // Pos2 has a total distance of 2 and remaining distance 2
  99. assertEquals(true, pos2.move());;
  100. assertEquals(1, pos2.getDistance());
  101. assertEquals(true, pos2.move());;
  102. assertEquals(0, pos2.getDistance()); // Distance should now be 0
  103. assertEquals(false, pos2.move());; // therefore move should return false
  104. assertEquals(0, pos2.getDistance());
  105. }
  106.  
  107. /**
  108. * Tests the turnAround method
  109. */
  110. @Test
  111. public void turnAround() {
  112. // pos1 is from A to B with total distance 4 and remaining distance 4
  113. // After moving 1 and then turning it should be from B to A with a remaining distance of 1
  114. pos1.move();
  115. pos1.turnAround();
  116. assertEquals(cityB, pos1.getFrom());
  117. assertEquals(cityA, pos1.getTo());
  118. assertEquals(1, pos1.getDistance());
  119.  
  120. pos1.turnAround();
  121. assertEquals(cityA, pos1.getFrom());
  122. assertEquals(cityB, pos1.getTo());
  123. assertEquals(3, pos1.getDistance());
  124.  
  125. // pos2 is from C to D with total distance 2 and remaining distance 2
  126. // Is used to check the outer cases where the player has arrived
  127. pos2.turnAround();
  128. assertEquals(cityD, pos2.getFrom());
  129. assertEquals(cityC, pos2.getTo());
  130. assertEquals(0, pos2.getDistance());
  131.  
  132. pos2.turnAround();
  133. assertEquals(cityC, pos2.getFrom());
  134. assertEquals(cityD, pos2.getTo());
  135. assertEquals(2, pos2.getDistance());
  136.  
  137. }
  138.  
  139. /**
  140. * Tests the hasArrived method
  141. * Expected to arrive after moving twice
  142. */
  143. @Test
  144. public void hasArrived()
  145. {
  146. // pos2 is from C to D with total distance 2 and remaining distance 2
  147. assertEquals(false, pos2.hasArrived());
  148. pos2.move();
  149. assertEquals(false, pos2.hasArrived());
  150. pos2.move();
  151. assertEquals(true, pos2.hasArrived());
  152.  
  153. }
  154.  
  155. /**
  156. * Tests the toString method
  157. */
  158. @Test
  159. public void testToString() {
  160. // City A has the value of 80, city B has the value of 60
  161. // pos1 is from A to B with total distance 4 and remaining distance 4
  162. assertEquals("City A (80) -> City B (60) : 4/4", pos1.toString());
  163.  
  164. // City C has the vaue of 40, city B has the value of 100
  165. // pos2 is from C to D with total distance 2 and remaining distance 2
  166. assertEquals("City C (40) -> City D (100) : 2/2", pos2.toString());
  167. pos2.move();
  168. assertEquals("City C (40) -> City D (100) : 1/2", pos2.toString());
  169. pos2.move();
  170. assertEquals("City C (40) -> City D (100) : 0/2", pos2.toString());
  171. // Note that the value of the city is not changed by any of the methods in the position class
  172.  
  173. }
  174.  
  175. /**
  176. * Tears down the test fixture.
  177. *
  178. * Called after every test case method.
  179. */
  180. @After
  181. public void tearDown()
  182. {
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement