Add start/stop methods

Closes #1025

Signed-off-by: emanueltrandafir1993 <emanueltrandafir1993@gmail.com>
This commit is contained in:
emanueltrandafir1993
2025-02-20 08:42:53 +02:00
committed by Josh Cummings
parent 739813c341
commit 4a3c3fead0
4 changed files with 102 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
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.
@@ -28,14 +29,17 @@ import com.unboundid.ldap.sdk.Entry;
* @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);
@@ -56,7 +60,36 @@ public final class EmbeddedLdapServer {
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);
}

View File

@@ -53,7 +53,7 @@ public class EmbeddedLdapServerFactoryBean extends AbstractFactoryBean<EmbeddedL
@Override
protected void destroyInstance(EmbeddedLdapServer instance) throws Exception {
instance.shutdown();
instance.close();
}
}

View File

@@ -92,7 +92,7 @@ public final class LdapTestUtils {
*/
public static void shutdownEmbeddedServer() throws Exception {
if (embeddedServer != null) {
embeddedServer.shutdown();
embeddedServer.close();
embeddedServer = null;
}
}

View File

@@ -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();
}
}
}