directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r894247 - in /directory/apacheds/branches/apacheds-schema: core-annotations/src/main/java/org/apache/directory/server/core/factory/ core-annotations/src/test/java/org/apache/directory/server/core/factory/ test-framework/src/main/java/org/ap...
Date Mon, 28 Dec 2009 21:10:02 GMT
Author: elecharny
Date: Mon Dec 28 21:09:47 2009
New Revision: 894247

URL: http://svn.apache.org/viewvc?rev=894247&view=rev
Log:
o Complete reorganization of the test framework : DS are now launched in each level instead
of having all the work done in the Runner.runChild() method 

Added:
    directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java
    directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
    directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceFactoryTest.java
      - copied, changed from r894166, directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceBuilderTest.java
Removed:
    directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceBuilderTest.java
Modified:
    directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
    directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java
    directory/apacheds/branches/apacheds-schema/test-framework/src/test/java/org/apache/directory/server/core/integ/TestClassC.java
    directory/apacheds/branches/apacheds-schema/test-framework/src/test/resources/log4j.properties

Added: directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java?rev=894247&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java
(added)
+++ directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java
Mon Dec 28 21:09:47 2009
@@ -0,0 +1,197 @@
+
+package org.apache.directory.server.core.factory;
+
+import java.util.List;
+
+import javax.naming.NamingException;
+
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.annotations.ApplyLdifFiles;
+import org.apache.directory.server.core.annotations.ApplyLdifs;
+import org.apache.directory.server.core.annotations.DSBuilder;
+import org.apache.directory.server.core.entry.DefaultServerEntry;
+import org.apache.directory.shared.ldap.ldif.LdifEntry;
+import org.apache.directory.shared.ldap.ldif.LdifReader;
+import org.junit.runner.Description;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DSBuilderAnnotationProcessor
+{
+    /** A logger for this class */
+    private static final Logger LOG = LoggerFactory.getLogger( DSBuilderAnnotationProcessor.class
);
+
+    public static DirectoryService getDirectoryService( Description description )
+    {
+        try
+        {
+            DSBuilder dsBuilder = description.getAnnotation( DSBuilder.class );
+            
+            if ( dsBuilder != null )
+            {
+                LOG.debug( "Starting DS {}...", dsBuilder.name() );
+                Class<?> factory = dsBuilder.factory();
+                DirectoryServiceFactory dsf = ( DirectoryServiceFactory ) factory.newInstance();
+                dsf.init( dsBuilder.name() );
+                
+                DirectoryService service = dsf.getDirectoryService();
+                
+                return service;
+            }
+            else
+            {
+                LOG.debug( "No {} DS.", description.getDisplayName() );
+                return null;
+            }
+        }
+        catch ( Exception e )
+        {
+            return null;
+        }
+    }
+    
+    
+    public static DirectoryService getClassDirectoryService( Class<?> clazz )
+    {
+        try
+        {
+            DSBuilder dsBuilder = clazz.getAnnotation( DSBuilder.class );
+            
+            if ( dsBuilder != null )
+            {
+                LOG.debug( "Starting the {} DS...", clazz.getName() );
+                Class<?> factory = dsBuilder.factory();
+                DirectoryServiceFactory dsf = ( DirectoryServiceFactory ) factory.newInstance();
+                dsf.init( dsBuilder.name() );
+                
+                DirectoryService service = dsf.getDirectoryService();
+                
+                return service;
+            }
+            else
+            {
+                LOG.debug( "No {} DS.", clazz.getName() );
+                return null;
+            }
+        }
+        catch ( Exception e )
+        {
+            return null;
+        }
+    }
+
+    
+    /**
+     * injects an LDIF entry in the given DirectoryService
+     * 
+     * @param entry the LdifEntry to be injected
+     * @param service the DirectoryService
+     * @throws Exception
+     */
+    private static void injectEntry( LdifEntry entry, DirectoryService service ) throws Exception
+    {
+        if ( entry.isChangeAdd() )
+        {
+            service.getAdminSession().add( new DefaultServerEntry( service.getSchemaManager(),
entry.getEntry() ) );
+        }
+        else if ( entry.isChangeModify() )
+        {
+            service.getAdminSession().modify( entry.getDn(), entry.getModificationItems()
);
+        }
+        else
+        {
+            String message = "Unsupported changetype found in LDIF: " + entry.getChangeType();
+            throw new NamingException( message );
+        }
+    }
+
+    
+    /**
+     * injects the LDIF entries present in a LDIF file
+     * 
+     * @param service the DirectoryService 
+     * @param ldifFiles the array of LDIF file names (only )
+     * @throws Exception
+     */
+    public static void injectLdifFiles( Class<?> clazz, DirectoryService service, String[]
ldifFiles ) throws Exception
+    {
+        if ( ( ldifFiles != null ) && ( ldifFiles.length > 0 ) )
+        {
+            for ( String ldifFile : ldifFiles )
+            {
+                try
+                {
+                    LdifReader ldifReader = new LdifReader( clazz.getClassLoader().getResourceAsStream(
ldifFile ) ); 
+    
+                    for ( LdifEntry entry : ldifReader )
+                    {
+                        injectEntry( entry, service );
+                    }
+                    
+                    ldifReader.close();
+                }
+                catch ( Exception e )
+                {
+                    LOG.error( "Cannot inject the following entry : {}. Error : {}.", ldifFile,
e.getMessage() );
+                }
+            }
+        }
+    }
+    
+    
+    /**
+     * Inject an ldif String into the server. DN must be relative to the
+     * root.
+     *
+     * @param service the directory service to use 
+     * @param ldif the ldif containing entries to add to the server.
+     * @throws NamingException if there is a problem adding the entries from the LDIF
+     */
+    public static void injectEntries( DirectoryService service, String ldif ) throws Exception
+    {
+        LdifReader reader = new LdifReader();
+        List<LdifEntry> entries = reader.parseLdif( ldif );
+
+        for ( LdifEntry entry : entries )
+        {
+            injectEntry( entry, service );
+        }
+
+        // And close the reader
+        reader.close();
+    }
+
+    
+    /**
+     * Apply the LDIF entries to the given service
+     */
+    public static void applyLdifs( Description desc, DirectoryService service ) throws Exception
+    {
+        if( desc == null )
+        {
+            return;
+        }
+        
+        ApplyLdifFiles applyLdifFiles = desc.getAnnotation( ApplyLdifFiles.class );
+
+        if ( applyLdifFiles != null )
+        {
+            LOG.debug( "Applying {} to {}", applyLdifFiles.value(), desc.getDisplayName()
);
+            injectLdifFiles( desc.getClass(), service, applyLdifFiles.value() );
+        }
+        
+        ApplyLdifs applyLdifs = desc.getAnnotation( ApplyLdifs.class ); 
+        
+        if ( ( applyLdifs != null ) && ( applyLdifs.value() != null ) )
+        {
+            String[] ldifs = applyLdifs.value();
+
+            for ( String s : ldifs )
+            {
+                LOG.debug( "Applying {} to {}", ldifs, desc.getDisplayName() );
+                injectEntries( service, s );
+            }
+        }
+    }
+
+}

Added: directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java?rev=894247&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
(added)
+++ directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
Mon Dec 28 21:09:47 2009
@@ -0,0 +1,50 @@
+/*
+ *   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.core.factory;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.annotations.DSBuilder;
+import org.junit.Test;
+
+
+/**
+ * Test the creation of a DS using a factory.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+@DSBuilder( name="test" )
+public class DirectoryServiceAnnotationTest
+{
+    @Test
+    public void testCreateDS() throws Exception
+    {
+        DirectoryService service = DSBuilderAnnotationProcessor.getClassDirectoryService(
this.getClass() );
+        
+        assertTrue( service.isStarted() );
+        
+        service.shutdown();
+        FileUtils.deleteDirectory( service.getWorkingDirectory() );
+    }
+}

Copied: directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceFactoryTest.java
(from r894166, directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceBuilderTest.java)
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceFactoryTest.java?p2=directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceFactoryTest.java&p1=directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceBuilderTest.java&r1=894166&r2=894247&rev=894247&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceBuilderTest.java
(original)
+++ directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceFactoryTest.java
Mon Dec 28 21:09:47 2009
@@ -20,18 +20,20 @@
 
 package org.apache.directory.server.core.factory;
 
+import static org.junit.Assert.assertTrue;
+
 import org.apache.commons.io.FileUtils;
 import org.apache.directory.server.core.DirectoryService;
 import org.junit.Test;
 
 
 /**
- * TODO DirectoryServiceBuilderTest.
+ * Test the creation of a DS using a factory.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class DirectoryServiceBuilderTest
+public class DirectoryServiceFactoryTest
 {
     @Test
     public void testCreateDS() throws Exception
@@ -41,6 +43,8 @@
         
         DirectoryService service = factory.getDirectoryService();
         
+        assertTrue( service.isStarted() );
+        
         service.shutdown();
         FileUtils.deleteDirectory( service.getWorkingDirectory() );
     }

Modified: directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java?rev=894247&r1=894246&r2=894247&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
(original)
+++ directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
Mon Dec 28 21:09:47 2009
@@ -20,24 +20,17 @@
 
 
 import java.lang.reflect.Field;
-import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import javax.naming.NamingException;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.directory.server.DefaultLdapServerFactory;
 import org.apache.directory.server.annotations.LdapServerBuilder;
 import org.apache.directory.server.core.DirectoryService;
-import org.apache.directory.server.core.annotations.ApplyLdifFiles;
-import org.apache.directory.server.core.annotations.ApplyLdifs;
 import org.apache.directory.server.core.annotations.DSBuilder;
-import org.apache.directory.server.core.entry.DefaultServerEntry;
+import org.apache.directory.server.core.factory.DSBuilderAnnotationProcessor;
 import org.apache.directory.server.core.factory.DefaultDirectoryServiceFactory;
 import org.apache.directory.server.core.factory.DirectoryServiceFactory;
-import org.apache.directory.shared.ldap.ldif.LdifEntry;
-import org.apache.directory.shared.ldap.ldif.LdifReader;
 import org.junit.Ignore;
 import org.junit.runner.Description;
 import org.junit.runner.notification.RunNotifier;
@@ -69,14 +62,11 @@
     /** The suite this class depend on, if any */
     private FrameworkSuite suite;
 
