diff --git a/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServer.java b/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServer.java index bae69bbdf8..2ea3ce36ce 100644 --- a/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServer.java +++ b/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServer.java @@ -37,10 +37,16 @@ private EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) { } public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, - String defaultPartitionSuffix, int port) throws Exception { + String defaultPartitionSuffix, int port) throws Exception { + return newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, port, + "uid=admin,ou=system", "secret"); + } + + public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, + String defaultPartitionSuffix, int port, String principal, String credentials) throws Exception { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig( defaultPartitionSuffix); - config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); + config.addAdditionalBindCredentials(principal, credentials); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); diff --git a/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/LdapTestUtils.java b/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/LdapTestUtils.java index 7a09639677..d7599945c9 100644 --- a/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/LdapTestUtils.java +++ b/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/LdapTestUtils.java @@ -63,34 +63,63 @@ private LdapTestUtils() { } /** - * Start an embedded Apache Directory Server. Only one embedded server will be permitted in the same JVM. + * Start an embedded UnboundId InMemory Directory Server. Only one embedded server will be permitted in the same JVM. * * @param port the port on which the server will be listening. * @param defaultPartitionSuffix The default base suffix that will be used * for the LDAP server. * @param defaultPartitionName The name to use in the directory server * configuration for the default base suffix. + * @param principal The principal to use when starting the directory server. + * @param credentials The credentials to use when starting the directory + * server. * * @throws IllegalStateException if an embedded server is already started. */ - public static void startEmbeddedServer(int port, String defaultPartitionSuffix, String defaultPartitionName) { + public static void startEmbeddedServer(int port, String defaultPartitionSuffix, String defaultPartitionName, + String principal, String credentials) { if(embeddedServer != null) { throw new IllegalStateException("An embedded server is already started"); } try { - embeddedServer = EmbeddedLdapServer.newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, port); + if(credentials != null && principal != null) { + embeddedServer = EmbeddedLdapServer.newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, + port, principal, credentials); + } else { + embeddedServer = EmbeddedLdapServer.newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, + port); + } } catch (Exception e) { throw new UncategorizedLdapException("Failed to start embedded server", e); } } /** - * Shuts down the embedded server, if there is one. If no server was previously started in this JVM - * this is silently ignored. + * Start an embedded UnboundId InMemory Directory Server. Only one embedded server will be permitted in the same JVM. + * If you use this method you can only connect using the principal uid=admin,ou=system and credential + * secret, otherwise consider using {@link #startEmbeddedServer(int, String, String, String, String)} * - * @throws Exception + * @param port the port on which the server will be listening. + * @param defaultPartitionSuffix The default base suffix that will be used + * for the LDAP server. + * @param defaultPartitionName The name to use in the directory server + * configuration for the default base suffix. + * server. + * + * @throws IllegalStateException if an embedded server is already started. */ + public static void startEmbeddedServer(int port, String defaultPartitionSuffix, String defaultPartitionName) { + startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName, null, null); + } + + + /** + * Shuts down the embedded server, if there is one. If no server was previously started in this JVM + * this is silently ignored. + * + * @throws Exception + */ public static void shutdownEmbeddedServer() throws Exception { if(embeddedServer != null) { embeddedServer.shutdown(); diff --git a/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBean.java b/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBean.java index 81ee5c37e9..401d913522 100644 --- a/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBean.java +++ b/test-support-unboundid/src/main/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBean.java @@ -99,7 +99,7 @@ public void setContextSource(ContextSource contextSource) { protected ContextSource createInstance() throws Exception { LdapTestUtils.startEmbeddedServer(port, - defaultPartitionSuffix, defaultPartitionName); + defaultPartitionSuffix, defaultPartitionName, principal, password); if (contextSource == null) { // If not explicitly configured, create a new instance. diff --git a/test-support-unboundid/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTest.java b/test-support-unboundid/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTest.java index 1a45e09745..5e540fee44 100644 --- a/test-support-unboundid/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTest.java +++ b/test-support-unboundid/src/test/java/org/springframework/ldap/test/unboundid/TestContextSourceFactoryBeanTest.java @@ -16,6 +16,8 @@ package org.springframework.ldap.test.unboundid; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import javax.naming.NamingException; @@ -24,6 +26,10 @@ import org.junit.After; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.junit.runners.Parameterized.Parameter; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; @@ -31,9 +37,20 @@ import static org.assertj.core.api.Assertions.assertThat; +@RunWith(Parameterized.class) public class TestContextSourceFactoryBeanTest { ClassPathXmlApplicationContext ctx; + @Parameter + public String configLocation; + + @Parameters(name = "configLocation={0}") + public static Collection data() { + return Arrays.asList(new Object[][] { + { "/applicationContext-testContextSource.xml" }, { "/applicationContext-testContextSource_different_credentials.xml" } + }); + } + @After public void setup() { if(ctx != null) { @@ -43,7 +60,7 @@ public void setup() { @Test public void testServerStartup() throws Exception { - ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml"); + ctx = new ClassPathXmlApplicationContext(configLocation); LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class); assertThat(ldapTemplate).isNotNull(); diff --git a/test-support-unboundid/src/test/resources/applicationContext-testContextSource_different_credentials.xml b/test-support-unboundid/src/test/resources/applicationContext-testContextSource_different_credentials.xml new file mode 100644 index 0000000000..27753b3054 --- /dev/null +++ b/test-support-unboundid/src/test/resources/applicationContext-testContextSource_different_credentials.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + +