directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: rev 6824 - incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources
Date Sun, 22 Feb 2004 19:38:59 GMT
Author: psteitz
Date: Sun Feb 22 11:38:58 2004
New Revision: 6824

Modified:
   incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java
Log:
Added schema and attribute modification tests, improved documentation.

Modified: incubator/directory/naming/trunk/core/src/test/org/apache/naming/resources/AbstractDirContextTest.java
==============================================================================
--- 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
Sun Feb 22 11:38:58 2004
@@ -20,17 +20,30 @@
 import java.util.Map;
 
 import javax.naming.directory.DirContext;
-import javax.naming.directory.Attributes;
 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;
 
-
-
 /**
- * Unit tests for basic ops on a {@link DirContext}.
+ * 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:
+ * 
+ * -- 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).
  *  
  * @version $Revision: 1.2 $ $Date: 2003/11/30 05:36:07 $
  */
@@ -39,8 +52,7 @@
         super(name);
     }
     
-    /** A few bytes to write to temp files created */
-    protected byte[] bytes = {'a', 'b', 'c'}; 
+    // ----------------  Override test data defaults from AbstractContextTest ---------------------
     
     /** firstContext name -- relative to InitialContext  */
     protected String firstContextName() {
@@ -72,6 +84,94 @@
         return new Resource(new ByteArrayInputStream(bytes));
     }
     
+    //---------------------- Test data for DirContext tests ---------------------------------------
+    
+    /** A few bytes to write to temp files created */
+    protected byte[] bytes = {'a', 'b', 'c'}; 
+    
+    /**
+     * Expected attributes associated with firstBoundObject()
+     */
+    protected Attributes expectedAttributes() {
+        return null;
+    }
+    
+    /** Name parser from initialContext */
+    protected NameParser nameParser = null;
+    
+    
+    //-------------- Switches to turn off tests if features are not supported ---------------------
+
+    /**
+     * Does this DirContext support shema -- i.e. implement getSchema(), getSchemaClassDefinition()?
+     * Override to return false if schema methods are not supported.
+     */
+    protected boolean isSchemaSupported() {
+        return true;
+    }
+    
+    /**
+     * Does this DirContext support attribute modification?
+     * Override to return false if attributes cannot be modified.
+     */
+    protected boolean isAttributeModificationSupported() {
+        return true;
+    }
+    
+    //---------------- Attribute names and values for modification tests --------------------------
+    /** 
+     *   Name of the attribute associated with firstBoundObject() to replace. 
+     *   Override to return the name of an attribute of firstBoundObject() whose value can
be replaced
+     *   if this is supported 
+     */
+    protected String replaceAttributeName() {
+        return null;
+    }
+    
+    /** 
+     *   Replacement value for replaceAttributeName.
+     *   Override to return the replacement value if this is supported.
+     */
+    protected Object replaceAttributeValue() {
+        return null;
+    }
+    
+    /** 
+     *   Name of attribute of firstBoundObject() object to add. 
+     *   Override to return the name of a new attribute to add to the attributes of firstBoundObject()
+     *   if this is supported.
+     */
+    protected String addAttributeName() {
+        return null;
+    }
+    
+    /** 
+     *   Value for addAttributeName.
+     *   Override to return the value for the new attribute if this is supported.
+     */
+    protected Object addAttributeValue() {
+        return null;
+    }
+    
+    /** 
+     *   Name of attribute of firstBoundObject() to remove. 
+     *   Override to return the name of an attribute to remove from the attributes of firstBoundObject()
+     *   if this is supported.
+     */
+    protected String removeAttributeName() {
+        return null;
+    }
+    
+    // --------------  Override AbstractContextTest.setup to add nameParser intialization
-----------------------------
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        nameParser = initialContext.getNameParser("");
+    }
+    
+    // -------------------------------- 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
@@ -91,8 +191,36 @@
         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>
