directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: rev 6613 - incubator/directory/naming/trunk/core/src/test/org/apache/naming
Date Wed, 11 Feb 2004 06:24:25 GMT
Author: psteitz
Date: Tue Feb 10 22:24:25 2004
New Revision: 6613

Modified:
   incubator/directory/naming/trunk/core/src/test/org/apache/naming/AbstractContextTest.java
Log:
Refactored to improve extensibility.

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
Tue Feb 10 22:24:25 2004
@@ -69,27 +69,55 @@
 import javax.naming.NamingException;
 import javax.naming.OperationNotSupportedException;
 
-import junit.framework.Test;
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
 
 /**
- * Unit tests for basic ops on a {@link Context}.
- * 
- *  
- * @version $Revision: 1.2 $ $Date: 2003/11/30 05:36:07 $
+ * Abstract base class for Context tests.
  */
 public abstract class AbstractContextTest extends TestCase {
-    protected HashMap envBinding;
+    
+    /** Initial Context used in tests */
     protected Context initialContext;
-    protected Context compContext;
-    protected Context envContext;
     
-    public AbstractContextTest(String name) {
-        super(name);
+    /** Immediate subcontext of initialContext */
+    protected Context firstContext;
+    
+    /** Immediate subcontext of firstContext */
+    protected Context secondContext;
+    
+    /** firstContext name -- relative to InitialContext  */
+    protected String firstContextName() {
+        return "java:comp";
+    }
+    
+    /** secondContext name -- relative to first context  */
+    protected String secondContextName() {
+       return "env";
+    }
+    
+    /** First name to bind */
+    protected String firstBoundName() {
+        return "hello";
+    }
+    
+    /** First bound object */
+    protected Object firstBoundObject() {
+        return "Hello";
+    }
+    
+    /** Second name to bind */
+    protected String secondBoundName() {
+        return "world";
+    }
+    
+    /** Second bound object */
+    protected Object secondBoundObject() {
+       return "World";
     }
     
+    /** HashMap of Object Bindings for verification */
+    protected HashMap binding;
+    
     /**
      * Does this context support getNameInNamespace()?
      * Defaults to true -- override if not supported
@@ -99,62 +127,128 @@
     }
     
     /**
+     * Can bindings be added to this context?
+     * Defaults to true -- override if not supported
+     */
+    protected boolean isWritable() {
+        return true;
+    }
+    
+    /**
      * Create an initial context to be used in tests
      */
     protected abstract Context makeInitialContext();
     
+    /**
+     *  Add bindings
+     */
+    protected void addBindings() throws Exception {
+        secondContext.bind(firstBoundName(), firstBoundObject());
+        secondContext.bind(secondBoundName(), secondBoundObject());
+        binding.put(firstBoundName(), firstBoundObject());
+        binding.put(secondBoundName(), secondBoundObject());     
+    }
+    
+    /**
+     *  Remove bindings
+     */
+    protected void removeBindings() throws Exception {
+        secondContext.unbind(firstBoundName());
+        secondContext.unbind(secondBoundName());
+        binding.clear(); 
+    }
+    
+    /**
+     *  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
+     *  object is not equal to the originally bound object.
+     */
+    protected void verifyLookup(Object boundObject, Object returnedObject) {
+        assertEquals(boundObject, returnedObject);
+    }
+    
+    /**
+     * Verify that the names and classes in the returned Map (from list() method)
+     * match expected results.  Override this method if the classes of bound objects
+     * returned by list() are not the same as the objects originally bound.
+     */
+    protected void verifyList(Map expected, Map returned) {
+        assertEquals(expected, returned);
+    }
+    
+    /**
+     * Verify that the names and objects in the returned Map (from listBindings() method)
+     * match expected results.  Override this method if bound objects
+     * returned by list() are not the same as the objects originally bound.
+     */
+    protected void verifyListBindings(Map expected, Map returned) {
+        assertEquals(expected, returned);
+    }
+    
+    public AbstractContextTest(String name) {
+        super(name);
+    }
+    
     protected void setUp() throws Exception {
         super.setUp();
+        binding = new HashMap();
         Hashtable env = new Hashtable();
-        envBinding = new HashMap();
         initialContext = makeInitialContext();
-        compContext = initialContext.createSubcontext("java:comp");
-        envContext =  compContext.createSubcontext("env");
-        envContext.bind("hello", "Hello");
-        envContext.bind("world", "World");
-        envBinding.put("hello", "Hello");
-        envBinding.put("world", "World");
+        if (isWritable()) {
+            firstContext = initialContext.createSubcontext(firstContextName());
+            secondContext =  firstContext.createSubcontext(secondContextName());
+            addBindings();
+        }
     }
     
     protected void tearDown() throws Exception {
-        compContext.destroySubcontext("env");
-        initialContext.destroySubcontext("java:comp");
+        if (isWritable()) {
+            removeBindings();
+            firstContext.destroySubcontext(secondContextName());
+            initialContext.destroySubcontext(firstContextName());
+        }
         initialContext = null;
     }
 
     public void testInitialContext() throws NamingException {
-        assertEquals("Hello", initialContext.lookup("java:comp/env/hello"));
-        assertEquals("World", initialContext.lookup(new CompositeName("java:comp/env/world")));
-        assertEquals(envContext.lookup("hello"), 
-            ((Context) envContext.lookup("")).lookup("hello"));  
+        verifyLookup(firstBoundObject(), 
+                initialContext.lookup(firstContextName() + "/"
+                        + secondContextName() +"/" + firstBoundName()));
+        verifyLookup(secondBoundObject(), 
+                initialContext.lookup(new CompositeName
+                        (firstContextName() + "/" + secondContextName()  + "/" + secondBoundName())));
+        verifyLookup(secondContext.lookup(firstBoundName()), 
+            ((Context) secondContext.lookup("")).lookup(firstBoundName()));  
     }
 
     public void testLookup() throws NamingException {
-        assertEquals("Hello", envContext.lookup("hello"));
-        assertEquals("Hello", compContext.lookup("env/hello"));
+        verifyLookup(firstBoundObject(), secondContext.lookup(firstBoundName()));
+        verifyLookup(firstBoundObject(), 
+                firstContext.lookup(secondContextName() + "/" +firstBoundName()));
         try {
-            envContext.lookup("foo");
+            secondContext.lookup("foo");
             fail("expecting NamingException");
         } catch (NamingException e) {
             // OK
         }
-        assertEquals("Hello", envContext.lookup(new CompositeName("hello")));
-        assertEquals("Hello", compContext.lookup(new CompositeName("env/hello")));
-        assertEquals("World", 
-            ((Context) initialContext.lookup("java:comp")).lookup("env/world"));
+        verifyLookup(firstBoundObject(), 
+                secondContext.lookup(new CompositeName(firstBoundName())));
+        verifyLookup(firstBoundObject(), 
+                firstContext.lookup(new CompositeName(secondContextName() + "/" + firstBoundName())));
+        verifyLookup(secondBoundObject(), 
+            ((Context) initialContext.lookup(firstContextName())).lookup(secondContextName()
+ "/" + secondBoundName()));
     }
     
-
     public void testComposeName() throws NamingException {
         assertEquals("org/research/user/jane", 
-            envContext.composeName("user/jane", "org/research"));
+            secondContext.composeName("user/jane", "org/research"));
         assertEquals("research/user/jane", 
-            envContext.composeName("user/jane", "research"));
+            secondContext.composeName("user/jane", "research"));
         assertEquals(new CompositeName("org/research/user/jane"), 
-            envContext.composeName(new CompositeName("user/jane"), 
+            secondContext.composeName(new CompositeName("user/jane"), 
                 new CompositeName("org/research")));
         assertEquals(new CompositeName("research/user/jane"), 
-            envContext.composeName(new CompositeName("user/jane"), 
+            secondContext.composeName(new CompositeName("user/jane"), 
                 new CompositeName("research")));
     }
 
@@ -164,17 +258,17 @@
         Map result;
 
         expected = new HashMap();
-        for (Iterator i = envBinding.entrySet().iterator(); i.hasNext();) {
+        for (Iterator i = binding.entrySet().iterator(); i.hasNext();) {
             Map.Entry entry = (Map.Entry) i.next();
             expected.put(entry.getKey(), entry.getValue().getClass().getName());
         }
-        enum = envContext.list("");
+        enum = secondContext.list("");
         result = new HashMap();
         while (enum.hasMore()) {
             NameClassPair pair = (NameClassPair) enum.next();
             result.put(pair.getName(), pair.getClassName());
         }
-        assertEquals(expected, result);
+        verifyList(expected, result);
 
         try {
             enum.next();
@@ -193,13 +287,13 @@
     public void testListBindings() throws NamingException {
         NamingEnumeration enum;
         Map result;
-        enum = envContext.listBindings("");
+        enum = secondContext.listBindings("");
         result = new HashMap();
         while (enum.hasMore()) {
             Binding pair = (Binding) enum.next();
             result.put(pair.getName(), pair.getObject());
         }
-        assertEquals(envBinding, result);
+        verifyListBindings(binding, result);
 
         try {
             enum.next();
@@ -229,7 +323,7 @@
     		}
     	} else {
     		try {
-    			String name = compContext.getNameInNamespace();
+    			String name = firstContext.getNameInNamespace();
     			fail("Expecting OperationNotSupportedException");
     		} catch(OperationNotSupportedException ex) {
     			// expected

Mime
View raw message