KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003-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.HashMap JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import javax.naming.Binding JavaDoc;
25 import javax.naming.CompositeName JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NameClassPair JavaDoc;
29 import javax.naming.NamingEnumeration JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35 import junit.textui.TestRunner;
36
37 import org.apache.naming.java.javaURLContextFactory;
38
39 /**
40  * Unit tests for basic ops on an {@link NamingContext}.
41  * Adapted from o.a.geronimo.naming test of the same name
42  *
43  * @version $Revision: 125387 $ $Date: 2005-01-16 22:36:57 -0500 (Sun, 16 Jan 2005) $
44  */

45 public class BasicContextTest extends TestCase {
46     private HashMap JavaDoc envBinding;
47     private Context JavaDoc initialContext;
48     private Context JavaDoc compContext;
49     private Context JavaDoc envContext;
50     
51     public BasicContextTest(String JavaDoc name) {
52         super(name);
53     }
54
55     public static void main(String JavaDoc[] args) {
56         TestRunner.run(suite());
57     }
58
59     public static Test suite() {
60         TestSuite suite = new TestSuite(BasicContextTest.class);
61         suite.setName("Basic Context Tests");
62         return suite;
63     }
64     
65     protected void setUp() throws Exception JavaDoc {
66         super.setUp();
67         Hashtable JavaDoc env = new Hashtable JavaDoc();
68         envBinding = new HashMap JavaDoc();
69         env.put(Context.INITIAL_CONTEXT_FACTORY,
70             "org.apache.naming.java.javaURLContextFactory");
71         env.put(Context.URL_PKG_PREFIXES,"org.apache.naming");
72         initialContext = new InitialContext JavaDoc(env);
73         compContext = initialContext.createSubcontext("java:comp");
74         envContext = compContext.createSubcontext("env");
75         envContext.bind("hello", "Hello");
76         envContext.bind("world", "World");
77         envBinding.put("hello", "Hello");
78         envBinding.put("world", "World");
79         ContextAccessController.setReadOnly(javaURLContextFactory.MAIN);
80         ContextAccessController.setSecurityToken(javaURLContextFactory.MAIN,"x");
81     }
82     
83     protected void tearDown() throws Exception JavaDoc {
84         ContextAccessController.setWritable(javaURLContextFactory.MAIN,"x");
85         compContext.destroySubcontext("env");
86         initialContext.destroySubcontext("java:comp");
87         initialContext = null;
88     }
89
90     public void testInitialContext() throws NamingException JavaDoc {
91         assertEquals("Hello", initialContext.lookup("java:comp/env/hello"));
92         assertEquals("World", initialContext.lookup(new CompositeName JavaDoc("java:comp/env/world")));
93         assertEquals(envContext.lookup("hello"),
94             ((Context JavaDoc) envContext.lookup("")).lookup("hello"));
95     }
96
97     public void testLookup() throws NamingException JavaDoc {
98         assertEquals("Hello", envContext.lookup("hello"));
99         assertEquals("Hello", compContext.lookup("env/hello"));
100         try {
101             envContext.lookup("foo");
102             fail("expecting NamingException");
103         } catch (NamingException JavaDoc e) {
104             // OK
105
}
106         assertEquals("Hello", envContext.lookup(new CompositeName JavaDoc("hello")));
107         assertEquals("Hello", compContext.lookup(new CompositeName JavaDoc("env/hello")));
108         assertEquals("World",
109             ((Context JavaDoc) initialContext.lookup("java:comp")).lookup("env/world"));
110     }
111     
112
113     public void testComposeName() throws NamingException JavaDoc {
114         assertEquals("org/research/user/jane",
115             envContext.composeName("user/jane", "org/research"));
116         assertEquals("research/user/jane",
117             envContext.composeName("user/jane", "research"));
118         assertEquals(new CompositeName JavaDoc("org/research/user/jane"),
119             envContext.composeName(new CompositeName JavaDoc("user/jane"),
120                 new CompositeName JavaDoc("org/research")));
121         assertEquals(new CompositeName JavaDoc("research/user/jane"),
122             envContext.composeName(new CompositeName JavaDoc("user/jane"),
123                 new CompositeName JavaDoc("research")));
124     }
125
126     public void testList() throws NamingException JavaDoc {
127         NamingEnumeration JavaDoc enumeration;
128         Map JavaDoc expected;
129         Map JavaDoc result;
130
131         expected = new HashMap JavaDoc();
132         for (Iterator JavaDoc i = envBinding.entrySet().iterator(); i.hasNext();) {
133             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
134             expected.put(entry.getKey(), entry.getValue().getClass().getName());
135         }
136         enumeration = envContext.list("");
137         result = new HashMap JavaDoc();
138         while (enumeration.hasMore()) {
139             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) enumeration.next();
140             result.put(pair.getName(), pair.getClassName());
141         }
142         assertEquals(expected, result);
143
144         try {
145             enumeration.next();
146             fail();
147         } catch (NoSuchElementException JavaDoc e) {
148             // ok
149
}
150         try {
151             enumeration.nextElement();
152             fail();
153         } catch (NoSuchElementException JavaDoc e) {
154             // ok
155
}
156     }
157
158     public void testListBindings() throws NamingException JavaDoc {
159         NamingEnumeration JavaDoc enumeration;
160         Map JavaDoc result;
161         enumeration = envContext.listBindings("");
162         result = new HashMap JavaDoc();
163         while (enumeration.hasMore()) {
164             Binding JavaDoc pair = (Binding JavaDoc) enumeration.next();
165             result.put(pair.getName(), pair.getObject());
166         }
167         assertEquals(envBinding, result);
168
169         try {
170             enumeration.next();
171             fail();
172         } catch (NoSuchElementException JavaDoc e) {
173             // ok
174
}
175         try {
176             enumeration.nextElement();
177             fail();
178         } catch (NoSuchElementException JavaDoc e) {
179             // ok
180
}
181     }
182     
183     public void testAccess() throws NamingException JavaDoc {
184         try {
185             envContext.bind("goodbye", "Goodbye");
186             fail("expecting NamingException"); // Context is read only
187
} catch (NamingException JavaDoc ex) {}
188         ContextAccessController.setWritable(javaURLContextFactory.MAIN,"x");
189         envContext.bind("goodbye", "Goodbye"); // Unlocked now
190
}
191 }
192
Popular Tags