KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004,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 import javax.naming.NameNotFoundException JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28
29 /**
30  * Unit tests for basic ops on an {@link SelectorContext}.
31  *
32  * @version $Revision: 125387 $ $Date: 2003/11/30 05:36:07 $
33  */

34 public class SelectorContextTest extends AbstractContextTest {
35     
36     public SelectorContextTest(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(SelectorContextTest.class);
46         suite.setName("Selector Context Tests");
47         return suite;
48     }
49     
50     protected Context JavaDoc makeInitialContext() {
51         return new SelectorContext(new Hashtable JavaDoc(), true);
52     }
53     
54     public void testGetNameInNamespace() throws Exception JavaDoc {
55         assertEquals(SelectorContext.prefix, initialContext.getNameInNamespace());
56     }
57         
58     public void testParseName() throws Exception JavaDoc {
59         try {
60             initialContext.lookup("java:x");
61         } catch (NameNotFoundException JavaDoc ex) {
62             // expected
63
}
64         try {
65             initialContext.lookup("hava:x");
66         } catch (NamingException JavaDoc ex) {
67             // expected
68
}
69     }
70     
71     public void testGetBoundContext() throws Exception JavaDoc {
72        
73         Context JavaDoc initCtx = new SelectorContext(new Hashtable JavaDoc(), true);
74         // Initial context should be bound under the name IC_PREFIX
75
initCtx.bind("java:comp:a", "initial");
76         assertEquals("initial", ContextBindings.getContext
77                 (SelectorContext.IC_PREFIX).lookup("java:comp:a"));
78         
79         // Bind classloader
80
Context JavaDoc clContext = new NamingContext(new Hashtable JavaDoc(), "cl");
81         clContext.bind("a", "classloader");
82         ContextBindings.bindContext("cl", clContext);
83         ContextBindings.bindClassLoader("cl");
84         
85         // Get a SelectorContext that is not an initial context
86
Context JavaDoc selCtx = new SelectorContext(new Hashtable JavaDoc(), false);
87         // lookup should strip URL header, then delegate to clContext
88
assertEquals("classloader", (String JavaDoc) selCtx.lookup("java:a")); // URL is stripped
89

90         // Now bind thread
91
Context JavaDoc threadContext = new NamingContext(new Hashtable JavaDoc(), "th");
92         threadContext.bind("a", "thread");
93         ContextBindings.bindContext("th", threadContext);
94         ContextBindings.bindThread("th");
95         // thread binding takes precedence, so threadContext should get the call
96
assertEquals("thread", (String JavaDoc) selCtx.lookup("java:a")); // URL is stripped
97

98         // Fix up so teardown does not blow
99
initialContext.createSubcontext(firstContextName());
100     }
101 }
102
Popular Tags