001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ServerInterceptorTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.interceptor;
025:
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Set;
029: import java.lang.reflect.Constructor;
030: import java.util.concurrent.ConcurrentHashMap;
031: import java.util.concurrent.ConcurrentMap;
032:
033: // unit test
034: import junit.framework.*;
035:
036: // logging
037: import org.apache.log4j.Logger;
038:
039: // coadunation
040: import com.rift.coad.lib.configuration.Configuration;
041: import com.rift.coad.lib.configuration.ConfigurationFactory;
042: import com.rift.coad.lib.interceptor.credentials.Credential;
043: import com.rift.coad.lib.interceptor.credentials.Login;
044: import com.rift.coad.lib.interceptor.credentials.Session;
045: import com.rift.coad.lib.interceptor.authenticator.SessionAuthenticator;
046: import com.rift.coad.lib.security.AuthorizationException;
047: import com.rift.coad.lib.security.RoleManager;
048: import com.rift.coad.lib.security.ThreadsPermissionContainer;
049: import com.rift.coad.lib.security.ThreadPermissionSession;
050: import com.rift.coad.lib.security.UserSession;
051: import com.rift.coad.lib.security.user.UserSessionManager;
052: import com.rift.coad.lib.security.user.UserStoreManager;
053: import com.rift.coad.lib.security.SessionManager;
054: import com.rift.coad.lib.security.login.LoginManager;
055: import com.rift.coad.lib.thread.CoadunationThreadGroup;
056:
057: /**
058: * This class test the server interceptor.
059: *
060: * @author Brett Chaldecott
061: */
062: public class ServerInterceptorTest extends TestCase {
063:
064: /**
065: * This class is responsible for supplying access to the Server
066: * interceptor methods
067: */
068: public class TestInterceptor extends InterceptorWrapper {
069:
070: // private member variables
071: private ServerInterceptor interceptor = null;
072:
073: /**
074: * The constructor of the test interceptor.
075: */
076: public TestInterceptor() throws Exception {
077: interceptor = this .getServerInterceptor();
078: }
079:
080: /**
081: * This method overwrites the existing session using the credentials
082: * passed in.
083: *
084: * @param credential The credentials overwrite the old session with.
085: * @exception Exception
086: */
087: public void setSession(Credential credential) throws Exception {
088: interceptor.setSession(credential);
089: }
090:
091: /**
092: * This method creates a new session using the credentials passed in.
093: *
094: * @param credential The credentials to create a new session for.
095: * @exception Exception
096: */
097: public void createSession(Credential credential)
098: throws Exception {
099: interceptor.createSession(credential);
100: }
101:
102: /**
103: * This method releases this thread, removing all user information from it.
104: *
105: * @exception InterceptorException
106: */
107: public void release() throws InterceptorException {
108: interceptor.release();
109: }
110:
111: }
112:
113: public ServerInterceptorTest(String testName) {
114: super (testName);
115: }
116:
117: protected void setUp() throws Exception {
118: }
119:
120: protected void tearDown() throws Exception {
121: }
122:
123: public static Test suite() {
124: TestSuite suite = new TestSuite(ServerInterceptorTest.class);
125:
126: return suite;
127: }
128:
129: /**
130: * Test of createSession method, of class com.rift.coad.lib.interceptor.ServerInterceptor.
131: */
132: public void testServerInterceptor() throws Exception {
133: System.out.println("testServerInterceptor");
134:
135: // init the session information
136: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
137: SessionManager.init(permissions);
138: UserStoreManager userStoreManager = new UserStoreManager();
139: UserSessionManager sessionManager = new UserSessionManager(
140: permissions, userStoreManager);
141: LoginManager.init(sessionManager, userStoreManager);
142: // instanciate the thread manager
143: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
144: sessionManager, userStoreManager);
145:
146: // add a user to the session for the current thread
147: RoleManager.getInstance();
148:
149: InterceptorFactory.init(permissions, sessionManager,
150: userStoreManager);
151:
152: TestInterceptor testInterceptor = new TestInterceptor();
153:
154: Set principals = new HashSet();
155: principals.add("test");
156: Session session = new Session("test", "xxxxxxxxxxxx",
157: principals);
158:
159: if (null != permissions.getSession(Thread.currentThread()
160: .getId())) {
161: fail("There is an existing session for this user");
162: }
163:
164: testInterceptor.createSession(session);
165:
166: ThreadPermissionSession threadSession = permissions
167: .getSession();
168:
169: if (!threadSession.getUser().getName().equals("test")) {
170: fail("The user name is not test");
171: }
172: if (!threadSession.getUser().getSessionId().equals(
173: "xxxxxxxxxxxx")) {
174: fail("The session id [xxxxxxxxxxxx] was not found ["
175: + threadSession.getUser().getSessionId() + "]");
176: }
177: if (!threadSession.getPrincipals().contains("test")) {
178: fail("The principals are not setup correctly.");
179: }
180:
181: principals = new HashSet();
182: principals.add("test");
183: principals.add("test1");
184: principals.add("test2");
185: session = new Session("test1", "yyyyyyyyyyyyy", principals);
186:
187: testInterceptor.createSession(session);
188:
189: threadSession = permissions.getSession();
190:
191: if (!threadSession.getUser().getName().equals("test1")) {
192: fail("The user name is test");
193: }
194: if (!threadSession.getUser().getSessionId().contains(
195: "yyyyyyyyyyyyy")) {
196: fail("The session id [yyyyyyyyyyyyy] was not found.");
197: }
198: if (!threadSession.getPrincipals().contains("test")
199: || !threadSession.getPrincipals().contains("test1")
200: || !threadSession.getPrincipals().contains("test2")) {
201: fail("The principals are not setup correctly.");
202: }
203:
204: testInterceptor.release();
205:
206: threadSession = permissions.getSession();
207:
208: if (!threadSession.getUser().getName().equals("test")) {
209: fail("The user name is test");
210: }
211: if (!threadSession.getUser().getSessionId().equals(
212: "xxxxxxxxxxxx")) {
213: fail("The session id [xxxxxxxxxxxx] was not found");
214: }
215: if (!threadSession.getPrincipals().contains("test")
216: || threadSession.getPrincipals().contains("test1")
217: || threadSession.getPrincipals().contains("test2")) {
218: String principalString = "";
219: for (Iterator iter = threadSession.getPrincipals()
220: .iterator(); iter.hasNext();) {
221: principalString += "," + (String) iter.next();
222: }
223: fail("The principals are not setup correctly ["
224: + principalString + "]");
225: }
226:
227: testInterceptor.release();
228:
229: if (null != permissions.getSession(Thread.currentThread()
230: .getId())) {
231: fail("There is an existing session for this user");
232: }
233:
234: // test set session
235: principals = new HashSet();
236: principals.add("test");
237: principals.add("test1");
238: principals.add("test2");
239: session = new Session("test1", "yyyyyyyyyyyyy", principals);
240:
241: testInterceptor.setSession(session);
242:
243: threadSession = permissions.getSession();
244:
245: if (!threadSession.getUser().getName().equals("test1")) {
246: fail("The user name is test");
247: }
248: if (!threadSession.getUser().getSessionId().contains(
249: "yyyyyyyyyyyyy")) {
250: fail("The session id [yyyyyyyyyyyyy] was not found.");
251: }
252: if (!threadSession.getPrincipals().contains("test")
253: || !threadSession.getPrincipals().contains("test1")
254: || !threadSession.getPrincipals().contains("test2")) {
255: fail("The principals are not setup correctly.");
256: }
257:
258: }
259:
260: }
|