KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gjndi > JavaCompContextTest


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

17 package org.apache.geronimo.gjndi;
18
19 import junit.framework.TestCase;
20
21 import javax.naming.NamingException JavaDoc;
22 import javax.naming.CompositeName JavaDoc;
23 import javax.naming.CompoundName JavaDoc;
24 import javax.naming.Context JavaDoc;
25 import javax.naming.NamingEnumeration JavaDoc;
26 import javax.naming.NameClassPair JavaDoc;
27 import javax.naming.Binding JavaDoc;
28 import javax.naming.LinkRef JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.NoSuchElementException JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Collections JavaDoc;
36
37 import org.apache.geronimo.naming.java.RootContext;
38 import org.apache.xbean.naming.context.ImmutableContext;
39 import org.apache.xbean.naming.context.WritableContext;
40 import org.apache.xbean.naming.context.ContextAccess;
41 import org.apache.xbean.naming.global.GlobalContextManager;
42
43 /**
44  * @version $Rev$ $Date$
45  */

46 public class JavaCompContextTest extends TestCase {
47     protected Context JavaDoc readOnlyContext;
48     protected Properties JavaDoc syntax;
49     protected Map JavaDoc envBinding;
50     protected Context JavaDoc initialContext;
51     protected Context JavaDoc compContext;
52     protected Context JavaDoc envContext;
53
54     public void testInitialContext() throws NamingException JavaDoc {
55         assertEquals("Hello", initialContext.lookup("java:comp/env/hello"));
56         assertEquals("Hello", initialContext.lookup(new CompositeName JavaDoc("java:comp/env/hello")));
57     }
58
59     public void testLookup() throws NamingException JavaDoc {
60         assertEquals("Hello", envContext.lookup("hello"));
61         assertEquals("Hello", compContext.lookup("env/hello"));
62         try {
63             envContext.lookup("foo");
64             fail();
65         } catch (NamingException JavaDoc e) {
66             // OK
67
}
68         assertEquals("Hello", envContext.lookup(new CompositeName JavaDoc("hello")));
69         assertEquals("Hello", compContext.lookup(new CompositeName JavaDoc("env/hello")));
70         assertEquals("Hello", envContext.lookup(new CompoundName JavaDoc("hello", syntax)));
71         assertEquals("Hello", compContext.lookup(new CompoundName JavaDoc("env/hello", syntax)));
72
73         assertEquals(envContext, envContext.lookup(""));
74     }
75
76     public void testSubContext() throws NamingException JavaDoc {
77         assertEquals("long name", initialContext.lookup("java:comp/env/here/there/anywhere"));
78         Context JavaDoc intermediate = (Context JavaDoc)initialContext.lookup("java:comp/env/here/there");
79         assertNotNull(intermediate);
80         assertEquals("long name", intermediate.lookup("anywhere"));
81     }
82
83     public void testSchemeLookup() throws NamingException JavaDoc {
84 // envContext.lookup("dns:apache.org");
85
assertEquals("Hello", envContext.lookup("java:comp/env/hello"));
86         assertEquals("Hello", compContext.lookup("java:comp/env/hello"));
87     }
88
89     public void testLookupLink() throws NamingException JavaDoc {
90         assertEquals("Hello", envContext.lookup("link"));
91     }
92
93     public void testComposeName() throws NamingException JavaDoc {
94         assertEquals("org/research/user/jane", envContext.composeName("user/jane", "org/research"));
95         assertEquals("research/user/jane", envContext.composeName("user/jane", "research"));
96         assertEquals(new CompositeName JavaDoc("org/research/user/jane"), envContext.composeName(new CompositeName JavaDoc("user/jane"), new CompositeName JavaDoc("org/research")));
97         assertEquals(new CompositeName JavaDoc("research/user/jane"), envContext.composeName(new CompositeName JavaDoc("user/jane"), new CompositeName JavaDoc("research")));
98     }
99
100     public void testList() throws NamingException JavaDoc {
101         NamingEnumeration JavaDoc ne;
102         Map JavaDoc expected;
103         Map JavaDoc result;
104
105         expected = new HashMap JavaDoc();
106         for (Iterator JavaDoc i = envBinding.entrySet().iterator(); i.hasNext();) {
107             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
108             expected.put(entry.getKey(), entry.getValue().getClass().getName());
109         }
110         ne = envContext.list("");
111         result = new HashMap JavaDoc();
112         while (ne.hasMore()) {
113             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) ne.next();
114             result.put(pair.getName(), pair.getClassName());
115         }
116         assertEquals(expected, result);
117
118         try {
119             ne.next();
120             fail();
121         } catch (NoSuchElementException JavaDoc e) {
122             // ok
123
}
124         try {
125             ne.nextElement();
126             fail();
127         } catch (NoSuchElementException JavaDoc e) {
128             // ok
129
}
130     }
131
132     public void testListBindings() throws NamingException JavaDoc {
133         NamingEnumeration JavaDoc ne;
134         ne = envContext.listBindings("");
135         int count = 0;
136         while (ne.hasMore()) {
137             count ++;
138             Binding JavaDoc pair = (Binding JavaDoc) ne.next();
139             assertTrue(envBinding.containsKey(pair.getName()));
140             if (! (envBinding.get(pair.getName()) instanceof Context JavaDoc)) {
141                 assertEquals(pair.getObject(), envBinding.get(pair.getName()));
142             }
143         }
144         assertEquals(envBinding.size(), count);
145
146         try {
147             ne.next();
148             fail();
149         } catch (NoSuchElementException JavaDoc e) {
150             // ok
151
}
152         try {
153             ne.nextElement();
154             fail();
155         } catch (NoSuchElementException JavaDoc e) {
156             // ok
157
}
158     }
159
160     public void testSpeed() throws NamingException JavaDoc {
161         Context JavaDoc comp = (Context JavaDoc) initialContext.lookup("java:comp");
162
163         long start = System.currentTimeMillis();
164         for (int i=0; i < 1000000; i++) {
165             // initialContext.lookup("java:comp/hello"); // this is sloooow due to scheme resolution
166
// envContext.lookup("hello");
167
comp.lookup("env/hello");
168         }
169
170         long end = System.currentTimeMillis();
171         System.out.println("lookup(String) milliseconds: " + (end - start));
172     }
173
174     protected void setUp() throws Exception JavaDoc {
175         super.setUp();
176         System.setProperty("java.naming.factory.initial", GlobalContextManager.class.getName());
177         System.setProperty("java.naming.factory.url.pkgs", "org.apache.geronimo.knaming");
178
179         LinkRef JavaDoc link = new LinkRef JavaDoc("java:comp/env/hello");
180
181         Map JavaDoc bindings = new HashMap JavaDoc();
182         bindings.put("env/hello", "Hello");
183         bindings.put("env/world", "Hello World");
184         bindings.put("env/here/there/anywhere", "long name");
185         bindings.put("env/link", link);
186
187         readOnlyContext = new WritableContext("", bindings, ContextAccess.UNMODIFIABLE);
188
189         envBinding = new HashMap JavaDoc();
190         envBinding.put("hello", "Hello");
191         envBinding.put("world", "Hello World");
192         envBinding.put("here", readOnlyContext.lookup("env/here"));
193         envBinding.put("link", link);
194
195         RootContext.setComponentContext(readOnlyContext);
196
197         Context JavaDoc javaCompContext = new JavaCompContextGBean();
198         Context JavaDoc globalContext = new ImmutableContext(Collections.singletonMap(javaCompContext.getNameInNamespace(), javaCompContext));
199         GlobalContextManager.setGlobalContext(globalContext);
200
201         initialContext = new InitialContext JavaDoc();
202         compContext = (Context JavaDoc) initialContext.lookup("java:comp");
203         envContext = (Context JavaDoc) initialContext.lookup("java:comp/env");
204
205         syntax = new Properties JavaDoc();
206     }
207 }
208
Popular Tags