KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > security > tests > UserTest


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

65 package com.jcorporate.expresso.core.security.tests;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.security.User;
69 import com.jcorporate.expresso.ext.dbobj.regobj.Address;
70 import com.jcorporate.expresso.services.test.ExpressoTestCase;
71 import com.jcorporate.expresso.services.test.TestSystemInitializer;
72 import junit.framework.Test;
73 import junit.framework.TestSuite;
74
75 /**
76  * A unit test case for the User object
77  *
78  * @author Michael Rimov
79  * @version $Revision: 1.2 $ $Date: 2004/11/17 20:48:23 $
80  * @created December 13, 2002
81  */

82 public class UserTest
83         extends ExpressoTestCase {
84     /**
85      * Constructor for the UserTest object
86      *
87      * @param testName Description of the Parameter
88      * @throws Exception Description of the Exception
89      */

90     public UserTest(String JavaDoc testName)
91             throws Exception JavaDoc {
92         super(testName);
93     }
94
95
96     /**
97      * The main program for the UserTest class
98      *
99      * @param args The command line arguments
100      * @throws Exception Description of the Exception
101      */

102     public static void main(String JavaDoc[] args)
103             throws Exception JavaDoc {
104
105         //Set the system properties we need
106
junit.textui.TestRunner.run(suite());
107     }
108
109
110     /**
111      * Define the suite of tests that verify each function of the cache
112      *
113      * @return The instantiated Test Suite
114      * @see junit.framework.TestSuite
115      * @see junit.framework.Test
116      */

117     public static Test suite()
118             throws Exception JavaDoc {
119         return new TestSuite(UserTest.class);
120     }
121
122
123     /*
124      * suite()
125      */

126
127     /**
128      * A unit test for JUnit
129      *
130      * @throws Exception Description of the Exception
131      */

132     public void testUser()
133             throws Exception JavaDoc {
134
135 /*
136          * Add a new user
137          */

138         System.out.println("UserTest :Creating test user");
139
140         User newUser = new User();
141         newUser.setLoginName("TEST");
142         newUser.setDataContext(TestSystemInitializer.getTestContext());
143
144         if (newUser.find()) {
145             newUser.delete();
146         }
147
148         newUser.setLoginName("TEST");
149         newUser.setDisplayName("Initial");
150         newUser.setEmail("root@javacorp.com");
151         newUser.setPassword("TEST");
152         newUser.add();
153         assertTrue("Password Test #1", passwdTest("TEST", "1"));
154
155 /*
156          * Now see if updating something fouls it up
157          */

158         User testUser = new User();
159         testUser.setDataContext(TestSystemInitializer.getTestContext());
160         testUser.setLoginName("TEST");
161         testUser.find();
162         testUser.setDisplayName("Test User");
163         testUser.update();
164         assertTrue("Password Test #2", passwdTest("TEST", "2"));
165
166 /*
167          * Now change the password
168          */

169         testUser = new User();
170         testUser.setDataContext(TestSystemInitializer.getTestContext());
171         testUser.setLoginName("TEST");
172         testUser.find();
173         testUser.setPassword("ANOTHER");
174         testUser.update();
175
176 /*
177          * Now see if the password is correct - we do it twice
178          */

179 /*
180          * as the first time around it get's hashed & re-saved
181          */

182         assertTrue("Password Test #3", passwdTest("ANOTHER", "3"));
183         assertTrue("Password Test #4", passwdTest("ANOTHER", "4"));
184         testUser.setPassword("");
185         testUser.update();
186         assertTrue("Password Test #5", passwdTest("", "5"));
187         testUser.setPassword("SECOND");
188         testUser.update();
189         testUser = new User();
190         testUser.setDataContext(TestSystemInitializer.getTestContext());
191         testUser.setLoginName("TEST");
192         testUser.find();
193         assertTrue("PasswordTest #6", passwdTest("SECOND", "6"));
194         testUser = new User();
195         testUser.setDataContext(TestSystemInitializer.getTestContext());
196         testUser.setLoginName("TEST");
197         testUser.find();
198         assertTrue("PasswordTest #7", passwdTest("SECOND", "7"));
199         testUser.delete();
200     }
201
202
203     /**
204      * @param newPass New password
205      * @param place Location in test cases
206      * @return true if successful
207      */

208     private boolean passwdTest(String JavaDoc newPass, String JavaDoc place)
209             throws DBException {
210         final String JavaDoc myName = "UserTest.passwdTest(String,String)";
211
212         /*
213          * Now see if the password is correct
214          */

215         User testUser = new User();
216         testUser.setDataContext(TestSystemInitializer.getTestContext());
217         testUser.setLoginName("TEST");
218         testUser.find();
219
220         if (!testUser.passwordEquals(newPass)) {
221             System.out.println(myName + ":Password not stored correctly (" +
222                     place + ")");
223
224             return false;
225         }
226
227         System.out.println(myName + ":Password OK (" + place + ")");
228
229         return true;
230     }
231
232
233     /*
234      * passwdTest(String, STring, PrintStream)
235      */

236
237     /**
238      * Tests if the UserListeners are working correctly. This won't test all of them
239      * just the Address object
240      *
241      * @throws Exception Description of the Exception
242      */

243     public void testUserListener() throws Exception JavaDoc {
244         System.out.println("UserTest: Testing the listener");
245
246         User newUser = new User();
247         newUser.setLoginName("TEST");
248         newUser.setDataContext(TestSystemInitializer.getTestContext());
249
250         if (newUser.find()) {
251             System.out.println("deleting user");
252             newUser.delete();
253         }
254
255         // create the user first
256
newUser.setLoginName("TEST");
257         newUser.setDisplayName("Initial");
258         newUser.setEmail("root@javacorp.com");
259         newUser.setPassword("TEST");
260         newUser.add();
261
262         // Create registration Objects assocated with this user
263

264         Address addr = new Address();
265         addr.setDataContext(TestSystemInitializer.getTestContext());
266         addr.setField("ExpUid", newUser.getUid());
267         addr.setField("Street1", "test str");
268         addr.setField("City", "testcity");
269         addr.setField("State", "teststate");
270         addr.setField("Zip", "ziptest");
271         addr.setField("Country", "countrytest");
272         addr.add();
273
274         Address addr2 = new Address();
275         addr2.setDataContext(TestSystemInitializer.getTestContext());
276         addr2.setField("ExpUid", newUser.getUid());
277         if (!addr2.find()) {
278             fail("address object not found");
279         }
280
281         // now delete the user
282
newUser.delete();
283
284         // now try to find the address obj
285
if (addr2.find()) {
286             fail("address object was found after user was deleted");
287         }
288
289
290     }
291
292
293     /**
294      * The teardown method for JUnit
295      *
296      * @throws Exception Description of the Exception
297      */

298     public void tearDown() throws Exception JavaDoc {
299         User newUser = new User();
300         newUser.setLoginName("TEST");
301         newUser.setDataContext(TestSystemInitializer.getTestContext());
302
303         if (newUser.find()) {
304             System.out.println("deleting user in the tear down");
305             newUser.delete();
306         }
307     }
308
309
310 }
311
Popular Tags