KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > reg > UserTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.reg;
22
23 import java.util.*;
24 import java.io.*;
25 import java.sql.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.aikp.*;
30 import com.methodhead.test.*;
31 import com.methodhead.sitecontext.*;
32 import com.methodhead.*;
33 import com.methodhead.transfer.*;
34
35 public class UserTest extends TestCase {
36
37   private User user = null;
38   private Contact contact = null;
39   private ResultSet rs = null;
40   private List list = null;
41   private SiteContext siteContext = null;
42   private Role role = null;
43
44   static {
45     TestUtils.initLogger();
46     TestUtils.initDb();
47   }
48
49   public UserTest( String JavaDoc name ) {
50     super( name );
51   }
52
53   protected void setUp() {
54     try {
55       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
56     }
57     catch ( Exception JavaDoc e ) {
58       fail( e.getMessage() );
59     }
60   }
61
62   protected void tearDown() {
63   }
64
65   public void testProperties() {
66     try {
67       user = new User();
68
69       assertNotNull( user.getRoles() );
70       assertEquals( 0, user.getRoles().size() );
71
72       assertNotNull( user.getContact() );
73     }
74     catch ( Exception JavaDoc e ) {
75       e.printStackTrace();
76       fail();
77     }
78   }
79
80   public void testCompareTo() {
81     TestData.createUsers();
82
83     assertEquals( -1, TestData.user1.compareTo( TestData.user2 ) );
84     assertEquals( 0, TestData.user1.compareTo( TestData.user1 ) );
85     assertEquals( 1, TestData.user2.compareTo( TestData.user1 ) );
86   }
87
88   public void testGetPublicSecret() {
89
90     //
91
// without password encryption
92
//
93
user = new User();
94     user.setString( "password", "password" );
95     assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getPublicSecret() );
96
97     //
98
// with password encryption
99
//
100
user = new User();
101     user.setPasswordEncrypted( true );
102     user.setString( "password", "password" );
103     assertEquals( "dgx+mS8hYD3yKR2nr1/AwA==", user.getPublicSecret() );
104   }
105
106   public void testHasRole() {
107     TestData.createRoles();
108
109     user = new User();
110     user.getRoles().add( TestData.role1 );
111     user.getRoles().add( TestData.role2 );
112
113     assertTrue( user.hasRole( TestData.siteContext1, DefaultTransferPolicy.ROLE_SYSADMIN ) );
114     assertTrue( !user.hasRole( TestData.siteContext1, DefaultTransferPolicy.ROLE_WEBMASTER ) );
115   }
116
117   public void testSaveNew() {
118     try {
119       TestData.createRoles();
120
121       user = new User();
122       user.getRoles().add( TestData.role1 );
123       user.getContact().setString( "firstname", "firstname" );
124       user.saveNew();
125
126       rs = ConnectionSingleton.runQuery( "SELECT sitecontext_id,name FROM reg_role WHERE user_id=" + user.getInt( "id" ) );
127       assertTrue( rs.next() );
128       assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, rs.getString( "name" ) );
129       assertEquals( 1, rs.getInt( "sitecontext_id" ) );
130       assertTrue( !rs.next() );
131       ConnectionSingleton.close( rs );
132
133       contact = new Contact();
134       contact.load( new IntKey( user.getInt( "contact_id" ) ) );
135       assertEquals( "firstname", contact.getString( "firstname" ) );
136     }
137     catch ( Exception JavaDoc e ) {
138       e.printStackTrace();
139       fail();
140     }
141   }
142
143   public void testLoad() {
144     try {
145       TestData.createUsers();
146
147       user = new User();
148       user.load( new IntKey( 1 ) );
149
150       contact = user.getContact();
151       assertEquals( "User1", contact.getString( "firstname" ) );
152
153       list = user.getRoles();
154       assertEquals( 2, list.size() );
155
156       role = ( Role )list.get( 0 );
157       assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() );
158       assertEquals( TestData.siteContext1, role.getSiteContext() );
159
160       role = ( Role )list.get( 1 );
161       assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() );
162       assertEquals( SiteContext.getDefaultContext(), role.getSiteContext() );
163     }
164     catch ( Exception JavaDoc e ) {
165       e.printStackTrace();
166       fail();
167     }
168   }
169
170   public void testSave() {
171     try {
172       TestData.createUsers();
173
174       user = new User();
175       user.load( new IntKey( 1 ) );
176       user.getContact().setString( "firstname", "User1Updated" );
177       user.getRoles().add( TestData.role2 );
178       user.save();
179
180       user = new User();
181       user.load( new IntKey( 1 ) );
182
183       contact = user.getContact();
184       assertEquals( "User1Updated", contact.getString( "firstname" ) );
185
186       list = user.getRoles();
187       assertEquals( 3, list.size() );
188
189       role = ( Role )list.get( 0 );
190       assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() );
191       assertEquals( TestData.siteContext1, role.getSiteContext() );
192
193       role = ( Role )list.get( 1 );
194       assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() );
195       assertEquals( SiteContext.getDefaultContext(), role.getSiteContext() );
196
197       role = ( Role )list.get( 2 );
198       assertEquals( DefaultTransferPolicy.ROLE_SITEADMIN, role.getName() );
199       assertEquals( TestData.siteContext1, role.getSiteContext() );
200     }
201     catch ( Exception JavaDoc e ) {
202       e.printStackTrace();
203       fail();
204     }
205   }
206
207   public void testDelete() {
208     try {
209       TestData.createUsers();
210
211       user = new User();
212       user.load( new IntKey( 1 ) );
213       user.delete();
214
215       rs = ConnectionSingleton.runQuery( "SELECT name FROM reg_role WHERE user_id=" + user.getInt( "id" ) );
216       assertTrue( !rs.next() );
217       ConnectionSingleton.close( rs );
218
219       try {
220         contact = new Contact();
221         contact.load( new IntKey( user.getInt( "contact_id" ) ) );
222         fail();
223       }
224       catch ( PersistableException e ) {}
225     }
226     catch ( Exception JavaDoc e ) {
227       e.printStackTrace();
228       fail();
229     }
230   }
231
232   public void testLoadForLogin() {
233     try {
234       TestData.createUsers();
235
236       user = new User();
237
238       assertTrue( !user.loadForLogin( "invalidlogin" ) );
239       assertTrue( user.loadForLogin( "test1@methodhead.com" ) );
240
241       assertEquals( TestData.user1.getInt( "id" ), user.getInt( "id" ) );
242     }
243     catch ( Exception JavaDoc e ) {
244       e.printStackTrace();
245       fail();
246     }
247   }
248
249   public void testLoadAllForSiteContext() {
250     try {
251       TestData.createUsers();
252
253       user = new User();
254
255       list = user.loadAllForSiteContext( TestData.siteContext1 );
256       assertNotNull( list );
257       assertEquals( 2, list.size() );
258
259       user = ( User )list.get( 0 );
260       assertEquals( 1, user.getInt( "id" ) );
261
262       user = ( User )list.get( 1 );
263       assertEquals( 2, user.getInt( "id" ) );
264     }
265     catch ( Exception JavaDoc e ) {
266       e.printStackTrace();
267       fail();
268     }
269   }
270
271   public void testEncryptPassword() {
272     try {
273       TestData.createUsers();
274
275       //
276
// password should be encrypted
277
//
278
user = new User();
279       assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.encryptPassword( "password" ) );
280
281       //
282
// result should be different for a different password
283
//
284
assertEquals( "bLdfZSqbUnmOts8iAQV8cw==", user.encryptPassword( "password2" ) );
285
286       //
287
// result should be less than 128 characters even for a long password
288
//
289
assertEquals( "f3v9NIcJ3uqs4Z4/U1+MVA==", user.encryptPassword( "0123456789012345678901234567890123456789012345678901234567890123" ) );
290     }
291     catch ( Exception JavaDoc e ) {
292       e.printStackTrace();
293       fail();
294     }
295   }
296
297   public void testSetPasswordEncrypted() {
298     try {
299       user = new User();
300
301       //
302
// password should not encrypt by default
303
//
304
user.setString( "password", "password" );
305       assertEquals( "password", user.getString( "password" ) );
306
307       //
308
// authentication should work with password
309
//
310
assertEquals( true, user.authenticate( "password" ) );
311       assertEquals( false, user.authenticate( "invalidpassword" ) );
312
313       //
314
// set the encrypt flag
315
//
316
user.setPasswordEncrypted( true );
317
318       //
319
// password should be encrypted
320
//
321
user.setString( "password", "password" );
322       assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getString( "password" ) );
323
324       //
325
// authentication should work with password
326
//
327
assertEquals( true, user.authenticate( "password" ) );
328       assertEquals( false, user.authenticate( "invalidpassword" ) );
329
330       //
331
// should be able save and load the user
332
//
333
user.getContact().setString( "email", "test" );
334       user.saveNew();
335       user = new User();
336       user.setPasswordEncrypted( true );
337       user.load( new IntKey( 1 ) );
338       assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getString( "password" ) );
339
340       user = new User();
341       user.load( new IntKey( 1 ) );
342       user.setPasswordEncrypted( true );
343       assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getString( "password" ) );
344
345       //
346
//
347
//
348
user = new User();
349       user.setPasswordEncrypted( true );
350       user.loadForLogin( "test" );
351       assertEquals( true, user.authenticate( "password" ) );
352     }
353     catch ( Exception JavaDoc e ) {
354       e.printStackTrace();
355       fail();
356     }
357   }
358 }
359
Popular Tags