SEC-2558: Fix failing ApacheDSContainerTests when port is taken

This commit is contained in:
Rob Winch 2014-04-15 11:52:09 -05:00
parent 5b216bd0b2
commit fa6218cf73
1 changed files with 29 additions and 1 deletions

View File

@ -17,6 +17,11 @@ package org.springframework.security.ldap.server;
import static junit.framework.Assert.fail; import static junit.framework.Assert.fail;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test; import org.junit.Test;
/** /**
@ -32,7 +37,10 @@ public class ApacheDSContainerTests {
@Test @Test
public void failsToStartThrowsException() throws Exception { public void failsToStartThrowsException() throws Exception {
ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:missing.ldif");
List<Integer> ports = getDefaultPorts(1);
server1.setPort(ports.get(0));
server2.setPort(ports.get(0));
try { try {
server1.afterPropertiesSet(); server1.afterPropertiesSet();
try { try {
@ -54,6 +62,9 @@ public class ApacheDSContainerTests {
public void multipleInstancesSimultanciously() throws Exception { public void multipleInstancesSimultanciously() throws Exception {
ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
List<Integer> ports = getDefaultPorts(2);
server1.setPort(ports.get(0));
server2.setPort(ports.get(1));
try { try {
server1.afterPropertiesSet(); server1.afterPropertiesSet();
server2.afterPropertiesSet(); server2.afterPropertiesSet();
@ -66,4 +77,21 @@ public class ApacheDSContainerTests {
}catch(Throwable t) {} }catch(Throwable t) {}
} }
} }
private List<Integer> getDefaultPorts(int count) throws IOException {
List<ServerSocket> connections = new ArrayList<ServerSocket>();
List<Integer> availablePorts = new ArrayList<Integer>(count);
try {
for(int i=0;i<count;i++) {
ServerSocket socket = new ServerSocket(0);
connections.add(socket);
availablePorts.add(socket.getLocalPort());
}
return availablePorts;
} finally {
for(ServerSocket conn : connections) {
conn.close();
}
}
}
} }