KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > tests > LoginControllerTest


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.controller.tests;
66
67 import com.jcorporate.expresso.core.controller.ControllerResponse;
68 import com.jcorporate.expresso.core.controller.Input;
69 import com.jcorporate.expresso.core.controller.Transition;
70 import com.jcorporate.expresso.services.test.ControllerTestCase;
71 import com.jcorporate.expresso.services.test.ControllerTestSuite;
72 import junit.framework.TestSuite;
73 import org.apache.cactus.WebRequest;
74
75
76 /**
77  * Test case to make sure LoginController appears to be working ok without
78  * throwing errors
79  *
80  * @author Michael Rimov
81  * @version $Revision: 1.2 $ on $Date: 2004/11/17 20:48:23 $
82  */

83
84 public class LoginControllerTest extends ControllerTestCase {
85
86     /**
87      * Instantiates this controller. All that is needed is call super() with
88      * the testName passed to it as well as the name of the class we're testing.
89      *
90      * @param testName the name of the test
91      * @throws Exception upon error
92      */

93     public LoginControllerTest(String JavaDoc testName) throws Exception JavaDoc {
94         super(testName, com.jcorporate.expresso.services.controller.SimpleLoginController.class);
95     }
96
97
98     /**
99      * Convenience method so we can test just this class.
100      *
101      * @param args the command line arguments
102      * @throws Exception upon error
103      */

104     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
105         junit.textui.TestRunner.run(suite());
106     }
107
108
109     /**
110      * Determines what tests are run in the test suite. We also tell ControllerTestSuite
111      * that we need ExpressoSchema to exist for our tests. We also tell it that
112      * we aren't modifying the underlying database for our own tests so the schema
113      * doesn't necessarily have to be deleted when we're done with it.
114      *
115      * @return an instantiated testsuite
116      * @throws Exception upon error
117      */

118     public static TestSuite suite() throws Exception JavaDoc {
119         ControllerTestSuite cts = new ControllerTestSuite();
120         cts.addReadOnlySchemaDependency(com.jcorporate.expresso.core.ExpressoSchema.class);
121         cts.addTestSuite(LoginControllerTest.class);
122
123         return cts;
124     }
125
126
127     /**
128      * The next three classes are for testing the prompt state.
129      * beginPromptState is executed on the client side. It sets up the appropriate
130      * parameters including using what Controller state we're testing. As well
131      * as setting up cookies for proper security credentials
132      *
133      * @param theRequest a "hyper" ServletTestRequest that allows us to set URLS,
134      * set cookies, set URL parameters, etc before the request is sent to the
135      * servlet container.
136      * @throws Exception if something went wrong with the Crypto stuff
137      */

138
139     public void beginPromptState(WebRequest theRequest) throws Exception JavaDoc {
140         super.setupParameters("promptLogin", theRequest);
141     }
142
143
144     /**
145      * Executed on the server side. All it has to do is call the super's
146      * controllerProcess() to the Controller's State properly executed.
147      *
148      * @throws Exception upon error
149      */

150     public void testPromptState() throws Exception JavaDoc {
151         ControllerResponse response = super.controllerProcess();
152         assertTrue("Must get a non-null ControllerResponse Object", response != null);
153
154         Input i = response.getInput("dbContext");
155         assertTrue("Must have a dbContext Input Object", i != null);
156
157         verifyLoginForm(response);
158
159     }
160
161     /**
162      * Verify login form is a helper function that verifies the expected basic
163      * items in the controller response for a login page.
164      *
165      * @param response the ControllerResponse to test
166      * @throws Exception upon error
167      */

168     private void verifyLoginForm(ControllerResponse response) throws Exception JavaDoc {
169         Input i = response.getInput("LoginName");
170         assertTrue("Must have a LoginName Input Object", i != null);
171
172         i = response.getInput("Password");
173         assertTrue("Must have a Password Input Object", i != null);
174
175         i = response.getInput("Remember");
176         assertTrue("Must have a Remember Input Object", i != null);
177
178         Transition t = response.getTransition("processLogin");
179         assertTrue("Must have a processLogin Transition Object", t != null);
180
181         t = response.getTransition("processLogin");
182         assertTrue("Must have a promptRegister Transition Object", t != null);
183
184     }
185
186
187     /**
188      * This test is based upon the fact that the login form should be a little
189      * different for a user already logged in. For example, the change password
190      * transition should be available and other changes. This test
191      * makes sure that these details are functioning properly.
192      *
193      * @param theRequest the WebRequest object passed in by Cactus
194      * @throws Exception upon error
195      */

196     public void beginPromptStateLoggedIn(WebRequest theRequest) throws Exception JavaDoc {
197         super.setupParameters("promptLogin", theRequest);
198         super.logIn(theRequest);
199     }
200
201     public void testPromptStateLoggedIn() throws Exception JavaDoc {
202         ControllerResponse response = super.controllerProcess();
203         assertTrue("Must get a non-null ControllerResponse Object", response != null);
204
205         verifyLoginForm(response);
206         /**
207          * @todo Finish this out
208          */

209     }
210
211
212     /**
213      * This function makes sure that errors are proper for processing a bad
214      * login
215      *
216      * @param theRequest the WebRequest object passed in by Cactus
217      * @throws Exception upon error
218      */

219     public void beginTestBadLogin(WebRequest theRequest) throws Exception JavaDoc {
220         super.setupParameters("processLogin", theRequest);
221         theRequest.addParameter("LoginName", "dummyFalsePassword");
222     }
223
224
225     public void testTestBadLogin() throws Exception JavaDoc {
226         ControllerResponse response = super.controllerProcess();
227         assertTrue("Must get a non-null ControllerResponse Object", response != null);
228
229         //
230
//Now we should actually have the login form again.
231
//
232
verifyLoginForm(response);
233
234         assertTrue("Must have login error message for bad login attempt",
235                 response.getErrors().size() > 0);
236     }
237
238     /**
239      * Tests to make sure that the change password dialog is getting correctly
240      * displayed.
241      *
242      * @param theRequest the WebRequest object passed in by Cactus
243      * @throws Exception upon error
244      */

245     public void beginPromptChangePasswordState(WebRequest theRequest) throws Exception JavaDoc {
246         super.setupParameters("promptChangePassword", theRequest);
247         super.logIn(theRequest);
248     }
249
250     /**
251      * Tests the 'prompt change password' state
252      *
253      * @throws Exception upon error
254      */

255     public void testPromptChangePasswordState() throws Exception JavaDoc {
256         ControllerResponse response = super.controllerProcess();
257
258         assertTrue("Must get a non-null ControllerResponse Object", response != null);
259         Input i = response.getInput("oldPassword");
260         assertTrue("Old Password Input Must Exist", i != null);
261
262         i = response.getInput("Password");
263         assertTrue("New Password Input Must Exist", i != null);
264
265         i = response.getInput("password_verify");
266         assertTrue("Password Verify Input Must Exist", i != null);
267
268         Transition t = response.getTransition("processChangePassword");
269         assertTrue("Process Change Password Transition must exist", t != null);
270
271     }
272 }
Popular Tags