KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > java > JavaURLContextFactoryTest


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.java;
17
18 import java.util.Hashtable JavaDoc;
19 import javax.naming.Context JavaDoc;
20
21 import org.apache.naming.ContextBindings;
22 import org.apache.naming.NamingContext;
23 import org.apache.naming.SelectorContext;
24 import org.apache.naming.SynchronizedContext;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import junit.textui.TestRunner;
30
31 /**
32  * Tests for JavaURLContextFactory
33  *
34  */

35 public class JavaURLContextFactoryTest extends TestCase {
36     
37     public JavaURLContextFactoryTest(String JavaDoc name) {
38         super(name);
39     }
40     
41     public static void main(String JavaDoc[] args) {
42         TestRunner.run(suite());
43     }
44
45     public static Test suite() {
46         TestSuite suite = new TestSuite(JavaURLContextFactoryTest.class);
47         suite.setName("JavaURLContextFactory Tests");
48         return suite;
49     }
50     
51     protected javaURLContextFactory contextFactory = new javaURLContextFactory();
52     
53     public void testGetInitialContext() throws Exception JavaDoc {
54         // nothing bound, empty environment -> NamingContext
55
Hashtable JavaDoc env = new Hashtable JavaDoc();
56         Context JavaDoc ctx = contextFactory.getInitialContext(env);
57         assertTrue(ctx instanceof NamingContext);
58         ContextBindings.bindContext("ctxName", ctx);
59         
60         // bind thread -> SelectorContext
61
ContextBindings.bindThread("ctxName");
62         Context JavaDoc selCtx = contextFactory.getInitialContext(env);
63         assertTrue(selCtx instanceof SelectorContext);
64         
65         // add synch property to environment, but still thread bound
66
// -> SelectorContext
67
env.put(SynchronizedContext.SYNCHRONIZED, "true");
68         Context JavaDoc unSynchCtx = contextFactory.getInitialContext(env);
69         assertTrue(unSynchCtx instanceof SelectorContext);
70         
71         // unbind thread, but no synch prop -> NamingContext
72
ContextBindings.unbindThread("ctxName");
73         unSynchCtx = contextFactory.getInitialContext(env);
74         assertTrue(unSynchCtx instanceof NamingContext);
75         // need to null shared context; otherwise will be returned again
76
javaURLContextFactory.initialContext = null;
77         
78         // unbound thread, synch property -> SynchronizedContext
79
env.put(SynchronizedContext.SYNCHRONIZED, "true");
80         Context JavaDoc synchCtx = contextFactory.getInitialContext(env);
81         assertTrue(synchCtx instanceof SynchronizedContext);
82     }
83     
84     public void testGetObjectInstance() throws Exception JavaDoc {
85         // Create and name a context
86
Hashtable JavaDoc env = new Hashtable JavaDoc();
87         Context JavaDoc ctx = contextFactory.getInitialContext(env);
88         ContextBindings.bindContext("ctxName", ctx);
89         
90         // Nothing bound -> null returned
91
Object JavaDoc obj = contextFactory.getObjectInstance(null, null, null, env);
92         assertNull(obj);
93         
94         // bind thread -> SelectorContext
95
ContextBindings.bindThread("ctxName");
96         Context JavaDoc getCtx = (Context JavaDoc) contextFactory.getObjectInstance(null, null, null, env);
97         assertTrue(getCtx instanceof SelectorContext);
98     }
99 }
100
Popular Tags