forked from spring-projects/spring-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes spring-projects#1025 Signed-off-by: emanueltrandafir1993 <[email protected]>
- Loading branch information
1 parent
cc1a15f
commit d327843
Showing
4 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |