directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lucasthei...@apache.org
Subject svn commit: r1592292 - in /directory/apacheds/trunk/test-framework/src: main/java/org/apache/directory/server/core/integ/ test/java/org/apache/directory/server/core/integ/
Date Sat, 03 May 2014 23:10:43 GMT
Author: lucastheisen
Date: Sat May  3 23:10:43 2014
New Revision: 1592292

URL: http://svn.apache.org/r1592292
Log:
Added TestRule's that can be used instead of FrameworkRunner and some unit tests that show
how to use them

Added:
    directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateDsRule.java
  (with props)
    directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java
  (with props)
    directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java
  (with props)
    directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java
  (with props)

Added: directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateDsRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateDsRule.java?rev=1592292&view=auto
==============================================================================
--- directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateDsRule.java
(added)
+++ directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateDsRule.java
Sat May  3 23:10:43 2014
@@ -0,0 +1,112 @@
+package org.apache.directory.server.core.integ;
+
+
+import org.apache.commons.io.FileUtils;
+import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.api.DirectoryService;
+import org.apache.directory.server.core.api.changelog.Tag;
+import org.apache.directory.server.core.factory.DSAnnotationProcessor;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class CreateDsRule implements TestRule
+{
+    private static Logger LOG = LoggerFactory.getLogger( CreateDsRule.class );
+
+    private DirectoryService directoryService;
+    private CreateDsRule outerCreateDsRule;
+
+
+    public CreateDsRule()
+    {
+    }
+
+
+    public CreateDsRule( CreateDsRule outerCreateDsRule )
+    {
+        this.outerCreateDsRule = outerCreateDsRule;
+    }
+
+
+    public DirectoryService getDirectoryService()
+    {
+        return directoryService == null
+            ? ( outerCreateDsRule == null
+                ? null
+                : outerCreateDsRule.getDirectoryService() )
+            : directoryService;
+    }
+
+
+    @Override
+    public Statement apply( final Statement base, final Description description )
+    {
+        final CreateDS createDs = description.getAnnotation( CreateDS.class );
+        if ( createDs == null )
+        {
+            final DirectoryService directoryService = getDirectoryService();
+            if ( directoryService != null && directoryService.getChangeLog().isEnabled()
)
+            {
+                return new Statement()
+                {
+                    @Override
+                    public void evaluate() throws Throwable
+                    {
+                        Tag tag = directoryService.getChangeLog().tag();
+                        DSAnnotationProcessor.applyLdifs( description, directoryService );
+                        LOG.debug( "Tagged change log: {}", tag );
+                        try
+                        {
+                            base.evaluate();
+                        }
+                        finally
+                        {
+                            if ( directoryService.getChangeLog().getCurrentRevision() >
tag.getRevision() )
+                            {
+                                LOG.debug( "Reverting to tag: {}", tag );
+                                directoryService.revert( tag.getRevision() );
+                            }
+                            else
+                            {
+                                LOG.debug( "No changes made, nothing to revert" );
+                            }
+                        }
+                    }
+                };
+            }
+            else
+            {
+                LOG.trace( "no @CreateDS and no outer @CreateDS on: {}", description );
+                return base;
+            }
+        }
+        else
+        {
+            return new Statement()
+            {
+                @Override
+                public void evaluate() throws Throwable
+                {
+                    LOG.trace( "Creating directory service" );
+                    directoryService = DSAnnotationProcessor.getDirectoryService( description
);
+                    DSAnnotationProcessor.applyLdifs( description, directoryService );
+
+                    try
+                    {
+                        base.evaluate();
+                    }
+                    finally
+                    {
+                        LOG.trace( "Shutting down directory service" );
+                        directoryService.shutdown();
+                        FileUtils.deleteDirectory( directoryService.getInstanceLayout().getInstanceDirectory()
);
+                    }
+                }
+            };
+        }
+    }
+}

