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