Using the embedded ApacheDS server for integration testing

The following example demonstrates how to use the embedded LDAP Directory Server for integrations that require a running LDAP directory server.

<project>
  <modelVersion>4.0.0</modelVersion>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

In this example we are using the LDAP Maven Plugin in conjunction with the Maven Failsafe Plugin which while run some unit tests in the integration-test phase of the build life cycle.

      <plugin>
        <groupId>com.btmatthews.maven.plugins</groupId>
        <artifactId>ldap-maven-plugin</artifactId>
        <version>1.3.0</version>
        <configuration>
          <monitorPort>11389</monitorPort>
          <monitorKey>ldap</monitorKey>
          <daemon>true</daemon>
          <rootDn>dc=btmatthews,dc=com</rootDn>
          <ldapPort>10389</ldapPort>
          <ldifFile>src/test/resources/data.ldif</ldifFile>
        </configuration>
        <executions>

The embedded LDAP directory server is configured to run as a daemon by setting <daemon/> to true. This will run the embedded LDAP directory server in the background while the build proceeds. The embedded LDAP directory server is controlled by a monitor thread which listens for the stop signal on the port specified by <monitorPort/>. The stop command must also be accompanied by a key specified by <monitorKey/> to ensure that stop signal is being sent to the correct instance.

          <execution>
            <id>start-server</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>

The above execution will invoke the ldap:run goal during the pre-integration-test phase of the build life cycle. The embedded LDAP directory server is created listening on the port specified by <ldapPort/> and an empty repository is created with at the DN specified by <rootDn/>. Then the repository is seeded with data in the LDIF file specified by <ldifFile/>.

          <execution>
            <id>stop-server</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>

The above execution will invoke the ldap:stop goal during the post-integration-test phase of the build life cycle. This achieved by sending the stop command to the monitor controlling the embedded LDAP directory server.

        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>