How to make LdapContextSource pointing to UnboundID InMemoryDirectoryServer?

spring-ldap, unboundid-ldap-sdk

Solution

There is really not much difference between the case of an external and an embedded LDAP server. When configuring the `LdapContextSource`, you will have to set the url of the server to something like `ldap://localhost:33389/` (assuming your embedded server listens at port 33389).

Be aware that by default the UnboundID `InMemoryDirectoryServer` will pick a free port randomly at runtime unless you configure it to listen to a fix port. This might help you getting started:

InMemoryDirectoryServerConfig config = 
        new InMemoryDirectoryServerConfig("dc=example, dc=com");

// make sure that the server listens on port 33389
config.setListenerConfigs(
        new InMemoryListenerConfig("myListener", null, 33389, null, null, null));

InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);

ds.startListening();

// import some test data from an ldif file
ds.importFromLDIF(true,"content.ldif");

Problem

I have a set of old automated test cases which are based on Spring LDAP framework. They connect to an external LDAP server. I am thinking about replacing the external server with an embedded one. The UnboundID InMemoryDirectoryServer appears attractive, especially if there is a way allowing Spring LDAP based clients to connect UnboundID-based embedded server. The question is: How to do it? I am new to LDAP, please help.

Original source