Author: kelly
Date: Wed Jul 14 22:50:59 2010
New Revision: 964238
URL: http://svn.apache.org/viewvc?rev=964238&view=rev
Log:
WIP OODT-15
Import additional code to test OODT Object Contexts.
Added:
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/ObjectContextTest.java
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContext.java
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContextTest.java
Added:
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/ObjectContextTest.java
URL:
http://svn.apache.org/viewvc/incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/ObjectContextTest.java?rev=964238&view=auto
==============================================================================
---
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/ObjectContextTest.java
(added)
+++
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/ObjectContextTest.java
Wed Jul 14 22:50:59 2010
@@ -0,0 +1,203 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
contributor
+// license agreements. See the NOTICE.txt file distributed with this work for
+// additional information regarding copyright ownership. The ASF licenses this
+// file to you 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.oodt.commons.object.jndi;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import javax.naming.Binding;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import org.apache.oodt.commons.object.jndi.ObjectContext;
+import junit.framework.TestCase;
+
+/**
+ * Unit test for {...@link ObjectContext}.
+ *
+ * @author Kelly
+ * @version $Revision: 1.2 $
+ */
+public class ObjectContextTest extends TestCase {
+ /**
+ * Creates a new <code>ObjectContextTest</code> instance.
+ *
+ * @param caseName a <code>String</code> value.
+ */
+ public ObjectContextTest(String caseName) {
+ super(caseName);
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+
+ aliasFile = File.createTempFile("test", ".properties");
+ aliasFile.deleteOnExit();
+ Properties aliases = new Properties();
+ aliases.setProperty("urn:alias:x", "urn:a:x");
+ FileOutputStream out = new FileOutputStream(aliasFile);
+ aliases.save(out, "Temporary properties");
+ out.close();
+
+ a1 = new TestContext("urn:a");
+ a2 = new TestContext("urn:a");
+ b = new TestContext("urn:b");
+
+ oldValue =
System.getProperty("org.apache.oodt.commons.object.jndi.aliases");
+
System.setProperty("org.apache.oodt.commons.object.jndi.aliases",
aliasFile.toString());
+
+ List contexts = new ArrayList();
+ contexts.add(a1);
+ contexts.add(a2);
+ contexts.add(b);
+ context = new ObjectContext(contexts);
+ }
+
+ public void tearDown() throws Exception {
+ aliasFile.delete();
+ if (oldValue == null)
+
System.getProperties().remove("org.apache.oodt.commons.object.jndi.aliases");
+ else
+
System.setProperty("org.apache.oodt.commons.object.jndi.aliases", oldValue);
+ super.tearDown();
+ }
+
+ /**
+ * Test various aspects of the object context.
+ *
+ * @throws NamingException if an error occurs.
+ */
+ public void testObjectContext() throws NamingException {
+ // Test retrieval of nonexistent bindings
+ try {
+ context.lookup("urn:a:x");
+ fail("Found nonexistent object");
+ } catch (NamingException ex) {}
+
+ // Test binding names that don't match any delegate's namespace
prefix.
+ try {
+ context.bind("urn:c:x", this);
+ fail("Bound nonconforming name");
+ } catch (NamingException ex) {}
+
+ // Test binding and retrieval
+ context.bind("urn:a:x", this);
// Bind something
+ assertSame(this, context.lookup("urn:a:x"));
// Look it up
+ assertSame(this, context.lookup("urn:alias:x"));
// Try the alias
+ assertTrue(a1.bindings.values().contains(this));
// It should be in both a1...
+ assertTrue(a2.bindings.values().contains(this));
// ...and a2
+ assertTrue(!b.bindings.values().contains(this));
// But not b.
+
+ context.bind("urn:b:x", getClass());
// Now bind something for b
+ assertSame(getClass(), context.lookup("urn:b:x"));
// Look it up
+ assertTrue(!a1.bindings.values().contains(getClass()));
// It should not be in a1...
+ assertTrue(!a2.bindings.values().contains(getClass()));
// ...nor a2
+ assertTrue(b.bindings.values().contains(getClass()));
// But should be in b.
+
+ // Test binding a bound name
+ try {
+ context.bind("urn:a:x", "");
+ fail("Able to bind an already-bound name");
+ } catch (NamingException ex) {}
+
+ // Test rebinding a bound name
+ context.rebind("urn:a:x", context);
// Bind to a different object
+ assertSame(context, context.lookup("urn:a:x"));
// Look it up
+ assertTrue(!a1.bindings.values().contains(this));
// The old object should be gone from a1...
+ assertTrue(!a2.bindings.values().contains(this));
// ...and from a2
+ assertTrue(!b.bindings.values().contains(context));
// And the new object isn't in b
+
+ // Test renaming
+ context.rename("urn:a:x", "urn:a:y");
// Change x to y
+ try {
+ context.lookup("urn:a:x");
// Look it up
+ fail("Found object under old name");
+ } catch (NamingException ex) {}
+ assertSame(context, context.lookup("urn:a:y"));
// Just the name has changed
+ assertTrue(a1.bindings.keySet().contains("urn:a:y"));
// The new name is in a1
+ assertTrue(!a1.bindings.keySet().contains("urn:a:x"));
// But not the old
+ assertTrue(a2.bindings.keySet().contains("urn:a:y"));
// The new name is in a2
+ assertTrue(!a2.bindings.keySet().contains("urn:a:x"));
// But not the old
+ assertTrue(!b.bindings.values().contains(context));
// It was never in b
+
+ // Test listing
+ int count = 0;
+ boolean sawA = false;
+ boolean sawB = false;
+ NamingEnumeration e = context.list("");
+ while (e.hasMore()) {
+ NameClassPair p = (NameClassPair) e.next();
+ if ("urn:a:y".equals(p.getName()) &&
context.getClass().getName().equals(p.getClassName()))
+ sawA = true;
+ else if ("urn:b:x".equals(p.getName()) &&
"java.lang.Class".equals(p.getClassName()))
+ sawB = true;
+ else
+ fail("Unexpected binding \"" + p.getName() +
"\" to " + p.getClassName());
+ ++count;
+ }
+ assertEquals(3, count);
+ assertTrue(sawA);
+ assertTrue(sawB);
+
+ // Test listing of bindings
+ count = 0;
+ sawA = false;
+ sawB = false;
+ e = context.listBindings("");
+ while (e.hasMore()) {
+ Binding b = (Binding) e.next();
+ if ("urn:a:y".equals(b.getName()) && context ==
b.getObject())
+ sawA = true;
+ else if ("urn:b:x".equals(b.getName()) && getClass() ==
b.getObject())
+ sawB = true;
+ else
+ fail("Unexpected binding \"" + b.getName() +
"\" to " + b.getObject());
+ ++count;
+ }
+ assertEquals(3, count);
+ assertTrue(sawA);
+ assertTrue(sawB);
+
+ // Test unbinding
+ context.unbind("urn:a:y");
// Unbind it
+ try {
+ context.lookup("urn:a:y");
// Look it up
+ fail("Found unbound object");
+ } catch (NamingException ex) {}
+ assertTrue(a1.bindings.isEmpty());
// It's not in a1...
+ assertTrue(a2.bindings.isEmpty());
// ...nor in a2
+ }
+
+ /** First delegate context for "urn:a" namespace. */
+ private TestContext a1;
+
+ /** Second delegate context for "urn:a" namespace. */
+ private TestContext a2;
+
+ /** Delegate context for "urn:b" namespace. */
+ private TestContext b;
+
+ /** Test subject: the object context. */
+ private ObjectContext context;
+
+ /** Test alias file. */
+ private File aliasFile;
+
+ /** Old value of sys prop org.apache.oodt.commons.object.jndi.aliases.
*/
+ private String oldValue;
+}
Added:
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContext.java
URL:
http://svn.apache.org/viewvc/incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContext.java?rev=964238&view=auto
==============================================================================
---
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContext.java
(added)
+++
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContext.java
Wed Jul 14 22:50:59 2010
@@ -0,0 +1,203 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
contributor
+// license agreements. See the NOTICE.txt file distributed with this work for
+// additional information regarding copyright ownership. The ASF licenses this
+// file to you 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.oodt.commons.object.jndi;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+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.NameNotFoundException;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.OperationNotSupportedException;
+
+/**
+ * A context for testing. This context uses a simple {...@link Map} to track
bindings.
+ * Names must start with a given prefix string or they cause {...@link
+ * InvalidNameException}s.
+ *
+ * @author Kelly
+ * @version $Revision: 1.1 $
+ */
+class TestContext implements Context {
+ /**
+ * Creates a new <code>TestContext</code> instance.
+ *
+ * @param prefix What every name must start with.
+ */
+ public TestContext(String prefix) {
+ this.prefix = prefix;
+ }
+
+ public Object lookup(Name name) throws NamingException {
+ return lookup(name.toString());
+ }
+ public Object lookup(String name) throws NamingException {
+ Object rc = bindings.get(name);
+ if (rc == null)
+ throw new NameNotFoundException(name);
+ return rc;
+ }
+ public void bind(Name name, Object obj) throws NamingException {
+ bind(name.toString(), obj);
+ }
+ public void bind(String name, Object obj) throws NamingException {
+ if (!name.startsWith(prefix)) throw new
InvalidNameException("Name doesn't start with " + prefix);
+ if (bindings.containsKey(name))
+ throw new NameAlreadyBoundException(name);
+ bindings.put(name, obj);
+ }
+ public void rebind(Name name, Object obj) throws NamingException {
+ rebind(name.toString(), obj);
+ }
+ public void rebind(String name, Object obj) throws NamingException {
+ if (!name.startsWith(prefix)) throw new
InvalidNameException("Name doesn't start with " + prefix);
+ bindings.put(name, obj);
+ }
+ public void unbind(Name name) throws NamingException {
+ unbind(name.toString());
+ }
+ public void unbind(String name) throws NamingException {
+ if (bindings.remove(name) == null)
+ throw new NameNotFoundException(name);
+ }
+ public void rename(Name oldName, Name newName) throws NamingException {
+ rename(oldName.toString(), newName.toString());
+ }
+ public void rename(String oldName, String newName) throws
NamingException {
+ if (!bindings.containsKey(oldName))
+ throw new NameNotFoundException(oldName);
+ if (bindings.containsKey(newName))
+ throw new NameAlreadyBoundException(newName);
+ if (!newName.startsWith(prefix))
+ throw new InvalidNameException("Name doesn't start with
" + prefix);
+ bindings.put(newName, bindings.remove(oldName));
+ }
+ public NamingEnumeration list(Name name) throws NamingException {
+ return list(name.toString());
+ }
+ public NamingEnumeration list(String name) throws NamingException {
+ if (name.length() > 0)
+ throw new OperationNotSupportedException("subcontexts
not supported");
+ final Iterator i = bindings.entrySet().iterator();
+ return new NamingEnumeration() {
+ public Object next() {
+ Map.Entry e = (Map.Entry) i.next();
+ return new NameClassPair((String) e.getKey(),
e.getValue().getClass().getName());
+ }
+ public boolean hasMore() {
+ return i.hasNext();
+ }
+ public void close() {}
+ public boolean hasMoreElements() {
+ return hasMore();
+ }
+ public Object nextElement() {
+ return next();
+ }
+ };
+ }
+ public NamingEnumeration listBindings(Name name) throws NamingException
{
+ return listBindings(name.toString());
+ }
+ public NamingEnumeration listBindings(String name) throws
NamingException {
+ if (name.length() > 0)
+ throw new OperationNotSupportedException("subcontexts
not supported");
+ final Iterator i = bindings.entrySet().iterator();
+ return new NamingEnumeration() {
+ public Object next() {
+ Map.Entry e = (Map.Entry) i.next();
+ return new Binding((String) e.getKey(),
e.getValue());
+ }
+ public boolean hasMore() {
+ return i.hasNext();
+ }
+ public void close() {}
+ public boolean hasMoreElements() {
+ return hasMore();
+ }
+ public Object nextElement() {
+ return next();
+ }
+
+ };
+ }
+ public void destroySubcontext(Name name) throws NamingException {
+ destroySubcontext(name.toString());
+ }
+ public void destroySubcontext(String name) throws NamingException {
+ throw new OperationNotSupportedException("subcontexts not
supported");
+ }
+ public Context createSubcontext(Name name) throws NamingException {
+ return createSubcontext(name.toString());
+ }
+ public Context createSubcontext(String name) throws NamingException {
+ throw new OperationNotSupportedException("subcontexts not
supported");
+ }
+ public Object lookupLink(Name name) throws NamingException {
+ return lookupLink(name.toString());
+ }
+ public Object lookupLink(String name) throws NamingException {
+ return lookup(name);
+ }
+ public NameParser getNameParser(Name name) {
+ return getNameParser(name.toString());
+ }
+ public NameParser getNameParser(String name) {
+ return new ObjectNameParser();
+ }
+ public String composeName(String name, String prefix) throws
NamingException {
+ return composeName(new CompositeName(name), new
CompositeName(prefix)).toString();
+ }
+ public Name composeName(Name name, Name prefix) throws NamingException {
+ Name result = (Name) prefix.clone();
+ result.addAll(name);
+ return result;
+ }
+ public Object addToEnvironment(String key, Object val) {
+ if (environment == null) environment = new Hashtable();
+ return environment.put(key, val);
+ }
+ public Object removeFromEnvironment(String key) {
+ if (environment == null) environment = new Hashtable();
+ return environment.remove(key);
+ }
+ public Hashtable getEnvironment() {
+ if (environment == null) environment = new Hashtable();
+ return environment;
+ }
+ public void close() {}
+ public String getNameInNamespace() {
+ return "";
+ }
+
+ /** What holds the bindings. Keys are {...@link String}s, values are
{...@link Object}s. */
+ Map bindings = new HashMap();
+
+ /** What every key must start with. */
+ private String prefix;
+
+ /** Context's environment. */
+ private Hashtable environment;
+}
Added:
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContextTest.java
URL:
http://svn.apache.org/viewvc/incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContextTest.java?rev=964238&view=auto
==============================================================================
---
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContextTest.java
(added)
+++
incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/object/jndi/TestContextTest.java
Wed Jul 14 22:50:59 2010
@@ -0,0 +1,96 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
contributor
+// license agreements. See the NOTICE.txt file distributed with this work for
+// additional information regarding copyright ownership. The ASF licenses this
+// file to you 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.oodt.commons.object.jndi;
+
+import javax.naming.Binding;
+import javax.naming.InvalidNameException;
+import javax.naming.NameAlreadyBoundException;
+import javax.naming.NameClassPair;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import junit.framework.TestCase;
+
+/**
+ * Unit test for the test context.
+ *
+ * @author Kelly
+ * @version $Revision: 1.1 $
+ */
+public class TestContextTest extends TestCase {
+ /**
+ * Creates a new <code>TestContextTest</code> instance.
+ *
+ * @param caseName a <code>String</code> value.
+ */
+ public TestContextTest(String caseName) {
+ super(caseName);
+ }
+
+ /**
+ * Test various aspects of the test context.
+ *
+ * @throws NamingException if an error occurs.
+ */
+ public void testTestContext() throws NamingException {
+ TestContext ctx = new TestContext("urn:x:");
// Make it
+ try {
+ ctx.lookup("urn:x:y");
// Lookup a nonexistent binding
+ fail("Got a binding that doesn't exist");
// Got something? Yikes.
+ } catch (NameNotFoundException ex) {}
+
+ try {
+ ctx.bind("urn:y:z", this);
// Bind an invalid prefix
+ fail("Bound an invalid prefix");
// Worked? Dang.
+ } catch (InvalidNameException ex) {}
+
+ ctx.bind("urn:x:a", this);
// Bind something.
+ assertSame(this, ctx.lookup("urn:x:a"));
// Look it up
+
+ try {
+ ctx.bind("urn:x:a", getClass());
// Bind it again
+ fail("Able to re-bind");
// Worked? Crap.
+ } catch (NameAlreadyBoundException ex) {}
+
+ ctx.rebind("urn:x:a", getClass());
// Rebind it again
+ assertSame(getClass(), ctx.lookup("urn:x:a"));
// Look it up
+
+ ctx.rename("urn:x:a", "urn:x:b");
// Rename the binding
+ assertSame(getClass(), ctx.lookup("urn:x:b"));
// Look it up
+
+ NamingEnumeration e = ctx.list("");
// List the context
+ assertTrue(e.hasMore());
// Got something? Good.
+ NameClassPair p = (NameClassPair) e.next();
// Get it
+ assertEquals("urn:x:b", p.getName());
// Right name? Good.
+ assertEquals("java.lang.Class", p.getClassName());
// Right class? Good.
+ assertTrue(!e.hasMore());
// Got no more? Good.
+
+ e = ctx.listBindings("");
// List the bindings
+ assertTrue(e.hasMore());
// Got something? Good.
+ Binding b = (Binding) e.next();
// Get it
+ assertEquals("urn:x:b", p.getName());
// Right name? Good.
+ assertSame(getClass(), b.getObject());
// Right object? Good.
+ assertTrue(!e.hasMore());
// Got no more? Good.
+
+ assertSame(getClass(), ctx.lookupLink("urn:x:b"));
// Look up via the link
+
+ ctx.unbind("urn:x:b");
// Unbind it
+ e = ctx.list("");
// List the context
+ assertTrue(!e.hasMore());
// Got no more? Good.
+ e = ctx.listBindings("");
// List the bindings
+ assertTrue(!e.hasMore());
// Got no more? Good.
+ }
+}