-    /** The DSBuilder for this class, if any */
-    private DSBuilder classDSBuilder;
-    
     /** The LdapServerBuilder for this class, if any */
     private LdapServerBuilder classLdapServerBuilder;
 
     /** The DirectoryService for this class, if any */
-    private DirectoryService classService;
+    private DirectoryService classDS;
 
     /** A flag set to true when the class has been started */
     private boolean classStarted = false;
@@ -112,42 +102,101 @@
     @Override
     public void run( final RunNotifier notifier )
     {
-        // Get the class DSBuilder, if any
-        classDSBuilder = getDescription().getAnnotation( DSBuilder.class );
-        
+        // Before running any test, check to see if we must create a class DS
         // Get the LdapServerBuilder, if any
         classLdapServerBuilder = getDescription().getAnnotation( LdapServerBuilder.class
);
 
         try
         {
-            super.run( notifier );
+            classDS = DSBuilderAnnotationProcessor.getDirectoryService( getDescription()
);
+            long revision = 0L;
+            DirectoryService directoryService = null;
+
             
-            // cleanup classService if it is not the same as suite service or
-            // it is not null (this second case happens in the absence of a suite)
-            if( suite != null ) 
+            if ( classDS != null )
             {
-                if ( ( classService != null ) && ( classService != suite.getSuiteService()
) )
+                // We have a class DS defined, update it
+                directoryService = classDS;
+                
+                // Get the applyLdifs for each level and apply them
+                if ( suite != null )
                 {
-                    classService.shutdown();
-                    FileUtils.deleteDirectory( classService.getWorkingDirectory() );
+                    DSBuilderAnnotationProcessor.applyLdifs( suite.getDescription(), classDS
);
+                }
+                
+                DSBuilderAnnotationProcessor.applyLdifs( getDescription(), classDS );
+            }
+            else
+            {
+                // No class DS. Do we have a Suite ?
+                if ( suite != null ) 
+                {
+                    // yes. Do we have a suite DS ?
+                    directoryService = suite.getDirectoryService();
+
+                    if ( directoryService != null )
+                    {
+                        // yes : apply the class LDIFs only, and tag for reversion
+                        revision = directoryService.getChangeLog().getCurrentRevision();
+                        LOG.debug( "Create revision {}", revision );
+
+                        // apply the class LDIFs
+                        DSBuilderAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
+                    }
+                    else
+                    {
+                        // No : define a default DS for the suite then
+                        DirectoryServiceFactory dsf = DefaultDirectoryServiceFactory.DEFAULT;
+                        dsf.init( "default" + UUID.randomUUID().toString() );
+                        directoryService = dsf.getDirectoryService();
+                        
+                        // Stores it into the suite
+                        suite.setDirectoryService( directoryService );
+                        
+                        // Apply the suite LDIF first
+                        DSBuilderAnnotationProcessor.applyLdifs( suite.getDescription(),
directoryService );
+                        
+                        // Then tag for reversion and apply the class LDIFs
+                        revision = directoryService.getChangeLog().getCurrentRevision();
+                        LOG.debug( "Create revision {}", revision );
+                        
+                        DSBuilderAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
+                    }
                 }
-                else if ( testCount.get() == suite.testCount() )
+                else
                 {
-                    suite.getSuiteService().shutdown();
-                    FileUtils.deleteDirectory( suite.getSuiteService().getWorkingDirectory()
);
-                    testCount.set( 0 );
+                    // No : define a default class DS then
+                    DirectoryServiceFactory dsf = DefaultDirectoryServiceFactory.DEFAULT;
+                    dsf.init( "default" + UUID.randomUUID().toString() );
+                    directoryService = dsf.getDirectoryService();
+                    
+                    // Stores the defaultDS in the classDS
+                    classDS = directoryService;
+
+                    // Apply the class LDIFs
+                    DSBuilderAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
                 }
             }
-            else if ( classService != null )
+
+            super.run( notifier );
+            
+            // cleanup classService if it is not the same as suite service or
+            // it is not null (this second case happens in the absence of a suite)
+            if ( classDS != null )
             {
-                classService.shutdown();
-                FileUtils.deleteDirectory( classService.getWorkingDirectory() );
+                LOG.debug( "Shuting down DS for {}", classDS.getInstanceId() );
+                classDS.shutdown();
+                FileUtils.deleteDirectory( classDS.getWorkingDirectory() );
             }
-            
-            
-            if ( suite == null )
+            else
             {
-                testCount.set( 0 );
+                // Revert the ldifs
+                if ( revision < directoryService.getChangeLog().getCurrentRevision() )
+                {
+                    LOG.debug( "Revert revision {}", revision );
+                    // We use a class or suite DS, just revert the current test's modifications
+                    directoryService.revert( revision );
+                }
             }
         }
         catch ( Exception e )
@@ -189,153 +238,60 @@
         // Ok, ready to run the test
         try
         {
-            DirectoryService service = null;
-            DirectoryService methodDS = null;
+            DirectoryService directoryService = null;
+            
+            // Set the revision to 0, we will revert only if it's set to another value
             long revision = 0L;
 
-            // First check if this method has a dedicated DSBuilder
-            DSBuilder methodDSBuilder = method.getAnnotation( DSBuilder.class );
+            // Check if this method has a dedicated DSBuilder
+            DirectoryService methodDS = DSBuilderAnnotationProcessor.getDirectoryService(
methodDescription );
 
-            if ( methodDSBuilder != null )
+            if ( methodDS != null )
             {
-                // yes : instantiate the factory for this method then
-                service = getDirectoryServer( methodDSBuilder );
-                methodDS = service;
-
                 // Apply all the LDIFs
-                applyLdifs( suiteDescription, service );
-                applyLdifs( classDescription, service );
-                applyLdifs( methodDescription, service );
+                DSBuilderAnnotationProcessor.applyLdifs( suiteDescription, methodDS );
+                DSBuilderAnnotationProcessor.applyLdifs( classDescription, methodDS );
+                DSBuilderAnnotationProcessor.applyLdifs( methodDescription, methodDS );
+                
+                directoryService = methodDS;
             }
-            else if ( classDSBuilder != null )
+            else if ( classDS != null )
             {
-                // Use the class DS. We now have to see if it's started
-                if ( classService != null )
-                {
-                    service = classService;
-
-                    // get the current revision, we need it to revert the modifications
-                    revision = service.getChangeLog().getCurrentRevision();
-
-                    // Only apply the method LDIF, the class and suite(if present) LDIFs
have
-                    // already been applied
-                    applyLdifs( methodDescription, service );
-                }
-                else
-                {
-                    // let's instantiate this class DS
-                    classService = getDirectoryServer( classDSBuilder );
-                    service = classService;
-
-                    // Apply all the LDIFs
-                    applyLdifs( suiteDescription, service );
-                    applyLdifs( classDescription, service );
-
-                    // get the current revision, we need it to revert the modifications
-                    revision = service.getChangeLog().getCurrentRevision();
-                 
-                    applyLdifs( methodDescription, service );
-                }
+                directoryService = classDS;
+                
+                // apply the method LDIFs, and tag for reversion
+                revision = directoryService.getChangeLog().getCurrentRevision();
+                LOG.debug( "Create revision {}", revision );
+                
+                DSBuilderAnnotationProcessor.applyLdifs( methodDescription, directoryService
);
             }
             else if ( suite != null )
             {
-                if ( suite.getSuiteDSBuilder() != null )
-                {
-                    // Use the suite DS. We now have to see if it's started
-                    if ( suite.getSuiteService() != null )
-                    {
-                        service = suite.getSuiteService();
-    
-                        // get the current revision, we need it to revert the modifications
-                        revision = service.getChangeLog().getCurrentRevision();
-    
-                        // Apply the method and class LDIFs, if the class
-                        // LDIFs haven't been applied yet
-                        if ( !classStarted )
-                        {
-                            applyLdifs( classDescription, service );
-                        }
-    
-                        // And also apply the method's LDIFs
-                        applyLdifs( methodDescription, service );
-                    }
-                    else
-                    {
-                        // Create the suite DS
-                        service = getDirectoryServer( suite.getSuiteDSBuilder() );
-                        
-                        suite.setSuiteService( service );
-    
-                        // apply the suite LDIFs first, these will never be reverted
-                        // during the running time of a test suite
-                        applyLdifs( suiteDescription, service );
-    
-                        // get the current revision, we need it to revert the modifications
-                        revision = service.getChangeLog().getCurrentRevision();
-    
-                        // Apply all the other LDIFs
-                        applyLdifs( classDescription, service );
-                        applyLdifs( methodDescription, service );
-                    }
-                }
-                else
-                {
-                    // Use the default DS
-                    DirectoryServiceFactory dsf = DefaultDirectoryServiceFactory.DEFAULT;
-                    dsf.init( "default" + UUID.randomUUID().toString() );
-                    service = dsf.getDirectoryService();
-                    
-                    // same as in above else-if condition 
-                    // apply the suite LDIFs first, these will never be reverted
-                    // during the running time of a test suite
-                    applyLdifs( suiteDescription, service );
-
-                    suite.setSuiteService( service );
-                    // get the current revision, we need it to revert the modifications
-                    revision = service.getChangeLog().getCurrentRevision();
-
-                    // Apply all the other LDIFs
-                    applyLdifs( classDescription, service );
-                    applyLdifs( methodDescription, service );
-                }
-            }
-            else 
-            {
-                // Use the default DS
-                DirectoryServiceFactory dsf = DefaultDirectoryServiceFactory.DEFAULT;
-                dsf.init( "default" + UUID.randomUUID().toString() );
-                service = dsf.getDirectoryService();
+                directoryService = suite.getDirectoryService();
+                
+                // apply the method LDIFs, and tag for reversion
+                revision = directoryService.getChangeLog().getCurrentRevision();
+                LOG.debug( "Create revision {}", revision );
                 
-                // same as in above else-if condition 
-                // apply the suite LDIFs first, these will never be reverted
-                // during the running time of a test suite
-                applyLdifs( suiteDescription, service );
-
-                suite.setSuiteService( service );
-                // get the current revision, we need it to revert the modifications
-                revision = service.getChangeLog().getCurrentRevision();
-
-                // Apply all the other LDIFs
-                applyLdifs( classDescription, service );
-                applyLdifs( methodDescription, service );
+                DSBuilderAnnotationProcessor.applyLdifs( methodDescription, directoryService
);
             }
 
             // At this point, we know which service to use.
             // Inject it into the class
             Field field = getTestClass().getJavaClass().getField( DIRECTORY_SERVICE_FIELD_NAME
);
-            field.set( getTestClass().getJavaClass(), service );
+            field.set( getTestClass().getJavaClass(), directoryService );
             
             // if we run this class in a suite, tell it to the test
             field = getTestClass().getJavaClass().getField( IS_RUN_IN_SUITE_FIELD_NAME );
             field.set( getTestClass().getJavaClass(), suite != null );
 
             // Last not least, see if we have to start a server
-            if ( suite.getSuiteLdapServerBuilder() != null )
+            if ( ( suite != null ) && ( suite.getSuiteLdapServerBuilder() != null
) )
             {
                 LdapServerBuilder ldapServerBuilder = suite.getSuiteLdapServerBuilder();
                 
                 DefaultLdapServerFactory ldapServerFactory = (DefaultLdapServerFactory)ldapServerBuilder.factory().newInstance();
-                ldapServerFactory.setDirectoryService( service );
+                ldapServerFactory.setDirectoryService( directoryService );
             }
 
             super.runChild( method, notifier );
@@ -343,13 +299,14 @@
             // Cleanup the methodDS if it has been created
             if ( methodDS != null )
             {
+                LOG.debug( "Shuting down DS for {}", methodDS.getInstanceId() );
                 methodDS.shutdown();
                 FileUtils.deleteDirectory( methodDS.getWorkingDirectory() );
             }
-            else if( revision < service.getChangeLog().getCurrentRevision() )
+            else if ( revision < directoryService.getChangeLog().getCurrentRevision()
)
             {
                 // We use a class or suite DS, just revert the current test's modifications
-                service.revert( revision );
+                directoryService.revert( revision );
             }
         }
         catch ( Exception e )
@@ -373,117 +330,4 @@
     {
         return suite;
     }
-
-
-    /**
-     * Apply the LDIF entries to the given service
-     */
-    private void applyLdifs( Description desc, DirectoryService service ) throws Exception
-    {
-        if( desc == null )
-        {
-            return;
-        }
-        
-        ApplyLdifFiles applyLdifFiles = desc.getAnnotation( ApplyLdifFiles.class );
-
-        if( applyLdifFiles != null )
-        {
-            injectLdifFiles( service, applyLdifFiles.value() );
-        }
-        
-        ApplyLdifs applyLdifs = desc.getAnnotation( ApplyLdifs.class ); 
-        
-        if ( ( applyLdifs != null ) && ( applyLdifs.value() != null ) )
-        {
-            String[] ldifs = applyLdifs.value();
-
-            for ( String s : ldifs )
-            {
-                injectEntries( service, s );
-            }
-        }
-    }
-
-    /**
-     * injects the LDIF entries present in a LDIF file
-     * 
-     * @param service the DirectoryService 
-     * @param ldifFiles the array of LDIF file names (only )
-     * @throws Exception
-     */
-    public void injectLdifFiles( DirectoryService service, String[] ldifFiles ) throws Exception
-    {
-        if ( ldifFiles != null && ldifFiles.length > 0 )
-        {
-            for ( String ldifFile : ldifFiles )
-            {
-                try
-                {
-                    Class<?> klaz = getTestClass().getJavaClass();
-
-                    LdifReader ldifReader = new LdifReader( klaz.getClassLoader().getResourceAsStream(
ldifFile ) ); 
-    
-                    for ( LdifEntry entry : ldifReader )
-                    {
-                        injectEntry( entry, service );
-                    }
-                    
-                    ldifReader.close();
-                }
-                catch ( Exception e )
-                {
-                    LOG.error( "Cannot inject the following entry : {}. Error : {}.", ldifFile,
e.getMessage() );
-                }
-            }
-        }
-    }
-    
-    
-    /**
-     * Inject an ldif String into the server. DN must be relative to the
-     * root.
-     *
-     * @param service the directory service to use 
-     * @param ldif the ldif containing entries to add to the server.
-     * @throws NamingException if there is a problem adding the entries from the LDIF
-     */
-    public void injectEntries( DirectoryService service, String ldif ) throws Exception
-    {
-        LdifReader reader = new LdifReader();
-        List<LdifEntry> entries = reader.parseLdif( ldif );
-
-        for ( LdifEntry entry : entries )
-        {
-            injectEntry( entry, service );
-        }
-
-        // And close the reader
-        reader.close();
-    }
-
-    
-    /**
-     * injects an LDIF entry in the given DirectoryService
-     * 
-     * @param entry the LdifEntry to be injected
-     * @param service the DirectoryService
-     * @throws Exception
-     */
-    private void injectEntry( LdifEntry entry, DirectoryService service ) throws Exception
-    {
-        if ( entry.isChangeAdd() )
-        {
-            service.getAdminSession().add( new DefaultServerEntry( service.getSchemaManager(),
entry.getEntry() ) );
-        }
-        else if ( entry.isChangeModify() )
-        {
-            service.getAdminSession().modify( entry.getDn(), entry.getModificationItems()
);
-        }
-        else
-        {
-            String message = "Unsupported changetype found in LDIF: " + entry.getChangeType();
-            throw new NamingException( message );
-        }
-    }
 }

