KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004,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.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 import javax.naming.Binding JavaDoc;
24 import javax.naming.CompositeName JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NameClassPair JavaDoc;
27 import javax.naming.NamingEnumeration JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.OperationNotSupportedException JavaDoc;
30
31 import junit.framework.TestCase;
32
33 /**
34  * Abstract base class for Context tests.
35  *
36  * Test classes derived from this class must implement makeInitialContext().
37  * If the context is not writable, override isWritable() to return false.
38  * If getNameInNamespace() is not supported, override isGetNameInNamespace() to
39  * return false.
40  *
41  * For writable contexts, the default setup() implementation creates:
42  *
43  * -- a subcontext (firstContext) of the InitialContext returned by makeInitialContext(),
44  * using firstContextName(), which defaults to "java:comp"
45  * -- a subcontext (secondContext) of firstContext (secondContext) using secondContextName() (default = "env")
46  * -- two object bindings on secondContext: firstBoundObject() bound to firstBoundName() and
47  * secondBoundObject() bound to secondBoundName()
48  *
49  * Basic tests included verify binding, context lookup, name composition, and list operations.
50  *
51  * @version $Revision: 125387 $ $Date: 2005-01-16 22:36:57 -0500 (Sun, 16 Jan 2005) $
52  */

53 public abstract class AbstractContextTest extends TestCase {
54     
55     public AbstractContextTest(String JavaDoc name) {
56         super(name);
57     }
58     
59     //-------------------------- Contexts to use in tests ----------------------------------------------------
60

61     /** Initial Context used in tests */
62     protected Context JavaDoc initialContext;
63     
64     /** Immediate subcontext of initialContext */
65     protected Context JavaDoc firstContext;
66     
67     /** Immediate subcontext of firstContext */
68     protected Context JavaDoc secondContext;
69     
70     /** HashMap of Object Bindings for verification */
71     protected HashMap JavaDoc binding;
72     
73     //-------------------- Override these methods to set up test namespace ------------------------
74

75     /** firstContext name -- relative to InitialContext */
76     protected String JavaDoc firstContextName() {
77         return "java:comp";
78     }
79     
80     /** secondContext name -- relative to first context */
81     protected String JavaDoc secondContextName() {
82        return "env";
83     }
84     
85     /** First name to bind */
86     protected String JavaDoc firstBoundName() {
87         return "hello";
88     }
89     
90     /** First bound object */
91     protected Object JavaDoc firstBoundObject() {
92         return "Hello";
93     }
94     
95     /** Second name to bind */
96     protected String JavaDoc secondBoundName() {
97         return "world";
98     }
99     
100     /** Second bound object */
101     protected Object JavaDoc secondBoundObject() {
102        return "World";
103     }
104     
105     //----------------- Switches to turn off tests for unsupported operations ----------------------
106

107     /**
108      * Does this context support getNameInNamespace()?
109      * Defaults to true -- override if not supported
110      */

111     protected boolean isGetNameInNamespaceSupported() {
112         return true;
113     }
114     
115     /**
116      * Can bindings be added to this context?
117      * Defaults to true -- override if not supported
118      */

119     protected boolean isWritable() {
120         return true;
121     }
122     
123     /**
124      * Create an initial context to be used in tests
125      */

126     protected abstract Context JavaDoc makeInitialContext();
127     
128     //---------------------------------- Setup / teardown operations -----------------------------
129

130     /**
131      * Add bindings
132      */

133     protected void addBindings() throws Exception JavaDoc {
134         secondContext.bind(firstBoundName(), firstBoundObject());
135         secondContext.bind(secondBoundName(), secondBoundObject());
136         binding.put(firstBoundName(), firstBoundObject());
137         binding.put(secondBoundName(), secondBoundObject());
138     }
139     
140     /**
141      * Remove bindings
142      */

143     protected void removeBindings() throws Exception JavaDoc {
144         secondContext.unbind(firstBoundName());
145         secondContext.unbind(secondBoundName());
146         binding.clear();
147     }
148     
149     protected void setUp() throws Exception JavaDoc {
150         super.setUp();
151         binding = new HashMap JavaDoc();
152         initialContext = makeInitialContext();
153         if (isWritable()) {
154             firstContext = initialContext.createSubcontext(firstContextName());
155             secondContext = firstContext.createSubcontext(secondContextName());
156             addBindings();
157         }
158     }
159     
160     protected void tearDown() throws Exception JavaDoc {
161         if (isWritable()) {
162             removeBindings();
163             firstContext.destroySubcontext(secondContextName());
164             initialContext.destroySubcontext(firstContextName());
165         }
166         initialContext = null;
167     }
168     
169     //-------------------------- Verification methods -------------------------------------------------
170

171     /**
172      * Verify that object returned by lookup operation is "same" as bound object.
173      * Override this method if the object returned by looking up the name of a bound
174      * object is not equal to the originally bound object.
175      */

176     protected void verifyLookup(Object JavaDoc boundObject, Object JavaDoc returnedObject) {
177         assertEquals(boundObject, returnedObject);
178     }
179     
180     /**
181      * Verify that the names and classes in the returned Map (from list() method)
182      * match expected results. Override this method if the classes of bound objects
183      * returned by list() are not the same as the objects originally bound.
184      */

185     protected void verifyList(Map JavaDoc expected, Map JavaDoc returned) {
186         assertEquals(expected, returned);
187     }
188     
189     /**
190      * Verify that the names and objects in the returned Map (from listBindings() method)
191      * match expected results. Override this method if bound objects
192      * returned by list() are not the same as the objects originally bound.
193      */

194     protected void verifyListBindings(Map JavaDoc expected, Map JavaDoc returned) {
195         assertEquals(expected, returned);
196     }
197     
198    //--------------------------- Default implementations for basic tests --------------------------
199

200     public void testInitialContext() throws NamingException JavaDoc {
201         verifyLookup(firstBoundObject(),
202                 initialContext.lookup(firstContextName() + "/"
203                         + secondContextName() +"/" + firstBoundName()));
204         verifyLookup(secondBoundObject(),
205                 initialContext.lookup(new CompositeName JavaDoc
206                         (firstContextName() + "/" + secondContextName() + "/" + secondBoundName())));
207         verifyLookup(secondContext.lookup(firstBoundName()),
208             ((Context JavaDoc) secondContext.lookup("")).lookup(firstBoundName()));
209     }
210
211     public void testLookup() throws NamingException JavaDoc {
212         verifyLookup(firstBoundObject(), secondContext.lookup(firstBoundName()));
213         verifyLookup(firstBoundObject(),
214                 firstContext.lookup(secondContextName() + "/" +firstBoundName()));
215         try {
216             secondContext.lookup("foo");
217             fail("expecting NamingException");
218         } catch (NamingException JavaDoc e) {
219             // expected
220
}
221         verifyLookup(firstBoundObject(),
222                 secondContext.lookup(new CompositeName JavaDoc(firstBoundName())));
223         verifyLookup(firstBoundObject(),
224                 firstContext.lookup(new CompositeName JavaDoc(secondContextName() + "/" + firstBoundName())));
225         verifyLookup(secondBoundObject(),
226             ((Context JavaDoc) initialContext.lookup(firstContextName())).lookup(secondContextName() + "/" + secondBoundName()));
227     }
228     
229     public void testComposeName() throws NamingException JavaDoc {
230         assertEquals("org/research/user/jane",
231             secondContext.composeName("user/jane", "org/research"));
232         assertEquals("research/user/jane",
233             secondContext.composeName("user/jane", "research"));
234         assertEquals(new CompositeName JavaDoc("org/research/user/jane"),
235             secondContext.composeName(new CompositeName JavaDoc("user/jane"),
236                 new CompositeName JavaDoc("org/research")));
237         assertEquals(new CompositeName JavaDoc("research/user/jane"),
238             secondContext.composeName(new CompositeName JavaDoc("user/jane"),
239                 new CompositeName JavaDoc("research")));
240     }
241
242     public void testList() throws NamingException JavaDoc {
243         NamingEnumeration JavaDoc enumeration;
244         Map JavaDoc expected;
245         Map JavaDoc result;
246
247         expected = new HashMap JavaDoc();
248         for (Iterator JavaDoc i = binding.entrySet().iterator(); i.hasNext();) {
249             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
250             expected.put(entry.getKey(), entry.getValue().getClass().getName());
251         }
252         enumeration = secondContext.list("");
253         result = new HashMap JavaDoc();
254         while (enumeration.hasMore()) {
255             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) enumeration.next();
256             result.put(pair.getName(), pair.getClassName());
257         }
258         verifyList(expected, result);
259
260         try {
261             enumeration.next();
262             fail("Expecting NoSuchElementException");
263         } catch (NoSuchElementException JavaDoc e) {
264             // expected
265
}
266         try {
267             enumeration.nextElement();
268             fail("Expecting NoSuchElementException");
269         } catch (NoSuchElementException JavaDoc e) {
270             // expected
271
}
272     }
273
274     public void testListBindings() throws NamingException JavaDoc {
275         NamingEnumeration JavaDoc enumeration;
276         Map JavaDoc result;
277         enumeration = secondContext.listBindings("");
278         result = new HashMap JavaDoc();
279         while (enumeration.hasMore()) {
280             Binding JavaDoc pair = (Binding JavaDoc) enumeration.next();
281             result.put(pair.getName(), pair.getObject());
282         }
283         verifyListBindings(binding, result);
284
285         try {
286             enumeration.next();
287             fail("Expecting NoSuchElementException");
288         } catch (NoSuchElementException JavaDoc e) {
289             // expected
290
}
291         try {
292             enumeration.nextElement();
293             fail("Expecting NoSuchElementException");
294         } catch (NoSuchElementException JavaDoc e) {
295             // expected
296
}
297     }
298     
299     /**
300      * Default implementation just tests to make sure non-null names are returned
301      * or correct exception is thrown.
302      */

