UnboundID LDAP SDK for Java

Ping Identity
Product Information

Using the In-Memory Directory Server

When developing directory-enabled applications, it is important to be able to easily test that application to ensure that it behaves correctly. If you're developing the application for use in an environment in which you know specifically what directory server will be used, then it is of course desirable to test with that server on occasion to ensure that your application works properly with it. However, it can often be inconvenient to do this for all of your testing because many directory servers aren't easy to set up and manage in an automated fashion, and even if they can, then it can be a heavyweight process that can add significantly to the time required to run those tests. It is also a good idea to occasionally test with different types of servers in order to avoid unintentionally relying on implementation-dependent or vendor-specific functionality. When developing applications designed to work with any kind of server, then it's useful to test with a server that is as simple and as efficient as possible.

In order to make testing directory-enabled applications as simple as possible, the UnboundID LDAP SDK for Java includes a simple yet fairly standards-compliant LDAP directory server can be easily configured and used for testing, as a simple server for demonstration purposes, or even for simple LDAP data processing tasks (e.g., load an LDIF and perform a set of queries or transformations against the data, and write it back out). This server stores all of its information in memory, and can be easily used easily from the command line or through Java code. Although it has a very small footprint and is very easy to use, it supports a wide range of features, including:

This fairly broad set of features means that the in-memory directory server may be suitable for use in conjunction with many kinds of directory-enabled applications. However, there are a few notable features that it does not provide, including;



Using the In-Memory Directory Server from Java Code

In order to obtain the greatest degree of flexibility with the in-memory directory server, you can create, start, and stop instances from within Java code, as well as interacting with the data contained in those instances. This is very useful for testing purposes because it is easy and lightweight to create and populate one or more instances within a test case, and then shut it down at the end of that test case. Alternately, you could create one or more instances that are shared across many test cases, and you can easily clear the server or reset it to some other predefined state between individual test cases.

Most of the classes that you will need when using an in-memory directory server instance reside in the com.unboundid.ldap.listener package, and include:

In the most common use cases, you will simply create an instance of the InMemoryDirectoryServerConfig class and use it to define the desired settings for the server, and then use that configuration to create an InMemoryDirectoryServer instance that you can use to control the server itself. For example, the provided code can be used to create a simple directory server instance that listen on an automatically-selected port and will allow entries below "dc=example,dc=com":

// Create the configuration to use for the server.
InMemoryDirectoryServerConfig config =
     new InMemoryDirectoryServerConfig("dc=example,dc=com");
config.addAdditionalBindCredentials("cn=Directory Manager", "password");

// Create the directory server instance, populate it with data from the
// "test-data.ldif" file, and start listening for client connections.
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
ds.importFromLDIF(true, "test-data.ldif");
ds.startListening();

// Get a client connection to the server and use it to perform various
// operations.
LDAPConnection conn = ds.getConnection();
SearchResultEntry entry = conn.getEntry("dc=example,dc=com");

// Do more stuff here....

// Disconnect from the server and cause the server to shut down.
conn.close();
ds.shutDown(true);


Using the In-Memory Directory Server from the Command Line

Although the in-memory directory server is primarily meant to be configured and started by Java code, it can also run from the command line. The tools/in-memory-directory-server shell script (or tools\in-memory-directory-server.bat batch file on Windows) can be used to start an instance with a number of settings defined through command-line arguments. You can run it with "--help" to see a list of all arguments that it supports, but some of them include:

For example, the provided command can be used to start a server listening on port 1234 with a base DN of "dc=example,dc=com" and initially populated with data read from the "example.ldif" file:

tools/in-memory-directory-server --baseDN "dc=example,dc=com" \
     --port 1234 \
     --ldifFile example.ldif