+     */
+    protected void verifyAttribute(DirContext context, String name, String attributeName,

+            Object attributeValue) throws Exception {
+        String attrIds[] = new String[1];
+        attrIds[0] = attributeName;
+        Attributes attrs = context.getAttributes(name, attrIds);
+        assertTrue(attrs.get(attributeName).contains(attributeValue)); 
+    }
+    
+    /**
+     * Verify that the Attributes associated with <name> in <context> do not
include an Attribute with 
+     * attribute ID = <attributeName>
+     */
+    protected void verifyAttributeMissing(DirContext context, String name, String attributeName)
throws Exception {
+        String attrIds[] = new String[1];
+        attrIds[0] = attributeName;
+        Attributes attrs = context.getAttributes(name, attrIds);
+        assertEquals(0, attrs.size()); 
+    }
+    
+    //----------------------------- Default implementations for basic tests --------------------------------------------
+    
     /**
-     * Verify that getAttributes returns a valid NamingEnumeration of Attributes
+     * Verify that getAttributes returns a valid NamingEnumeration of Attributes.
+     * If expectedAttributes() has been overriden to return a non-null set of attributes,

+     * this set is tested for equality with the Attributes returned by getAttributes.
      */
    public void testAttributes() throws Exception {
       DirContext context = (DirContext) initialContext.lookup(firstContextName()+ "/" + secondContextName());
@@ -101,6 +229,76 @@
       while (enum.hasMoreElements()) {
           assertTrue(enum.nextElement() instanceof Attribute);
       }
+      Attributes expected = expectedAttributes();
+      if (expected != null) {
+          assertEquals(expected, attrs);
+      }
    }
-    
+   
+   /**
+    * Verify that getSchema and getSchemaClassDefinition return non-null DirContexts.
+    * This test will only be run if isSchemaSupported returns true.
+    * @throws Exception
+    */
+   public void testGetSchema() throws Exception {
+       if (!isSchemaSupported()) {
+           try {
+               ((DirContext) initialContext.lookup("")).getSchema("");
+               fail("Expecting OperationNotSupportedException");
+           } catch (OperationNotSupportedException ex) {
+               // expected
+           }
+           return;
+       }
+       DirContext context = (DirContext) initialContext.lookup("");
+       DirContext schemaContext = context.getSchema("");
+       assertNotNull(schemaContext);
+       Name name = nameParser.parse(firstContext.getNameInNamespace());
+       schemaContext = context.getSchema(name);
+       assertNotNull(schemaContext);
+       schemaContext = context.getSchema(firstBoundName());
+       assertNotNull(schemaContext);
+       schemaContext = context.getSchemaClassDefinition(name);
+       assertNotNull(schemaContext);
+       schemaContext = context.getSchemaClassDefinition(firstBoundName());
+       assertNotNull(schemaContext);          
+   }
+   
+   /**
+    * Tests attribute modification operations on attributes -- add, update, delete.
+    * If no attribute modifications are supported, override isAttributeModificationSupported()
to return
+    * false and this test will be skipped.  If one or more of add, update, delete are not
supported,
+    * just leave the associated *attributeName() method returning null and the associated
operation
+    * will not be tested. 
+    */
+   public void testAttributeModification() throws Exception {
+       if (!isAttributeModificationSupported()) {
+           return;
+       }    
+       ModificationItem[] modifications = new ModificationItem[1];
+       DirContext context = (DirContext) initialContext.lookup(firstContextName() + "/"
+               + secondContextName() +"/" + firstBoundName()); 
+      
+      if(addAttributeName() != null) {
+          Attribute addModification = new BasicAttribute(addAttributeName(), addAttributeValue());
+          modifications[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, addModification);
    
+          context.modifyAttributes("", modifications);
+          verifyAttribute(context, "", addAttributeName(), addAttributeValue());
+      }
+      
+      if(replaceAttributeName() != null) {
+          Attribute replaceModification = new BasicAttribute(replaceAttributeName(), replaceAttributeValue());
+          modifications[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, replaceModification);
    
+          context.modifyAttributes("", modifications);
+          verifyAttribute(context, "", replaceAttributeName(), replaceAttributeValue());
+      }
+      
+      if(removeAttributeName() != null) {
+          Attribute removeModification = new BasicAttribute(removeAttributeName());
+          modifications[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, removeModification);
    
+          context.modifyAttributes("", modifications);
+          verifyAttributeMissing(context, "", removeAttributeName());
+      }
+   }
+  
 }

Mime
View raw message