|
| 1 | +/* |
| 2 | + * Copyright 2005-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ldap.test.unboundid; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.ServerSocket; |
| 21 | +import java.net.Socket; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +public class EmbeddedLdapServerTests { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void shouldStartAndCloseServer() throws Exception { |
| 31 | + int port = getFreePort(); |
| 32 | + assertThat(isPortOpen(port)).isFalse(); |
| 33 | + |
| 34 | + EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port); |
| 35 | + assertThat(isPortOpen(port)).isTrue(); |
| 36 | + |
| 37 | + server.close(); |
| 38 | + assertThat(isPortOpen(port)).isFalse(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void shouldStartAndAutoCloseServer() throws Exception { |
| 43 | + int port = getFreePort(); |
| 44 | + assertThat(isPortOpen(port)).isFalse(); |
| 45 | + |
| 46 | + try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port)) { |
| 47 | + assertThat(isPortOpen(port)).isTrue(); |
| 48 | + } |
| 49 | + assertThat(isPortOpen(port)).isFalse(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception { |
| 54 | + int port = getFreePort(); |
| 55 | + assertThat(isPortOpen(port)).isFalse(); |
| 56 | + |
| 57 | + LdapTestUtils.startEmbeddedServer(port, "dc=jayway,dc=se", "jayway"); |
| 58 | + assertThat(isPortOpen(port)).isTrue(); |
| 59 | + |
| 60 | + LdapTestUtils.shutdownEmbeddedServer(); |
| 61 | + assertThat(isPortOpen(port)).isFalse(); |
| 62 | + } |
| 63 | + |
| 64 | + static boolean isPortOpen(int port) { |
| 65 | + try (Socket ignored = new Socket("localhost", port)) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + catch (IOException ex) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static int getFreePort() throws IOException { |
| 74 | + try (ServerSocket serverSocket = new ServerSocket(0)) { |
| 75 | + return serverSocket.getLocalPort(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments