KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > ServerInterceptorTest


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

23
24 package com.rift.coad.lib.interceptor;
25
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.lang.reflect.Constructor JavaDoc;
30 import java.util.concurrent.ConcurrentHashMap JavaDoc;
31 import java.util.concurrent.ConcurrentMap JavaDoc;
32
33 // unit test
34
import junit.framework.*;
35
36 // logging
37
import org.apache.log4j.Logger;
38
39 // coadunation
40
import com.rift.coad.lib.configuration.Configuration;
41 import com.rift.coad.lib.configuration.ConfigurationFactory;
42 import com.rift.coad.lib.interceptor.credentials.Credential;
43 import com.rift.coad.lib.interceptor.credentials.Login;
44 import com.rift.coad.lib.interceptor.credentials.Session;
45 import com.rift.coad.lib.interceptor.authenticator.SessionAuthenticator;
46 import com.rift.coad.lib.security.AuthorizationException;
47 import com.rift.coad.lib.security.RoleManager;
48 import com.rift.coad.lib.security.ThreadsPermissionContainer;
49 import com.rift.coad.lib.security.ThreadPermissionSession;
50 import com.rift.coad.lib.security.UserSession;
51 import com.rift.coad.lib.security.user.UserSessionManager;
52 import com.rift.coad.lib.security.user.UserStoreManager;
53 import com.rift.coad.lib.security.SessionManager;
54 import com.rift.coad.lib.security.login.LoginManager;
55 import com.rift.coad.lib.thread.CoadunationThreadGroup;
56
57 /**
58  * This class test the server interceptor.
59  *
60  * @author Brett Chaldecott
61  */

62 public class ServerInterceptorTest extends TestCase {
63     
64     /**
65      * This class is responsible for supplying access to the Server
66      * interceptor methods
67      */

68     public class TestInterceptor extends InterceptorWrapper {
69         
70         // private member variables
71
private ServerInterceptor interceptor = null;
72         
73         
74         /**
75          * The constructor of the test interceptor.
76          */

77         public TestInterceptor() throws Exception JavaDoc {
78             interceptor = this.getServerInterceptor();
79         }
80         
81         
82         /**
83          * This method creates a new session using the credentials passed in.
84          *
85          * @param credential The credentials to create a new session for.
86          * @exception Exception
87          */

88         public void createSession(Credential credential) throws Exception JavaDoc {
89             interceptor.createSession(credential);
90         }
91         
92         
93         /**
94          * This method releases this thread, removing all user information from it.
95          *
96          * @exception InterceptorException
97          */

98         public void release() throws InterceptorException {
99             interceptor.release();
100         }
101         
102     }
103     
104     public ServerInterceptorTest(String JavaDoc testName) {
105         super(testName);
106     }
107     
108     protected void setUp() throws Exception JavaDoc {
109     }
110     
111     protected void tearDown() throws Exception JavaDoc {
112     }
113     
114     public static Test suite() {
115         TestSuite suite = new TestSuite(ServerInterceptorTest.class);
116         
117         return suite;
118     }
119     
120     /**
121      * Test of createSession method, of class com.rift.coad.lib.interceptor.ServerInterceptor.
122      */

123     public void testServerInterceptor() throws Exception JavaDoc {
124         System.out.println("testServerInterceptor");
125         
126         // init the session information
127
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
128         SessionManager.init(permissions);
129         UserStoreManager userStoreManager = new UserStoreManager();
130         UserSessionManager sessionManager = new UserSessionManager(permissions,
131                 userStoreManager);
132         LoginManager.init(sessionManager,userStoreManager);
133         // instanciate the thread manager
134
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
135             userStoreManager);
136         
137         // add a user to the session for the current thread
138
RoleManager.getInstance();
139         
140         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
141         
142         TestInterceptor testInterceptor = new TestInterceptor();
143         
144         
145         Set JavaDoc principals = new HashSet JavaDoc();
146         principals.add("test");
147         Session session = new Session("test","xxxxxxxxxxxx",principals);
148         
149         if (null != permissions.getSession(Thread.currentThread().getId())) {
150             fail("There is an existing session for this user");
151         }
152         
153         testInterceptor.createSession(session);
154         
155         ThreadPermissionSession threadSession = permissions.getSession();
156         
157         if (!threadSession.getUser().getName().equals("test")) {
158             fail("The user name is not test");
159         }
160         if (!threadSession.getUser().getSessionId().equals("xxxxxxxxxxxx")) {
161             fail("The session id [xxxxxxxxxxxx] was not found [" +
162                     threadSession.getUser().getSessionId() + "]");
163         }
164         if (!threadSession.getPrincipals().contains("test")) {
165             fail("The principals are not setup correctly.");
166         }
167         
168         principals = new HashSet JavaDoc();
169         principals.add("test");
170         principals.add("test1");
171         principals.add("test2");
172         session = new Session("test1","yyyyyyyyyyyyy",principals);
173         
174         testInterceptor.createSession(session);
175         
176         threadSession = permissions.getSession();
177         
178         if (!threadSession.getUser().getName().equals("test1")) {
179             fail("The user name is test");
180         }
181         if (!threadSession.getUser().getSessionId().contains("yyyyyyyyyyyyy")) {
182             fail("The session id [yyyyyyyyyyyyy] was not found.");
183         }
184         if (!threadSession.getPrincipals().contains("test") ||
185                 !threadSession.getPrincipals().contains("test1") ||
186                 !threadSession.getPrincipals().contains("test2")) {
187             fail("The principals are not setup correctly.");
188         }
189         
190         testInterceptor.release();
191         
192         threadSession = permissions.getSession();
193         
194         if (!threadSession.getUser().getName().equals("test")) {
195             fail("The user name is test");
196         }
197         if (!threadSession.getUser().getSessionId().equals("xxxxxxxxxxxx")) {
198             fail("The session id [xxxxxxxxxxxx] was not found");
199         }
200         if (!threadSession.getPrincipals().contains("test") ||
201                 threadSession.getPrincipals().contains("test1") ||
202                 threadSession.getPrincipals().contains("test2")) {
203             String JavaDoc principalString = "";
204             for (Iterator JavaDoc iter = threadSession.getPrincipals().iterator();
205                     iter.hasNext();) {
206                 principalString += "," + (String JavaDoc)iter.next();
207             }
208             fail("The principals are not setup correctly [" + principalString + "]");
209         }
210         
211         testInterceptor.release();
212         
213         if (null != permissions.getSession(Thread.currentThread().getId())) {
214             fail("There is an existing session for this user");
215         }
216     }
217     
218     
219     
220 }
221
Popular Tags