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: * ClientInterceptorTest.java
020: *
021: * JUnit based test
022: */
023:
024: // package path
025: package com.rift.coad.lib.interceptor;
026:
027: // java imports
028: import java.util.HashSet;
029: import java.util.Set;
030: import java.lang.reflect.Constructor;
031: import java.util.concurrent.ConcurrentHashMap;
032: import java.util.concurrent.ConcurrentMap;
033:
034: // unit test
035: import junit.framework.*;
036:
037: // logging
038: import org.apache.log4j.Logger;
039:
040: // coadunation
041: import com.rift.coad.lib.configuration.Configuration;
042: import com.rift.coad.lib.configuration.ConfigurationFactory;
043: import com.rift.coad.lib.interceptor.credentials.Credential;
044: import com.rift.coad.lib.interceptor.credentials.Login;
045: import com.rift.coad.lib.interceptor.credentials.Session;
046: import com.rift.coad.lib.interceptor.authenticator.SessionAuthenticator;
047: import com.rift.coad.lib.security.AuthorizationException;
048: import com.rift.coad.lib.security.RoleManager;
049: import com.rift.coad.lib.security.ThreadsPermissionContainer;
050: import com.rift.coad.lib.security.ThreadPermissionSession;
051: import com.rift.coad.lib.security.UserSession;
052: import com.rift.coad.lib.security.user.UserSessionManager;
053: import com.rift.coad.lib.security.user.UserStoreManager;
054: import com.rift.coad.lib.security.SessionManager;
055: import com.rift.coad.lib.security.login.LoginManager;
056: import com.rift.coad.lib.thread.CoadunationThreadGroup;
057:
058: /**
059: * The client interceptor test class
060: *
061: * @author Brett Chaldecott
062: */
063: public class ClientInterceptorTest extends TestCase {
064:
065: /**
066: * This class is responsible for supplying access to the Server
067: * interceptor methods
068: */
069: public class TestInterceptor extends InterceptorWrapper {
070:
071: // private member variables
072: private ClientInterceptor interceptor = null;
073:
074: /**
075: * The constructor of the test interceptor.
076: */
077: public TestInterceptor() throws Exception {
078: interceptor = this .getClientInterceptor();
079: }
080:
081: /**
082: * This method returns the session credentials
083: */
084: public Credential getSessionCredential()
085: throws InterceptorException {
086: return interceptor.getSessionCredential();
087: }
088:
089: }
090:
091: public ClientInterceptorTest(String testName) {
092: super (testName);
093: }
094:
095: protected void setUp() throws Exception {
096: }
097:
098: protected void tearDown() throws Exception {
099: }
100:
101: public static Test suite() {
102: TestSuite suite = new TestSuite(ClientInterceptorTest.class);
103:
104: return suite;
105: }
106:
107: /**
108: * Test of getSessionCredential method, of class com.rift.coad.lib.interceptor.ClientInterceptor.
109: */
110: public void testGetSessionCredential() throws Exception {
111: System.out.println("getSessionCredential");
112:
113: // init the session information
114: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
115: SessionManager.init(permissions);
116: UserStoreManager userStoreManager = new UserStoreManager();
117: UserSessionManager sessionManager = new UserSessionManager(
118: permissions, userStoreManager);
119: LoginManager.init(sessionManager, userStoreManager);
120: // instanciate the thread manager
121: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
122: sessionManager, userStoreManager);
123:
124: // add a user to the session for the current thread
125: RoleManager.getInstance();
126:
127: InterceptorFactory.init(permissions, sessionManager,
128: userStoreManager);
129:
130: // instanciate the test interceptor
131: TestInterceptor testInterceptor = new TestInterceptor();
132:
133: try {
134: testInterceptor.getSessionCredential();
135: fail("Was able to get the session credentials for a none "
136: + "existant session");
137: } catch (InterceptorException ex) {
138: // ignore
139: }
140:
141: // add a new user object and add to the permission
142: Set set = new HashSet();
143: set.add("test");
144: set.add("test1");
145: set.add("test2");
146: set.add("test3");
147: UserSession user = new UserSession("test", set);
148: ThreadPermissionSession currentSession = new ThreadPermissionSession(
149: new Long(Thread.currentThread().getId()), user);
150: permissions.putSession(
151: new Long(Thread.currentThread().getId()),
152: currentSession);
153:
154: try {
155: Session session = (Session) testInterceptor
156: .getSessionCredential();
157: if (!session.getUsername().equals("test")) {
158: fail("The username is not equal to test");
159: }
160: Set principals = session.getPrincipals();
161: if ((!principals.contains("test"))
162: || (!principals.contains("test1"))
163: || (!principals.contains("test2"))
164: || (!principals.contains("test3"))) {
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: }
|