KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > user > UserSessionManagerTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * UserSessionManagerTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.security.user;
25
26 import java.util.HashSet JavaDoc;
27 import junit.framework.*;
28 import com.rift.coad.lib.security.ThreadsPermissionContainer;
29 import com.rift.coad.lib.security.ThreadPermissionSession;
30 import com.rift.coad.lib.security.UserSession;
31
32 /**
33  * The definition of the user session manager class.
34  *
35  * @author Brett Chaldecott
36  */

37 public class UserSessionManagerTest extends TestCase {
38     
39     public UserSessionManagerTest(String JavaDoc testName) {
40         super(testName);
41     }
42
43     protected void setUp() throws Exception JavaDoc {
44     }
45
46     protected void tearDown() throws Exception JavaDoc {
47     }
48
49     public static Test suite() {
50         TestSuite suite = new TestSuite(UserSessionManagerTest.class);
51         
52         return suite;
53     }
54
55     /**
56      * Test of initSessionForUser method, of class com.rift.coad.lib.security.user.UserSessionManager.
57      */

58     public void testInitSessionForUser() throws Exception JavaDoc {
59         System.out.println("initSessionForUser");
60         
61         ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
62         UserStoreManager userStoreManager = new UserStoreManager();
63         UserSessionManager instance = new UserSessionManager(permissions,
64                 userStoreManager);
65         instance.startCleanup();
66         // attempt to init the user
67
instance.initSessionForUser("test1");
68         
69         // create a test user
70
UserSession user = new UserSession("test2",new HashSet JavaDoc());
71         user.setExpiryTime(500);
72         instance.initSessionForUser(user);
73         UserSession user2 = new UserSession("test2",new HashSet JavaDoc());
74         user2.setExpiryTime(500);
75         instance.initSessionForUser(user2);
76         
77         // retrieve the session via user
78
if (user != instance.getSessionById(user.getSessionId())) {
79             fail("The user session could not be found by id.");
80         }
81         if (user2 != instance.getSessionById(user2.getSessionId())) {
82             fail("The user 2 session could not be found by id.");
83         }
84         
85         for (int count = 0; count < 4; count++) {
86             Thread.sleep(450);
87             user2.touch();
88         }
89         
90         try {
91             instance.getSessionById(user.getSessionId());
92             fail("The user session could be found.");
93         } catch (UserException ex) {
94             // ignore
95
}
96         if (user2 != instance.getSessionById(user2.getSessionId())) {
97             fail("The user 2 session could be found.");
98         }
99         
100         instance.shutdown();
101     }
102     
103 }
104
Popular Tags