Below is the code to create an embedded instance of Apache Directory Server useful for JUnit tests, with custom schema extensions. Note that you cannot use ADS annotations or even the DefaultDirectoryServiceFactory. All objects are hand-created and the aggregate schema copied to a temporary work folder.
// Step 1: Extract schema LDIFs to disk (required once)
logger.info("Extracting default schema files");
File schemaRepo = new File("target/ldap-work");
SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(schemaRepo);
extractor.extractOrCopy(true);
// Step 2: Load the schema
logger.info("Loading default schema files");
File schemaFolder = new File("target/ldap-work/schema");
LdifSchemaLoader loader = new LdifSchemaLoader(schemaFolder);
// 2a) Write our custom schema additions
//writeCustomSchema(schemaFolder);
File sourceDir = new File("src/test/resources/ou=schema");
FileUtils.copyDirectory(sourceDir, schemaFolder);
// 2b) instantiate and load the schema manager
SchemaManager schemaManager = new DefaultSchemaManager(loader);
schemaManager.setRelaxed(); // BEFORE init
schemaManager.loadAllEnabled();
// Configure
logger.info("Instantiating directory service");
directoryService = new DefaultDirectoryService();
directoryService.setSchemaManager(schemaManager);
DnFactory dnFactory = new DefaultDnFactory(directoryService.getSchemaManager(), 1000);
// Schema partition - wrap it with an LDIF-based physical partition
LdifPartition ldifPartition = new LdifPartition(schemaManager, dnFactory);
ldifPartition.setId("schema");
ldifPartition.setSuffixDn(new Dn("ou=schema"));
ldifPartition.setPartitionPath(new File("target/ldap-work/schema-partition").toURI());
SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
schemaPartition.setWrappedPartition(ldifPartition);
directoryService.setSchemaPartition(schemaPartition);
directoryService.setInstanceLayout(new InstanceLayout(new File("target/ldap-work")));
directoryService.setAccessControlEnabled(false);
directoryService.setAllowAnonymousAccess(true);
directoryService.getChangeLog().setEnabled(false);
// Define the system partition
JdbmPartition systemPartition = new JdbmPartition(directoryService.getSchemaManager(), dnFactory);
systemPartition.setId("system");
systemPartition.setSuffixDn(new Dn("ou=system"));
systemPartition.setPartitionPath(new File("target/ldap-work/system").toURI());
systemPartition.initialize();
// Register the system partition
directoryService.setSystemPartition(systemPartition);
// Create your root partition
logger.info("Creating example partition");
JdbmPartition partition = new JdbmPartition(directoryService.getSchemaManager(), dnFactory);
partition.setId("example");
partition.setSuffixDn(new Dn("dc=example,dc=com"));
partition.setPartitionPath(new File("target/ldap-work/example").toURI());
partition.initialize();
directoryService.addPartition(partition);
// Start directory
logger.info("Starting directory service");
directoryService.startup();
// Ensure base DN exists
logger.info("Ensuring base DN exists");
if (!directoryService.getAdminSession().exists(partition.getSuffixDn())) {
Entry baseEntry = new DefaultEntry(
directoryService.getSchemaManager(),
"dc=example,dc=com",
"objectClass: top",
"objectClass: domain",
"dc: example"
);
directoryService.getAdminSession().add(baseEntry);
}
// Define LDAP server
logger.info("Starting the LDAP server");
ldapServer = new LdapServer();
ldapServer.setServiceName("DefaultLDAP");
ldapServer.setDirectoryService(directoryService);
ldapServer.setTransports(new TcpTransport("localhost", 10389)); // plain LDAP only
ldapServer.start();
// Manually load LDIF
logger.info("Loading test objects");
File ldif = new File("src/test/resources/test-users.ldif");
new LdifFileLoader(directoryService.getAdminSession(), ldif, null).execute();