directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r566915 - in /directory/apacheds/trunk/server-unit/src: main/java/org/apache/directory/server/unit/ test/java/org/apache/directory/server/ test/resources/ test/resources/org/ test/resources/org/apache/ test/resources/org/apache/directory/ t...
Date Fri, 17 Aug 2007 04:38:03 GMT
Author: akarasulu
Date: Thu Aug 16 21:37:58 2007
New Revision: 566915

URL: http://svn.apache.org/viewvc?view=rev&rev=566915
Log:
Preparing to add negation operator tests by adding some utility methods
that will reduce the amount of code in most tests

 o added methods to get JNDI context to rootDSE through wired connections
 o added methods that load LDIF files named after the test case 
 o cleaned up some warning associated with generics 
 o added stub test class for negations which only test the correct operation
   of the newly added functionality in AbstractServerTest at this point


Added:
    directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/NegationOperatorITest.java
    directory/apacheds/trunk/server-unit/src/test/resources/org/
    directory/apacheds/trunk/server-unit/src/test/resources/org/apache/
    directory/apacheds/trunk/server-unit/src/test/resources/org/apache/directory/
    directory/apacheds/trunk/server-unit/src/test/resources/org/apache/directory/server/
    directory/apacheds/trunk/server-unit/src/test/resources/org/apache/directory/server/NegationOperatorITest.ldif
Modified:
    directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
    directory/apacheds/trunk/server-unit/src/test/resources/log4j.properties

