SEC-2606: ApacheDSServerIntegrationTests scan for available port

This commit is contained in:
Rob Winch 2014-05-21 06:49:00 -05:00
parent e6e35932ed
commit 3ba1f66f9d
4 changed files with 32 additions and 4 deletions

View File

@ -25,7 +25,8 @@ public abstract class AbstractLdapIntegrationTests {
@BeforeClass
public static void createContextSource() throws Exception {
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:53389/dc=springframework,dc=org");
int serverPort = ApacheDSServerIntegrationTests.getServerPort();
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:" + serverPort + "/dc=springframework,dc=org");
// OpenLDAP configuration
// contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:22389/dc=springsource,dc=com");
// contextSource.setUserDn("cn=admin,dc=springsource,dc=com");

View File

@ -1,5 +1,8 @@
package org.springframework.security.ldap;
import java.io.IOException;
import java.net.ServerSocket;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@ -26,6 +29,7 @@ import org.springframework.security.ldap.userdetails.LdapUserDetailsManagerTests
)
public final class ApacheDSServerIntegrationTests {
private static ApacheDSContainer server;
private static Integer serverPort;
@BeforeClass
public static void startServer() throws Exception {
@ -34,12 +38,15 @@ public final class ApacheDSServerIntegrationTests {
// contextSource.setUserDn("cn=admin,dc=springsource,dc=com");
// contextSource.setPassword("password");
server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
server.setPort(53389);
int port = getAvailablePort();
server.setPort(port);
server.afterPropertiesSet();
serverPort = port;
}
@AfterClass
public static void stopServer() throws Exception {
serverPort = null;
if (server != null) {
server.stop();
}
@ -53,6 +60,12 @@ public final class ApacheDSServerIntegrationTests {
server.afterPropertiesSet();
}
public static int getServerPort() {
if(serverPort == null) {
throw new IllegalStateException("The ApacheDSContainer is not currently running");
}
return serverPort;
}
/*
@After
public final void reloadServerDataIfDirty() throws Exception {
@ -105,4 +118,18 @@ public final class ApacheDSServerIntegrationTests {
}
}
*/
private static int getAvailablePort() throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(0);
return serverSocket.getLocalPort();
} finally {
if(serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {}
}
}
}
}

View File

@ -71,7 +71,7 @@ public class DefaultSpringSecurityContextSourceTests extends AbstractLdapIntegra
@Test
public void serverUrlWithSpacesIsSupported() throws Exception {
DefaultSpringSecurityContextSource
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:53389/ou=space%20cadets,dc=springframework,dc=org");
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:" + ApacheDSServerIntegrationTests.getServerPort() + "/ou=space%20cadets,dc=springframework,dc=org");
contextSource.afterPropertiesSet();
contextSource.getContext("uid=space cadet,ou=space cadets,dc=springframework,dc=org", "spacecadetspassword");
}

View File

@ -121,7 +121,7 @@ public class SpringSecurityLdapTemplateITests extends AbstractLdapIntegrationTes
public void nonSpringLdapSearchCodeTestMethod() throws Exception {
java.util.Hashtable<String, String> env = new java.util.Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:53389");
env.put(Context.PROVIDER_URL, "ldap://localhost:" + ApacheDSServerIntegrationTests.getServerPort());
env.put(Context.SECURITY_PRINCIPAL, "");
env.put(Context.SECURITY_CREDENTIALS, "");