KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jndi > SimpleNamingContextTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
17 package org.springframework.jndi;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.naming.Binding JavaDoc;
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NameClassPair JavaDoc;
27 import javax.naming.NameNotFoundException JavaDoc;
28 import javax.naming.NamingEnumeration JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.naming.spi.InitialContextFactory JavaDoc;
31 import javax.sql.DataSource JavaDoc;
32
33 import junit.framework.TestCase;
34
35 import org.springframework.jdbc.datasource.DriverManagerDataSource;
36 import org.springframework.mock.jndi.SimpleNamingContext;
37 import org.springframework.mock.jndi.SimpleNamingContextBuilder;
38
39 /**
40  * @author Juergen Hoeller
41  */

42 public class SimpleNamingContextTests extends TestCase {
43
44     public SimpleNamingContextTests(String JavaDoc s) {
45         super(s);
46     }
47
48     public void testNamingContextBuilder() throws NamingException JavaDoc {
49         SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
50         InitialContextFactory JavaDoc factory = builder.createInitialContextFactory(null);
51
52         DataSource JavaDoc ds = new DriverManagerDataSource();
53         builder.bind("java:comp/env/jdbc/myds", ds);
54         Object JavaDoc obj = new Object JavaDoc();
55         builder.bind("myobject", obj);
56
57         Context JavaDoc context1 = factory.getInitialContext(null);
58         assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds);
59         assertTrue("Correct Object registered", context1.lookup("myobject") == obj);
60
61         Hashtable JavaDoc env2 = new Hashtable JavaDoc();
62         env2.put("key1", "value1");
63         Context JavaDoc context2 = factory.getInitialContext(env2);
64         assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds);
65         assertTrue("Correct Object registered", context2.lookup("myobject") == obj);
66         assertTrue("Correct environment", context2.getEnvironment() != env2);
67         assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1")));
68
69         Integer JavaDoc i = new Integer JavaDoc(0);
70         context1.rebind("myinteger", i);
71         String JavaDoc s = "";
72         context2.bind("mystring", s);
73
74         Context JavaDoc context3 = (Context JavaDoc) context2.lookup("");
75         context3.rename("java:comp/env/jdbc/myds", "jdbc/myds");
76         context3.unbind("myobject");
77
78         assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment());
79         context3.addToEnvironment("key2", "value2");
80         assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2")));
81         context3.removeFromEnvironment("key1");
82         assertTrue("key1 removed", context3.getEnvironment().get("key1") == null);
83
84         assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds);
85         try {
86             context1.lookup("myobject");
87             fail("Should have thrown NameNotFoundException");
88         }
89         catch (NameNotFoundException JavaDoc ex) {
90             // expected
91
}
92         assertTrue("Correct Integer registered", context1.lookup("myinteger") == i);
93         assertTrue("Correct String registered", context1.lookup("mystring") == s);
94
95         assertTrue("Correct DataSource registered", context2.lookup("jdbc/myds") == ds);
96         try {
97             context2.lookup("myobject");
98             fail("Should have thrown NameNotFoundException");
99         }
100         catch (NameNotFoundException JavaDoc ex) {
101             // expected
102
}
103         assertTrue("Correct Integer registered", context2.lookup("myinteger") == i);
104         assertTrue("Correct String registered", context2.lookup("mystring") == s);
105
106         assertTrue("Correct DataSource registered", context3.lookup("jdbc/myds") == ds);
107         try {
108             context3.lookup("myobject");
109             fail("Should have thrown NameNotFoundException");
110         }
111         catch (NameNotFoundException JavaDoc ex) {
112             // expected
113
}
114         assertTrue("Correct Integer registered", context3.lookup("myinteger") == i);
115         assertTrue("Correct String registered", context3.lookup("mystring") == s);
116
117         Map JavaDoc bindingMap = new HashMap JavaDoc();
118         NamingEnumeration JavaDoc bindingEnum = context3.listBindings("");
119         while (bindingEnum.hasMoreElements()) {
120             Binding JavaDoc binding = (Binding JavaDoc) bindingEnum.nextElement();
121             bindingMap.put(binding.getName(), binding);
122         }
123         assertTrue("Correct jdbc subcontext", ((Binding JavaDoc) bindingMap.get("jdbc")).getObject() instanceof Context JavaDoc);
124         assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(((Binding JavaDoc) bindingMap.get("jdbc")).getClassName()));
125
126         Context JavaDoc jdbcContext = (Context JavaDoc) context3.lookup("jdbc");
127         jdbcContext.bind("mydsX", ds);
128         Map JavaDoc subBindingMap = new HashMap JavaDoc();
129         NamingEnumeration JavaDoc subBindingEnum = jdbcContext.listBindings("");
130         while (subBindingEnum.hasMoreElements()) {
131             Binding JavaDoc binding = (Binding JavaDoc) subBindingEnum.nextElement();
132             subBindingMap.put(binding.getName(), binding);
133         }
134
135         assertTrue("Correct DataSource registered", ds.equals(((Binding JavaDoc) subBindingMap.get("myds")).getObject()));
136         assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding JavaDoc) subBindingMap.get("myds")).getClassName()));
137         assertTrue("Correct DataSource registered", ds.equals(((Binding JavaDoc) subBindingMap.get("mydsX")).getObject()));
138         assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding JavaDoc) subBindingMap.get("mydsX")).getClassName()));
139         assertTrue("Correct Integer registered", i.equals(((Binding JavaDoc) bindingMap.get("myinteger")).getObject()));
140         assertTrue("Correct Integer registered", Integer JavaDoc.class.getName().equals(((Binding JavaDoc) bindingMap.get("myinteger")).getClassName()));
141         assertTrue("Correct String registered", s.equals(((Binding JavaDoc) bindingMap.get("mystring")).getObject()));
142         assertTrue("Correct String registered", String JavaDoc.class.getName().equals(((Binding JavaDoc) bindingMap.get("mystring")).getClassName()));
143
144         context1.bind("jdbc/sub/subds", ds);
145
146         Map JavaDoc pairMap = new HashMap JavaDoc();
147         NamingEnumeration JavaDoc pairEnum = context2.list("jdbc");
148         while (pairEnum.hasMore()) {
149             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) pairEnum.next();
150             pairMap.put(pair.getName(), pair.getClassName());
151         }
152         assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub")));
153
154         Context JavaDoc subContext = (Context JavaDoc) context2.lookup("jdbc/sub");
155         Map JavaDoc subPairMap = new HashMap JavaDoc();
156         NamingEnumeration JavaDoc subPairEnum = subContext.list("");
157         while (subPairEnum.hasMoreElements()) {
158             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) subPairEnum.next();
159             subPairMap.put(pair.getName(), pair.getClassName());
160         }
161
162         assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(subPairMap.get("subds")));
163         assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("myds")));
164         assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("mydsX")));
165
166         pairMap.clear();
167         pairEnum = context1.list("jdbc/");
168         while (pairEnum.hasMore()) {
169             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) pairEnum.next();
170             pairMap.put(pair.getName(), pair.getClassName());
171         }
172         assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("myds")));
173         assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("mydsX")));
174     }
175     
176     /**
177      * Demonstrates how emptyActivatedContextBuilder() method can be
178      * used repeatedly, and how it affects creating a new InitialContext()
179      * @throws Exception
180      */

181     public void testCreateInitialContext() throws Exception JavaDoc {
182         SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
183         String JavaDoc name = "foo";
184         Object JavaDoc o = new Object JavaDoc();
185         builder.bind(name, o);
186         // Check it affects JNDI
187
Context JavaDoc ctx = new InitialContext JavaDoc();
188         assertTrue(ctx.lookup(name) == o);
189         // Check it returns mutable contexts
190
ctx.unbind(name);
191         try {
192             ctx = new InitialContext JavaDoc();
193             ctx.lookup(name);
194             fail("Should have thrown NamingException");
195         }
196         catch (NamingException JavaDoc ex) {
197             // expected
198
}
199         
200         // Check the same call will work again, but the context is empty
201
builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
202         try {
203             ctx = new InitialContext JavaDoc();
204             ctx.lookup(name);
205             fail("Should have thrown NamingException");
206         }
207         catch (NamingException JavaDoc ex) {
208             // expected
209
}
210         Object JavaDoc o2 = new Object JavaDoc();
211         builder.bind(name, o2);
212         assertEquals(ctx.lookup(name), o2);
213     }
214
215 }
216
Popular Tags