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/12/28 01:32:11 UTC

svn commit: r123458 - /incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java

Author: psteitz
Date: Mon Dec 27 16:32:09 2004
New Revision: 123458

URL: http://svn.apache.org/viewcvs?view=rev&rev=123458
Log:
Removed dependency of AbstractDirContextTest on AbstractContextTest.
Modified:
   incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java

Modified: incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java?view=diff&rev=123458&p1=incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java&r1=123457&p2=incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java&r2=123458
==============================================================================
--- incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java	(original)
+++ incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java	Mon Dec 27 16:32:09 2004
@@ -16,43 +16,85 @@
 package org.apache.naming.resources;
 
 import java.io.ByteArrayInputStream;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.NoSuchElementException;
 
+import javax.naming.Binding;
+import javax.naming.CompositeName;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.NameParser;
+import javax.naming.OperationNotSupportedException;
 import javax.naming.directory.DirContext;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttribute;
 import javax.naming.directory.ModificationItem;
-import javax.naming.Name;
-import javax.naming.NamingEnumeration;
-import javax.naming.NameParser;
-import javax.naming.OperationNotSupportedException;
 
-import org.apache.naming.AbstractContextTest;
-import org.apache.naming.resources.Resource;
+import junit.framework.TestCase;
 
 /**
  * Abstract base class for DirContext tests.
  * 
- * Extends AbstractContextTest to include constructs and tests for DirContext implementations.
- * 
- * In addition to overriding AbstractConextTest.makeInitialContext() and setting switches defined
- * in AbstractContextTest, test classes derived from this class should:
+ * In addition to overriding makeInitialContext() and 
+ * setting switches, test classes derived from this class should:
  * 
  * -- override isSchemaSupported() to return false if schema are not supported
- * -- override isAttributeModificationSupported() to return false if attribute modification is not supported
- * -- if attribute modification operations are supported, override modifyAttributeName(), modifyAttributeValue(),
- *     etc. to designate names and values appropriate for firstBoundObject() (from AbstractContextTest).
+ * -- override isAttributeModificationSupported() to return false if attribute
+ *     modification is not supported
+ * -- if attribute modification operations are supported, override 
+ *     modifyAttributeName(), modifyAttributeValue(), etc. to designate names
+ *     and values appropriate for firstBoundObject() (from AbstractContextTest).
  *  
  * @version $Revision: 1.2 $ $Date: 2003/11/30 05:36:07 $
  */
