0

I need to embeed a LDAP server with spring for testing purposes. The following code works:

src/main/webapp/WEB-INF/web.xml

[...]
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        [...]
        com.example.config.servlet.TestLDAPServerConfiguration
    </param-value>
</context-param>
[...]

src/main/java/com/example/config/servlet/TestLDAPServerConfiguration.java

package com.example.config.servlet;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource({"/WEB-INF/test-ldap-server.xml"})
public class TestLDAPServerConfiguration {
}

src/main/webapp/WEB-INF/test-ldap-server.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:s="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

  <s:ldap-server ldif="classpath:users.ldif" root="dc=nestle,dc=com" port="33389"/>
</beans>

I do need to use AnnotationConfigWebApplicationContext instead of XmlWebApplicationContext. However, here I am using a class TestLDAPServerConfiguration which just imports a test-ldap-server.xml, and this test-ldap-server.xml just declares the ldap-server.

I'd like to remove the test-ldap-server.xml file. How can I do the equivalent of s:ldap-server with java code inside the TestLDAPServerConfiguration class? And where is this documented?

David Portabella
  • 12,390
  • 27
  • 101
  • 182

1 Answers1

0

Try something like this;

protected static ApacheDSContainer server;

@BeforeClass
public static void startServer() throws Exception {
    server = new ApacheDSContainer( "dc=yourdomain,dc=com", "classpath:test-server.ldif" );
    server.setPort( 53389 );
    server.afterPropertiesSet();
    server.start();
}

@AfterClass
public static void stopServer() throws Exception {
    if( server != null ) {
        server.stop();
    }
}

You can get guides from the spring-security-ldap source code.

ivanorone
  • 450
  • 4
  • 17