KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > faces > sysclient > LogonTestCase


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.faces.sysclient;
18
19 import java.util.List JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
25 import com.gargoylesoftware.htmlunit.html.HtmlElement;
26 import com.gargoylesoftware.htmlunit.html.HtmlForm;
27 import com.gargoylesoftware.htmlunit.html.HtmlHiddenInput;
28 import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
29 import com.gargoylesoftware.htmlunit.html.HtmlResetInput;
30 import com.gargoylesoftware.htmlunit.html.HtmlSpan;
31 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
32 import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
33
34
35 /**
36  * <p>Test case for a logon form that accepts a username and password.</p>
37  *
38  * <p><strong>FIXME</strong> - The JavaScript execution support in
39  * HtmlUnit 1.2.3 is not sufficient to test clicking the hyperlink
40  * generated by a Command Link component, so it is not tested automatically.</p>
41  *
42  * @version $Rev: 165410 $ $Date: 2005-04-30 17:25:14 +0100 (Sat, 30 Apr 2005) $
43  */

44
45 public class LogonTestCase extends AbstractTestCase {
46
47
48     // ------------------------------------------------------------ Constructors
49

50
51     /**
52      * <p>Construct a new instance of this test case.</p>
53      *
54      * @param name Name of the new test case
55      */

56     public LogonTestCase(String JavaDoc name) {
57
58         super(name);
59
60     }
61
62
63     // ------------------------------------------------------ Instance Variables
64

65
66     // ------------------------------------------------------ Test Setup Methods
67

68
69     /**
70      * <p>Set up the instance variables required for this test case.</p>
71      */

72     public void setUp() throws Exception JavaDoc {
73
74         super.setUp();
75         page("/logon.faces");
76
77     }
78
79
80     /**
81      * <p>Return the set of tests included in this test suite.</p>
82      */

83     public static Test suite() {
84
85         return (new TestSuite(LogonTestCase.class));
86
87     }
88
89
90     /**
91      * <p>Tear down instance variables required by this test case.</p>
92      */

93     public void tearDown() {
94
95         super.tearDown();
96
97     }
98
99
100
101     // ------------------------------------------------- Individual Test Methods
102

103
104     /**
105      * <p>Submit incorrect input fields and verify the correct response.</p>
106      */

107     public void testIncorrect() throws Exception JavaDoc {
108
109         HtmlSpan span = null;
110         HtmlTextInput username = (HtmlTextInput) element("form:username");
111         HtmlPasswordInput password = (HtmlPasswordInput)
112             element("form:password");
113         HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
114
115         username.setValueAttribute("bb");
116         password.setValueAttribute("");
117         submit(submit);
118
119         assertEquals("logon", title());
120
121         span = (HtmlSpan) element("globalErrors");
122         assertNotNull(span);
123         assertEquals("[EH][EP] can not be less than 3 characters.[ES]" +
124                      "[EP] is required.[ES][EF]",
125                      span.asText());
126
127         username = (HtmlTextInput) element("form:username");
128         assertNotNull(username);
129         assertEquals("bb", username.getValueAttribute());
130
131         span = (HtmlSpan) element("form:usernameErrors");
132         assertNotNull(span);
133         // FIXME: response string should really include "Username:"
134
assertEquals("[EH][EP] can not be less than 3 characters.[ES][EF]",
135                      span.asText());
136
137         password = (HtmlPasswordInput) element("form:password");
138         assertNotNull(password);
139         assertEquals("", password.getValueAttribute());
140
141         span = (HtmlSpan) element("form:passwordErrors");
142         assertNotNull(span);
143         // FIXME: response string should really include "Password:"
144
assertEquals("[EH][EP] is required.[ES][EF]", span.asText());
145
146     }
147
148
149     /**
150      * <p>Verify the content of a pristine page returned when executing this
151      * view for the first time.</p>
152      */

153     public void testPristine() throws Exception JavaDoc {
154
155         HtmlSpan span = null;
156
157         HtmlElement html = page.getDocumentElement();
158         assertEquals("html", html.getTagName());
159         assertEquals("http://www.w3.org/1999/xhtml", html.getAttributeValue("xmlns"));
160
161         assertEquals("logon", title());
162
163         HtmlForm form = (HtmlForm) element("form");
164         assertNotNull(form);
165         assertEquals("", form.getAcceptAttribute());
166         assertEquals("", form.getAcceptCharsetAttribute());
167         String JavaDoc url = this.url.toString();
168         url = url.substring(0, url.length() - 1);
169         url = url.substring(url.lastIndexOf('/'));
170         String JavaDoc action = form.getActionAttribute();
171         int semicolon = action.indexOf(';');
172         if (semicolon >= 0) {
173             action = action.substring(0, semicolon);
174         }
175         assertEquals(url + "/logon.faces", action);
176         assertEquals("", form.getEnctypeAttribute());
177         assertEquals("post", form.getMethodAttribute());
178         assertEquals("logonForm", form.getNameAttribute());
179         assertEquals("", form.getOnResetAttribute());
180         assertEquals("", form.getOnSubmitAttribute());
181         assertEquals("", form.getTargetAttribute());
182
183         span = (HtmlSpan) element("form:usernamePrompt");
184         assertNotNull(span);
185         assertEquals("Username:", span.asText());
186
187         HtmlTextInput username = (HtmlTextInput) element("form:username");
188         assertNotNull(username);
189         assertEquals("", username.getLangAttribute());
190         assertEquals("form:username", username.getNameAttribute());
191         assertEquals("", username.getOnClickAttribute());
192         assertEquals("", username.getOnDblClickAttribute());
193         assertEquals("", username.getOnKeyDownAttribute());
194         assertEquals("", username.getOnKeyPressAttribute());
195         assertEquals("", username.getOnKeyUpAttribute());
196         assertEquals("", username.getOnMouseDownAttribute());
197         assertEquals("", username.getOnMouseMoveAttribute());
198         assertEquals("", username.getOnMouseOutAttribute());
199         assertEquals("", username.getOnMouseOverAttribute());
200         assertEquals("", username.getOnMouseUpAttribute());
201         assertEquals("text", username.getTypeAttribute());
202         assertEquals("", username.getValueAttribute());
203
204         span = (HtmlSpan) element("form:passwordPrompt");
205         assertNotNull(span);
206         assertEquals("Password:", span.asText());
207
208         HtmlPasswordInput password = (HtmlPasswordInput)
209             element("form:password");
210         assertNotNull(password);
211         assertEquals("", password.getLangAttribute());
212         assertEquals("form:password", password.getNameAttribute());
213         assertEquals("", password.getOnClickAttribute());
214         assertEquals("", password.getOnDblClickAttribute());
215         assertEquals("", password.getOnKeyDownAttribute());
216         assertEquals("", password.getOnKeyPressAttribute());
217         assertEquals("", password.getOnKeyUpAttribute());
218         assertEquals("", password.getOnMouseDownAttribute());
219         assertEquals("", password.getOnMouseMoveAttribute());
220         assertEquals("", password.getOnMouseOutAttribute());
221         assertEquals("", password.getOnMouseOverAttribute());
222         assertEquals("", password.getOnMouseUpAttribute());
223         assertEquals("password", password.getTypeAttribute());
224         assertEquals("", password.getValueAttribute());
225
226         HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
227         assertNotNull(submit);
228         assertEquals("", submit.getLangAttribute());
229         assertEquals("form:submit", submit.getNameAttribute());
230         assertEquals("", submit.getOnClickAttribute());
231         assertEquals("", submit.getOnDblClickAttribute());
232         assertEquals("", submit.getOnKeyDownAttribute());
233         assertEquals("", submit.getOnKeyPressAttribute());
234         assertEquals("", submit.getOnKeyUpAttribute());
235         assertEquals("", submit.getOnMouseDownAttribute());
236         assertEquals("", submit.getOnMouseMoveAttribute());
237         assertEquals("", submit.getOnMouseOutAttribute());
238         assertEquals("", submit.getOnMouseOverAttribute());
239         assertEquals("", submit.getOnMouseUpAttribute());
240         assertEquals("submit", submit.getTypeAttribute());
241         assertEquals("Logon", submit.getValueAttribute());
242
243         HtmlResetInput reset = (HtmlResetInput) element("form:reset");
244         assertNotNull(reset);
245         assertEquals("", reset.getLangAttribute());
246         assertEquals("form:reset", reset.getNameAttribute());
247         assertEquals("", reset.getOnClickAttribute());
248         assertEquals("", reset.getOnDblClickAttribute());
249         assertEquals("", reset.getOnKeyDownAttribute());
250         assertEquals("", reset.getOnKeyPressAttribute());
251         assertEquals("", reset.getOnKeyUpAttribute());
252         assertEquals("", reset.getOnMouseDownAttribute());
253         assertEquals("", reset.getOnMouseMoveAttribute());
254         assertEquals("", reset.getOnMouseOutAttribute());
255         assertEquals("", reset.getOnMouseOverAttribute());
256         assertEquals("", reset.getOnMouseUpAttribute());
257         assertEquals("reset", reset.getTypeAttribute());
258         assertEquals("Reset", reset.getValueAttribute());
259
260         HtmlSubmitInput cancel = (HtmlSubmitInput) element("form:cancel");
261         assertNotNull(cancel);
262         assertEquals("", cancel.getLangAttribute());
263         assertEquals("form:cancel", cancel.getNameAttribute());
264         assertEquals("", cancel.getOnClickAttribute());
265         assertEquals("", cancel.getOnDblClickAttribute());
266         assertEquals("", cancel.getOnKeyDownAttribute());
267         assertEquals("", cancel.getOnKeyPressAttribute());
268         assertEquals("", cancel.getOnKeyUpAttribute());
269         assertEquals("", cancel.getOnMouseDownAttribute());
270         assertEquals("", cancel.getOnMouseMoveAttribute());
271         assertEquals("", cancel.getOnMouseOutAttribute());
272         assertEquals("", cancel.getOnMouseOverAttribute());
273         assertEquals("", cancel.getOnMouseUpAttribute());
274         assertEquals("submit", cancel.getTypeAttribute());
275         assertEquals("Cancel", cancel.getValueAttribute());
276
277         HtmlAnchor anchor = (HtmlAnchor) element("form:submit2");
278         assertNotNull(anchor);
279         assertEquals("", anchor.getAccessKeyAttribute());
280         assertEquals("", anchor.getCharsetAttribute());
281         assertEquals("", anchor.getClassAttribute());
282         assertEquals("", anchor.getCoordsAttribute());
283         assertEquals("#", anchor.getHrefAttribute());
284         assertEquals("form:submit2", anchor.getIdAttribute());
285         assertEquals("", anchor.getLangAttribute());
286         assertEquals("", anchor.getHrefLangAttribute());
287         assertEquals("", anchor.getOnBlurAttribute());
288         assertTrue
289             (anchor.getOnClickAttribute().startsWith
290              ("document.forms['form']" +
291               "['org_apache_struts_faces_renderer_CommandLinkRenderer']" +
292               ".value='form:submit2';"));
293         assertEquals("", anchor.getOnDblClickAttribute());
294         assertEquals("", anchor.getOnKeyDownAttribute());
295         assertEquals("", anchor.getOnKeyPressAttribute());
296         assertEquals("", anchor.getOnKeyUpAttribute());
297         assertEquals("", anchor.getOnMouseDownAttribute());
298         assertEquals("", anchor.getOnMouseMoveAttribute());
299         assertEquals("", anchor.getOnMouseOutAttribute());
300         assertEquals("", anchor.getOnMouseOverAttribute());
301         assertEquals("", anchor.getOnMouseUpAttribute());
302         assertEquals("", anchor.getTextDirectionAttribute());
303         assertEquals("", anchor.getXmlLangAttribute());
304         assertEquals("", anchor.getOnFocusAttribute());
305         assertEquals("", anchor.getRelAttribute());
306         assertEquals("", anchor.getRevAttribute());
307         assertEquals("", anchor.getShapeAttribute());
308         assertEquals("", anchor.getStyleAttribute());
309         assertEquals("", anchor.getTabIndexAttribute());
310         assertEquals("a", anchor.getTagName());
311         assertEquals("", anchor.getTargetAttribute());
312         assertEquals("", anchor.getTitleAttribute());
313         assertEquals("", anchor.getTypeAttribute());
314         assertEquals("Logon", anchor.asText());
315
316         List JavaDoc list = form.getHtmlElementsByAttribute
317             ("input", "name",
318              "org_apache_struts_faces_renderer_CommandLinkRenderer");
319         assertNotNull(list);
320         assertEquals(1, list.size());
321         HtmlHiddenInput hidden = (HtmlHiddenInput) list.get(0);
322         assertEquals("hidden", hidden.getTypeAttribute());
323         assertEquals("", hidden.getValueAttribute());
324
325     }
326
327
328     /**
329      * <p>Submit known-bad mismatch and verify the correct response.</p>
330      */

331     public void testMismatch() throws Exception JavaDoc {
332
333         HtmlSpan span = null;
334         HtmlTextInput username = (HtmlTextInput) element("form:username");
335         HtmlPasswordInput password = (HtmlPasswordInput)
336             element("form:password");
337         HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
338
339         username.setValueAttribute("baduser");
340         password.setValueAttribute("badpass");
341         submit(submit);
342
343         assertEquals("logon", title());
344
345         span = (HtmlSpan) element("globalErrors");
346         assertNotNull(span);
347         assertEquals("[EH][EP]Invalid username/password combination[ES][EF]",
348                      span.asText());
349
350         username = (HtmlTextInput) element("form:username");
351         assertNotNull(username);
352         assertEquals("baduser", username.getValueAttribute());
353
354         span = (HtmlSpan) element("form:usernameErrors");
355         assertNotNull(span);
356         assertEquals("", span.asText());
357
358         password = (HtmlPasswordInput) element("form:password");
359         assertNotNull(password);
360         assertEquals("", password.getValueAttribute());
361
362         span = (HtmlSpan) element("form:passwordErrors");
363         assertNotNull(span);
364         assertEquals("", span.asText());
365
366     }
367
368
369     /**
370      * <p>Submit known-good username and password values, and
371      * verify the correct response.</p>
372      */

373     public void testSuccessful() throws Exception JavaDoc {
374
375         HtmlTextInput username = (HtmlTextInput) element("form:username");
376         HtmlPasswordInput password = (HtmlPasswordInput)
377             element("form:password");
378         HtmlSubmitInput submit = (HtmlSubmitInput) element("form:submit");
379
380         username.setValueAttribute("gooduser");
381         password.setValueAttribute("goodpass");
382         submit(submit);
383
384         assertEquals("logon1", title());
385
386     }
387
388
389 }
390
Popular Tags