303     public void testGetNameInNamespace() throws Exception JavaDoc {
304         if (isGetNameInNamespaceSupported()) {
305             String JavaDoc name = initialContext.getNameInNamespace();
306             if (name == null) {
307                 fail("Null NameInNamespace for initial context");
308             }
309         } else {
310             try {
311                 String JavaDoc name = firstContext.getNameInNamespace();
312                 fail("Expecting OperationNotSupportedException");
313             } catch(OperationNotSupportedException JavaDoc ex) {
314                 // expected
315
}
316         }
317     }
318     
319     /**
320      * Test rebind -- swap bound objects and verify
321      */

322     public void testRebind() throws Exception JavaDoc {
323         secondContext.rebind(firstBoundName(), secondBoundObject());
324         secondContext.rebind(secondBoundName(), firstBoundObject());
325         binding.put(firstBoundName(), secondBoundObject());
326         binding.put(secondBoundName(), firstBoundObject());
327         NamingEnumeration JavaDoc enumeration;
328         Map JavaDoc result;
329         enumeration = secondContext.listBindings("");
330         result = new HashMap JavaDoc();
331         while (enumeration.hasMore()) {
332             Binding JavaDoc pair = (Binding JavaDoc) enumeration.next();
333             result.put(pair.getName(), pair.getObject());
334         }
335         verifyListBindings(binding, result);
336     }
337 }
338
Popular Tags