1 16 package org.apache.naming; 17 18 import java.util.Hashtable ; 19 20 import javax.naming.Context ; 21 import javax.naming.NamingException ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 import junit.textui.TestRunner; 26 import junit.framework.TestCase; 27 28 29 34 public class ContextBindingsTest extends TestCase { 35 36 public ContextBindingsTest(String name) { 37 super(name); 38 } 39 40 public static void main(String [] args) { 41 TestRunner.run(suite()); 42 } 43 44 public static Test suite() { 45 TestSuite suite = new TestSuite(ContextBindingsTest.class); 46 suite.setName("ContextBindings Tests"); 47 return suite; 48 } 49 50 protected Context testContext = null; 51 protected Context testSubContext = null; 52 protected String testToken1 = "test1"; 53 protected String testToken2 = "test2"; 54 protected String contextName = "context"; 55 protected String subName = "sub"; 56 57 public void setUp() throws Exception { 58 testContext = new SelectorContext(new Hashtable (), true); 59 testSubContext = testContext.createSubcontext("env:comp"); 60 } 61 62 public void tearDown() throws Exception { 63 ContextAccessController.unsetSecurityToken(contextName, testToken1); 64 ContextAccessController.unsetSecurityToken(subName, testToken2); 65 ContextBindings.unbindContext(contextName); 66 ContextBindings.unbindContext(subName); 67 testContext.destroySubcontext("env:comp"); 68 testContext = null; 69 } 70 71 public void testNameBindings() throws Exception { 72 ContextBindings.bindContext(contextName, testContext); 74 assertEquals(testContext, ContextBindings.getContext(contextName)); 76 assertNull(ContextBindings.getContext(subName)); 78 ContextBindings.bindContext(contextName, testSubContext); 80 assertEquals(testSubContext, ContextBindings.getContext(contextName)); 81 ContextBindings.bindContext(subName, testSubContext); 83 assertEquals(testSubContext, ContextBindings.getContext(subName)); 84 ContextBindings.unbindContext(subName); 86 assertNull(ContextBindings.getContext(subName)); 87 ContextBindings.unbindContext(contextName); 88 ContextAccessController.setSecurityToken(contextName, testToken1); 90 ContextBindings.bindContext(contextName, testContext, testToken2); 92 assertNull(ContextBindings.getContext(contextName)); 93 ContextBindings.bindContext(contextName, testContext, testToken1); 95 assertEquals(testContext, ContextBindings.getContext(contextName)); 96 ContextBindings.unbindContext(contextName, testToken2); 98 assertEquals(testContext, ContextBindings.getContext(contextName)); 99 ContextBindings.unbindContext(contextName, testToken1); 101 assertNull(ContextBindings.getContext(contextName)); 102 } 103 104 public void testThreadBindings() throws Exception { 105 try { 107 ContextBindings.bindThread(contextName); 108 fail("Expecting NamingException for unbound name"); 109 } catch (NamingException ex) { 110 } 112 try { 114 ContextBindings.getThread(); 115 fail("Expecting NamingException for unbound thread"); 116 } catch (NamingException ex) { 117 } 119 try { 121 ContextBindings.getThreadName(); 122 fail("Expecting NamingException for unbound thread"); 123 } catch (NamingException ex) { 124 } 126 ContextBindings.bindContext(contextName, testContext); 128 ContextBindings.bindThread(contextName); 129 assertTrue(ContextBindings.isThreadBound()); 130 assertEquals(testContext, ContextBindings.getThread()); 132 assertEquals(contextName, ContextBindings.getThreadName()); 133 ContextBindings.bindContext(contextName, testSubContext); 135 assertEquals(testContext, ContextBindings.getThread()); 136 ContextBindings.unbindThread(contextName); 138 assertFalse(ContextBindings.isThreadBound()); 139 ContextAccessController.setSecurityToken(contextName, testToken1); 141 ContextBindings.bindThread(contextName, testToken2); 143 assertFalse(ContextBindings.isThreadBound()); 144 ContextBindings.bindThread(contextName, testToken1); 146 assertEquals(testSubContext, ContextBindings.getThread()); 147 ContextBindings.unbindThread(contextName, testToken2); 149 assertEquals(testSubContext, ContextBindings.getThread()); 150 assertTrue(ContextBindings.isThreadBound()); 151 ContextBindings.unbindThread(contextName, testToken1); 153 assertFalse(ContextBindings.isThreadBound()); 154 } 155 156 public void testClassloaderBindings() throws Exception { 157 try { 159 ContextBindings.bindClassLoader(contextName); 160 fail("Expecting NamingException for unbound name"); 161 } catch (NamingException ex) { 162 } 164 try { 166 ContextBindings.getClassLoader(); 167 fail("Expecting NamingException for unbound classloader"); 168 } catch (NamingException ex) { 169 } 171 try { 173 ContextBindings.getClassLoaderName(); 174 fail("Expecting NamingException for unbound classloader"); 175 } catch (NamingException ex) { 176 } 178 ContextBindings.bindContext(contextName, testContext); 180 ContextBindings.bindClassLoader(contextName); 181 assertTrue(ContextBindings.isClassLoaderBound()); 182 assertEquals(testContext, ContextBindings.getClassLoader()); 184 assertEquals(contextName, ContextBindings.getClassLoaderName()); 185 ContextBindings.bindContext(contextName, testSubContext); 187 assertEquals(testContext, ContextBindings.getClassLoader()); 188 ContextBindings.unbindClassLoader(contextName); 190 assertFalse(ContextBindings.isClassLoaderBound()); 191 ContextAccessController.setSecurityToken(contextName, testToken1); 193 ContextBindings.bindClassLoader(contextName, testToken2); 195 assertFalse(ContextBindings.isClassLoaderBound()); 196 ContextBindings.bindClassLoader(contextName, testToken1); 198 assertEquals(testSubContext, ContextBindings.getClassLoader()); 199 ContextBindings.unbindClassLoader(contextName, testToken2); 201 assertEquals(testSubContext, ContextBindings.getClassLoader()); 202 assertTrue(ContextBindings.isClassLoaderBound()); 203 ContextBindings.unbindClassLoader(contextName, testToken1); 205 assertFalse(ContextBindings.isClassLoaderBound()); 206 ContextBindings.bindClassLoader(contextName, testToken1, 208 Thread.currentThread().getContextClassLoader()); 209 assertTrue(ContextBindings.isClassLoaderBound()); 210 ContextBindings.unbindClassLoader(contextName, testToken1); 211 assertFalse(ContextBindings.isClassLoaderBound()); 212 } 213 } 214 | Popular Tags |