1 23 24 package com.rift.coad.lib.naming.cos; 25 26 import junit.framework.*; 27 import org.apache.log4j.Logger; 28 import java.util.StringTokenizer ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NameNotFoundException ; 32 import javax.naming.NamingException ; 33 import java.util.Hashtable ; 34 import java.net.URLClassLoader ; 35 import java.net.URL ; 36 37 import java.util.Set ; 38 import java.util.HashSet ; 39 import com.rift.coad.lib.naming.NamingDirector; 40 import com.rift.coad.lib.interceptor.InterceptorFactory; 41 import com.rift.coad.lib.security.RoleManager; 42 import com.rift.coad.lib.security.ThreadsPermissionContainer; 43 import com.rift.coad.lib.security.ThreadPermissionSession; 44 import com.rift.coad.lib.security.UserSession; 45 import com.rift.coad.lib.security.user.UserSessionManager; 46 import com.rift.coad.lib.security.user.UserStoreManager; 47 import com.rift.coad.lib.security.SessionManager; 48 import com.rift.coad.lib.security.login.LoginManager; 49 import com.rift.coad.lib.thread.CoadunationThreadGroup; 50 51 52 56 public class CosNamingContextManagerTest extends TestCase { 57 58 public CosNamingContextManagerTest(String testName) { 59 super(testName); 60 } 61 62 protected void setUp() throws Exception { 63 } 64 65 protected void tearDown() throws Exception { 66 } 67 68 public static Test suite() { 69 TestSuite suite = new TestSuite(CosNamingContextManagerTest.class); 70 71 return suite; 72 } 73 74 77 public void testContextManager() throws Exception { 78 System.out.println("testContextManager"); 79 80 ThreadsPermissionContainer permissions = new ThreadsPermissionContainer(); 82 SessionManager.init(permissions); 83 UserStoreManager userStoreManager = new UserStoreManager(); 84 UserSessionManager sessionManager = new UserSessionManager(permissions, 85 userStoreManager); 86 LoginManager.init(sessionManager,userStoreManager); 87 CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager, 89 userStoreManager); 90 91 RoleManager.getInstance(); 93 94 InterceptorFactory.init(permissions,sessionManager,userStoreManager); 95 96 Set set = new HashSet (); 98 set.add("test"); 99 UserSession user = new UserSession("test1", set); 100 permissions.putSession(new Long (Thread.currentThread().getId()), 101 new ThreadPermissionSession( 102 new Long (Thread.currentThread().getId()),user)); 103 104 105 NamingDirector.init(threadGroup); 106 107 Context context = new InitialContext (); 108 109 context.bind("java:comp/env/test","fred"); 110 context.bind("java:comp/env/test2","fred2"); 111 112 if (!context.lookup("java:comp/env/test").equals("fred")) { 113 fail("Could not retrieve the value for test"); 114 } 115 if (!context.lookup("java:comp/env/test2").equals("fred2")) { 116 fail("Could not retrieve the value for test2"); 117 } 118 System.out.println("Creating the sub context"); 119 Context subContext = context.createSubcontext("java:comp/env/test3/test3"); 120 System.out.println("Adding the binding for bob to the sub context"); 121 subContext.bind("bob","bob"); 122 System.out.println("Looking up the binding for bob on the sub context."); 123 Object value = subContext.lookup("bob"); 124 System.out.println("Object type is : " + value.getClass().getName()); 125 if (!value.equals("bob")) { 126 fail("Could not retrieve the value bob"); 127 } 128 if (!context.lookup("java:comp/env/test3/test3/bob").equals("bob")) { 129 fail("Could not retrieve the value bob"); 130 } 131 132 ClassLoader loader = new URLClassLoader (new URL [0]); 133 ClassLoader original = Thread.currentThread().getContextClassLoader(); 134 Thread.currentThread().setContextClassLoader(loader); 135 NamingDirector.getInstance().initContext(); 136 137 context.bind("java:comp/env/test5","fred5"); 138 if (!context.lookup("java:comp/env/test5").equals("fred5")) { 139 fail("Could not retrieve the value fred5"); 140 } 141 142 Thread.currentThread().setContextClassLoader(original); 143 144 try{ 145 context.lookup("java:comp/env/test5"); 146 fail("Failed retrieve a value that should not exist"); 147 } catch (NameNotFoundException ex) { 148 } 150 151 Thread.currentThread().setContextClassLoader(loader); 152 153 NamingDirector.getInstance().releaseContext(); 154 155 try{ 156 context.lookup("java:comp/env/test5"); 157 fail("Failed retrieve a value that should not exist"); 158 } catch (NameNotFoundException ex) { 159 } 161 Thread.currentThread().setContextClassLoader(original); 162 System.out.println("Add value 1"); 163 context.bind("basic","basic"); 164 System.out.println("Add value 2"); 165 context.bind("basic2/bob","basic2"); 166 if (!context.lookup("basic").equals("basic")) { 167 fail("Could not retrieve the basic value from the JNDI"); 168 } 169 if (!context.lookup("basic2/bob").equals("basic2")) { 170 fail("Could not retrieve the basic value from the JNDI"); 171 } 172 173 try { 174 context.bind("java:network/env/test","fred"); 175 fail("This should have thrown as only relative urls can be used " + 176 "JNDI"); 177 } catch (NamingException ex) { 178 } 180 181 try { 182 context.unbind("java:network/env/test"); 183 fail("This should have thrown as only relative urls can be used " + 184 "JNDI"); 185 } catch (NamingException ex) { 186 } 188 context.rebind("basic","test1"); 189 context.rebind("basic2/bob","test2"); 190 191 if (!context.lookup("basic").equals("test1")) { 192 fail("Could not retrieve the basic value from the JNDI"); 193 } 194 if (!context.lookup("basic2/bob").equals("test2")) { 195 fail("Could not retrieve the basic value from the JNDI"); 196 } 197 198 context.unbind("basic"); 199 context.unbind("basic2/bob"); 200 201 try{ 202 context.lookup("basic2/bob"); 203 fail("The basic bob value could still be found"); 204 } catch (NameNotFoundException ex) { 205 } 207 208 NamingDirector.getInstance().shutdown(); 209 } 210 211 212 } 213 | Popular Tags |