KickJava   Java API By Example, From Geeks To Geeks.

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


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.Context JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.naming.Name JavaDoc;
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NameClassPair JavaDoc;
26 import javax.naming.Binding JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.TreeSet JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 import org.apache.xbean.naming.context.ContextUtil;
33
34 /**
35  * @version $Rev$ $Date$
36  */

37 public abstract class AbstractContextTest extends TestCase {
38     public static void assertEq(Map JavaDoc expected, Context JavaDoc actual) throws NamingException JavaDoc {
39         AbstractContextTest.assertEq(ContextUtil.buildMapTree(expected), actual, actual, null);
40     }
41
42     public static void assertEq(Map JavaDoc expected, String JavaDoc pathInExpected, Context JavaDoc actual) throws NamingException JavaDoc {
43         ContextUtil.Node node = ContextUtil.buildMapTree(expected);
44         Name JavaDoc parsedName = actual.getNameParser("").parse(pathInExpected);
45         for (int i = 0; i < parsedName.size(); i++) {
46             String JavaDoc part = parsedName.get(i);
47             Object JavaDoc value = node.get(part);
48             if (value == null) {
49                 throw new NamingException JavaDoc("look for " + parsedName.getPrefix(i+1) + " in node tree is null ");
50             }
51             node = (ContextUtil.Node) value;
52         }
53
54         AbstractContextTest.assertEq(node, actual, actual, null);
55     }
56
57     private static void assertEq(ContextUtil.Node node, Context JavaDoc rootContext, Context JavaDoc currentContext, String JavaDoc path) throws NamingException JavaDoc {
58         for (Iterator JavaDoc iterator = node.entrySet().iterator(); iterator.hasNext();) {
59             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
60             String JavaDoc expectedName = (String JavaDoc) entry.getKey();
61             Object JavaDoc expectedValue = entry.getValue();
62
63             String JavaDoc fullName = path == null ? expectedName : path + "/" + expectedName;
64
65             // verify we can lookup by string name and parsed name using the root context and current context
66
Object JavaDoc value = AbstractContextTest.assertLookup(expectedValue, currentContext, expectedName);
67             Object JavaDoc absoluteValue = AbstractContextTest.assertLookup(expectedValue, rootContext, fullName);
68             assertSame(fullName, value, absoluteValue);
69
70             if (expectedValue instanceof ContextUtil.Node) {
71                 ContextUtil.Node expectedNode = (ContextUtil.Node) expectedValue;
72
73                 // verufy listing of this context returns the expected results
74
AbstractContextTest.assertList(expectedNode, currentContext, expectedName);
75                 AbstractContextTest.assertList(expectedNode, rootContext, fullName);
76
77                 AbstractContextTest.assertEq(expectedNode, rootContext, (Context JavaDoc) value, fullName);
78             }
79         }
80     }
81
82     public static Object JavaDoc assertLookup(Object JavaDoc expectedValue, Context JavaDoc context, String JavaDoc name) throws NamingException JavaDoc {
83         Object JavaDoc value = context.lookup(name);
84
85         String JavaDoc contextName = context.getNameInNamespace();
86         if (contextName == null || contextName.length() == 0) contextName = "<root>";
87
88         assertNotNull("lookup of " + name + " on " + contextName + " returned null", value);
89
90         if (expectedValue instanceof ContextUtil.Node) {
91             assertTrue("Expected lookup of " + name + " on " + contextName + " to return a Context, but got a " + value.getClass().getName(),
92                     value instanceof Context JavaDoc);
93         } else {
94             assertEquals("lookup of " + name + " on " + contextName, expectedValue, value);
95         }
96
97         Name JavaDoc parsedName = context.getNameParser("").parse(name);
98         Object JavaDoc valueFromParsedName = context.lookup(parsedName);
99         assertSame("lookup of " + name + " on " + contextName + " using a parsed name", value, valueFromParsedName);
100
101         return value;
102     }
103
104     public static void assertList(ContextUtil.Node node, Context JavaDoc context, String JavaDoc name) throws NamingException JavaDoc {
105         String JavaDoc contextName = context.getNameInNamespace();
106         if (contextName == null || contextName.length() == 0) contextName = "<root>";
107
108         AbstractContextTest.assertListResults(node, context.list(name), contextName, name, false);
109         AbstractContextTest.assertListResults(node, context.listBindings(name), contextName, name, true);
110
111         Name JavaDoc parsedName = context.getNameParser("").parse(name);
112         AbstractContextTest.assertListResults(node, context.list(parsedName), contextName, "parsed name " + name, false);
113         AbstractContextTest.assertListResults(node, context.listBindings(parsedName), contextName, "parsed name " + name, true);
114     }
115
116     public static void assertListResults(ContextUtil.Node node, NamingEnumeration JavaDoc enumeration, String JavaDoc contextName, String JavaDoc name, boolean wasListBinding) {
117         Map JavaDoc actualValues;
118         if (wasListBinding) {
119             actualValues = AbstractContextTest.toListBindingResults(enumeration);
120         } else {
121             actualValues = AbstractContextTest.toListResults(enumeration);
122         }
123
124         for (Iterator JavaDoc iterator = node.entrySet().iterator(); iterator.hasNext();) {
125             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
126             String JavaDoc expectedName = (String JavaDoc) entry.getKey();
127             Object JavaDoc expectedValue = entry.getValue();
128
129             Object JavaDoc actualValue = actualValues.get(expectedName);
130
131             assertNotNull("list of " + name + " on " + contextName + " did not find value for " + expectedName, actualValue);
132             if (wasListBinding) {
133                 if (expectedValue instanceof ContextUtil.Node) {
134                     assertTrue("Expected list of " + name + " on " + contextName + " result value for " + expectedName + " to return a Context, but got a " + actualValue.getClass().getName(),
135                         actualValue instanceof Context JavaDoc);
136                 } else {
137                     assertEquals("list of " + name + " on " + contextName + " for value for " + expectedName, expectedValue, actualValue);
138                 }
139             } else {
140                 if (!(expectedValue instanceof ContextUtil.Node)) {
141                     assertEquals("list of " + name + " on " + contextName + " for value for " + expectedName, expectedValue.getClass().getName(), actualValue);
142                 } else {
143                     // can't really test this since it the value is the name of a nested node class
144
}
145             }
146         }
147
148         TreeSet JavaDoc extraNames = new TreeSet JavaDoc(actualValues.keySet());
149         extraNames.removeAll(node.keySet());
150         if (!extraNames.isEmpty()) {
151             fail("list of " + name + " on " + contextName + " found extra values: " + extraNames);
152         }
153     }
154
155     private static Map JavaDoc toListResults(NamingEnumeration JavaDoc enumeration) {
156         Map JavaDoc result = new HashMap JavaDoc();
157         while (enumeration.hasMoreElements()) {
158             NameClassPair JavaDoc nameClassPair = (NameClassPair JavaDoc) enumeration.nextElement();
159             String JavaDoc name = nameClassPair.getName();
160             assertFalse(result.containsKey(name));
161             result.put(name, nameClassPair.getClassName());
162         }
163         return result;
164     }
165
166     private static Map JavaDoc toListBindingResults(NamingEnumeration JavaDoc enumeration) {
167         Map JavaDoc result = new HashMap JavaDoc();
168         while (enumeration.hasMoreElements()) {
169             Binding JavaDoc binding = (Binding JavaDoc) enumeration.nextElement();
170             String JavaDoc name = binding.getName();
171             assertFalse(result.containsKey(name));
172             result.put(name, binding.getObject());
173         }
174         return result;
175     }
176 }
177
Popular Tags