Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve EmbeddedLdapServer Lifecycle Support #1027

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.sdk.DN;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;

/**
* Helper class for embedded Unboundid ldap server.
*
* @author Eddu Melendez
* @since 2.1.0
*/
public final class EmbeddedLdapServer {
public final class EmbeddedLdapServer implements AutoCloseable {

private InMemoryDirectoryServer directoryServer;
private final InMemoryDirectoryServer directoryServer;

private EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
this.directoryServer = directoryServer;
}

/**
* Creates and starts new embedded LDAP server.
*/
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix,
int port) throws Exception {
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(defaultPartitionSuffix);
Expand All @@ -56,7 +60,36 @@ public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName,
return new EmbeddedLdapServer(directoryServer);
}

public void shutdown() throws Exception {
/**
* Starts the embedded LDAP server.
*
* @since 3.3
*/
public void start() {
try {
this.directoryServer.startListening();
}
catch (LDAPException ex) {
throw new RuntimeException(ex);
}
}

/**
* Closes the embedded LDAP server and releases resource, closing existing
* connections.
*
* @since 3.3
*/
@Override
public void close() {
this.directoryServer.shutDown(true);
}

/**
* @deprecated Use {@link #close()} instead.
*/
@Deprecated(since = "3.3")
public void shutdown() {
this.directoryServer.shutDown(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected EmbeddedLdapServer createInstance() throws Exception {

@Override
protected void destroyInstance(EmbeddedLdapServer instance) throws Exception {
instance.shutdown();
instance.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void startEmbeddedServer(int port, String defaultPartitionSuffix,
*/
public static void shutdownEmbeddedServer() throws Exception {
if (embeddedServer != null) {
embeddedServer.shutdown();
embeddedServer.close();
embeddedServer = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.springframework.ldap.test.unboundid;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

public class EmbeddedLdapServerTest {

@Test
public void shouldStartAndCloseServer() throws Exception {
int port = getFreePort();
assertFalse(isPortOpen(port));

EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port);
assertTrue(isPortOpen(port));

server.close();
assertFalse(isPortOpen(port));
}

@Test
public void shouldStartAndAutoCloseServer() throws Exception {
int port = getFreePort();
assertFalse(isPortOpen(port));

try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port)) {
assertTrue(isPortOpen(port));
}
assertFalse(isPortOpen(port));
}

@Test
public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
int port = getFreePort();
assertFalse(isPortOpen(port));

LdapTestUtils.startEmbeddedServer(port, "dc=jayway,dc=se", "jayway");
assertTrue(isPortOpen(port));

LdapTestUtils.shutdownEmbeddedServer();
assertFalse(isPortOpen(port));
}

static boolean isPortOpen(int port) {
try (Socket ignored = new Socket("localhost", port)) {
return true;
}
catch (IOException e) {
return false;
}
}

static int getFreePort() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
}

}