You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/12/08 06:56:11 UTC

svn commit: r111219 - /incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java /incubator/directory/eve/trunk/dib/src/test/org/apache/eve/jndi/PartitionContextBuilderTest.java

Author: akarasulu
Date: Tue Dec  7 21:56:10 2004
New Revision: 111219

URL: http://svn.apache.org/viewcvs?view=rev&rev=111219
Log:
Changes ...

 o added partition configuration builder test cases 
 o ran test case which now pass
 o made changes to builder class after finding bugs with test case


Added:
   incubator/directory/eve/trunk/dib/src/test/org/apache/eve/jndi/PartitionContextBuilderTest.java
Modified:
   incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java

Modified: incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java?view=diff&rev=111219&p1=incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java&r1=111218&p2=incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java&r2=111219
==============================================================================
--- incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java	(original)
+++ incubator/directory/eve/trunk/dib/src/java/org/apache/eve/jndi/PartitionConfigBuilder.java	Tue Dec  7 21:56:10 2004
@@ -20,9 +20,14 @@
 import java.util.Hashtable;
 import java.util.Enumeration;
 
+import javax.naming.NamingException;
+
 import org.apache.eve.ContextPartitionConfig;
 import org.apache.ldap.common.message.LockableAttributesImpl;
 import org.apache.ldap.common.message.LockableAttributeImpl;
+import org.apache.ldap.common.util.ArrayUtils;
+import org.apache.ldap.common.util.StringTools;
+import org.apache.ldap.common.name.LdapName;
 
 
 /**
@@ -34,6 +39,10 @@
  */
 public class PartitionConfigBuilder
 {
+    /** keep this so we do not have create empty ones over and over again */
+    private final static ContextPartitionConfig[] EMPTY = new ContextPartitionConfig[0];
+
+
     /**
      * Extracts properties from a Hashtable and builds a configuration bean for
      * a ContextPartition.
@@ -41,21 +50,52 @@
      * @param id the id of the partition to extract configs for
      * @param env the Hastable containing usually JNDI environment settings
      * @return the extracted configuration object
+     * @throws NamingException if a partition suffix is malformed
      */
     public static ContextPartitionConfig getContextPartitionConfig( String id, Hashtable env )
+            throws NamingException
     {
-        StringBuffer buf = new StringBuffer();
-        ContextPartitionConfig config = new ContextPartitionConfig();
-        LockableAttributesImpl attrs = new LockableAttributesImpl();
+        final StringBuffer buf = new StringBuffer();
+        final ContextPartitionConfig config = new ContextPartitionConfig();
+        final LockableAttributesImpl attrs = new LockableAttributesImpl();
+
+        // --------------------------------------------------------------------
+        // set id, empty attributes, and lookup the suffix for config
+        // --------------------------------------------------------------------
 
         config.setId( id );
-
+        config.setAttributes( attrs );
         buf.append( EnvKeys.SUFFIX ).append( id );
-        config.setSuffix( ( String ) env.get(  buf.toString() ) );
+        String suffix = ( String ) env.get(  buf.toString() );
+
+        if ( suffix != null )
+        {
+            suffix = new LdapName( suffix ).toString();
+        }
+
+        config.setSuffix( suffix );
+
+        // --------------------------------------------------------------------
+        // extract index list and set the list of indices in config
+        // --------------------------------------------------------------------
 
         buf.setLength( 0 );
         buf.append( EnvKeys.INDICES ).append( id );
-        config.setIndices( ( ( String ) env.get( buf.toString() ) ).split( " " ) );
+        String indexList = ( ( String ) env.get( buf.toString() ) );
+
+        if ( indexList == null || indexList.trim().length() == 0 )
+        {
+            config.setIndices( ArrayUtils.EMPTY_STRING_ARRAY );
+        }
+        else
+        {
+            indexList = StringTools.deepTrim( indexList );
+            config.setIndices( indexList.split( " " ) );
+        }
+
+        // --------------------------------------------------------------------
+        // extract attributes and values adding them to the config
+        // --------------------------------------------------------------------
 
         buf.setLength( 0 );
         buf.append( EnvKeys.ATTRIBUTES ).append( id ).append( "." );
@@ -68,12 +108,24 @@
             if ( attrKey.startsWith( keyBase ) )
             {
                 LockableAttributeImpl attr = new LockableAttributeImpl( attrs,
-                        attrKey.substring( attrKey.length() ) ) ;
-                String[] values = ( String[] ) env.get( attrKey );
+                        attrKey.substring( keyBase.length() ) ) ;
+                String valueList = ( String ) env.get( attrKey );
+
+                if ( valueList == null || valueList.trim().length() == 0 )
+                {
+                    // add the empty attribute
+                    attrs.put( attr );
+                    continue;
+                }
+
+                valueList = StringTools.deepTrim( valueList );
+                String[] values = valueList.split( " " );
                 for ( int ii = 0; ii < values.length; ii++ )
                 {
                     attr.add( values[ii] );
                 }
+
+                attrs.put( attr );
             }
         }
 
