You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by da...@apache.org on 2006/08/17 20:13:37 UTC
svn commit: r432313 - in /geronimo/xbean/branches/colossus/xbean-naming/src:
main/java/org/apache/xbean/naming/context/
test/java/org/apache/xbean/naming/context/
Author: dain
Date: Thu Aug 17 11:13:37 2006
New Revision: 432313
URL: http://svn.apache.org/viewvc?rev=432313&view=rev
Log:
Added test case for WritableContext
Cleaned up excption handling
Added:
geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/WritableContextTest.java
Modified:
geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java
geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java
Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java?rev=432313&r1=432312&r2=432313&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java Thu Aug 17 11:13:37 2006
@@ -18,19 +18,16 @@
import org.apache.geronimo.naming.enc.EnterpriseNamingContextNameParser;
-import javax.naming.Binding;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InvalidNameException;
import javax.naming.Name;
import javax.naming.NameAlreadyBoundException;
-import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.NotContextException;
import java.util.Hashtable;
-import java.util.Iterator;
import java.util.Map;
public abstract class AbstractContext implements Context {
@@ -83,10 +80,12 @@
}
public void destroySubcontext(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
destroySubcontext(new CompositeName(name));
}
public void unbind(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
unbind(new CompositeName(name));
}
@@ -95,6 +94,7 @@
}
public void destroySubcontext(Name name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
if (name.isEmpty()) {
throw new InvalidNameException("Cannot destroy subcontext with empty name");
}
@@ -102,6 +102,7 @@
}
public void unbind(Name name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
if (name.isEmpty()) {
throw new InvalidNameException("Cannot unbind empty name");
}
@@ -109,38 +110,39 @@
}
public Object lookup(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return lookup(new CompositeName(name));
}
public Object lookupLink(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return lookupLink(new CompositeName(name));
}
public Object removeFromEnvironment(String propName) throws NamingException {
- if (propName == null) {
- throw new NamingException("The parameter for this method cannot be null");
- }
+ if (propName == null) throw new NullPointerException("propName is null");
Object obj = env.get(propName);
env.remove(propName);
return obj;
}
public void bind(String name, Object obj) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
bind(new CompositeName(name), obj);
}
public void rebind(String name, Object obj) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
rebind(new CompositeName(name), obj);
}
public Object lookup(Name name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return internalLookup(name, true);
}
public Object lookupLink(Name name) throws NamingException {
- if (name.isEmpty()) {
- throw new InvalidNameException("Name cannot be empty");
- }
+ if (name == null) throw new NullPointerException("name is null");
return internalLookup(name, false);
}
@@ -161,28 +163,34 @@
* @throws NamingException if a naming exception is encountered
*/
public void bind(Name name, Object obj) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
if (name.isEmpty()) {
- throw new InvalidNameException("Cannot bind empty name");
+ throw new NameAlreadyBoundException("Cannot bind to an empty name (this context)");
}
addBinding(name, obj, false);
}
public void rebind(Name name, Object obj) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
if (name.isEmpty()) {
- throw new InvalidNameException("Cannot bind empty name");
+ throw new NameAlreadyBoundException("Cannot rebind an empty name (this context)");
}
addBinding(name, obj, true);
}
public synchronized void rename(String oldName, String newName) throws NamingException {
+ if (oldName == null) throw new NullPointerException("oldName is null");
+ if (newName == null) throw new NullPointerException("newName is null");
rename(new CompositeName(oldName), new CompositeName(newName));
}
public Context createSubcontext(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return createSubcontext(new CompositeName(name));
}
public Context createSubcontext(Name name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
if (name.isEmpty()) {
throw new InvalidNameException("Cannot create a subcontext if the name is empty");
}
@@ -194,7 +202,7 @@
public synchronized void rename(Name oldName, Name newName) throws NamingException {
if (oldName == null || newName == null) {
- throw new InvalidNameException("Name cannot be null");
+ throw new NullPointerException("name is null");
} else if (oldName.isEmpty() || newName.isEmpty()) {
throw new InvalidNameException("Name cannot be empty");
} else if (this.lookup(newName) != null) {
@@ -205,24 +213,29 @@
}
public NameParser getNameParser(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return getNameParser(new CompositeName(name));
}
public NameParser getNameParser(Name name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return EnterpriseNamingContextNameParser.INSTANCE;
}
public NamingEnumeration list(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return list(new CompositeName(name));
}
public NamingEnumeration listBindings(String name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
return listBindings(new CompositeName(name));
}
public NamingEnumeration list(Name name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
if (name.isEmpty()) {
- return new ListEnumeration(getBindingsCopy());
+ return new ContextUtil.ListEnumeration(getBindingsCopy());
}
Object target = lookup(name);
if (target instanceof Context) {
@@ -232,8 +245,9 @@
}
public NamingEnumeration listBindings(Name name) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
if (name.isEmpty()) {
- return new ListBindingEnumeration(getBindingsCopy());
+ return new ContextUtil.ListBindingEnumeration(getBindingsCopy());
}
Object target = lookup(name);
if (target instanceof Context) {
@@ -252,10 +266,14 @@
}
public String composeName(String name, String prefix) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
+ if (prefix == null) throw new NullPointerException("prefix is null");
return composeName(new CompositeName(name), new CompositeName(prefix)) .toString();
}
public Name composeName(Name name, Name prefix) throws NamingException {
+ if (name == null) throw new NullPointerException("name is null");
+ if (prefix == null) throw new NullPointerException("prefix is null");
if (name == null) {
throw new NullPointerException("name is null");
}
@@ -266,67 +284,5 @@
Name result = (Name) prefix.clone();
result.addAll(name);
return result;
- }
-
- private static final class ListEnumeration implements NamingEnumeration {
- private final Iterator iterator;
-
- public ListEnumeration(Map localBindings) {
- this.iterator = localBindings.entrySet().iterator();
- }
-
- public boolean hasMore() {
- return iterator.hasNext();
- }
-
- public boolean hasMoreElements() {
- return iterator.hasNext();
- }
-
- public Object next() {
- return nextElement();
- }
-
- public Object nextElement() {
- Map.Entry entry = (Map.Entry) iterator.next();
- String name = (String) entry.getKey();
- Object value = entry.getValue();
- String className = null;
- className = value.getClass().getName();
- return new NameClassPair(name, className);
- }
-
- public void close() {
- }
- }
-
- private static final class ListBindingEnumeration implements NamingEnumeration {
- private final Iterator iterator;
-
- public ListBindingEnumeration(Map localBindings) {
- this.iterator = localBindings.entrySet().iterator();
- }
-
- public boolean hasMore() {
- return iterator.hasNext();
- }
-
- public boolean hasMoreElements() {
- return iterator.hasNext();
- }
-
- public Object next() {
- return nextElement();
- }
-
- public Object nextElement() {
- Map.Entry entry = (Map.Entry) iterator.next();
- String name = (String) entry.getKey();
- Object value = entry.getValue();
- return new Binding(name, value);
- }
-
- public void close() {
- }
}
}
Modified: geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java?rev=432313&r1=432312&r2=432313&view=diff
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java (original)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/main/java/org/apache/xbean/naming/context/GeronimoGlobalContext.java Thu Aug 17 11:13:37 2006
@@ -37,6 +37,7 @@
public GeronimoGlobalContext(AbstractContext parent, Hashtable environment, Name contextAtomicName) {
super(parent, environment, contextAtomicName);
+ bindings = new HashMap();
}
public GeronimoGlobalContext(Hashtable env) {
Added: geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/WritableContextTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/WritableContextTest.java?rev=432313&view=auto
==============================================================================
--- geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/WritableContextTest.java (added)
+++ geronimo/xbean/branches/colossus/xbean-naming/src/test/java/org/apache/xbean/naming/context/WritableContextTest.java Thu Aug 17 11:13:37 2006
@@ -0,0 +1,114 @@
+/**
+ *
+ * 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.NamingException;
+import javax.naming.Context;
+import javax.naming.Name;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
+ */
+public class WritableContextTest extends AbstractContextTest {
+ private static final String STRING_VAL = "some string";
+
+ public void testBasic() throws Exception {
+ Map map = new HashMap();
+ map.put("string", WritableContextTest.STRING_VAL);
+ map.put("nested/context/string", WritableContextTest.STRING_VAL);
+ map.put("a/b/c/d/e/string", WritableContextTest.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 GeronimoGlobalContext();
+ bind(context, map);
+
+ assertEq(map, context);
+ }
+
+ public void testAddBinding() throws Exception {
+ Map map = new HashMap();
+ map.put("string", WritableContextTest.STRING_VAL);
+ map.put("nested/context/string", WritableContextTest.STRING_VAL);
+ map.put("a/b/c/d/e/string", WritableContextTest.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 GeronimoGlobalContext();
+ bind(context, map);
+
+ assertEq(map, context);
+
+ // add a new deep tree
+ map.put("a/b/c/d/e/forty-two", new Integer(42));
+ context.bind("a/b/c/d/e/forty-two", new Integer(42));
+
+ assertEq(map, context);
+
+ }
+
+
+ public void testRemoveBinding() throws Exception {
+ Map map = new HashMap();
+ map.put("string", WritableContextTest.STRING_VAL);
+ map.put("nested/context/string", WritableContextTest.STRING_VAL);
+ map.put("a/b/c/d/e/string", WritableContextTest.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 GeronimoGlobalContext();
+ bind(context, map);
+
+ assertEq(map, context);
+
+ // remove from an exisitng node
+ map.remove("a/b/c/d/e/three");
+ context.unbind("a/b/c/d/e/three");
+
+ assertEq(map, context);
+ }
+
+ public static void bind(Context context, Map bindings) throws NamingException {
+ for (Iterator iterator = bindings.entrySet().iterator(); iterator.hasNext();) {
+ Map.Entry entry = (Map.Entry) iterator.next();
+ String name = (String) entry.getKey();
+ Object value = entry.getValue();
+ Name parsedName = context.getNameParser("").parse(name);
+ for (int i =1; i < parsedName.size(); i++) {
+ Name contextName = parsedName.getPrefix(i);
+ if (!bindingExists(context, contextName)) {
+ context.createSubcontext(contextName);
+ }
+ }
+ context.bind(name, value);
+ }
+ }
+
+ public static boolean bindingExists(Context context, Name contextName) {
+ try {
+ return context.lookup(contextName) != null;
+ } catch (NamingException e) {
+ }
+ return false;
+ }
+}