1 | package jpacman.model; |
2 | |
3 | import static org.junit.Assert.assertEquals; |
4 | import static org.junit.Assert.assertTrue; |
5 | |
6 | import org.junit.Before; |
7 | import org.junit.Test; |
8 | |
9 | import jpacman.TestUtils; |
10 | |
11 | /** |
12 | * One of the simpler test classes, so a good point to start understanding |
13 | * how testing JPacman has been setup. |
14 | * |
15 | * @author Arie van Deursen; Jul 29, 2003 |
16 | * @version $Id: BoardTest.java,v 1.10 2008/02/03 19:43:38 arie Exp $ |
17 | */ |
18 | public class BoardTest { |
19 | |
20 | /** |
21 | * The width & height of the board to be used. |
22 | */ |
23 | private final int width = 5, height = 10; |
24 | |
25 | /** |
26 | * The board to be used in the tests. |
27 | */ |
28 | private Board theBoard; |
29 | |
30 | /** |
31 | * Create a simple (empty) board to be used for testing. |
32 | * Note that JUnit invokes such a Before method before every test case |
33 | * so that we start in a fresh situation. |
34 | */ |
35 | @Before |
36 | public void setUp() { |
37 | theBoard = new Board(width, height); |
38 | } |
39 | |
40 | /** |
41 | * Simple test of the width/height getters, |
42 | * to get in the mood for testing. |
43 | */ |
44 | @Test |
45 | public void testGettingWidthHeight() { |
46 | assertEquals(width, theBoard.getWidth()); |
47 | assertEquals(height, theBoard.getHeight()); |
48 | } |
49 | |
50 | /** |
51 | * Obtain a cell that is on the board at given (x,y) position, |
52 | * and check if the (x,y) coordinates of the Cell obtained are ok. |
53 | * To make the test a little more exciting use a point that is on the |
54 | * border (an "invariant on point" in Binder's terminology). |
55 | */ |
56 | @Test |
57 | public void testGettingCellsFromBoard() { |
58 | // the coordinates to be used. |
59 | int x = 0; |
60 | int y = 0; |
61 | |
62 | // actually get the cell. |
63 | Cell aCell = theBoard.getCell(x, y); |
64 | |
65 | // compare the coordinates. |
66 | assertEquals(x, aCell.getX()); |
67 | assertEquals(y, aCell.getY()); |
68 | } |
69 | |
70 | /** |
71 | * Let a guest occupy one of the Cells on the board, and |
72 | * check if it is actually there. |
73 | * Again, pick a point on the border (but a different one) |
74 | * to (slightly) increase the chances of failure. |
75 | */ |
76 | @Test |
77 | public void testOccupy() { |
78 | // place to put the guest. |
79 | int x = width - 1; |
80 | int y = height - 1; |
81 | Cell aCell = theBoard.getCell(x, y); |
82 | |
83 | // guest to be put on the board. |
84 | Food food = new Food(); |
85 | |
86 | // put the guest on the cell. |
87 | food.occupy(aCell); |
88 | |
89 | // verify its presence. |
90 | assertEquals(food, theBoard.getGuest(x, y)); |
91 | assertEquals(Guest.FOOD_TYPE, theBoard.guestCode(x, y)); |
92 | } |
93 | |
94 | /** |
95 | * Try to create an illegal board (e.g., negative sizes), |
96 | * and verify that this generates an assertion failure. |
97 | * This test also serves to illustrate how to test whether a method |
98 | * generates an assertion failure if a precondition is violated. |
99 | */ |
100 | @Test |
101 | public void testFailingBoardCreation() { |
102 | if (TestUtils.assertionsEnabled()) { |
103 | boolean failureGenerated; |
104 | try { |
105 | new Board(-1, -1); |
106 | failureGenerated = false; |
107 | } catch (AssertionError ae) { |
108 | failureGenerated = true; |
109 | } |
110 | assertTrue(failureGenerated); |
111 | } |
112 | // else: nothing to test -- no guarantees what so ever! |
113 | } |
114 | |
115 | |
116 | } |