@@ -87,12 +139,22 @@
      *
      * @param env the Hastable containing usually JNDI environment settings
      * @return all the extracted configuration objects configured
+     * @throws NamingException if a partition suffix is malformed
      */
     public static ContextPartitionConfig[] getContextPartitionConfigs( Hashtable env )
+            throws NamingException
     {
-        final String[] ids = ( String[] ) env.get( EnvKeys.PARTITIONS );
-        final ContextPartitionConfig[] configs = new ContextPartitionConfig[ids.length];
+        String idList = ( String ) env.get( EnvKeys.PARTITIONS );
+
+        // return empty array when we got nothin to work with!
+        if ( idList == null || idList.trim().length() == 0 )
+        {
+            return EMPTY;
+        }
 
+        idList = StringTools.deepTrim( idList );
+        final String[] ids = idList.split( " " );
+        final ContextPartitionConfig[] configs = new ContextPartitionConfig[ids.length];
         for ( int ii = 0; ii < configs.length; ii++ )
         {
             configs[ii] = getContextPartitionConfig( ids[ii], env );

Added: incubator/directory/eve/trunk/dib/src/test/org/apache/eve/jndi/PartitionContextBuilderTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/eve/trunk/dib/src/test/org/apache/eve/jndi/PartitionContextBuilderTest.java?view=auto&rev=111219
==============================================================================
--- (empty file)
+++ incubator/directory/eve/trunk/dib/src/test/org/apache/eve/jndi/PartitionContextBuilderTest.java	Tue Dec  7 21:56:10 2004
@@ -0,0 +1,362 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.eve.jndi;
+
+
+import java.util.Hashtable;
+
+import javax.naming.NamingException;
+
+import org.apache.eve.ContextPartitionConfig;
+import org.apache.ldap.common.util.ArrayUtils;
+import org.apache.ldap.common.message.LockableAttributesImpl;
+import org.apache.ldap.common.message.LockableAttributeImpl;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Testcase which tests the correct operation of the PartitionContextBuilder.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class PartitionContextBuilderTest extends TestCase
+{
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using an empty Hashtable.
+     */
+    public void testEmptyEnvironment() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        ContextPartitionConfig[] configs = null;
+
+        configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
+        assertNotNull( configs );
+        assertEquals( 0, configs.length );
+    }
+
+
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using a Hashtable with only partition names.
+     */
+    public void testPartialConfig() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        ContextPartitionConfig[] configs = null;
+
+        // setup everything and build config bean
+        env.put( EnvKeys.PARTITIONS, "apache test" );
+        configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
+
+        // start testing return values
+        assertNotNull( configs );
+        assertEquals( 2, configs.length );
+
+        // test the apache config bean
+        assertEquals( "apache", configs[0].getId() );
+        assertNull( configs[0].getSuffix() );
+        assertNotNull( configs[0].getAttributes() );
+        assertEquals( 0, configs[0].getAttributes().size() );
+        assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
+                configs[0].getIndices() ) );
+
+        // test the 'test' config bean
+        assertEquals( "test", configs[1].getId() );
+        assertNull( configs[1].getSuffix() );
+        assertNotNull( configs[1].getAttributes() );
+        assertEquals( 0, configs[1].getAttributes().size() );
+        assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
+                configs[1].getIndices() ) );
+    }
+
+
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using a Hashtable with only partition names but the list property has
+     * extra spaces in between and at the ends.
+     */
+    public void testPartialConfigWithExtraWhitespace() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        ContextPartitionConfig[] configs = null;
+
+        // setup everything and build config bean
+        env.put( EnvKeys.PARTITIONS, "  apache        test " );
+        configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
+
+        // start testing return values
+        assertNotNull( configs );
+        assertEquals( 2, configs.length );
+
+        // test the apache config bean
+        assertEquals( "apache", configs[0].getId() );
+        assertNull( configs[0].getSuffix() );
+        assertNotNull( configs[0].getAttributes() );
+        assertEquals( 0, configs[0].getAttributes().size() );
+        assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
+                configs[0].getIndices() ) );
+
+        // test the 'test' config bean
+        assertEquals( "test", configs[1].getId() );
+        assertNull( configs[1].getSuffix() );
+        assertNotNull( configs[1].getAttributes() );
+        assertEquals( 0, configs[1].getAttributes().size() );
+        assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
+                configs[1].getIndices() ) );
+    }
+
+
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using a Hashtable with partitions that have a suffix.  Correctness with
+     * whitespace varience is tested.
+     */
+    public void testSuffixKeys() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        ContextPartitionConfig[] configs = null;
+
+        // setup everything and build config bean
+        env.put( EnvKeys.PARTITIONS, "apache test" );
+        env.put( EnvKeys.SUFFIX + "apache", " dc= apache,    dc=org" );
+        env.put( EnvKeys.SUFFIX + "test", "   ou   =  test " );
+        configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
+
+        // start testing return values
+        assertNotNull( configs );
+        assertEquals( 2, configs.length );
+
+        // test the apache config bean
+        assertEquals( "apache", configs[0].getId() );
+        assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
+        assertNotNull( configs[0].getAttributes() );
+        assertEquals( 0, configs[0].getAttributes().size() );
+        assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
+                configs[0].getIndices() ) );
+
+        // test the 'test' config bean
+        assertEquals( "test", configs[1].getId() );
+        assertEquals( "ou=test", configs[1].getSuffix() );
+        assertNotNull( configs[1].getAttributes() );
+        assertEquals( 0, configs[1].getAttributes().size() );
+        assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
+                configs[1].getIndices() ) );
+    }
+
+
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using a Hashtable with partitions that have malformed suffix
+     * distinguished names.  We test for failure.
+     */
+    public void testSuffixKeysWithMalformedDN()
+    {
+        Hashtable env = new Hashtable();
+
+        // setup everything and build config bean
+        env.put( EnvKeys.PARTITIONS, "apache test" );
+        env.put( EnvKeys.SUFFIX + "apache", " dcapachedcorg" );
+
+        try
+        {
+            PartitionConfigBuilder.getContextPartitionConfigs( env );
+            fail( "should never get here due to an exception" );
+        }
+        catch( NamingException e )
+        {
+        }
+    }
+
+
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using a Hashtable with partitions that have suffixes and indices set.
+     */
+    public void testIndexKeys() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        ContextPartitionConfig[] configs = null;
+
+        // setup everything and build config bean
+        env.put( EnvKeys.PARTITIONS, "apache test" );
+        env.put( EnvKeys.SUFFIX  + "apache", "dc=apache,dc=org" );
+        env.put( EnvKeys.SUFFIX  + "test", "ou=test" );
+        env.put( EnvKeys.INDICES + "apache", "ou objectClass      uid" );
+        env.put( EnvKeys.INDICES + "test", "ou objectClass  " );
+        configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
+
+        // start testing return values
+        assertNotNull( configs );
+        assertEquals( 2, configs.length );
+
+        // test the apache config bean
+        assertEquals( "apache", configs[0].getId() );
+        assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
+        assertNotNull( configs[0].getAttributes() );
+        assertEquals( 0, configs[0].getAttributes().size() );
+        assertEquals( 3, configs[0].getIndices().length );
+        assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass", "uid" },
+                configs[0].getIndices() ) );
+
+        // test the 'test' config bean
+        assertEquals( "test", configs[1].getId() );
+        assertEquals( "ou=test", configs[1].getSuffix() );
+        assertNotNull( configs[1].getAttributes() );
+        assertEquals( 0, configs[1].getAttributes().size() );
+        assertEquals( 2, configs[1].getIndices().length );
+        assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass" },
+                configs[1].getIndices() ) );
+    }
+
+
+
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using a Hashtable with partitions that have suffixes, indices and
+     * attributes set.
+     */
+    public void testAttributeKeys() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        ContextPartitionConfig[] configs = null;
+
+        // setup everything and build config bean
+        env.put( EnvKeys.PARTITIONS, "apache test" );
+        env.put( EnvKeys.SUFFIX  + "apache", "dc=apache,dc=org" );
+        env.put( EnvKeys.SUFFIX  + "test", "ou=test" );
+        env.put( EnvKeys.INDICES + "apache", "ou objectClass      uid" );
+        env.put( EnvKeys.INDICES + "test", "ou objectClass  " );
+        env.put( EnvKeys.ATTRIBUTES + "apache" + ".dc", "apache" );
+        env.put( EnvKeys.ATTRIBUTES + "apache" + ".objectClass", "top domain extensibleObject" );
+        env.put( EnvKeys.ATTRIBUTES + "test" + ".ou", "test" );
+        env.put( EnvKeys.ATTRIBUTES + "test" + ".objectClass", "top extensibleObject organizationalUnit" );
+        configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
+
+        // start testing return values
+        assertNotNull( configs );
+        assertEquals( 2, configs.length );
+
+        // test the apache config bean
+        assertEquals( "apache", configs[0].getId() );
+        assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
+        assertNotNull( configs[0].getAttributes() );
+        assertEquals( 2, configs[0].getAttributes().size() );
+        assertEquals( 3, configs[0].getIndices().length );
+        assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass", "uid" },
+                configs[0].getIndices() ) );
+        LockableAttributesImpl attrs = new LockableAttributesImpl();
+        LockableAttributeImpl attr = new LockableAttributeImpl( "dc" );
+        attrs.put( attr );
+        attr.add( "apache" );
+        attr = new LockableAttributeImpl( "objectClass" );
+        attrs.put( attr );
+        attr.add( "top" );
+        attr.add( "domain" );
+        attr.add( "extensibleObject" );
+        assertTrue( attrs.equals( configs[0].getAttributes() ) );
+
+        // test the 'test' config bean
+        assertEquals( "test", configs[1].getId() );
+        assertEquals( "ou=test", configs[1].getSuffix() );
+        assertNotNull( configs[1].getAttributes() );
+        assertEquals( 2, configs[1].getAttributes().size() );
+        assertEquals( 2, configs[1].getIndices().length );
+        assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass" },
+                configs[1].getIndices() ) );
+        attrs = new LockableAttributesImpl();
+        attr = new LockableAttributeImpl( "ou" );
+        attrs.put( attr );
+        attr.add( "test" );
+        attr = new LockableAttributeImpl( "objectClass" );
+        attrs.put( attr );
+        attr.add( "top" );
+        attr.add( "extensibleObject" );
+        attr.add( "organizationalUnit" );
+        assertTrue( attrs.equals( configs[1].getAttributes() ) );
+    }
+
+
+    /**
+     * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
+     * using a Hashtable with partitions that have suffixes, indices and
+     * attributes set however values have some space variance.
+     */
+    public void testAttributeValuesWithWhitespace() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        ContextPartitionConfig[] configs = null;
+
+        // setup everything and build config bean
+        env.put( EnvKeys.PARTITIONS, "apache test" );
+        env.put( EnvKeys.SUFFIX  + "apache", "dc=apache,dc=org" );
+        env.put( EnvKeys.SUFFIX  + "test", "ou=test" );
+        env.put( EnvKeys.INDICES + "apache", "ou objectClass      uid" );
+        env.put( EnvKeys.INDICES + "test", "ou objectClass  " );
+        env.put( EnvKeys.ATTRIBUTES + "apache" + ".dc", "apache" );
+        env.put( EnvKeys.ATTRIBUTES + "apache" + ".objectClass",
+                "     top    domain    extensibleObject " );
+        env.put( EnvKeys.ATTRIBUTES + "test" + ".ou", "test" );
+        env.put( EnvKeys.ATTRIBUTES + "test" + ".objectClass",
+                "top extensibleObject    organizationalUnit" );
+        configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
+
+        // start testing return values
+        assertNotNull( configs );
+        assertEquals( 2, configs.length );
+
+        // test the apache config bean
+        assertEquals( "apache", configs[0].getId() );
+        assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
+        assertNotNull( configs[0].getAttributes() );
+        assertEquals( 2, configs[0].getAttributes().size() );
+        assertEquals( 3, configs[0].getIndices().length );
+        assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass", "uid" },
+                configs[0].getIndices() ) );
+        LockableAttributesImpl attrs = new LockableAttributesImpl();
+        LockableAttributeImpl attr = new LockableAttributeImpl( "dc" );
+        attrs.put( attr );
+        attr.add( "apache" );
+        attr = new LockableAttributeImpl( "objectClass" );
+        attrs.put( attr );
+        attr.add( "top" );
+        attr.add( "domain" );
+        attr.add( "extensibleObject" );
+        assertTrue( attrs.equals( configs[0].getAttributes() ) );
+
+        // test the 'test' config bean
+        assertEquals( "test", configs[1].getId() );
+        assertEquals( "ou=test", configs[1].getSuffix() );
+        assertNotNull( configs[1].getAttributes() );
+        assertEquals( 2, configs[1].getAttributes().size() );
+        assertEquals( 2, configs[1].getIndices().length );
+        assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass" },
+                configs[1].getIndices() ) );
+        attrs = new LockableAttributesImpl();
+        attr = new LockableAttributeImpl( "ou" );
+        attrs.put( attr );
+        attr.add( "test" );
+        attr = new LockableAttributeImpl( "objectClass" );
+        attrs.put( attr );
+        attr.add( "top" );
+        attr.add( "extensibleObject" );
+        attr.add( "organizationalUnit" );
+        assertTrue( attrs.equals( configs[1].getAttributes() ) );
+    }
+}