/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.security.ldap.server;

import static junit.framework.Assert.fail;

import org.junit.Test;

/**
 * Useful for debugging the container by itself.
 *
 * @author Luke Taylor
 * @author Rob Winch
 * @since 3.0
 */
public class ApacheDSContainerTests {

    // SEC-2162
    @Test
    public void failsToStartThrowsException() throws Exception {
        ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
        ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
        try {
            server1.afterPropertiesSet();
            try {
                server2.afterPropertiesSet();
                fail("Expected Exception");
            } catch(RuntimeException success) {}
        } finally {
            try {
                server1.destroy();
            }catch(Throwable t) {}
            try {
                server2.destroy();
            }catch(Throwable t) {}
        }
    }

    // SEC-2161
    @Test
    public void multipleInstancesSimultanciously() throws Exception {
        ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
        ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
        try {
            server1.afterPropertiesSet();
            server2.afterPropertiesSet();
        } finally {
            try {
                server1.destroy();
            }catch(Throwable t) {}
            try {
                server2.destroy();
            }catch(Throwable t) {}
        }
    }
}

@@ -709,16 +709,126 @@
 .fail;%0A%0A
+import java.io.IOException;%0Aimport java.net.ServerSocket;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import o
@@ -842,16 +842,16 @@
 t.Test;%0A
-
 %0A/**%0A * 
@@ -1307,33 +1307,157 @@
 asspath:
-test-server.ldif%22
+missing.ldif%22);%0A        List%3CInteger%3E ports = getDefaultPorts(1);%0A        server1.setPort(ports.get(0));%0A        server2.setPort(ports.get(0)
 );%0A     
@@ -2201,32 +2201,160 @@
 -server.ldif%22);%0A
+        List%3CInteger%3E ports = getDefaultPorts(2);%0A        server1.setPort(ports.get(0));%0A        server2.setPort(ports.get(1));%0A
         try %7B%0A  
@@ -2588,32 +2588,32 @@
 ver2.destroy();%0A
-
             %7Dcat
@@ -2646,11 +2646,620 @@
  %7D%0A    %7D
+%0A%0A    private List%3CInteger%3E getDefaultPorts(int count) throws IOException %7B%0A        List%3CServerSocket%3E connections = new ArrayList%3CServerSocket%3E();%0A        List%3CInteger%3E availablePorts = new ArrayList%3CInteger%3E(count);%0A        try %7B%0A            for(int i=0;i%3Ccount;i++) %7B%0A                ServerSocket socket = new ServerSocket(0);%0A                connections.add(socket);%0A                availablePorts.add(socket.getLocalPort());%0A            %7D%0A            return availablePorts;%0A        %7D finally %7B%0A            for(ServerSocket conn : connections) %7B%0A                conn.close();%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Fix failing ApacheDSContainerTests when port is taken