001: /*
002: * Tomcat: The deployer for the tomcat daemon
003: * Copyright (C) 2007 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: * CoadunationInterceptorTest.java
020: */
021:
022: // package imports
023: package com.rift.coad.daemon.tomcat.security;
024:
025: // java imports
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: // junit framework
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.InterceptorFactory;
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: *
060: * @author brett
061: */
062: public class CoadunationInterceptorTest extends TestCase {
063:
064: public CoadunationInterceptorTest(String testName) {
065: super (testName);
066: }
067:
068: protected void setUp() throws Exception {
069: }
070:
071: protected void tearDown() throws Exception {
072: }
073:
074: /**
075: * Test of pushUser method, of class com.rift.coad.daemon.tomcat.security.CoadunationInterceptor.
076: */
077: public void testPushUser() throws Exception {
078: System.out.println("pushUser");
079:
080: // init the session information
081: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
082: SessionManager.init(permissions);
083: UserStoreManager userStoreManager = new UserStoreManager();
084: UserSessionManager sessionManager = new UserSessionManager(
085: permissions, userStoreManager);
086: LoginManager.init(sessionManager, userStoreManager);
087: // instanciate the thread manager
088: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
089: sessionManager, userStoreManager);
090:
091: // add a user to the session for the current thread
092: RoleManager.getInstance();
093:
094: InterceptorFactory.init(permissions, sessionManager,
095: userStoreManager);
096:
097: CoadunationInterceptor instance = new CoadunationInterceptor();
098:
099: Set principals = new HashSet();
100:
101: instance.pushUser(null);
102:
103: try {
104: permissions.getSession();
105: fail("Should not have been able to get the session");
106: } catch (com.rift.coad.lib.security.SecurityException ex) {
107: // ignore
108: }
109:
110: principals = new HashSet();
111: principals.add("test1");
112: principals.add("test2");
113: UserSession session2 = new UserSession("test1",
114: "yyyyyyyyyyyyy", principals);
115:
116: instance.pushUser(session2);
117:
118: ThreadPermissionSession threadSession = permissions
119: .getSession();
120:
121: if (!threadSession.getUser().getName().equals("test1")) {
122: fail("The user name is ["
123: + threadSession.getUser().getName() + "]");
124: }
125: if (!threadSession.getUser().getSessionId().contains(
126: "yyyyyyyyyyyyy")) {
127: fail("The session id [yyyyyyyyyyyyy] was not found.");
128: }
129: if (!threadSession.getPrincipals().contains("test1")
130: || !threadSession.getPrincipals().contains("test2")) {
131: fail("The principals are not setup correctly.");
132: }
133:
134: instance.popUser();
135:
136: try {
137: permissions.getSession();
138: fail("Should not have been able to get the session");
139: } catch (com.rift.coad.lib.security.SecurityException ex) {
140: // ignore
141: }
142:
143: instance.popUser();
144:
145: try {
146: permissions.getSession();
147: fail("Should not have been able to get the session");
148: } catch (com.rift.coad.lib.security.SecurityException ex) {
149: // ignore
150: }
151:
152: }
153:
154: }
|