Skip to content

Commit db7ed4c

Browse files
committed
Error When Starting Twice
Issue spring-projectsgh-1025
1 parent 936112c commit db7ed4c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

test-support/src/main/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.unboundid.ldap.sdk.Entry;
2424
import com.unboundid.ldap.sdk.LDAPException;
2525

26+
import org.springframework.util.Assert;
27+
2628
/**
2729
* Helper class for embedded Unboundid ldap server.
2830
*
@@ -33,7 +35,13 @@ public final class EmbeddedLdapServer implements AutoCloseable {
3335

3436
private final InMemoryDirectoryServer directoryServer;
3537

36-
private EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
38+
/**
39+
* Construct an {@link EmbeddedLdapServer} using the provided
40+
* {@link InMemoryDirectoryServer}.
41+
*
42+
* @since 3.3
43+
*/
44+
public EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
3745
this.directoryServer = directoryServer;
3846
}
3947

@@ -66,6 +74,7 @@ public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName,
6674
* @since 3.3
6775
*/
6876
public void start() {
77+
Assert.isTrue(this.directoryServer.getListenPort() == -1, "The server has already been started.");
6978
try {
7079
this.directoryServer.startListening();
7180
}

test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
import java.net.ServerSocket;
2121
import java.net.Socket;
2222

23+
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
24+
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
25+
import com.unboundid.ldap.listener.InMemoryListenerConfig;
2326
import org.junit.Test;
2427

2528
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2630

2731
public class EmbeddedLdapServerTests {
2832

@@ -61,6 +65,38 @@ public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
6165
assertThat(isPortOpen(port)).isFalse();
6266
}
6367

68+
@Test
69+
public void startWhenNewEmbeddedServerThenException() throws Exception {
70+
int port = getFreePort();
71+
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port);
72+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
73+
}
74+
75+
@Test
76+
public void startWhenUnstartedThenWorks() throws Exception {
77+
int port = getFreePort();
78+
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
79+
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
80+
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
81+
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
82+
server.start();
83+
assertThat(isPortOpen(port)).isTrue();
84+
}
85+
}
86+
87+
@Test
88+
public void startWhenAlreadyStartedThenFails() throws Exception {
89+
int port = getFreePort();
90+
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
91+
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
92+
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
93+
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
94+
server.start();
95+
assertThat(isPortOpen(port)).isTrue();
96+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
97+
}
98+
}
99+
64100
static boolean isPortOpen(int port) {
65101
try (Socket ignored = new Socket("localhost", port)) {
66102
return true;

0 commit comments

Comments
 (0)