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: * ContextManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.naming;
025:
026: import junit.framework.*;
027: import org.apache.log4j.Logger;
028: import java.util.StringTokenizer;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import java.util.Hashtable;
032:
033: import java.util.Set;
034: import java.util.HashSet;
035: import com.rift.coad.lib.interceptor.InterceptorFactory;
036: import com.rift.coad.lib.security.RoleManager;
037: import com.rift.coad.lib.security.ThreadsPermissionContainer;
038: import com.rift.coad.lib.security.ThreadPermissionSession;
039: import com.rift.coad.lib.security.UserSession;
040: import com.rift.coad.lib.security.user.UserSessionManager;
041: import com.rift.coad.lib.security.user.UserStoreManager;
042: import com.rift.coad.lib.security.SessionManager;
043: import com.rift.coad.lib.security.login.LoginManager;
044: import com.rift.coad.lib.thread.CoadunationThreadGroup;
045:
046: /**
047: *
048: * @author Brett Chaldecott
049: */
050: public class ContextManagerTest extends TestCase {
051:
052: public ContextManagerTest(String testName) {
053: super (testName);
054: }
055:
056: protected void setUp() throws Exception {
057: }
058:
059: protected void tearDown() throws Exception {
060: }
061:
062: public static Test suite() {
063: TestSuite suite = new TestSuite(ContextManagerTest.class);
064:
065: return suite;
066: }
067:
068: /**
069: * Test of testContextManager, of class com.rift.coad.lib.naming.ContextManager.
070: */
071: public void testContextManager() throws Exception {
072: System.out.println("testContextManager");
073:
074: // init the session information
075: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
076: SessionManager.init(permissions);
077: UserStoreManager userStoreManager = new UserStoreManager();
078: UserSessionManager sessionManager = new UserSessionManager(
079: permissions, userStoreManager);
080: LoginManager.init(sessionManager, userStoreManager);
081: // instanciate the thread manager
082: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
083: sessionManager, userStoreManager);
084:
085: // add a user to the session for the current thread
086: RoleManager.getInstance();
087:
088: InterceptorFactory.init(permissions, sessionManager,
089: userStoreManager);
090:
091: // add a new user object and add to the permission
092: Set set = new HashSet();
093: set.add("test");
094: UserSession user = new UserSession("test1", set);
095: permissions.putSession(
096: new Long(Thread.currentThread().getId()),
097: new ThreadPermissionSession(new Long(Thread
098: .currentThread().getId()), user));
099:
100: NamingDirector.init(threadGroup);
101:
102: Context startCtx = new InitialContext();
103:
104: ContextManager instance1 = new ContextManager(
105: "java:comp/env/test1");
106: ContextManager instance2 = new ContextManager(
107: "java:comp/env/test2");
108:
109: System.out.println("Bind value 1");
110: instance1.bind("test1", "value1");
111: System.out.println("Bind value 2");
112: instance2.bind("test2", "value2");
113:
114: System.out.println("Perform lookup");
115: String value1 = (String) startCtx
116: .lookup("java:comp/env/test1/test1");
117: System.out.println("User the values");
118:
119: if ((value1 == null) || (value1.equals("value1") == false)) {
120: fail("Failed to retrieve value 1");
121: }
122: System.out.println("Test value 1 : " + value1);
123:
124: String value2 = (String) startCtx
125: .lookup("java:comp/env/test2/test2");
126: if ((value2 == null) || (value2.equals("value2") == false)) {
127: fail("Failed to retrieve value 2");
128: }
129: System.out.println("Test value 2 : " + value2);
130:
131: instance1.unbind("test1");
132: try {
133: value1 = (String) startCtx
134: .lookup("java:comp/env/test1/test1");
135: fail("Test 1 value is still bound");
136: } catch (Exception ex) {
137: // ignore
138: }
139:
140: NamingDirector.getInstance().shutdown();
141: }
142:
143: }
|