KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

78         public TestInterceptor() throws Exception JavaDoc {
79             interceptor = this.getClientInterceptor();
80         }
81         
82         
83         /**
84          * This method returns the session credentials
85          */

86         public Credential getSessionCredential() throws InterceptorException {
87             return interceptor.getSessionCredential();
88         }
89         
90         
91     }
92     
93     public ClientInterceptorTest(String JavaDoc testName) {
94         super(testName);
95     }
96
97     protected void setUp() throws Exception JavaDoc {
98     }
99
100     protected void tearDown() throws Exception JavaDoc {
101     }
102
103     public static Test suite() {
104         TestSuite suite = new TestSuite(ClientInterceptorTest.class);
105         
106         return suite;
107     }
108
109     /**
110      * Test of getSessionCredential method, of class com.rift.coad.lib.interceptor.ClientInterceptor.
111      */

112     public void testGetSessionCredential() throws Exception JavaDoc {
113         System.out.println("getSessionCredential");
114         
115         // init the session information
116
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
117         SessionManager.init(permissions);
118         UserStoreManager userStoreManager = new UserStoreManager();
119         UserSessionManager sessionManager = new UserSessionManager(permissions,
120                 userStoreManager);
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         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
130         
131         // instanciate the test interceptor
132
TestInterceptor testInterceptor = new TestInterceptor();
133         
134         try {
135             testInterceptor.getSessionCredential();
136             fail("Was able to get the session credentials for a none " +
137                     "existant session");
138         } catch (InterceptorException ex) {
139             // ignore
140
}
141         
142         // add a new user object and add to the permission
143
Set JavaDoc set = new HashSet JavaDoc();
144         set.add("test");
145         set.add("test1");
146         set.add("test2");
147         set.add("test3");
148         UserSession user = new UserSession("test", set);
149         ThreadPermissionSession currentSession = new ThreadPermissionSession(
150                 new Long JavaDoc(Thread.currentThread().getId()),user);
151         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
152                 currentSession);
153         
154         try {
155             Session session = (Session)testInterceptor.getSessionCredential();
156             if (!session.getUsername().equals("test")) {
157                 fail("The username is not equal to test");
158             }
159             Set JavaDoc principals = session.getPrincipals();
160             if ((!principals.contains("test")) ||
161                     (!principals.contains("test1")) ||
162                     (!principals.contains("test2")) ||
163                     (!principals.contains("test3")))
164             {
165                 fail("Principals not set correctly");
166             }
167         } catch (InterceptorException ex) {
168             fail("Should have been able to retrieve credentials");
169         }
170         
171     }
172     
173 }
174
Popular Tags