Modified: directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java?view=diff&rev=566915&r1=566914&r2=566915
==============================================================================
--- directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
(original)
+++ directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
Thu Aug 16 21:37:58 2007
@@ -23,13 +23,19 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 
+import javax.naming.ConfigurationException;
 import javax.naming.Context;
 import javax.naming.InitialContext;
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 
@@ -44,6 +50,8 @@
 import org.apache.directory.shared.ldap.ldif.LdifReader;
 import org.apache.directory.shared.ldap.name.LdapDN;
 import org.apache.mina.util.AvailablePortFinder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -54,6 +62,10 @@
  */
 public abstract class AbstractServerTest extends TestCase
 {
+    private static final Logger log = LoggerFactory.getLogger( AbstractServerTest.class );
+    private static final List<Entry> EMPTY_LIST = Collections.unmodifiableList( new
ArrayList<Entry>( 0 ) );
+    private static final String CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
+
     /** the context root for the system partition */
     protected LdapContext sysRoot;
 
@@ -63,6 +75,9 @@
     /** the context root for the schema */
     protected LdapContext schemaRoot;
 
+    /** flag that indicates whether or not the server has started */
+    private boolean serverOnline = false;
+    
     /** flag whether to delete database files for each test or not */
     protected boolean doDelete = true;
 
@@ -70,8 +85,135 @@
 
     protected int port = -1;
 
+    
+    /**
+     * Tells subclasses whether or not the server is online.
+     *
+     * @return true if the server has started false otherwise.
+     */
+    public boolean isServerOnline()
+    {
+        return serverOnline;
+    }
+    
+    
+    /**
+     * If there is an LDIF file with the same name as the test class 
+     * but with the .ldif extension then it is read and the entries 
+     * it contains are added to the server.  It appears as though the
+     * administor adds these entries to the server.
+     *
+     * @param verifyEntries whether or not all entry additions are checked
+     * to see if they were in fact correctly added to the server
+     * @return a list of entries added to the server in the order they were added
+     * @throws NamingException
+     */
+    protected List<Entry> loadTestLdif( boolean verifyEntries ) throws NamingException
+    {
+        InputStream in = getClass().getResourceAsStream( getClass().getSimpleName() + ".ldif"
);
+        if ( in == null )
+        {
+            return EMPTY_LIST;
+        }
+        
+        if ( ! serverOnline )
+        {
+            throw new ConfigurationException( "The server has not been started - cannot add
entries." );
+        }
+        
+        LdifReader ldifReader = new LdifReader( in );
+        List<Entry> entries = new ArrayList<Entry>();
+        while ( ldifReader.hasNext() )
+        {
+            Entry entry = ldifReader.next();
+            rootDSE.createSubcontext( entry.getDn(), entry.getAttributes() );
+            
+            if ( verifyEntries )
+            {
+                verify( entry );
+                log.info( "Successfully verified addition of entry {}", entry.getDn() );
+            }
+            else
+            {
+                log.info( "Added entry {} without verification", entry.getDn() );
+            }
+            
+            entries.add( entry );
+        }
+        
+        return entries;
+    }
+    
 
     /**
+     * Verifies that an entry exists in the directory with the 
+     * specified attributes.
+     *
+     * @param entry the entry to verify
+     * @throws NamingException if there are problems accessing the entry
+     */
+    protected void verify( Entry entry ) throws NamingException
+    {
+        Attributes readAttributes = rootDSE.getAttributes( entry.getDn() );
+        NamingEnumeration<String> readIds = entry.getAttributes().getIDs();
+        while ( readIds.hasMore() )
+        {
+            String id = readIds.next();
+            Attribute readAttribute = readAttributes.get( id );
+            Attribute origAttribute = entry.getAttributes().get( id );
+            
+            for ( int ii = 0; ii < origAttribute.size(); ii++ )
+            {
+                assertTrue( readAttribute.contains( origAttribute.get( ii ) ) );
+            }
+        }
+    }
+    
+
+    /**
+     * Common code to get an initial context via a simple bind to the 
+     * server over the wire using the SUN JNDI LDAP provider. Do not use 
+     * this method until after the setUp() method is called to start the
+     * server otherwise it will fail. 
+     *
+     * @return an LDAP context as the the administrator to the rootDSE
+     * @throws NamingException if the server cannot be contacted
+     */
+    protected LdapContext getWiredContext() throws NamingException
+    {
+        return getWiredContext( "uid=admin,ou=system", "secret" );
+    }
+    
+    
+    /**
+     * Common code to get an initial context via a simple bind to the 
+     * server over the wire using the SUN JNDI LDAP provider. Do not use 
+     * this method until after the setUp() method is called to start the
+     * server otherwise it will fail.
+     *
+     * @param bindPrincipalDn the DN of the principal to bind as
+     * @param password the password of the bind principal
+     * @return an LDAP context as the the administrator to the rootDSE
+     * @throws NamingException if the server cannot be contacted
+     */
+    protected LdapContext getWiredContext( String bindPrincipalDn, String password ) throws
NamingException
+    {
+        if ( ! serverOnline )
+        {
+            throw new ConfigurationException( "The server is not online! Cannot connect to
it." );
+        }
+        
+        Hashtable<String, String> env = new Hashtable<String, String>();
+        env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY );
+        env.put( Context.PROVIDER_URL, "ldap://localhost:" + port );
+        env.put( Context.SECURITY_PRINCIPAL, bindPrincipalDn );
+        env.put( Context.SECURITY_CREDENTIALS, password );
+        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
+        return new InitialLdapContext( env, null );
+    }
+    
+    
+    /**
      * Get's the initial context factory for the provider's ou=system context
      * root.
      *
@@ -85,7 +227,17 @@
         port = AvailablePortFinder.getNextAvailable( 1024 );
         configuration.getLdapConfiguration().setIpPort( port );
         configuration.setShutdownHookEnabled( false );
-        setContexts( "uid=admin,ou=system", "secret" );
+        
+        try
+        {
+            setContexts( "uid=admin,ou=system", "secret" );
+            serverOnline = true;
+        }
+        catch( Exception e )
+        {
+            serverOnline = false;
+            throw e;
+        }
     }
 
 
@@ -166,6 +318,7 @@
         try
         {
             new InitialContext( env );
+            serverOnline = false;
         }
         catch ( Exception e )
         {
@@ -190,11 +343,11 @@
     {
         try
         {
-            Iterator iterator = new LdifReader( in );
+            Iterator<Entry> iterator = new LdifReader( in );
 
             while ( iterator.hasNext() )
             {
-                Entry entry = ( Entry ) iterator.next();
+                Entry entry = iterator.next();
                 LdapDN dn = new LdapDN( entry.getDn() );
                 rootDSE.createSubcontext( dn, entry.getAttributes() );
             }

Added: directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/NegationOperatorITest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/NegationOperatorITest.java?view=auto&rev=566915
==============================================================================
--- directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/NegationOperatorITest.java
(added)
+++ directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/NegationOperatorITest.java
Thu Aug 16 21:37:58 2007
@@ -0,0 +1,68 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.server;
+
+
+import javax.naming.ldap.LdapContext;
+
+import org.apache.directory.server.unit.AbstractServerTest;
+
+
+/**
+ * A set of tests to make sure the negation operator is working 
+ * properly when included in search filters. Created in response
+ * to JIRA issue 
+ * <a href="https://issues.apache.org/jira/browse/DIRSERVER-951">DIRSERVER-951</a>.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 519077 $
+ */
+public class NegationOperatorITest extends AbstractServerTest
+{
+    private LdapContext ctx = null;
+
+
+    /**
+     * Create context and entries for tests.
+     */
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        ctx = getWiredContext();
+        assertNotNull( ctx );
+    }
+
+
+    /**
+     * Closes context and destroys server.
+     */
+    public void tearDown() throws Exception
+    {
+        ctx.close();
+        ctx = null;
+        super.tearDown();
+    }
+    
+    
+    public void testLoad() throws Exception
+    {
+        assertEquals( 2, super.loadTestLdif( true ).size() );
+    }
+}

Modified: directory/apacheds/trunk/server-unit/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-unit/src/test/resources/log4j.properties?view=diff&rev=566915&r1=566914&r2=566915
==============================================================================
--- directory/apacheds/trunk/server-unit/src/test/resources/log4j.properties (original)
+++ directory/apacheds/trunk/server-unit/src/test/resources/log4j.properties Thu Aug 16 21:37:58
2007
@@ -1,4 +1,4 @@
-log4j.rootCategory=OFF, stdout
+log4j.rootCategory=INFO, stdout
 
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

Added: directory/apacheds/trunk/server-unit/src/test/resources/org/apache/directory/server/NegationOperatorITest.ldif
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-unit/src/test/resources/org/apache/directory/server/NegationOperatorITest.ldif?view=auto&rev=566915
==============================================================================
--- directory/apacheds/trunk/server-unit/src/test/resources/org/apache/directory/server/NegationOperatorITest.ldif
(added)
+++ directory/apacheds/trunk/server-unit/src/test/resources/org/apache/directory/server/NegationOperatorITest.ldif
Thu Aug 16 21:37:58 2007
@@ -0,0 +1,13 @@
+dn: ou=people,ou=system
+objectClass: top
+objectClass: organizationalUnit
+ou: people
+
+dn: uid=jblack,ou=people,ou=system
+objectClass: top
+objectClass: person
+objectClass: uidObject
+uid: jblack
+cn: Jack Black
+sn: Black 
+



Mime
View raw message