KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > java > BasicContextTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 package org.apache.geronimo.naming.java;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 import javax.naming.Binding JavaDoc;
26 import javax.naming.CompositeName JavaDoc;
27 import javax.naming.CompoundName JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.NameClassPair JavaDoc;
30 import javax.naming.NamingEnumeration JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 /**
34 * Unit tests for basic ops on an {@link javax.naming.InitialContext}.
35  *
36  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
37  */

38 public class BasicContextTest extends AbstractContextTest {
39
40     public void testInitialContext() throws NamingException JavaDoc {
41         assertEquals("Hello", initialContext.lookup("java:comp/env/hello"));
42         assertEquals("Hello", initialContext.lookup(new CompositeName JavaDoc("java:comp/env/hello")));
43     }
44
45     public void testLookup() throws NamingException JavaDoc {
46         assertEquals("Hello", envContext.lookup("hello"));
47         assertEquals("Hello", compContext.lookup("env/hello"));
48         try {
49             envContext.lookup("foo");
50             fail();
51         } catch (NamingException JavaDoc e) {
52             // OK
53
}
54         assertEquals("Hello", envContext.lookup(new CompositeName JavaDoc("hello")));
55         assertEquals("Hello", compContext.lookup(new CompositeName JavaDoc("env/hello")));
56         assertEquals("Hello", envContext.lookup(new CompoundName JavaDoc("hello", syntax)));
57         assertEquals("Hello", compContext.lookup(new CompoundName JavaDoc("env/hello", syntax)));
58
59         assertEquals(envContext, envContext.lookup(""));
60     }
61
62     public void testSubContext() throws NamingException JavaDoc {
63         assertEquals("long name", initialContext.lookup("java:comp/env/here/there/anywhere"));
64         Context JavaDoc intermediate = (Context JavaDoc)initialContext.lookup("java:comp/env/here/there");
65         assertNotNull(intermediate);
66         assertEquals("long name", intermediate.lookup("anywhere"));
67     }
68
69     public void testSchemeLookup() throws NamingException JavaDoc {
70 // envContext.lookup("dns:apache.org");
71
assertEquals("Hello", envContext.lookup("java:comp/env/hello"));
72         assertEquals("Hello", compContext.lookup("java:comp/env/hello"));
73     }
74
75     public void testLookupLink() throws NamingException JavaDoc {
76         assertEquals("Hello", envContext.lookup("link"));
77     }
78
79     public void testComposeName() throws NamingException JavaDoc {
80         assertEquals("org/research/user/jane", envContext.composeName("user/jane", "org/research"));
81         assertEquals("research/user/jane", envContext.composeName("user/jane", "research"));
82         assertEquals(new CompositeName JavaDoc("org/research/user/jane"), envContext.composeName(new CompositeName JavaDoc("user/jane"), new CompositeName JavaDoc("org/research")));
83         assertEquals(new CompositeName JavaDoc("research/user/jane"), envContext.composeName(new CompositeName JavaDoc("user/jane"), new CompositeName JavaDoc("research")));
84     }
85
86     public void testList() throws NamingException JavaDoc {
87         NamingEnumeration JavaDoc ne;
88         Map JavaDoc expected;
89         Map JavaDoc result;
90
91         expected = new HashMap JavaDoc();
92         for (Iterator JavaDoc i = envBinding.entrySet().iterator(); i.hasNext();) {
93             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
94             expected.put(entry.getKey(), entry.getValue().getClass().getName());
95         }
96         ne = envContext.list("");
97         result = new HashMap JavaDoc();
98         while (ne.hasMore()) {
99             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) ne.next();
100             result.put(pair.getName(), pair.getClassName());
101         }
102         assertEquals(expected, result);
103
104         try {
105             ne.next();
106             fail();
107         } catch (NoSuchElementException JavaDoc e) {
108             // ok
109
}
110         try {
111             ne.nextElement();
112             fail();
113         } catch (NoSuchElementException JavaDoc e) {
114             // ok
115
}
116     }
117
118     public void testListBindings() throws NamingException JavaDoc {
119         NamingEnumeration JavaDoc ne;
120         ne = envContext.listBindings("");
121         int count = 0;
122         while (ne.hasMore()) {
123             count ++;
124             Binding JavaDoc pair = (Binding JavaDoc) ne.next();
125             assertTrue(envBinding.containsKey(pair.getName()));
126             if (! (envBinding.get(pair.getName()) instanceof ReadOnlyContext)) {
127                 assertEquals(pair.getObject(), envBinding.get(pair.getName()));
128             }
129         }
130         assertEquals(envBinding.size(), count);
131
132         try {
133             ne.next();
134             fail();
135         } catch (NoSuchElementException JavaDoc e) {
136             // ok
137
}
138         try {
139             ne.nextElement();
140             fail();
141         } catch (NoSuchElementException JavaDoc e) {
142             // ok
143
}
144     }
145
146     public void testSpeed() throws NamingException JavaDoc {
147         Context JavaDoc comp = (Context JavaDoc) initialContext.lookup("java:comp");
148
149         long start = System.currentTimeMillis();
150         for (int i=0; i < 1000000; i++) {
151             // initialContext.lookup("java:comp/hello"); // this is sloooow due to scheme resolution
152
// envContext.lookup("hello");
153
comp.lookup("env/hello");
154         }
155
156         long end = System.currentTimeMillis();
157         System.out.println("lookup(String) milliseconds: " + (end - start));
158     }
159
160 }
161
Popular Tags