You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ps...@apache.org on 2004/02/22 20:37:25 UTC
svn commit: rev 6823 - incubator/directory/naming/trunk/core/src/test/org/apache/naming
Author: psteitz
Date: Sun Feb 22 11:37:25 2004
New Revision: 6823
Modified:
incubator/directory/naming/trunk/core/src/test/org/apache/naming/AbstractContextTest.java
Log:
Improved organization, documentation, failure messages.
Modified: incubator/directory/naming/trunk/core/src/test/org/apache/naming/AbstractContextTest.java
==============================================================================
--- incubator/directory/naming/trunk/core/src/test/org/apache/naming/AbstractContextTest.java (original)
+++ incubator/directory/naming/trunk/core/src/test/org/apache/naming/AbstractContextTest.java Sun Feb 22 11:37:25 2004
@@ -16,7 +16,6 @@
package org.apache.naming;
import java.util.HashMap;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
@@ -33,9 +32,30 @@
/**
* Abstract base class for Context tests.
+ *
+ * Test classes derived from this class must implement makeInitialContext().
+ * If the context is not writable, override isWritable() to return false.
+ * If getNameInNamespace() is not supported, override isGetNameInNamespace() to
+ * return false.
+ *
+ * For writable contexts, the default setup() implementation creates:
+ *
+ * -- a subcontext (firstContext) of the InitialContext returned by makeInitialContext(),
+ * using firstContextName(), which defaults to "java:comp"
+ * -- a subcontext (secondContext) of firstContext (secondContext) using secondContextName() (default = "env")
+ * -- two object bindings on secondContext: firstBoundObject() bound to firstBoundName() and
+ * secondBoundObject() bound to secondBoundName()
+ *
+ * Basic tests included verify binding, context lookup, name composition, and list operations.
*/
public abstract class AbstractContextTest extends TestCase {
+ public AbstractContextTest(String name) {
+ super(name);
+ }
+
+ //-------------------------- Contexts to use in tests ----------------------------------------------------
+
/** Initial Context used in tests */
protected Context initialContext;
@@ -45,6 +65,11 @@
/** Immediate subcontext of firstContext */
protected Context secondContext;
+ /** HashMap of Object Bindings for verification */
+ protected HashMap binding;
+
+ //-------------------- Override these methods to set up test namespace ------------------------
+
/** firstContext name -- relative to InitialContext */
protected String firstContextName() {
return "java:comp";
@@ -75,8 +100,7 @@
return "World";
}
- /** HashMap of Object Bindings for verification */
- protected HashMap binding;
+ //----------------- Switches to turn off tests for unsupported operations ----------------------
/**
* Does this context support getNameInNamespace()?
@@ -99,6 +123,8 @@
*/
protected abstract Context makeInitialContext();
+ //---------------------------------- Setup / teardown operations -----------------------------
+
/**
* Add bindings
*/
@@ -118,6 +144,28 @@
binding.clear();
}
+ protected void setUp() throws Exception {
+ super.setUp();
+ binding = new HashMap();
+ initialContext = makeInitialContext();
+ if (isWritable()) {
+ firstContext = initialContext.createSubcontext(firstContextName());
+ secondContext = firstContext.createSubcontext(secondContextName());
+ addBindings();
+ }
+ }
+
+ protected void tearDown() throws Exception {
+ if (isWritable()) {
+ removeBindings();
+ firstContext.destroySubcontext(secondContextName());
+ initialContext.destroySubcontext(firstContextName());
+ }
+ initialContext = null;
+ }
+
+ //-------------------------- Verification methods -------------------------------------------------
+
/**
* Verify that object returned by lookup operation is "same" as bound object.
* Override this method if the object returned by looking up the name of a bound
@@ -145,30 +193,7 @@
assertEquals(expected, returned);
}
- public AbstractContextTest(String name) {
- super(name);
- }
-
- protected void setUp() throws Exception {
- super.setUp();
- binding = new HashMap();
- Hashtable env = new Hashtable();
- initialContext = makeInitialContext();
- if (isWritable()) {
- firstContext = initialContext.createSubcontext(firstContextName());
- secondContext = firstContext.createSubcontext(secondContextName());
- addBindings();
- }
- }
-
- protected void tearDown() throws Exception {
- if (isWritable()) {
- removeBindings();
- firstContext.destroySubcontext(secondContextName());
- initialContext.destroySubcontext(firstContextName());
- }
- initialContext = null;
- }
+ //--------------------------- Default implementations for basic tests --------------------------
public void testInitialContext() throws NamingException {
verifyLookup(firstBoundObject(),
@@ -189,7 +214,7 @@
secondContext.lookup("foo");
fail("expecting NamingException");
} catch (NamingException e) {
- // OK
+ // expected
}
verifyLookup(firstBoundObject(),
secondContext.lookup(new CompositeName(firstBoundName())));
@@ -232,15 +257,15 @@
try {
enum.next();
- fail();
+ fail("Expecting NoSuchElementException");
} catch (NoSuchElementException e) {
- // ok
+ // expected
}
try {
enum.nextElement();
- fail();
+ fail("Expecting NoSuchElementException");
} catch (NoSuchElementException e) {
- // ok
+ // expected
}
}
@@ -257,23 +282,21 @@
try {
enum.next();
- fail();
+ fail("Expecting NoSuchElementException");
} catch (NoSuchElementException e) {
- // ok
+ // expected
}
try {
enum.nextElement();
- fail();
+ fail("Expecting NoSuchElementException");
} catch (NoSuchElementException e) {
- // ok
+ // expected
}
}
/**
* Default implementation just tests to make sure non-null names are returned
* or correct exception is thrown.
- *
- * @throws Exception
*/
public void testGetNameInNamespace() throws Exception {
if (isGetNameInNamespaceSupported()) {