Modified: directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java?rev=894247&r1=894246&r2=894247&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java
(original)
+++ directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java
Mon Dec 28 21:09:47 2009
@@ -19,16 +19,19 @@
 package org.apache.directory.server.core.integ;
 
 
+import org.apache.commons.io.FileUtils;
 import org.apache.directory.server.annotations.LdapServerBuilder;
 import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.server.core.annotations.ApplyLdifs;
-import org.apache.directory.server.core.annotations.DSBuilder;
+import org.apache.directory.server.core.factory.DSBuilderAnnotationProcessor;
 import org.apache.directory.server.ldap.LdapServer;
 import org.junit.runner.Runner;
 import org.junit.runner.notification.RunNotifier;
 import org.junit.runners.Suite;
 import org.junit.runners.model.InitializationError;
 import org.junit.runners.model.RunnerBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -40,15 +43,15 @@
  */
 public class FrameworkSuite extends Suite
 {
-    /** The DSBuilder for this suite, if any */
-    private DSBuilder suiteDSBuilder;
+    /** A logger for this class */
+    private static final Logger LOG = LoggerFactory.getLogger( FrameworkSuite.class );
+
+    /** The suite DS, if any */
+    private DirectoryService directoryService;
     
     /** The LdapServerBuilder for this class, if any */
     private LdapServerBuilder suiteLdapServerBuilder;
 
-    /** The DirectoryService for this suite, if any */
-    private DirectoryService suiteService;
-    
     /** The LdapServer for this class, if any */
     private LdapServer suiteLdapServer;
 
@@ -64,17 +67,56 @@
         super( clazz, builder );
     }
 
