JBoss Community Archive (Read Only)

PicketBox

PicketBox LDAP Server

As part of the PicketBox ecosystem, we need to test PicketBox for LDAP functionality.  So we decided to provide a standalone project to embed Apache Directory Server and also provide an Base Class for unit testing with ldap server.

LDAP Server functionality is provided by Apache Directory Server.

Maven Dependencies

<dependency>
  <groupId>org.picketbox</groupId>
  <artifactId>picketbox-ldap</artifactId>
  <version>1.0.2.Final</version>
</dependency>

This delivers a single class called as org.picketbox.ldap.EmbeddedApacheDS

This class contains the logic for embedding Apache Directory Server.

Usage

import org.picketbox.ldap.EmbeddedApacheDS;

String tempDir = System.getProperty("java.io.tmpdir");

File workDir = new File(tempDir + "/server-work");

workDir.mkdirs();

EmbeddedApacheDS ds = new EmbeddedApacheDS(workDir);

ds.createBaseDN("apache", "dc=apache,dc=org");

//Replace following with your base DN
ds.createBaseDN("jboss", "dc=jboss,dc=org");

ds.startServer();

//Once you done with your work
ds.stopServer();

Unit Testing

When you want to unit test applications using an embedded ldap server, you can try to reuse the testing functionality provided by PicketBox LDAP.

<dependency>
    <groupId>org.picketbox</groupId>
    <artifactId>picketbox-ldap</artifactId>
    <version>1.0.2.Final</version>
    <scope>test</scope>
    <type>test-jar</type>
</dependency>

PicketBox LDAP provides a base class called as org.picketbox.test.ldap.AbstractLDAPTest

import org.picketbox.test.ldap.AbstractLDAPTest;
public class LDAPBasedSecurityWorkflowTestCase extends AbstractLDAPTest {

    @Before
    public void setup() throws Exception {
        super.setup();
        importLDIF("ldap/users.ldif");
    }
    }

We need to provide an LDIF file to load the ldap database.

After that, it is regular JNDI code for you to interact with the ldap server. The details for the JNDI connection are:
LDAP Store URL = "ldap://localhost:10389/"
adminPW = "secret";
adminDN = "uid=admin,ou=system";



If you are unable to extend your test class from AbstractLDAPTest, then you can always use a class "org.picketbox.test.ldap.LDAPTestUtil"

Use Of LDAPTestUtil
import org.picketbox.test.ldap.LDAPTestUtil;


protected LDAPTestUtil testUtil = null;

@After
public void tearDown() throws Exception {
        super.tearDown();
    if (testUtil != null) {
      testUtil.tearDown();
    }
}

@Test
public void test(){
// Deal with LDAP Server
   try {
      testUtil = new LDAPTestUtil();
      testUtil.setup();            testUtil.createBaseDN("jboss", "dc=jboss,dc=org");
                testUtil.importLDIF("ldap/users.ldif");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
}

Troubleshooting

Directory Server shutdown is very verbose

EmbeddedApacheDS.disableShutdownHook() will do the trick.  The AbstractLdapTest has been updated with this.

slf4j dependencies are complaining in maven build

The version that is compatible is 1.5.6

<dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-simple</artifactId>
         <version>1.5.6</version>
         <scope>test</scope>
      </dependency>
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 12:16:32 UTC, last content change 2012-10-17 17:57:23 UTC.