-public abstract class AbstractDirContextTest extends AbstractContextTest {
+public abstract class AbstractDirContextTest extends TestCase {
     public AbstractDirContextTest(String name) {
         super(name);
     }
+    //-------------------------- Contexts to use in tests ----------------------------------------------------
+    
+    /** Initial Context used in tests */
+    protected Context initialContext;
+    
+    /** Immediate subcontext of initialContext */
+    protected Context firstContext;
     
-    // ----------------  Override test data defaults from AbstractContextTest ---------------------
+    /** Immediate subcontext of firstContext */
+    protected Context secondContext;
+    
+    /** HashMap of Object Bindings for verification */
+    protected HashMap binding;
+    
+  //----------------- Switches to turn off tests for unsupported operations ----------------------
+    
+    /**
+     * Does this context support getNameInNamespace()?
+     * Defaults to true -- override if not supported
+     */
+    protected boolean isGetNameInNamespaceSupported() {
+        return true;
+    }
+    
+    /**
+     * 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();
+    
+    //-------------------- Override these methods to set up test namespace ------------------------
     
     /** firstContext name -- relative to InitialContext  */
     protected String firstContextName() {
@@ -84,6 +126,48 @@
         return new Resource(new ByteArrayInputStream(bytes));
     }
     
+//----------------------------------  Setup / teardown operations -----------------------------
+    
+    /**
+     *  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(); 
+    }
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        binding = new HashMap();
+        initialContext = makeInitialContext();
+        if (isWritable()) {
+            firstContext = initialContext.createSubcontext(firstContextName());
+            secondContext =  firstContext.createSubcontext(secondContextName());
+            addBindings();
+        }
+        nameParser = initialContext.getNameParser("");
+    }
+    
+    protected void tearDown() throws Exception {
+        if (isWritable()) {
+            removeBindings();
+            firstContext.destroySubcontext(secondContextName());
+            initialContext.destroySubcontext(firstContextName());
+        }
+        initialContext = null;
+    }
+    
     //---------------------- Test data for DirContext tests ---------------------------------------
     
     /** A few bytes to write to temp files created */
@@ -99,7 +183,6 @@
     /** Name parser from initialContext */
     protected NameParser nameParser = null;
     
-    
     //-------------- Switches to turn off tests if features are not supported ---------------------
 
     /**
@@ -162,16 +245,17 @@
         return null;
     }
     
-    // --------------  Override AbstractContextTest.setup to add nameParser intialization -----------------------------
+    // -------------------------------- Verification methods -----------------------------------------------
     
-    protected void setUp() throws Exception {
-        super.setUp();
-        nameParser = initialContext.getNameParser("");
+    /**
+     *  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);
     }
     
-    // -------------------------------- Verification methods -----------------------------------------------
-    
-    //--------------------------------- Overrides of AbstractContextTest defaults -------------------
     /**
      * Verify that the listed bound names match expectation and
      * that the class names of bound objects are not empty
@@ -191,7 +275,6 @@
         assertEquals(expected.keySet(), returned.keySet());
     }
     
-    //--------------------------------------- DirContext verification methods ------------------------------------
     /**
      * Verify that the Attributes associated with <name> in <context> include an Attribute with 
      * attribute ID = <attributeName> and this Attribute contains the value <attributeValue>
@@ -215,7 +298,145 @@
         assertEquals(0, attrs.size()); 
     }
     
-    //----------------------------- Default implementations for basic tests --------------------------------------------
+    //--------------------------- Default implementations for basic tests -------------------------- 
+
+    public void testInitialContext() throws NamingException {
+        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 {
+        verifyLookup(firstBoundObject(), secondContext.lookup(firstBoundName()));
+        verifyLookup(firstBoundObject(), 
+                firstContext.lookup(secondContextName() + "/" +firstBoundName()));
+        try {
+            secondContext.lookup("foo");
+            fail("expecting NamingException");
+        } catch (NamingException e) {
+            // expected
+        }
+        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", 
+                secondContext.composeName("user/jane", "org/research"));
+        assertEquals("research/user/jane", 
+                secondContext.composeName("user/jane", "research"));
+        assertEquals(new CompositeName("org/research/user/jane"), 
+                secondContext.composeName(new CompositeName("user/jane"), 
+                        new CompositeName("org/research")));
+        assertEquals(new CompositeName("research/user/jane"), 
+                secondContext.composeName(new CompositeName("user/jane"), 
+                        new CompositeName("research")));
+    }
+
+    public void testList() throws NamingException {
+        NamingEnumeration enumeration;
+        Map expected;
+        Map result;
+
+        expected = new HashMap();
+        for (Iterator i = binding.entrySet().iterator(); i.hasNext();) {
+            Map.Entry entry = (Map.Entry) i.next();
+            expected.put(entry.getKey(), entry.getValue().getClass().getName());
+        }
+        enumeration = secondContext.list("");
+        result = new HashMap();
+        while (enumeration.hasMore()) {
+            NameClassPair pair = (NameClassPair) enumeration.next();
+            result.put(pair.getName(), pair.getClassName());
+        }
+        verifyList(expected, result);
+
+        try {
+            enumeration.next();
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+        try {
+            enumeration.nextElement();
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+
+    public void testListBindings() throws NamingException {
+        NamingEnumeration enumeration;
+        Map result;
+        enumeration = secondContext.listBindings("");
+        result = new HashMap();
+        while (enumeration.hasMore()) {
+            Binding pair = (Binding) enumeration.next();
+            result.put(pair.getName(), pair.getObject());
+        }
+        verifyListBindings(binding, result);
+
+        try {
+            enumeration.next();
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+        try {
+            enumeration.nextElement();
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+    
+    /**
+     * Default implementation just tests to make sure non-null names are returned
+     * or correct exception is thrown.
+     */
+    public void testGetNameInNamespace() throws Exception {
+        if (isGetNameInNamespaceSupported()) {
+            String name = initialContext.getNameInNamespace();
+            if (name == null) {
+                fail("Null NameInNamespace for initial context");
+            }
+        } else {
+            try {
+                String name = firstContext.getNameInNamespace();
+                fail("Expecting OperationNotSupportedException");
+            } catch(OperationNotSupportedException ex) {
+                // expected
+            }
+        }   
+    }
+    
+    /**
+     *  Test rebind -- swap bound objects and verify
+     */
+    public void testRebind() throws Exception {
+        secondContext.rebind(firstBoundName(), secondBoundObject());
+        secondContext.rebind(secondBoundName(), firstBoundObject());
+        binding.put(firstBoundName(), secondBoundObject());
+        binding.put(secondBoundName(), firstBoundObject());      
+        NamingEnumeration enumeration;
+        Map result;
+        enumeration = secondContext.listBindings("");
+        result = new HashMap();
+        while (enumeration.hasMore()) {
+            Binding pair = (Binding) enumeration.next();
+            result.put(pair.getName(), pair.getObject());
+        }
+        verifyListBindings(binding, result);     
+    }
     
     /**
      * Verify that getAttributes returns a valid NamingEnumeration of Attributes.