001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.examples.hangman;
018:
019: import java.util.Iterator;
020:
021: import org.apache.wicket.examples.WicketWebTestCase;
022: import org.apache.wicket.examples.hangman.Game;
023: import org.apache.wicket.examples.hangman.Letter;
024: import org.apache.wicket.examples.hangman.WordGenerator;
025:
026: import junit.framework.Assert;
027: import junit.framework.Test;
028:
029: /**
030: * Testcase for the <code>Game</code> class.
031: *
032: * @author Chris Turner
033: * @version 1.0
034: */
035: public class HangManTest extends WicketWebTestCase {
036: /**
037: *
038: * @return Test
039: */
040: public static Test suite() {
041: return suite(HangManTest.class);
042: }
043:
044: /**
045: * Create the test case.
046: *
047: * @param message
048: * The test name
049: */
050: public HangManTest(String message) {
051: super (message);
052: }
053:
054: /**
055: * Tests the hangman class directly for a winning game.
056: *
057: * @throws Exception
058: */
059: public void test_1() throws Exception {
060: Game hangman = new Game();
061: hangman.newGame(5,
062: new WordGenerator(new String[] { "testing" }));
063:
064: Assert.assertEquals(5, hangman.getGuessesRemaining());
065: Assert.assertFalse(hangman.isWon());
066: Assert.assertFalse(hangman.isLost());
067:
068: doGuessTest(hangman, 'a', false);
069: Assert.assertEquals(4, hangman.getGuessesRemaining());
070: Assert.assertFalse(hangman.isWon());
071: Assert.assertFalse(hangman.isLost());
072:
073: guess(hangman, 'a');
074: Assert.assertEquals(4, hangman.getGuessesRemaining());
075: Assert.assertFalse(hangman.isWon());
076: Assert.assertFalse(hangman.isLost());
077:
078: doGuessTest(hangman, 't', true);
079: Assert.assertEquals(4, hangman.getGuessesRemaining());
080: Assert.assertFalse(hangman.isWon());
081: Assert.assertFalse(hangman.isLost());
082:
083: doGuessTest(hangman, 'e', true);
084: Assert.assertEquals(4, hangman.getGuessesRemaining());
085: Assert.assertFalse(hangman.isWon());
086: Assert.assertFalse(hangman.isLost());
087:
088: doGuessTest(hangman, 's', true);
089: Assert.assertEquals(4, hangman.getGuessesRemaining());
090: Assert.assertFalse(hangman.isWon());
091: Assert.assertFalse(hangman.isLost());
092:
093: doGuessTest(hangman, 'i', true);
094: Assert.assertEquals(4, hangman.getGuessesRemaining());
095: Assert.assertFalse(hangman.isWon());
096: Assert.assertFalse(hangman.isLost());
097:
098: doGuessTest(hangman, 'n', true);
099: Assert.assertEquals(4, hangman.getGuessesRemaining());
100: Assert.assertFalse(hangman.isWon());
101: Assert.assertFalse(hangman.isLost());
102:
103: doGuessTest(hangman, 'g', true);
104: Assert.assertEquals(4, hangman.getGuessesRemaining());
105: Assert.assertTrue(hangman.isWon());
106: Assert.assertFalse(hangman.isLost());
107: }
108:
109: private Letter letter(Game hangman, char c) {
110: for (Iterator iter = hangman.getLetters().iterator(); iter
111: .hasNext();) {
112: Letter letter = (Letter) iter.next();
113: if (letter.asString().equalsIgnoreCase(
114: Character.toString(c))) {
115: return letter;
116: }
117: }
118: return null;
119: }
120:
121: private boolean guess(Game hangman, char c) {
122: return hangman.guess(letter(hangman, c));
123: }
124:
125: /**
126: * Tests the hangman class directly for a lost game.
127: *
128: * @throws Exception
129: */
130: public void testHangmanLoseGame() throws Exception {
131: Game hangman = new Game();
132: hangman.newGame(2, new WordGenerator(new String[] { "foo" }));
133:
134: Assert.assertEquals(2, hangman.getGuessesRemaining());
135: Assert.assertFalse(hangman.isWon());
136: Assert.assertFalse(hangman.isLost());
137:
138: doGuessTest(hangman, 'z', false);
139: Assert.assertEquals(1, hangman.getGuessesRemaining());
140: Assert.assertFalse(hangman.isWon());
141: Assert.assertFalse(hangman.isLost());
142:
143: doGuessTest(hangman, 'v', false);
144: Assert.assertEquals(0, hangman.getGuessesRemaining());
145: Assert.assertFalse(hangman.isWon());
146: Assert.assertTrue(hangman.isLost());
147: }
148:
149: /**
150: * Tests the webapplication for a successful match.
151: */
152: public void testHangmanSuccessWebGame() {
153:
154: getTestContext().setBaseUrl(
155: "http://localhost:8098/wicket-examples");
156: beginAt("/hangman/?word=hangman");
157:
158: assertTitleEquals("Wicket Examples - hangman");
159: assertLinkPresent("start");
160: clickLink("start");
161: assertTitleEquals("Wicket Examples - hangman");
162: assertTextPresent("guesses remaining");
163:
164: assertElementPresent("guessesRemaining");
165: assertTextInElement("guessesRemaining", "5");
166:
167: clickLink("letter_f");
168:
169: assertElementPresent("guessesRemaining");
170: assertTextInElement("guessesRemaining", "4");
171:
172: clickLink("letter_h");
173: assertElementPresent("guessesRemaining");
174: assertTextInElement("guessesRemaining", "4");
175:
176: clickLink("letter_a");
177: clickLink("letter_n");
178: clickLink("letter_g");
179: clickLink("letter_m");
180:
181: assertTextPresent("Congratulations! You guessed that the word was ");
182: }
183:
184: /**
185: * Tests the webapplication for an unsuccessful match.
186: */
187: public void testHangmanFailureWebGame() {
188: getTestContext().setBaseUrl(
189: "http://localhost:8098/wicket-examples");
190: beginAt("/hangman/?word=hangman");
191:
192: assertTitleEquals("Wicket Examples - hangman");
193: assertLinkPresent("start");
194: clickLink("start");
195: assertTitleEquals("Wicket Examples - hangman");
196: assertTextPresent("guesses remaining");
197:
198: assertElementPresent("guessesRemaining");
199: assertTextInElement("guessesRemaining", "5");
200:
201: clickLink("letter_f");
202:
203: assertElementPresent("guessesRemaining");
204: assertTextInElement("guessesRemaining", "4");
205:
206: // todo jwebunit doesn't really test if it is a link!
207: // it just finds a html element by that id, and we have one
208: // assertLinkNotPresent("letter_f");
209: clickLink("letter_x");
210: assertTextInElement("guessesRemaining", "3");
211:
212: clickLink("letter_e");
213: assertTextInElement("guessesRemaining", "2");
214:
215: clickLink("letter_t");
216: assertTextInElement("guessesRemaining", "1");
217:
218: clickLink("letter_v");
219: assertTextPresent("Bad luck. You failed to guess that the word was");
220: }
221:
222: /**
223: * Performs a guess.
224: *
225: * @param hangman
226: * @param c
227: * @param expected
228: */
229: private void doGuessTest(Game hangman, char c, boolean expected) {
230: Assert.assertFalse(letter(hangman, c).isGuessed());
231: Assert.assertEquals(expected, guess(hangman, c));
232: Assert.assertTrue(letter(hangman, c).isGuessed());
233: }
234: }
|