Added: geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/AbstractContextTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/AbstractContextTest.java?rev=432088&view=auto
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/AbstractContextTest.java
(added)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/AbstractContextTest.java
Wed Aug 16 17:50:03 2006
@@ -0,0 +1,174 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.xbean.naming.context;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.Name;
+import javax.naming.NamingEnumeration;
+import javax.naming.NameClassPair;
+import javax.naming.Binding;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.TreeSet;
+import java.util.HashMap;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public abstract class AbstractContextTest extends TestCase {
+ public static void assertEq(Map expected, Context actual) throws NamingException {
+ AbstractContextTest.assertEq(ContextUtil.buildMapTree(expected), actual, actual,
null);
+ }
+
+ public static void assertEq(Map expected, String pathInExpected, Context actual) throws
NamingException {
+ ContextUtil.Node node = ContextUtil.buildMapTree(expected);
+ Name parsedName = actual.getNameParser("").parse(pathInExpected);
+ for (int i = 0; i < parsedName.size(); i++) {
+ String part = parsedName.get(i);
+ Object value = node.get(part);
+ if (value == null) {
+ throw new NamingException("look for " + parsedName.getPrefix(i+1) + " in
node tree is null ");
+ }
+ node = (ContextUtil.Node) value;
+ }
+
+ AbstractContextTest.assertEq(node, actual, actual, null);
+ }
+
+ private static void assertEq(ContextUtil.Node node, Context rootContext, Context currentContext,
String path) throws NamingException {
+ for (Iterator iterator = node.entrySet().iterator(); iterator.hasNext();) {
+ Map.Entry entry = (Map.Entry) iterator.next();
+ String expectedName = (String) entry.getKey();
+ Object expectedValue = entry.getValue();
+
+ String fullName = path == null ? expectedName : path + "/" + expectedName;
+
+ // verify we can lookup by string name and parsed name using the root context
and current context
+ Object value = AbstractContextTest.assertLookup(expectedValue, currentContext,
expectedName);
+ Object absoluteValue = AbstractContextTest.assertLookup(expectedValue, rootContext,
fullName);
+ assertSame(fullName, value, absoluteValue);
+
+ if (expectedValue instanceof ContextUtil.Node) {
+ ContextUtil.Node expectedNode = (ContextUtil.Node) expectedValue;
+
+ // verufy listing of this context returns the expected results
+ AbstractContextTest.assertList(expectedNode, currentContext, expectedName);
+ AbstractContextTest.assertList(expectedNode, rootContext, fullName);
+
+ AbstractContextTest.assertEq(expectedNode, rootContext, (Context) value,
fullName);
+ }
+ }
+ }
+
+ public static Object assertLookup(Object expectedValue, Context context, String name)
throws NamingException {
+ Object value = context.lookup(name);
+
+ String contextName = context.getNameInNamespace();
+ if (contextName == null || contextName.length() == 0) contextName = "<root>";
+
+ assertNotNull("lookup of " + name + " on " + contextName + " returned null", value);
+
+ if (expectedValue instanceof ContextUtil.Node) {
+ assertTrue("Expected lookup of " + name + " on " + contextName + " to return
a Context, but got a " + value.getClass().getName(),
+ value instanceof Context);
+ } else {
+ assertEquals("lookup of " + name + " on " + contextName, expectedValue, value);
+ }
+
+ Name parsedName = context.getNameParser("").parse(name);
+ Object valueFromParsedName = context.lookup(parsedName);
+ assertSame("lookup of " + name + " on " + contextName + " using a parsed name",
value, valueFromParsedName);
+
+ return value;
+ }
+
+ public static void assertList(ContextUtil.Node node, Context context, String name) throws
NamingException {
+ String contextName = context.getNameInNamespace();
+ if (contextName == null || contextName.length() == 0) contextName = "<root>";
+
+ AbstractContextTest.assertListResults(node, context.list(name), contextName, name,
false);
+ AbstractContextTest.assertListResults(node, context.listBindings(name), contextName,
name, true);
+
+ Name parsedName = context.getNameParser("").parse(name);
+ AbstractContextTest.assertListResults(node, context.list(parsedName), contextName,
"parsed name " + name, false);
+ AbstractContextTest.assertListResults(node, context.listBindings(parsedName), contextName,
"parsed name " + name, true);
+ }
+
+ public static void assertListResults(ContextUtil.Node node, NamingEnumeration enumeration,
String contextName, String name, boolean wasListBinding) {
+ Map actualValues;
+ if (wasListBinding) {
+ actualValues = AbstractContextTest.toListBindingResults(enumeration);
+ } else {
+ actualValues = AbstractContextTest.toListResults(enumeration);
+ }
+
+ for (Iterator iterator = node.entrySet().iterator(); iterator.hasNext();) {
+ Map.Entry entry = (Map.Entry) iterator.next();
+ String expectedName = (String) entry.getKey();
+ Object expectedValue = entry.getValue();
+
+ Object actualValue = actualValues.get(expectedName);
+
+ assertNotNull("list of " + name + " on " + contextName + " did not find value
for " + name, actualValue);
+ if (wasListBinding) {
+ if (expectedValue instanceof ContextUtil.Node) {
+ assertTrue("Expected list of " + name + " on " + contextName + " result
value for " + name + " to return a Context, but got a " + actualValue.getClass().getName(),
+ actualValue instanceof Context);
+ } else {
+ assertEquals("list of " + name + " on " + contextName + " for value for
" + name, expectedValue, actualValue);
+ }
+ } else {
+ if (!(expectedValue instanceof ContextUtil.Node)) {
+ assertEquals("list of " + name + " on " + contextName + " for value for
" + name, expectedValue.getClass().getName(), actualValue);
+ } else {
+ // can't really test this since it the value is the name of a nested
node class
+ }
+ }
+ }
+
+ TreeSet extraNames = new TreeSet(actualValues.keySet());
+ extraNames.removeAll(node.keySet());
+ if (!extraNames.isEmpty()) {
+ fail("list of " + name + " on " + contextName + " found extra values: " + extraNames);
+ }
+ }
+
+ private static Map toListResults(NamingEnumeration enumeration) {
+ Map result = new HashMap();
+ while (enumeration.hasMoreElements()) {
+ NameClassPair nameClassPair = (NameClassPair) enumeration.nextElement();
+ String name = nameClassPair.getName();
+ assertFalse(result.containsKey(name));
+ result.put(name, nameClassPair.getClassName());
+ }
+ return result;
+ }
+
+ private static Map toListBindingResults(NamingEnumeration enumeration) {
+ Map result = new HashMap();
+ while (enumeration.hasMoreElements()) {
+ Binding binding = (Binding) enumeration.nextElement();
+ String name = binding.getName();
+ assertFalse(result.containsKey(name));
+ result.put(name, binding.getObject());
+ }
+ return result;
+ }
+}
Added: geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/ImmutableContextTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/ImmutableContextTest.java?rev=432088&view=auto
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/ImmutableContextTest.java
(added)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/ImmutableContextTest.java
Wed Aug 16 17:50:03 2006
@@ -0,0 +1,42 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.xbean.naming.context;
+
+import javax.naming.Context;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
+ */
+public class ImmutableContextTest extends AbstractContextTest {
+ private static final String STRING_VAL = "some string";
+
+ public void testBasic() throws Exception {
+ Map map = new HashMap();
+ map.put("string", STRING_VAL);
+ map.put("nested/context/string", STRING_VAL);
+ map.put("a/b/c/d/e/string", STRING_VAL);
+ map.put("a/b/c/d/e/one", new Integer(1));
+ map.put("a/b/c/d/e/two", new Integer(2));
+ map.put("a/b/c/d/e/three", new Integer(3));
+
+ Context context = new ImmutableContext(map);
+
+ assertEq(map, context);
+ }
+}
Added: geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/UnmodifiableContextTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/UnmodifiableContextTest.java?rev=432088&view=auto
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/UnmodifiableContextTest.java
(added)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/UnmodifiableContextTest.java
Wed Aug 16 17:50:03 2006
@@ -0,0 +1,112 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.xbean.naming.context;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
+ */
+public class UnmodifiableContextTest extends AbstractContextTest {
+ private static final String STRING_VAL = "some string";
+
+ private final class MutableContext extends UnmodifiableContext {
+ public MutableContext(Map bindings) throws NamingException {
+ super(bindings);
+ }
+
+ public void addDeepBinding(String name, Object value) throws NamingException {
+ super.addDeepBinding(name, value);
+ }
+
+ public void removeDeepBinding(String name) throws NamingException {
+ super.removeDeepBinding(name);
+ }
+ }
+
+ public void testBasic() throws Exception {
+ Map map = new HashMap();
+ map.put("string", STRING_VAL);
+ map.put("nested/context/string", STRING_VAL);
+ map.put("a/b/c/d/e/string", STRING_VAL);
+ map.put("a/b/c/d/e/one", new Integer(1));
+ map.put("a/b/c/d/e/two", new Integer(2));
+ map.put("a/b/c/d/e/three", new Integer(3));
+
+ Context context = new UnmodifiableContext(map);
+
+ assertEq(map, context);
+ }
+
+ public void testAddBinding() throws Exception {
+ Map map = new HashMap();
+ map.put("string", STRING_VAL);
+ map.put("nested/context/string", STRING_VAL);
+ map.put("a/b/c/d/e/string", STRING_VAL);
+ map.put("a/b/c/d/e/one", new Integer(1));
+ map.put("a/b/c/d/e/two", new Integer(2));
+ map.put("a/b/c/d/e/three", new Integer(3));
+
+ MutableContext context = new MutableContext(map);
+
+ assertEq(map, context);
+
+ // add a new deep tree
+ map.put("uno/dos/tres", new Integer(123));
+ context.addDeepBinding("uno/dos/tres", new Integer(123));
+
+ assertEq(map, context);
+
+ // modify an existing context
+ map.put("a/b/c/d/e/four", new Integer(4));
+ context.addDeepBinding("a/b/c/d/e/four", new Integer(4));
+
+ assertEq(map, context);
+ }
+
+
+ public void testRemoveBinding() throws Exception {
+ Map map = new HashMap();
+ map.put("string", STRING_VAL);
+ map.put("nested/context/string", STRING_VAL);
+ map.put("a/b/c/d/e/string", STRING_VAL);
+ map.put("a/b/c/d/e/one", new Integer(1));
+ map.put("a/b/c/d/e/two", new Integer(2));
+ map.put("a/b/c/d/e/three", new Integer(3));
+
+ MutableContext context = new MutableContext(map);
+
+ assertEq(map, context);
+
+ // remove from an exisitng node
+ map.remove("a/b/c/d/e/three");
+ context.removeDeepBinding("a/b/c/d/e/three");
+
+ assertEq(map, context);
+
+ // remove a deep single element element... empty nodes should be removed
+ map.remove("nested/context/string");
+ context.removeDeepBinding("nested/context/string");
+
+ assertEq(map, context);
+ }
+
+
+}
Added: geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/global/GlobalContextManagerTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/global/GlobalContextManagerTest.java?rev=432088&view=auto
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/global/GlobalContextManagerTest.java
(added)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/global/GlobalContextManagerTest.java
Wed Aug 16 17:50:03 2006
@@ -0,0 +1,94 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.xbean.naming.global;
+
+import org.apache.xbean.naming.context.AbstractContextTest;
+import org.apache.xbean.naming.context.ImmutableContext;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NoInitialContextException;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class GlobalContextManagerTest extends AbstractContextTest {
+ public void testNoGlobalContextSet() throws Exception {
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName());
+
+ try {
+ InitialContext initialContext = new InitialContext(env);
+ initialContext.lookup("");
+ fail("expected a NoInitialContextException");
+ } catch (NoInitialContextException expected) {
+ }
+ }
+
+ public void testDefaultInitialContext() throws Exception {
+ Map bindings = new HashMap();
+
+ bindings.put("string", "foo");
+ bindings.put("nested/context/string", "bar");
+ bindings.put("a/b/c/d/e/string", "beer");
+ bindings.put("a/b/c/d/e/one", new Integer(1));
+ bindings.put("a/b/c/d/e/two", new Integer(2));
+ bindings.put("a/b/c/d/e/three", new Integer(3));
+ bindings.put("java:comp/env/foo", new Integer(42));
+ bindings.put("foo:bar/baz", "caz");
+ ImmutableContext immutableContext = new ImmutableContext(bindings);
+
+ assertEq(bindings, immutableContext);
+
+ GlobalContextManager.setGlobalContext(immutableContext);
+
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName());
+
+ InitialContext initialContext = new InitialContext(env);
+
+ assertEq(bindings, initialContext);
+ }
+
+ public void testPackageDir() throws Exception {
+ Map bindings = new HashMap();
+
+ bindings.put("java:comp/string", "foo");
+ bindings.put("java:comp/nested/context/string", "bar");
+ bindings.put("java:comp/a/b/c/d/e/string", "beer");
+ bindings.put("java:comp/a/b/c/d/e/one", new Integer(1));
+ bindings.put("java:comp/a/b/c/d/e/two", new Integer(2));
+ bindings.put("java:comp/a/b/c/d/e/three", new Integer(3));
+ bindings.put("java:comp/env/foo", new Integer(42));
+ bindings.put("java:comp/baz", "caz");
+ ImmutableContext immutableContext = new ImmutableContext(bindings);
+
+ assertEq(bindings, immutableContext);
+
+ GlobalContextManager.setGlobalContext(immutableContext);
+
+ Hashtable env = new Hashtable();
+ env.put(Context.URL_PKG_PREFIXES, "org.apache.xbean.naming");
+
+ Context ctx = (Context) new InitialContext(env).lookup("java:comp");
+
+ assertEq(bindings, "java:comp", ctx);
+ }
+}
|