001: /*
002: * ====================================================================
003: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
004: *
005: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by Jcorporate Ltd.
022: * (http://www.jcorporate.com/)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. "Jcorporate" and product names such as "Expresso" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written permission,
029: * please contact info@jcorporate.com.
030: *
031: * 5. Products derived from this software may not be called "Expresso",
032: * or other Jcorporate product names; nor may "Expresso" or other
033: * Jcorporate product names appear in their name, without prior
034: * written permission of Jcorporate Ltd.
035: *
036: * 6. No product derived from this software may compete in the same
037: * market space, i.e. framework, without prior written permission
038: * of Jcorporate Ltd. For written permission, please contact
039: * partners@jcorporate.com.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
045: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
046: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
047: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Jcorporate Ltd. Contributions back
057: * to the project(s) are encouraged when you make modifications.
058: * Please send them to support@jcorporate.com. For more information
059: * on Jcorporate Ltd. and its products, please see
060: * <http://www.jcorporate.com/>.
061: *
062: * Portions of this software are based upon other open source
063: * products and are subject to their respective licenses.
064: */
065: package com.jcorporate.expresso.core.security.tests;
066:
067: import com.jcorporate.expresso.core.db.DBException;
068: import com.jcorporate.expresso.core.security.User;
069: import com.jcorporate.expresso.ext.dbobj.regobj.Address;
070: import com.jcorporate.expresso.services.test.ExpressoTestCase;
071: import com.jcorporate.expresso.services.test.TestSystemInitializer;
072: import junit.framework.Test;
073: import junit.framework.TestSuite;
074:
075: /**
076: * A unit test case for the User object
077: *
078: * @author Michael Rimov
079: * @version $Revision: 1.2 $ $Date: 2004/11/17 20:48:23 $
080: * @created December 13, 2002
081: */
082: public class UserTest extends ExpressoTestCase {
083: /**
084: * Constructor for the UserTest object
085: *
086: * @param testName Description of the Parameter
087: * @throws Exception Description of the Exception
088: */
089: public UserTest(String testName) throws Exception {
090: super (testName);
091: }
092:
093: /**
094: * The main program for the UserTest class
095: *
096: * @param args The command line arguments
097: * @throws Exception Description of the Exception
098: */
099: public static void main(String[] args) throws Exception {
100:
101: //Set the system properties we need
102: junit.textui.TestRunner.run(suite());
103: }
104:
105: /**
106: * Define the suite of tests that verify each function of the cache
107: *
108: * @return The instantiated Test Suite
109: * @see junit.framework.TestSuite
110: * @see junit.framework.Test
111: */
112: public static Test suite() throws Exception {
113: return new TestSuite(UserTest.class);
114: }
115:
116: /*
117: * suite()
118: */
119:
120: /**
121: * A unit test for JUnit
122: *
123: * @throws Exception Description of the Exception
124: */
125: public void testUser() throws Exception {
126:
127: /*
128: * Add a new user
129: */
130: System.out.println("UserTest :Creating test user");
131:
132: User newUser = new User();
133: newUser.setLoginName("TEST");
134: newUser.setDataContext(TestSystemInitializer.getTestContext());
135:
136: if (newUser.find()) {
137: newUser.delete();
138: }
139:
140: newUser.setLoginName("TEST");
141: newUser.setDisplayName("Initial");
142: newUser.setEmail("root@javacorp.com");
143: newUser.setPassword("TEST");
144: newUser.add();
145: assertTrue("Password Test #1", passwdTest("TEST", "1"));
146:
147: /*
148: * Now see if updating something fouls it up
149: */
150: User testUser = new User();
151: testUser.setDataContext(TestSystemInitializer.getTestContext());
152: testUser.setLoginName("TEST");
153: testUser.find();
154: testUser.setDisplayName("Test User");
155: testUser.update();
156: assertTrue("Password Test #2", passwdTest("TEST", "2"));
157:
158: /*
159: * Now change the password
160: */
161: testUser = new User();
162: testUser.setDataContext(TestSystemInitializer.getTestContext());
163: testUser.setLoginName("TEST");
164: testUser.find();
165: testUser.setPassword("ANOTHER");
166: testUser.update();
167:
168: /*
169: * Now see if the password is correct - we do it twice
170: */
171: /*
172: * as the first time around it get's hashed & re-saved
173: */
174: assertTrue("Password Test #3", passwdTest("ANOTHER", "3"));
175: assertTrue("Password Test #4", passwdTest("ANOTHER", "4"));
176: testUser.setPassword("");
177: testUser.update();
178: assertTrue("Password Test #5", passwdTest("", "5"));
179: testUser.setPassword("SECOND");
180: testUser.update();
181: testUser = new User();
182: testUser.setDataContext(TestSystemInitializer.getTestContext());
183: testUser.setLoginName("TEST");
184: testUser.find();
185: assertTrue("PasswordTest #6", passwdTest("SECOND", "6"));
186: testUser = new User();
187: testUser.setDataContext(TestSystemInitializer.getTestContext());
188: testUser.setLoginName("TEST");
189: testUser.find();
190: assertTrue("PasswordTest #7", passwdTest("SECOND", "7"));
191: testUser.delete();
192: }
193:
194: /**
195: * @param newPass New password
196: * @param place Location in test cases
197: * @return true if successful
198: */
199: private boolean passwdTest(String newPass, String place)
200: throws DBException {
201: final String myName = "UserTest.passwdTest(String,String)";
202:
203: /*
204: * Now see if the password is correct
205: */
206: User testUser = new User();
207: testUser.setDataContext(TestSystemInitializer.getTestContext());
208: testUser.setLoginName("TEST");
209: testUser.find();
210:
211: if (!testUser.passwordEquals(newPass)) {
212: System.out.println(myName
213: + ":Password not stored correctly (" + place + ")");
214:
215: return false;
216: }
217:
218: System.out.println(myName + ":Password OK (" + place + ")");
219:
220: return true;
221: }
222:
223: /*
224: * passwdTest(String, STring, PrintStream)
225: */
226:
227: /**
228: * Tests if the UserListeners are working correctly. This won't test all of them
229: * just the Address object
230: *
231: * @throws Exception Description of the Exception
232: */
233: public void testUserListener() throws Exception {
234: System.out.println("UserTest: Testing the listener");
235:
236: User newUser = new User();
237: newUser.setLoginName("TEST");
238: newUser.setDataContext(TestSystemInitializer.getTestContext());
239:
240: if (newUser.find()) {
241: System.out.println("deleting user");
242: newUser.delete();
243: }
244:
245: // create the user first
246: newUser.setLoginName("TEST");
247: newUser.setDisplayName("Initial");
248: newUser.setEmail("root@javacorp.com");
249: newUser.setPassword("TEST");
250: newUser.add();
251:
252: // Create registration Objects assocated with this user
253:
254: Address addr = new Address();
255: addr.setDataContext(TestSystemInitializer.getTestContext());
256: addr.setField("ExpUid", newUser.getUid());
257: addr.setField("Street1", "test str");
258: addr.setField("City", "testcity");
259: addr.setField("State", "teststate");
260: addr.setField("Zip", "ziptest");
261: addr.setField("Country", "countrytest");
262: addr.add();
263:
264: Address addr2 = new Address();
265: addr2.setDataContext(TestSystemInitializer.getTestContext());
266: addr2.setField("ExpUid", newUser.getUid());
267: if (!addr2.find()) {
268: fail("address object not found");
269: }
270:
271: // now delete the user
272: newUser.delete();
273:
274: // now try to find the address obj
275: if (addr2.find()) {
276: fail("address object was found after user was deleted");
277: }
278:
279: }
280:
281: /**
282: * The teardown method for JUnit
283: *
284: * @throws Exception Description of the Exception
285: */
286: public void tearDown() throws Exception {
287: User newUser = new User();
288: newUser.setLoginName("TEST");
289: newUser.setDataContext(TestSystemInitializer.getTestContext());
290:
291: if (newUser.find()) {
292: System.out.println("deleting user in the tear down");
293: newUser.delete();
294: }
295: }
296:
297: }
|