KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > login > SessionLoginTest


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  * SessionLoginTest.java
20  *
21  * JUnit based test
22  */

23
24 // package path
25
package com.rift.coad.lib.security.login;
26
27 import junit.framework.*;
28
29 // java imports
30
import org.apache.log4j.BasicConfigurator;
31
32 // coadunation imports
33
import com.rift.coad.lib.security.ThreadsPermissionContainer;
34 import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
35 import com.rift.coad.lib.security.user.UserStoreManager;
36 import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
37 import com.rift.coad.lib.security.user.UserSessionManager;
38 import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
39 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
40 import com.rift.coad.lib.security.SessionManager;
41 import com.rift.coad.lib.security.RoleManager;
42 import com.rift.coad.lib.security.Validator;
43 import com.rift.coad.lib.security.UserSession;
44 import com.rift.coad.lib.thread.BasicThread;
45 import com.rift.coad.lib.thread.CoadunationThreadGroup;
46 import com.rift.coad.lib.security.sudo.Sudo;
47 import com.rift.coad.lib.security.sudo.SudoCallbackHandler;
48
49 /**
50  * The class that tests the session login
51  *
52  * @author Brett Chaldecott
53  */

54 public class SessionLoginTest extends TestCase implements SudoCallbackHandler {
55     
56     /**
57      * This object will sudo by user
58      */

59     public class SudoBySessionIdTest extends BasicThread {
60         
61         private String JavaDoc sessionId = null;
62         private SudoCallbackHandler handler = null;
63         
64         /**
65          * The constructor of the handler
66          */

67         public SudoBySessionIdTest (String JavaDoc sessionId,
68                 SudoCallbackHandler handler) throws Exception JavaDoc {
69             this.sessionId = sessionId;
70             this.handler = handler;
71         }
72         
73         
74         /**
75          * This method will process the result
76          */

77         public void process() {
78             try {
79                 Sudo.sudoThreadBySessionId(sessionId, handler);
80             } catch (Exception JavaDoc ex) {
81                 System.out.println("Failed to process : " + ex.getMessage());
82                 ex.printStackTrace(System.out);
83             }
84         }
85     }
86     
87     // private member variables
88
private boolean called = false;
89     
90     public SessionLoginTest(String JavaDoc testName) {
91         super(testName);
92         BasicConfigurator.configure();
93     }
94
95     protected void setUp() throws Exception JavaDoc {
96     }
97
98     protected void tearDown() throws Exception JavaDoc {
99     }
100
101     public static Test suite() {
102         TestSuite suite = new TestSuite(SessionLoginTest.class);
103         
104         return suite;
105     }
106
107     /**
108      * Test of login method, of class com.rift.coad.lib.security.login.SessionLogin.
109      */

110     public void testLogin() throws Exception JavaDoc {
111         System.out.println("login");
112         
113         ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
114         ThreadsPermissionContainerAccessor.init(permissions);
115         SessionManager.init(permissions);
116         UserStoreManager userStoreManager = new UserStoreManager();
117         UserStoreManagerAccessor.init(userStoreManager);
118         UserSessionManager sessionManager = new UserSessionManager(permissions,
119                 userStoreManager);
120         UserSessionManagerAccessor.init(sessionManager);
121         LoginManager.init(sessionManager,userStoreManager);
122         // instanciate the thread manager
123
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
124             userStoreManager);
125         
126         // add a user to the session for the current thread
127
RoleManager.getInstance();
128         
129         SessionLogin instance = new SessionLogin(
130                 new PasswordInfoHandler("test1","112233"));
131         
132         instance.login();
133         
134         // retrieve the user object
135
UserSession user = instance.getUser();
136         if (!user.getName().equals("test1")) {
137             fail("Login failed");
138         }
139         
140         // run as a user
141
called = false;
142         SudoBySessionIdTest sudoBySessionIdTest = new
143                 SudoBySessionIdTest (user.getSessionId(), this);
144         threadGroup.addThread(sudoBySessionIdTest,"test1");
145         sudoBySessionIdTest.start();
146         sudoBySessionIdTest.join();
147         if (!called) {
148             fail("Failed to call");
149         }
150     }
151     
152     
153     /**
154      * This method is called to process
155      */

156     public void process() {
157         called = true;
158     }
159 }
160
Popular Tags