Propchange: directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateDsRule.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java?rev=1592292&view=auto
==============================================================================
--- directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java
(added)
+++ directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java
Sat May  3 23:10:43 2014
@@ -0,0 +1,105 @@
+package org.apache.directory.server.core.integ;
+
+
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.core.api.DirectoryService;
+import org.apache.directory.server.factory.ServerAnnotationProcessor;
+import org.apache.directory.server.ldap.LdapServer;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class CreateLdapServerRule implements TestRule
+{
+    private static Logger LOG = LoggerFactory.getLogger( CreateLdapServerRule.class );
+
+    private CreateDsRule createDsRule;
+    private LdapServer ldapServer;
+    private CreateLdapServerRule outerCreateLdapServerRule;
+
+
+    public CreateLdapServerRule( CreateDsRule createDsRule )
+    {
+        this( createDsRule, null );
+    }
+
+
+    public CreateLdapServerRule( CreateDsRule createDsRule, CreateLdapServerRule createLdapServerRule
)
+    {
+        this.createDsRule = createDsRule;
+        this.outerCreateLdapServerRule = createLdapServerRule;
+    }
+
+
+    public LdapServer getLdapServer()
+    {
+        return ldapServer == null
+            ? ( outerCreateLdapServerRule == null ? null : outerCreateLdapServerRule.getLdapServer()
)
+            : ldapServer;
+    }
+
+
+    @Override
+    public Statement apply( final Statement base, final Description description )
+    {
+        final CreateLdapServer createLdapServer = description.getAnnotation( CreateLdapServer.class
);
+        if ( createLdapServer == null )
+        {
+            return new Statement() 
+            {
+                @Override
+                public void evaluate() throws Throwable
+                {
+                    LdapServer ldapServer = getLdapServer();
+                    DirectoryService directoryService = createDsRule.getDirectoryService();
+                    if ( ldapServer != null && directoryService != ldapServer.getDirectoryService()
) {
+                        LOG.trace( "Changing to new directory service" );
+                        DirectoryService oldDirectoryService = ldapServer.getDirectoryService();
+                        ldapServer.setDirectoryService( directoryService );
+
+                        try
+                        {
+                            base.evaluate();
+                        }
+                        finally
+                        {
+                            LOG.trace( "Reverting to old directory service" );
+                            ldapServer.setDirectoryService( oldDirectoryService );
+                        }
+                   
+                    }
+                    else {
+                        LOG.trace( "no @CreateLdapServer on: {}", description );
+                        base.evaluate();
+                    }
+                }
+            };
+        }
+        else
+        {
+            return new Statement()
+            {
+                @Override
+                public void evaluate() throws Throwable
+                {
+                    LOG.trace( "Creating ldap server" );
+                    ldapServer = ServerAnnotationProcessor.createLdapServer( description,

+                        createDsRule.getDirectoryService() );
+
+                    try
+                    {
+                        base.evaluate();
+                    }
+                    finally
+                    {
+                        LOG.trace( "Stopping ldap server" );
+                        ldapServer.stop();
+                    }
+                }
+            };
+        }
+    }
+}

Propchange: directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java?rev=1592292&view=auto
==============================================================================
--- directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java
(added)
+++ directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java
Sat May  3 23:10:43 2014
@@ -0,0 +1,146 @@
+package org.apache.directory.server.core.integ;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException;
+import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.server.core.annotations.ApplyLdifs;
+import org.apache.directory.server.core.annotations.ContextEntry;
+import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.annotations.CreateIndex;
+import org.apache.directory.server.core.annotations.CreatePartition;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+
+
+@CreateDS(name = "classDS",
+    enableChangeLog = true,
+    partitions =
+        {
+            @CreatePartition(
+                name = "example",
+                suffix = "dc=example,dc=com",
+                contextEntry = @ContextEntry(
+                    entryLdif =
+                    "dn: dc=example,dc=com\n" +
+                        "objectClass: domain\n" +
+                        "objectClass: top\n" +
+                        "dc: example\n\n"
+                ),
+                indexes =
+                    {
+                        @CreateIndex(attribute = "objectClass"),
+                        @CreateIndex(attribute = "dc"),
+                        @CreateIndex(attribute = "ou")
+                }
+            )
+    })
+@ApplyLdifs(
+    {
+        "dn: cn=class,ou=system",
+        "objectClass: person",
+        "cn: class",
+        "sn: sn_class"
+    })
+public class TestCreateDsRule
+{
+    @ClassRule
+    public static CreateDsRule classCreateDsRule = new CreateDsRule();
+
+    public CreateDsRule createDsRule = new CreateDsRule( classCreateDsRule );
+    @Rule
+    public TestRule ruleChain = RuleChain.outerRule( createDsRule );
+
+
+    @Test
+    @ApplyLdifs(
+        {
+            "dn: cn=classDsOnly,ou=system",
+            "objectClass: person",
+            "cn: classDsOnly",
+            "sn: sn_classDsOnly"
+        })
+    public void testClassDsOnly()
+    {
+        assertEquals( classCreateDsRule.getDirectoryService(), createDsRule.getDirectoryService()
);
+        try
+        {
+            Dn dn = new Dn( "cn=class,ou=system" );
+            Entry entry = createDsRule.getDirectoryService().getAdminSession().lookup( dn
);
+            assertNotNull( entry );
+            assertEquals( "class", entry.get( "cn" ).get().getValue() );
+
+            dn = new Dn( "cn=classDsOnly,ou=system" );
+            entry = createDsRule.getDirectoryService().getAdminSession().lookup( dn );
+            assertNotNull( entry );
+            assertEquals( "classDsOnly", entry.get( "cn" ).get().getValue() );
+        }
+        catch ( LdapException e )
+        {
+            fail( e.getMessage() );
+        }
+    }
+
+
+    @Test
+    @CreateDS(name = "methodDS",
+        enableChangeLog = true)
+    @ApplyLdifs(
+        {
+            "dn: cn=classAndMethodDs,ou=system",
+            "objectClass: person",
+            "cn: classAndMethodDs",
+            "sn: sn_classAndMethodDs"
+        })
+    public void testClassAndMethodDs()
+    {
+        assertNotEquals( classCreateDsRule.getDirectoryService(), createDsRule.getDirectoryService()
);
+        try
+        {
+            Dn dn = new Dn( "cn=classAndMethodDs,ou=system" );
+            Entry entry = createDsRule.getDirectoryService().getAdminSession().lookup( dn
);
+            assertNotNull( entry );
+            assertEquals( "classAndMethodDs", entry.get( "cn" ).get().getValue() );
+        }
+        catch ( LdapException e )
+        {
+            fail( e.getMessage() );
+        }
+
+        try
+        {
+            Dn dn = new Dn( "cn=class,ou=system" );
+            Entry entry = createDsRule.getDirectoryService().getAdminSession().lookup( dn
);
+            assertNull( entry );
+        }
+        catch ( LdapNoSuchObjectException e )
+        {
+            // expected
+        }
+        catch ( LdapException e ) 
+        {
+            fail( e.getClass().getName() );
+        }
+
+        try {
+            Dn dn = new Dn( "cn=class,ou=system" );
+            Entry entry = classCreateDsRule.getDirectoryService().getAdminSession().lookup(
dn );
+            assertNotNull( entry );
+            assertEquals( "class", entry.get( "cn" ).get().getValue() );
+        }
+        catch ( LdapException e ) 
+        {
+            fail( e.getClass().getName() );
+        }
+    }
+}

