KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > sample > LoginPageTest


1 /*
2  * User: djoiner
3  * Date: May 31, 2002
4  * Time: 4:41:11 PM
5  */

6 package net.sourceforge.jwebunit.sample;
7
8 import com.meterware.httpunit.HttpUnitOptions;
9 import com.meterware.pseudoserver.PseudoServer;
10 import com.meterware.pseudoserver.PseudoServlet;
11 import com.meterware.pseudoserver.WebResource;
12 import net.sourceforge.jwebunit.WebTestCase;
13
14
15 public class LoginPageTest extends WebTestCase {
16
17     private static final String JavaDoc VALID_UID = "validuid";
18     private static final String JavaDoc VALID_PWD = "validpwd";
19     private static final String JavaDoc INVALID_UID = "invaliduid";
20     private static final String JavaDoc INVALID_PWD = "invalidpwd";
21     private PseudoServer server;
22     private String JavaDoc hostPath;
23
24
25     public LoginPageTest(String JavaDoc name) {
26         super(name);
27         getTestContext().setBaseUrl("http://localhost:80");
28     }
29
30     public void setUp() throws Exception JavaDoc {
31         server = new PseudoServer();
32         HttpUnitOptions.reset();
33         hostPath = "http://localhost:" + server.getConnectedPort();
34         getTestContext().setBaseUrl(hostPath);
35         defineResouces();
36     }
37
38     public void tearDown() throws Exception JavaDoc {
39         if (server != null) server.shutDown();
40     }
41
42     public void testInitialState() {
43         beginAt("/");
44         assertFormElementPresent("username");
45         assertFormElementPresent("password");
46         assertSubmitButtonPresent("login");
47     }
48
49     public void testValidLogin() {
50         loginAs(VALID_UID, VALID_PWD);
51         assertTitleEquals("Logged In");
52     }
53
54     public void testInvalidUsername() {
55         loginAs(INVALID_UID, VALID_PWD);
56         assertTitleEquals("Login");
57         assertTextPresent("Invalid Login");
58     }
59
60     public void testInvalidPassword() {
61         loginAs(VALID_UID, INVALID_PWD);
62         assertTitleEquals("Login");
63         assertTextPresent("Invalid Login");
64     }
65
66     private void loginAs(String JavaDoc user, String JavaDoc pass) {
67         beginAt("/");
68         setFormElement("username", user);
69         setFormElement("password", pass);
70         submit("login");
71     }
72
73     private void defineResouces() {
74         server.setResource("/",
75                            "<html>" +
76                            "<head>" +
77                            " <title>Login</title>" +
78                            "</head>" +
79                            "<body>" +
80                            " <form action=\"login\" method=\"post\">" +
81                            " <table align=\"center\">" +
82                            " <tr>" +
83                            " <td>Username:</td>" +
84                            " <td><input type=\"text\" length=\"15\" name=\"username\"/></td>" +
85                            " </tr>" +
86                            " <tr>" +
87                            " <td>Password:</td>" +
88                            " <td><input type=\"password\" length=\"15\" name=\"password\"/></td>" +
89                            " </tr>" +
90                            " <tr>" +
91                            " <td colspan=\"10\" align=\"center\"><input type=\"submit\" value=\"Login\" name=\"login\"/></td>" +
92                            " </tr>" +
93                            " </table>" +
94                            " </form>" +
95                            "</body>" +
96                            "</html>");
97
98         server.setResource("login", new PseudoServlet() {
99             public WebResource getPostResponse() {
100                 String JavaDoc uid = getParameter("username")[0];
101                 String JavaDoc pwd = getParameter("password")[0];
102                 if (uid.equals(VALID_UID) && pwd.equals(VALID_PWD)) {
103                     return new WebResource("<html><head><title>Logged In</title></head><body></body></html>");
104                 }
105                 return new WebResource("<html><head><title>Login</title></head><body>Invalid Login</body></html>");
106             }
107         });
108     }
109
110
111 }
112
113
Popular Tags