KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > ContextBindingsTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.naming;
17
18 import java.util.Hashtable JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26 import junit.framework.TestCase;
27
28
29 /**
30  * Unit tests for {@link ContextBindings}.
31  *
32  * @version $Revision: 125387 $ $Date: 2003/11/30 05:36:07 $
33  */

34 public class ContextBindingsTest extends TestCase {
35     
36     public ContextBindingsTest(String JavaDoc name) {
37         super(name);
38     }
39
40     public static void main(String JavaDoc[] 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 JavaDoc testContext = null;
51     protected Context JavaDoc testSubContext = null;
52     protected String JavaDoc testToken1 = "test1";
53     protected String JavaDoc testToken2 = "test2";
54     protected String JavaDoc contextName = "context";
55     protected String JavaDoc subName = "sub";
56     
57     public void setUp() throws Exception JavaDoc {
58         testContext = new SelectorContext(new Hashtable JavaDoc(), true);
59         testSubContext = testContext.createSubcontext("env:comp");
60     }
61     
62     public void tearDown() throws Exception JavaDoc {
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 JavaDoc {
72        // bind an unsecured context
73
ContextBindings.bindContext(contextName, testContext);
74        // test lookup
75
assertEquals(testContext, ContextBindings.getContext(contextName));
76        // lookup on unbound name returns null
77
assertNull(ContextBindings.getContext(subName));
78        // overwrite
79
ContextBindings.bindContext(contextName, testSubContext);
80        assertEquals(testSubContext, ContextBindings.getContext(contextName));
81        // two names are OK
82
ContextBindings.bindContext(subName, testSubContext);
83        assertEquals(testSubContext, ContextBindings.getContext(subName));
84        //unbind
85
ContextBindings.unbindContext(subName);
86        assertNull(ContextBindings.getContext(subName));
87        ContextBindings.unbindContext(contextName);
88        // secure and rebind
89
ContextAccessController.setSecurityToken(contextName, testToken1);
90        // wrong token does nothing
91
ContextBindings.bindContext(contextName, testContext, testToken2);
92        assertNull(ContextBindings.getContext(contextName));
93        // good token works
94
ContextBindings.bindContext(contextName, testContext, testToken1);
95        assertEquals(testContext, ContextBindings.getContext(contextName));
96        // unbind fails silently with wrong token
97
ContextBindings.unbindContext(contextName, testToken2);
98        assertEquals(testContext, ContextBindings.getContext(contextName));
99        // good token works
100
ContextBindings.unbindContext(contextName, testToken1);
101        assertNull(ContextBindings.getContext(contextName));
102    }
103    
104    public void testThreadBindings() throws Exception JavaDoc {
105        // unbound name should generate NamingExeption for bindThread
106
try {
107            ContextBindings.bindThread(contextName);
108            fail("Expecting NamingException for unbound name");
109        } catch (NamingException JavaDoc ex) {
110            // expected
111
}
112        // unbound thread should generate NamingExeption for getThread
113
try {
114            ContextBindings.getThread();
115            fail("Expecting NamingException for unbound thread");
116        } catch (NamingException JavaDoc ex) {
117            // expected
118
}
119        // unbound thread should generate NamingExeption for getThreadName
120
try {
121            ContextBindings.getThreadName();
122            fail("Expecting NamingException for unbound thread");
123        } catch (NamingException JavaDoc ex) {
124            // expected
125
}
126        // now bind name and bind thread to context
127
ContextBindings.bindContext(contextName, testContext);
128        ContextBindings.bindThread(contextName);
129        assertTrue(ContextBindings.isThreadBound());
130        // test lookup
131
assertEquals(testContext, ContextBindings.getThread());
132        assertEquals(contextName, ContextBindings.getThreadName());
133        // switch name binding -- thread binding does not change
134
ContextBindings.bindContext(contextName, testSubContext);
135        assertEquals(testContext, ContextBindings.getThread());
136        //unbind
137
ContextBindings.unbindThread(contextName);
138        assertFalse(ContextBindings.isThreadBound());
139        // secure and rebind
140
ContextAccessController.setSecurityToken(contextName, testToken1);
141        // wrong token does nothing
142
ContextBindings.bindThread(contextName, testToken2);
143        assertFalse(ContextBindings.isThreadBound());
144        // good token works
145
ContextBindings.bindThread(contextName, testToken1);
146        assertEquals(testSubContext, ContextBindings.getThread());
147        // unbind fails silently with wrong token
148
ContextBindings.unbindThread(contextName, testToken2);
149        assertEquals(testSubContext, ContextBindings.getThread());
150        assertTrue(ContextBindings.isThreadBound());
151        // good token works
152
ContextBindings.unbindThread(contextName, testToken1);
153        assertFalse(ContextBindings.isThreadBound());
154    }
155    
156    public void testClassloaderBindings() throws Exception JavaDoc {
157        // unbound name should generate NamingExeption for bindClassLoader
158
try {
159            ContextBindings.bindClassLoader(contextName);
160            fail("Expecting NamingException for unbound name");
161        } catch (NamingException JavaDoc ex) {
162            // expected
163
}
164        // unbound cl should generate NamingExeption for getClassLoader
165
try {
166            ContextBindings.getClassLoader();
167            fail("Expecting NamingException for unbound classloader");
168        } catch (NamingException JavaDoc ex) {
169            // expected
170
}
171        // unbound cl should generate NamingExeption for getClassLoaderName
172
try {
173            ContextBindings.getClassLoaderName();
174            fail("Expecting NamingException for unbound classloader");
175        } catch (NamingException JavaDoc ex) {
176            // expected
177
}
178        // now bind name and indirectly bind cl to context
179
ContextBindings.bindContext(contextName, testContext);
180        ContextBindings.bindClassLoader(contextName);
181        assertTrue(ContextBindings.isClassLoaderBound());
182        // test lookup
183
assertEquals(testContext, ContextBindings.getClassLoader());
184        assertEquals(contextName, ContextBindings.getClassLoaderName());
185        // switch name binding -- cl binding does not change
186
ContextBindings.bindContext(contextName, testSubContext);
187        assertEquals(testContext, ContextBindings.getClassLoader());
188        //unbind
189
ContextBindings.unbindClassLoader(contextName);
190        assertFalse(ContextBindings.isClassLoaderBound());
191        // secure and rebind
192
ContextAccessController.setSecurityToken(contextName, testToken1);
193        // wrong token does nothing
194
ContextBindings.bindClassLoader(contextName, testToken2);
195        assertFalse(ContextBindings.isClassLoaderBound());
196        // good token works
197
ContextBindings.bindClassLoader(contextName, testToken1);
198        assertEquals(testSubContext, ContextBindings.getClassLoader());
199        // unbind fails silently with wrong token
200
ContextBindings.unbindClassLoader(contextName, testToken2);
201        assertEquals(testSubContext, ContextBindings.getClassLoader());
202        assertTrue(ContextBindings.isClassLoaderBound());
203        // good token works
204
ContextBindings.unbindClassLoader(contextName, testToken1);
205        assertFalse(ContextBindings.isClassLoaderBound());
206        // bind current thread classloader explicitly
207
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