Propchange: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java?rev=1592292&view=auto
==============================================================================
--- directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java
(added)
+++ directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java
Sat May  3 23:10:43 2014
@@ -0,0 +1,241 @@
+package org.apache.directory.server.core.integ;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException;
+import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.apache.directory.ldap.client.api.LdapNetworkConnection;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.annotations.ApplyLdifs;
+import org.apache.directory.server.core.annotations.ContextEntry;
+import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.annotations.CreateIndex;
+import org.apache.directory.server.core.annotations.CreatePartition;
+import org.apache.directory.server.core.api.DirectoryService;
+import org.apache.directory.server.ldap.LdapServer;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+@CreateDS(name = "classDS",
+    enableChangeLog = true,
+    partitions =
+        {
+            @CreatePartition(
+                name = "example",
+                suffix = "dc=example,dc=com",
+                contextEntry = @ContextEntry(
+                    entryLdif =
+                    "dn: dc=example,dc=com\n" +
+                        "objectClass: domain\n" +
+                        "objectClass: top\n" +
+                        "dc: example\n\n"
+                ),
+                indexes =
+                    {
+                        @CreateIndex(attribute = "objectClass"),
+                        @CreateIndex(attribute = "dc"),
+                        @CreateIndex(attribute = "ou")
+                }
+            )
+    })
+@CreateLdapServer(
+    transports =
+        {
+            @CreateTransport(protocol = "LDAP")
+    })
+@ApplyLdifs(
+    {
+        "dn: cn=class,ou=system",
+        "objectClass: person",
+        "cn: class",
+        "sn: sn_class"
+})
+public class TestCreateLdapServerRule
+{
+    private static Logger LOG = LoggerFactory.getLogger( TestCreateLdapServerRule.class );
+    public static CreateDsRule classCreateDsRule = new CreateDsRule();
+    public static CreateLdapServerRule classCreateLdapServerRule = new CreateLdapServerRule(
+        classCreateDsRule );
+    @ClassRule
+    public static TestRule classRuleChain = RuleChain.outerRule( classCreateDsRule )
+        .around( classCreateLdapServerRule );
+    
+    public CreateDsRule createDsRule = new CreateDsRule( classCreateDsRule );
+    public CreateLdapServerRule createLdapServerRule = new CreateLdapServerRule(
+        createDsRule, classCreateLdapServerRule );
+    @Rule
+    public TestRule ruleChain = RuleChain.outerRule( createDsRule )
+        .around( createLdapServerRule );
+    
+    
+    @Test
+    @CreateDS(name = "methodDS",
+        enableChangeLog = true)
+    @ApplyLdifs(
+        {
+            "dn: cn=methodDs,ou=system",
+            "objectClass: person",
+            "cn: methodDs",
+            "sn: sn_methodDs"
+    })
+    public void testMethodDs()
+    {
+        assertEquals( createLdapServerRule.getLdapServer(), classCreateLdapServerRule.getLdapServer()
);
+    
+        LdapConnection ldapConnection = null;
+        try
+        {
+            LdapServer ldapServer = createLdapServerRule.getLdapServer();
+            ldapServer.getPort();
+            ldapConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort()
);
+            ldapConnection.connect();
+            ldapConnection.bind( "uid=admin,ou=system", "secret" );
+    
+            Dn dn = new Dn( "cn=methodDs,ou=system" );
+            Entry entry = ldapConnection.lookup( dn );
+            assertNotNull( entry );
+            assertEquals( "methodDs", entry.get( "cn" ).get().getValue() );
+    
+            try
+            {
+                dn = new Dn( "cn=class,ou=system" );
+                entry = ldapConnection.lookup( dn );
+                assertNull( entry );
+            }
+            catch ( LdapNoSuchObjectException e )
+            {
+                // expected
+            }
+        }
+        catch ( LdapException e )
+        {
+            fail( e.getMessage() );
+        }
+        finally
+        {
+            if ( ldapConnection != null )
+            {
+                try
+                {
+                    ldapConnection.close();
+                }
+                catch ( IOException e )
+                {
+                    // who cares!
+                }
+            }
+        }
+    }
+    
+    
+    @Test
+    @CreateLdapServer(
+        transports =
+            {
+                @CreateTransport(protocol = "LDAP")
+        })
+    public void testMethodLdapServer()
+    {
+        assertNotEquals( createLdapServerRule.getLdapServer(), classCreateLdapServerRule.getLdapServer()
);
+        assertNotEquals( createLdapServerRule.getLdapServer().getPort(),
+            classCreateLdapServerRule.getLdapServer().getPort() );
+    
+        LdapConnection ldapConnection = null;
+        try
+        {
+            LdapServer ldapServer = createLdapServerRule.getLdapServer();
+            ldapServer.getPort();
+            ldapConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort()
);
+            ldapConnection.connect();
+            ldapConnection.bind( "uid=admin,ou=system", "secret" );
+    
+            Dn dn = new Dn( "cn=class,ou=system" );
+            Entry entry = ldapConnection.lookup( dn );
+            assertNotNull( entry );
+            assertEquals( "class", entry.get( "cn" ).get().getValue() );
+        }
+        catch ( LdapException e )
+        {
+            fail( e.getMessage() );
+        }
+        finally
+        {
+            if ( ldapConnection != null )
+            {
+                try
+                {
+                    ldapConnection.close();
+                }
+                catch ( IOException e )
+                {
+                    // who cares!
+                }
+            }
+        }
+    }
+    
+    
+    @Test
+    public void testNetworkConnection()
+    {
+        assertEquals( classCreateDsRule.getDirectoryService(), createDsRule.getDirectoryService()
);
+        assertEquals( classCreateLdapServerRule.getLdapServer(), createLdapServerRule.getLdapServer()
);
+        LdapServer ldapServer = createLdapServerRule.getLdapServer();
+        DirectoryService directoryService = ldapServer.getDirectoryService();
+        assertEquals( classCreateDsRule.getDirectoryService(), directoryService );
+    
+        LdapConnection ldapConnection = null;
+        try
+        {
+            Dn dn = new Dn( "cn=class,ou=system" );
+            Entry entry = directoryService.getAdminSession().lookup( dn );
+            assertNotNull( entry );
+            assertEquals( "class", entry.get( "cn" ).get().getValue() );
+    
+            LOG.debug( "getting network connection" );
+            ldapServer.getPort();
+            ldapConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort()
);
+            ldapConnection.connect();
+            ldapConnection.bind( "uid=admin,ou=system", "secret" );
+    
+            entry = ldapConnection.lookup( dn );
+            assertNotNull( entry );
+            assertEquals( "class", entry.get( "cn" ).get().getValue() );
+        }
+        catch ( LdapException e )
+        {
+            fail( e.getMessage() );
+        }
+        finally
+        {
+            if ( ldapConnection != null )
+            {
+                try
+                {
+                    ldapConnection.close();
+                }
+                catch ( IOException e )
+                {
+                    // who cares!
+                }
+            }
+        }
+    }
+}

Propchange: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message