-
+    
     /**
      * {@inheritDoc}
      */
     @Override
-    protected void runChild( Runner runner, RunNotifier notifier )
+    public void run( final RunNotifier notifier )
     {
-        suiteDSBuilder = getDescription().getAnnotation( DSBuilder.class );
         suiteLdifs = getDescription().getAnnotation( ApplyLdifs.class );
         suiteLdapServerBuilder = getDescription().getAnnotation( LdapServerBuilder.class
);
 
+        // Initialize and start the DS before running any test, if we have a DS annotation
+        directoryService = DSBuilderAnnotationProcessor.getDirectoryService( getDescription()
);
+        
+        // and inject LDIFs if needed
+        if ( directoryService != null )
+        {
+            try
+            {
+                DSBuilderAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
+            }
+            catch ( Exception e )
+            {
+                return;
+            }
+        }
+        
+        super.run( notifier );
+        
+        // last, stop the DS if we have one
+        if ( directoryService != null )
+        {
+            try
+            {
+                LOG.debug( "Shuting down DS for {}", directoryService.getInstanceId() );
+                directoryService.shutdown();
+                FileUtils.deleteDirectory( directoryService.getWorkingDirectory() );
+            }
+            catch ( Exception e )
+            {
+                // Do nothing
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected void runChild( Runner runner, RunNotifier notifier )
+    {
         // Store the suite into the class we will run
         ( ( FrameworkRunner ) runner ).setSuite( this );
         
@@ -84,38 +126,29 @@
 
 
     /**
-     * @return the suiteDSBuilder
+     * @return the suiteLdifs
      */
-    public DSBuilder getSuiteDSBuilder()
+    public ApplyLdifs getSuiteLdifs()
     {
-        return suiteDSBuilder;
+        return suiteLdifs;
     }
 
 
     /**
-     * @return the suiteService
-     */
-    public DirectoryService getSuiteService()
-    {
-        return suiteService;
-    }
-    
-    
-    /**
-     * @param suiteService the suiteService to set
+     * @return the DirectoryService instance
      */
-    public void setSuiteService( DirectoryService suiteService )
+    public DirectoryService getDirectoryService()
     {
-        this.suiteService = suiteService;
+        return directoryService;
     }
-    
-    
+
+
     /**
-     * @return the suiteLdifs
+     * @param directoryService the directoryService to set
      */
-    public ApplyLdifs getSuiteLdifs()
+    public void setDirectoryService( DirectoryService directoryService )
     {
-        return suiteLdifs;
+        this.directoryService = directoryService;
     }
 
 

Modified: directory/apacheds/branches/apacheds-schema/test-framework/src/test/java/org/apache/directory/server/core/integ/TestClassC.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/test-framework/src/test/java/org/apache/directory/server/core/integ/TestClassC.java?rev=894247&r1=894246&r2=894247&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/test-framework/src/test/java/org/apache/directory/server/core/integ/TestClassC.java
(original)
+++ directory/apacheds/branches/apacheds-schema/test-framework/src/test/java/org/apache/directory/server/core/integ/TestClassC.java
Mon Dec 28 21:09:47 2009
@@ -23,19 +23,11 @@
 import static org.junit.Assert.assertTrue;
 
 import org.apache.directory.server.core.annotations.ApplyLdifFiles;
-import org.apache.directory.server.core.annotations.ApplyLdifs;
 import org.apache.directory.shared.ldap.name.LdapDN;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 @RunWith( FrameworkRunner.class )
-@ApplyLdifs(
-    {
-        "dn: cn=testClassC,ou=system\n" + 
-        "objectClass: person\n" + 
-        "cn: testClassC\n" + 
-        "sn: sn_testClassC\n"
-    })
 public class TestClassC extends AbstractTestUnit
 {
     @Test
@@ -46,7 +38,7 @@
             assertTrue( service.getAdminSession().exists( new LdapDN( "cn=testSuite,ou=system"
) ) );
         }
 
-        assertTrue( service.getAdminSession().exists( new LdapDN( "cn=testClassC,ou=system"
) ) );
+        assertFalse( service.getAdminSession().exists( new LdapDN( "cn=testClassC,ou=system"
) ) );
         
         // the below DN will be injected in TestClassB when ran as suite, but that DN
         // shouldn't be present in the suite level DS cause of revert operation

Modified: directory/apacheds/branches/apacheds-schema/test-framework/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/test-framework/src/test/resources/log4j.properties?rev=894247&r1=894246&r2=894247&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/test-framework/src/test/resources/log4j.properties
(original)
+++ directory/apacheds/branches/apacheds-schema/test-framework/src/test/resources/log4j.properties
Mon Dec 28 21:09:47 2009
@@ -14,9 +14,14 @@
 #    See the License for the specific language governing permissions and
 #    limitations under the License.
 #############################################################################
-log4j.rootCategory=OFF, stdout
+log4j.rootCategory=ERROR, stdout
 
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss}] %p [%c] - %m%n
 
+log4j.logger.org.apache.directory.server.core.factory=DEBUG
+log4j.logger.org.apache.directory.server.core.integ=DEBUG
+log4j.logger.org.apache.directory.shared=ERROR
+log4j.logger.org.apache.directory.server.core.partition=ERROR
+



Mime
View raw message