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: * CosNamingContextManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.naming.cos;
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 javax.naming.NameNotFoundException;
032: import javax.naming.NamingException;
033: import java.util.Hashtable;
034: import java.net.URLClassLoader;
035: import java.net.URL;
036:
037: import java.util.Set;
038: import java.util.HashSet;
039: import com.rift.coad.lib.naming.NamingDirector;
040: import com.rift.coad.lib.interceptor.InterceptorFactory;
041: import com.rift.coad.lib.security.RoleManager;
042: import com.rift.coad.lib.security.ThreadsPermissionContainer;
043: import com.rift.coad.lib.security.ThreadPermissionSession;
044: import com.rift.coad.lib.security.UserSession;
045: import com.rift.coad.lib.security.user.UserSessionManager;
046: import com.rift.coad.lib.security.user.UserStoreManager;
047: import com.rift.coad.lib.security.SessionManager;
048: import com.rift.coad.lib.security.login.LoginManager;
049: import com.rift.coad.lib.thread.CoadunationThreadGroup;
050:
051: /**
052: *
053: * @author Brett Chaldecott
054: */
055: public class CosNamingContextManagerTest extends TestCase {
056:
057: public CosNamingContextManagerTest(String testName) {
058: super (testName);
059: }
060:
061: protected void setUp() throws Exception {
062: }
063:
064: protected void tearDown() throws Exception {
065: }
066:
067: public static Test suite() {
068: TestSuite suite = new TestSuite(
069: CosNamingContextManagerTest.class);
070:
071: return suite;
072: }
073:
074: /**
075: * Test of testContextManager, of class com.rift.coad.lib.naming.ContextManager.
076: */
077: public void testContextManager() throws Exception {
078: System.out.println("testContextManager");
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: // add a new user object and add to the permission
098: Set set = new HashSet();
099: set.add("test");
100: UserSession user = new UserSession("test1", set);
101: permissions.putSession(
102: new Long(Thread.currentThread().getId()),
103: new ThreadPermissionSession(new Long(Thread
104: .currentThread().getId()), user));
105:
106: NamingDirector.init(threadGroup);
107:
108: Context context = new InitialContext();
109:
110: context.bind("java:comp/env/test", "fred");
111: context.bind("java:comp/env/test2", "fred2");
112:
113: if (!context.lookup("java:comp/env/test").equals("fred")) {
114: fail("Could not retrieve the value for test");
115: }
116: if (!context.lookup("java:comp/env/test2").equals("fred2")) {
117: fail("Could not retrieve the value for test2");
118: }
119: System.out.println("Creating the sub context");
120: Context subContext = context
121: .createSubcontext("java:comp/env/test3/test3");
122: System.out
123: .println("Adding the binding for bob to the sub context");
124: subContext.bind("bob", "bob");
125: System.out
126: .println("Looking up the binding for bob on the sub context.");
127: Object value = subContext.lookup("bob");
128: System.out.println("Object type is : "
129: + value.getClass().getName());
130: if (!value.equals("bob")) {
131: fail("Could not retrieve the value bob");
132: }
133: if (!context.lookup("java:comp/env/test3/test3/bob").equals(
134: "bob")) {
135: fail("Could not retrieve the value bob");
136: }
137:
138: ClassLoader loader = new URLClassLoader(new URL[0]);
139: ClassLoader original = Thread.currentThread()
140: .getContextClassLoader();
141: Thread.currentThread().setContextClassLoader(loader);
142: NamingDirector.getInstance().initContext();
143:
144: context.bind("java:comp/env/test5", "fred5");
145: if (!context.lookup("java:comp/env/test5").equals("fred5")) {
146: fail("Could not retrieve the value fred5");
147: }
148:
149: Thread.currentThread().setContextClassLoader(original);
150:
151: try {
152: context.lookup("java:comp/env/test5");
153: fail("Failed retrieve a value that should not exist");
154: } catch (NameNotFoundException ex) {
155: // ignore
156: }
157:
158: Thread.currentThread().setContextClassLoader(loader);
159:
160: NamingDirector.getInstance().releaseContext();
161:
162: try {
163: context.lookup("java:comp/env/test5");
164: fail("Failed retrieve a value that should not exist");
165: } catch (NameNotFoundException ex) {
166: // ignore
167: }
168: Thread.currentThread().setContextClassLoader(original);
169: System.out.println("Add value 1");
170: context.bind("basic", "basic");
171: System.out.println("Add value 2");
172: context.bind("basic2/bob", "basic2");
173: if (!context.lookup("basic").equals("basic")) {
174: fail("Could not retrieve the basic value from the JNDI");
175: }
176: if (!context.lookup("basic2/bob").equals("basic2")) {
177: fail("Could not retrieve the basic value from the JNDI");
178: }
179:
180: try {
181: context.bind("java:network/env/test", "fred");
182: fail("This should have thrown as only relative urls can be used "
183: + "JNDI");
184: } catch (NamingException ex) {
185: // ignore
186: }
187:
188: try {
189: context.unbind("java:network/env/test");
190: fail("This should have thrown as only relative urls can be used "
191: + "JNDI");
192: } catch (NamingException ex) {
193: // ignore
194: }
195: context.rebind("basic", "test1");
196: context.rebind("basic2/bob", "test2");
197:
198: if (!context.lookup("basic").equals("test1")) {
199: fail("Could not retrieve the basic value from the JNDI");
200: }
201: if (!context.lookup("basic2/bob").equals("test2")) {
202: fail("Could not retrieve the basic value from the JNDI");
203: }
204:
205: context.unbind("basic");
206: context.unbind("basic2/bob");
207:
208: try {
209: context.lookup("basic2/bob");
210: fail("The basic bob value could still be found");
211: } catch (NameNotFoundException ex) {
212: // ignore
213: }
214:
215: NamingDirector.getInstance().shutdown();
216: }
217:
218: }
|