001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gjndi;
017:
018: import junit.framework.TestCase;
019:
020: import javax.naming.Context;
021: import javax.naming.NamingException;
022: import javax.naming.Name;
023: import javax.naming.NamingEnumeration;
024: import javax.naming.NameClassPair;
025: import javax.naming.Binding;
026: import java.util.Map;
027: import java.util.Iterator;
028: import java.util.TreeSet;
029: import java.util.HashMap;
030:
031: import org.apache.xbean.naming.context.ContextUtil;
032:
033: /**
034: * @version $Rev$ $Date$
035: */
036: public abstract class AbstractContextTest extends TestCase {
037: public static void assertEq(Map expected, Context actual)
038: throws NamingException {
039: AbstractContextTest.assertEq(
040: ContextUtil.buildMapTree(expected), actual, actual,
041: null);
042: }
043:
044: public static void assertEq(Map expected, String pathInExpected,
045: Context actual) throws NamingException {
046: ContextUtil.Node node = ContextUtil.buildMapTree(expected);
047: Name parsedName = actual.getNameParser("")
048: .parse(pathInExpected);
049: for (int i = 0; i < parsedName.size(); i++) {
050: String part = parsedName.get(i);
051: Object value = node.get(part);
052: if (value == null) {
053: throw new NamingException("look for "
054: + parsedName.getPrefix(i + 1)
055: + " in node tree is null ");
056: }
057: node = (ContextUtil.Node) value;
058: }
059:
060: AbstractContextTest.assertEq(node, actual, actual, null);
061: }
062:
063: private static void assertEq(ContextUtil.Node node,
064: Context rootContext, Context currentContext, String path)
065: throws NamingException {
066: for (Iterator iterator = node.entrySet().iterator(); iterator
067: .hasNext();) {
068: Map.Entry entry = (Map.Entry) iterator.next();
069: String expectedName = (String) entry.getKey();
070: Object expectedValue = entry.getValue();
071:
072: String fullName = path == null ? expectedName : path + "/"
073: + expectedName;
074:
075: // verify we can lookup by string name and parsed name using the root context and current context
076: Object value = AbstractContextTest.assertLookup(
077: expectedValue, currentContext, expectedName);
078: Object absoluteValue = AbstractContextTest.assertLookup(
079: expectedValue, rootContext, fullName);
080: assertSame(fullName, value, absoluteValue);
081:
082: if (expectedValue instanceof ContextUtil.Node) {
083: ContextUtil.Node expectedNode = (ContextUtil.Node) expectedValue;
084:
085: // verufy listing of this context returns the expected results
086: AbstractContextTest.assertList(expectedNode,
087: currentContext, expectedName);
088: AbstractContextTest.assertList(expectedNode,
089: rootContext, fullName);
090:
091: AbstractContextTest.assertEq(expectedNode, rootContext,
092: (Context) value, fullName);
093: }
094: }
095: }
096:
097: public static Object assertLookup(Object expectedValue,
098: Context context, String name) throws NamingException {
099: Object value = context.lookup(name);
100:
101: String contextName = context.getNameInNamespace();
102: if (contextName == null || contextName.length() == 0)
103: contextName = "<root>";
104:
105: assertNotNull("lookup of " + name + " on " + contextName
106: + " returned null", value);
107:
108: if (expectedValue instanceof ContextUtil.Node) {
109: assertTrue("Expected lookup of " + name + " on "
110: + contextName + " to return a Context, but got a "
111: + value.getClass().getName(),
112: value instanceof Context);
113: } else {
114: assertEquals("lookup of " + name + " on " + contextName,
115: expectedValue, value);
116: }
117:
118: Name parsedName = context.getNameParser("").parse(name);
119: Object valueFromParsedName = context.lookup(parsedName);
120: assertSame("lookup of " + name + " on " + contextName
121: + " using a parsed name", value, valueFromParsedName);
122:
123: return value;
124: }
125:
126: public static void assertList(ContextUtil.Node node,
127: Context context, String name) throws NamingException {
128: String contextName = context.getNameInNamespace();
129: if (contextName == null || contextName.length() == 0)
130: contextName = "<root>";
131:
132: AbstractContextTest.assertListResults(node, context.list(name),
133: contextName, name, false);
134: AbstractContextTest.assertListResults(node, context
135: .listBindings(name), contextName, name, true);
136:
137: Name parsedName = context.getNameParser("").parse(name);
138: AbstractContextTest.assertListResults(node, context
139: .list(parsedName), contextName, "parsed name " + name,
140: false);
141: AbstractContextTest.assertListResults(node, context
142: .listBindings(parsedName), contextName, "parsed name "
143: + name, true);
144: }
145:
146: public static void assertListResults(ContextUtil.Node node,
147: NamingEnumeration enumeration, String contextName,
148: String name, boolean wasListBinding) {
149: Map actualValues;
150: if (wasListBinding) {
151: actualValues = AbstractContextTest
152: .toListBindingResults(enumeration);
153: } else {
154: actualValues = AbstractContextTest
155: .toListResults(enumeration);
156: }
157:
158: for (Iterator iterator = node.entrySet().iterator(); iterator
159: .hasNext();) {
160: Map.Entry entry = (Map.Entry) iterator.next();
161: String expectedName = (String) entry.getKey();
162: Object expectedValue = entry.getValue();
163:
164: Object actualValue = actualValues.get(expectedName);
165:
166: assertNotNull("list of " + name + " on " + contextName
167: + " did not find value for " + expectedName,
168: actualValue);
169: if (wasListBinding) {
170: if (expectedValue instanceof ContextUtil.Node) {
171: assertTrue("Expected list of " + name + " on "
172: + contextName + " result value for "
173: + expectedName
174: + " to return a Context, but got a "
175: + actualValue.getClass().getName(),
176: actualValue instanceof Context);
177: } else {
178: assertEquals("list of " + name + " on "
179: + contextName + " for value for "
180: + expectedName, expectedValue, actualValue);
181: }
182: } else {
183: if (!(expectedValue instanceof ContextUtil.Node)) {
184: assertEquals("list of " + name + " on "
185: + contextName + " for value for "
186: + expectedName, expectedValue.getClass()
187: .getName(), actualValue);
188: } else {
189: // can't really test this since it the value is the name of a nested node class
190: }
191: }
192: }
193:
194: TreeSet extraNames = new TreeSet(actualValues.keySet());
195: extraNames.removeAll(node.keySet());
196: if (!extraNames.isEmpty()) {
197: fail("list of " + name + " on " + contextName
198: + " found extra values: " + extraNames);
199: }
200: }
201:
202: private static Map toListResults(NamingEnumeration enumeration) {
203: Map result = new HashMap();
204: while (enumeration.hasMoreElements()) {
205: NameClassPair nameClassPair = (NameClassPair) enumeration
206: .nextElement();
207: String name = nameClassPair.getName();
208: assertFalse(result.containsKey(name));
209: result.put(name, nameClassPair.getClassName());
210: }
211: return result;
212: }
213:
214: private static Map toListBindingResults(
215: NamingEnumeration enumeration) {
216: Map result = new HashMap();
217: while (enumeration.hasMoreElements()) {
218: Binding binding = (Binding) enumeration.nextElement();
219: String name = binding.getName();
220: assertFalse(result.containsKey(name));
221: result.put(name, binding.getObject());
222: }
223: return result;
224: }
225: }
|