Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Error When Starting Twice
Issue gh-1025
  • Loading branch information
jzheaux committed Mar 11, 2025
commit db7ed4cf07a23ac842e4687180383241b350b2b0
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;

import org.springframework.util.Assert;

/**
* Helper class for embedded Unboundid ldap server.
*
Expand All @@ -33,7 +35,13 @@ public final class EmbeddedLdapServer implements AutoCloseable {

private final InMemoryDirectoryServer directoryServer;

private EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
/**
* Construct an {@link EmbeddedLdapServer} using the provided
* {@link InMemoryDirectoryServer}.
*
* @since 3.3
*/
public EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
this.directoryServer = directoryServer;
}

Expand Down Expand Up @@ -66,6 +74,7 @@ public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName,
* @since 3.3
*/
public void start() {
Assert.isTrue(this.directoryServer.getListenPort() == -1, "The server has already been started.");
try {
this.directoryServer.startListening();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
import java.net.ServerSocket;
import java.net.Socket;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class EmbeddedLdapServerTests {

Expand Down Expand Up @@ -61,6 +65,38 @@ public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
assertThat(isPortOpen(port)).isFalse();
}

@Test
public void startWhenNewEmbeddedServerThenException() throws Exception {
int port = getFreePort();
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port);
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
}

@Test
public void startWhenUnstartedThenWorks() throws Exception {
int port = getFreePort();
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
server.start();
assertThat(isPortOpen(port)).isTrue();
}
}

@Test
public void startWhenAlreadyStartedThenFails() throws Exception {
int port = getFreePort();
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
server.start();
assertThat(isPortOpen(port)).isTrue();
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
}
}

static boolean isPortOpen(int port) {
try (Socket ignored = new Socket("localhost", port)) {
return true;
Expand Down
Loading