Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | daniel-mar | 1 | package gdi.ws0809.test; |
2 | |||
3 | |||
4 | import static org.junit.Assert.assertEquals; |
||
5 | import static org.junit.Assert.assertFalse; |
||
6 | import static org.junit.Assert.assertTrue; |
||
7 | |||
8 | import java.io.BufferedReader; |
||
9 | import java.io.File; |
||
10 | import java.io.FileReader; |
||
11 | import java.io.FileWriter; |
||
12 | import java.net.URISyntaxException; |
||
13 | |||
14 | import org.junit.Before; |
||
15 | import org.junit.BeforeClass; |
||
16 | import org.junit.Test; |
||
17 | |||
18 | |||
19 | /** |
||
20 | * Public tests for the 1st extended Sokoban implementation level |
||
21 | * |
||
22 | * @author Steven Arzt, Oren Avni |
||
23 | * @version 1.0 |
||
24 | */ |
||
25 | public class SokobanTestExtended1 { |
||
26 | |||
27 | /** |
||
28 | * the tested Sokoban implementation |
||
29 | */ |
||
30 | private SokobanTest testee = null; |
||
31 | |||
32 | |||
33 | private static File validLevel; |
||
34 | private static final String validLevelString = "####\n# .#\n# ###\n#*@ #\n# $ #\n# ###\n####\n"; |
||
35 | private static final String validLevelMovedString = "####\n# .#\n# ###\n#* @ #\n# $ #\n# ###\n####\n"; |
||
36 | private static final String validLevelMoved2String = "####\n# .#\n# ###\n#* @#\n# $ #\n# ###\n####\n"; |
||
37 | |||
38 | private static final String secondLevelString = "####\n#$.#\n#@ #\n####\n"; |
||
39 | |||
40 | private static File levelDir; |
||
41 | private static File highScoreFile; |
||
42 | |||
43 | |||
44 | @BeforeClass |
||
45 | public static void init() { |
||
46 | try { |
||
47 | validLevel = new File(ClassLoader.getSystemClassLoader().getResource("valid.txt").toURI()); |
||
48 | levelDir = new File(ClassLoader.getSystemClassLoader().getResource("testlevel").toURI()); |
||
49 | highScoreFile = new File(levelDir, "highscore.txt"); |
||
50 | } catch (final URISyntaxException e) { |
||
51 | e.printStackTrace(); |
||
52 | } |
||
53 | SokobanTestAdapter.init(); |
||
54 | } |
||
55 | |||
56 | @Before |
||
57 | public void before() { |
||
58 | testee = new SokobanTestAdapter(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Requirement "moveWorker" |
||
63 | */ |
||
64 | @Test |
||
65 | public void moveWorker() throws Exception { |
||
66 | testee.loadLevel(validLevel); |
||
67 | testee.moveWorker('R'); |
||
68 | assertEquals("Worker not moved to the right.",validLevelMovedString,testee.currentLevelToString()); |
||
69 | } |
||
70 | |||
71 | @Test |
||
72 | public void doNotPerformInvalidMoves() throws Exception { |
||
73 | testee.loadLevel(validLevel); |
||
74 | testee.moveWorker('R'); |
||
75 | testee.moveWorker('R'); |
||
76 | testee.moveWorker('R'); |
||
77 | assertEquals("Worker not moved to the right.",validLevelMoved2String,testee.currentLevelToString()); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Requirement "startNext" |
||
82 | */ |
||
83 | @Test |
||
84 | public void loadLevelFromDir() throws Exception { |
||
85 | testee.setLevelDir(levelDir); |
||
86 | testee.startNextLevel(); |
||
87 | assertEquals("Current level should be the first level to start with.",validLevelString,testee.currentLevelToString()); |
||
88 | testee.startNextLevel(); |
||
89 | assertEquals("Current level is not the second level.",secondLevelString,testee.currentLevelToString()); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Requirement "countSteps" |
||
94 | */ |
||
95 | @Test |
||
96 | public void countSteps() throws Exception { |
||
97 | testee.loadLevel(validLevel); |
||
98 | testee.moveWorker('R'); |
||
99 | assertEquals("One step should have been performed.", 1, testee.getStepsInCurrentLevel()); |
||
100 | testee.moveWorker('R'); |
||
101 | assertEquals("Two steps should have been performed.", 2, testee.getStepsInCurrentLevel()); |
||
102 | testee.moveWorker('R'); |
||
103 | assertEquals("Two steps should have been performed. Did you count illegal moves?", 2, testee.getStepsInCurrentLevel()); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Requirement "highScoreDB" |
||
108 | * @throws Exception |
||
109 | */ |
||
110 | @Test |
||
111 | public void highscoreDbCreate() throws Exception { |
||
112 | if (highScoreFile.exists()) |
||
113 | highScoreFile.delete(); |
||
114 | testee.setLevelDir(levelDir); |
||
115 | testee.startNextLevel(); |
||
116 | |||
117 | testee.writeHighScoreFile(); |
||
118 | assertTrue("Highscore file 'highscore.txt' not written.",highScoreFile.exists()); |
||
119 | assertTrue("Could not delete highscore file. Did you leave any Reader/Writer open?", highScoreFile.delete()); |
||
120 | } |
||
121 | |||
122 | @Test |
||
123 | public void highscoreDbRead() throws Exception { |
||
124 | if (highScoreFile.exists()) { |
||
125 | highScoreFile.delete(); |
||
126 | assertFalse("Could not delete highscore file", highScoreFile.exists()); |
||
127 | } |
||
128 | highScoreFile.createNewFile(); |
||
129 | final FileWriter fw = new FileWriter(highScoreFile); |
||
130 | final String highScoreData = "1\ttestplayer\t2\n"; |
||
131 | fw.append(highScoreData); |
||
132 | fw.flush(); |
||
133 | fw.close(); |
||
134 | testee.setLevelDir(levelDir); |
||
135 | highScoreFile.delete(); |
||
136 | assertFalse("Could not delete highscore file", highScoreFile.exists()); |
||
137 | |||
138 | // As there might be a time or not in the data the student solutions |
||
139 | // write out, we read 14 characters (which includes level number, player |
||
140 | // name and step count) and append a \n as a line terminator in order to |
||
141 | // accept both cases. |
||
142 | |||
143 | testee.writeHighScoreFile(); |
||
144 | final FileReader fr = new FileReader(highScoreFile); |
||
145 | final char[] buff = new char[15]; |
||
146 | fr.read(buff); |
||
147 | fr.close(); |
||
148 | buff [14] = '\n'; |
||
149 | assertEquals("Existing highscore data not treated properly.", highScoreData, new String(buff)); |
||
150 | assertTrue("Could not delete highscore file. Did you leave any Reader/Writer open?", highScoreFile.delete()); |
||
151 | } |
||
152 | |||
153 | @Test |
||
154 | public void highscoreClear() throws Exception { |
||
155 | if (highScoreFile.exists()) { |
||
156 | highScoreFile.delete(); |
||
157 | assertFalse("Could not delete highscore file", highScoreFile.exists()); |
||
158 | } |
||
159 | highScoreFile.createNewFile(); |
||
160 | final FileWriter fw = new FileWriter(highScoreFile); |
||
161 | final String highScoreData = "1\ttestplayer\t2\n"; |
||
162 | fw.append(highScoreData); |
||
163 | fw.flush(); |
||
164 | fw.close(); |
||
165 | testee.setLevelDir(levelDir); |
||
166 | testee.clearHighscoreList(); |
||
167 | testee.writeHighScoreFile(); |
||
168 | if (highScoreFile.exists()) { |
||
169 | final BufferedReader br = new BufferedReader(new FileReader(highScoreFile)); |
||
170 | final String line = br.readLine(); |
||
171 | br.close(); |
||
172 | assertTrue("Existing highscore data not treated properly.", (null == line) || ("" == line)); |
||
173 | assertTrue("Could not delete highscore file. Did you leave any Reader/Writer open?", highScoreFile.delete()); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | |||
178 | @Test |
||
179 | public void highscoreDbWrite() throws Exception { |
||
180 | if (highScoreFile.exists()) |
||
181 | highScoreFile.delete(); |
||
182 | testee.setLevelDir(levelDir); |
||
183 | testee.startNextLevel(); |
||
184 | |||
185 | testee.setPlayerName("testplayer"); |
||
186 | testee.startNextLevel(); |
||
187 | testee.startNextLevel(); |
||
188 | testee.moveWorker('R'); |
||
189 | try { |
||
190 | testee.startNextLevel(); |
||
191 | } catch (final Exception e) { |
||
192 | } |
||
193 | testee.writeHighScoreFile(); |
||
194 | final BufferedReader br = new BufferedReader(new FileReader(highScoreFile)); |
||
195 | System.out.println(highScoreFile.toString()); |
||
196 | final String line = br.readLine(); |
||
197 | final String[] parts = line.split("\t"); |
||
198 | br.close(); |
||
199 | assertEquals("Solved level should be 3.", "3", parts[0]); |
||
200 | assertEquals("Player name should be testplayer.", "testplayer", parts[1]); |
||
201 | assertEquals("Step count should be 1.", "1", parts[2]); |
||
202 | assertTrue("Could not delete highscore file. Did you leave any Reader/Writer open?", |
||
203 | highScoreFile.delete()); |
||
204 | } |
||
205 | |||
206 | @Test |
||
207 | public void testGetWorkerPositionX() throws Exception { |
||
208 | testee.loadLevel(validLevel); |
||
209 | assertEquals ("The worker inside the first level is located at X = 2)", 2, testee.getWorkerPositionX()); |
||
210 | } |
||
211 | |||
212 | @Test |
||
213 | public void testGetWorkerPositionY() throws Exception { |
||
214 | testee.loadLevel(validLevel); |
||
215 | assertEquals ("The worker inside the first level is located at (2, 3)", 3, testee.getWorkerPositionY()); |
||
216 | } |
||
217 | |||
218 | @Test |
||
219 | public void testIsCrateAt() throws Exception { |
||
220 | testee.loadLevel(validLevel); |
||
221 | assertFalse("No crate at 0,0", testee.isCrateAt(0, 0)); |
||
222 | assertFalse("No crate at 0,3", testee.isCrateAt(0, 3)); |
||
223 | assertTrue("There should be a crate at 1,3", testee.isCrateAt(1, 3)); |
||
224 | } |
||
225 | |||
226 | @Test |
||
227 | public void testIsWallAt() throws Exception { |
||
228 | testee.loadLevel(validLevel); |
||
229 | assertTrue("Wall at 0,0", testee.isWallAt(0, 0)); |
||
230 | assertTrue("Wall at 0,3", testee.isWallAt(0, 3)); |
||
231 | assertTrue("Wall at 5,4", testee.isWallAt(5, 4)); |
||
232 | assertTrue("Wall at 5,5", testee.isWallAt(5, 5)); |
||
233 | assertFalse("No Wall at 2,2", testee.isWallAt(1, 1)); |
||
234 | } |
||
235 | |||
236 | @Test |
||
237 | public void testIsGoalAt() throws Exception { |
||
238 | testee.loadLevel(validLevel); |
||
239 | assertFalse ("No goal at 0,0", testee.isGoalAt(0, 0)); |
||
240 | assertFalse ("No goal at 0,3", testee.isGoalAt(0, 3)); |
||
241 | assertFalse ("No goal at 4,0", testee.isGoalAt(4, 0)); |
||
242 | assertFalse ("No goal at 4,2", testee.isGoalAt(4, 2)); |
||
243 | assertFalse ("No goal at 1,6", testee.isGoalAt(1, 6)); |
||
244 | assertFalse ("No goal at 4,1", testee.isGoalAt(4, 1)); |
||
245 | assertTrue ("Goal at 2,1", testee.isGoalAt(2, 1)); |
||
246 | } |
||
247 | |||
248 | |||
249 | @Test |
||
250 | public void testCanMoveCrate() throws Exception { |
||
251 | testee.loadLevel(validLevel); |
||
252 | assertTrue("Crate at 3,4 can be moved left", testee.canMoveCrate (3, 4, 'L')); |
||
253 | assertTrue("Crate at 3,4 can be moved right", testee.canMoveCrate (3, 4, 'R')); |
||
254 | assertFalse("Crate at 3,4 cannot be moved down", testee.canMoveCrate (3, 4, 'D')); |
||
255 | } |
||
256 | |||
257 | @Test |
||
258 | public void testHighscoreCounts() throws Exception { |
||
259 | if (highScoreFile.exists()) |
||
260 | highScoreFile.delete(); |
||
261 | testee.setLevelDir(levelDir); |
||
262 | assertEquals ("No highscores loaded yet", 0, testee.getHighscoreCount()); |
||
263 | assertTrue(testee.createHighscoreEntry("Habakuk", 1, 20, 12)); |
||
264 | assertEquals ("One highscore entry should be there.", 1, testee.getHighscoreCount()); |
||
265 | |||
266 | assertTrue(testee.createHighscoreEntry("Rainer Zufall", 2, 15, 5)); |
||
267 | assertEquals ("Two highscore entries should be there", 2, testee.getHighscoreCount()); |
||
268 | |||
269 | assertTrue(testee.createHighscoreEntry("Konstantin Opel", 1, 25, 8)); |
||
270 | assertEquals ("three highscore entries should be there", 3, testee.getHighscoreCount ()); |
||
271 | |||
272 | // fill the list |
||
273 | for (int i = 3; i <= 10; i++) |
||
274 | assertTrue (testee.createHighscoreEntry("Hans Wurst", 1, 25 + (i * 2), 10 + (i * 3))); |
||
275 | assertEquals (11, testee.getHighscoreCount()); |
||
276 | |||
277 | assertFalse("Score to bad to be put in highscore table", testee.createHighscoreEntry("Arno Nyhm", 1, 250, 300)); |
||
278 | assertEquals("Still eleven entries in highscore table", 11, testee.getHighscoreCount()); |
||
279 | |||
280 | assertTrue ("Should override an highscore entry", testee.createHighscoreEntry("Kai Pirinha", 1, 30, 45)); |
||
281 | assertEquals("Still eleven entries in highscore table", 11, testee.getHighscoreCount()); |
||
282 | } |
||
283 | |||
284 | @Test |
||
285 | public void testGetBestPlayerName() { |
||
286 | if (highScoreFile.exists()) |
||
287 | highScoreFile.delete(); |
||
288 | testee.setLevelDir(levelDir); |
||
289 | |||
290 | assertTrue(testee.createHighscoreEntry ("Habakuk", 1, 20, 12)); |
||
291 | assertEquals("Playername of best player should be set", "Habakuk", testee.getBestPlayerName()); |
||
292 | |||
293 | assertTrue(testee.createHighscoreEntry("Ich", 1, 15, 17)); |
||
294 | assertEquals("Ich", testee.getBestPlayerName()); |
||
295 | |||
296 | assertTrue(testee.createHighscoreEntry("Du", 1, 17, 23)); |
||
297 | assertEquals("Ich", testee.getBestPlayerName()); |
||
298 | } |
||
299 | |||
300 | /*========================================================================*/ |
||
301 | |||
302 | } |