|
28 | 28 | import com.unboundid.ldap.listener.InMemoryDirectoryServer;
|
29 | 29 | import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
|
30 | 30 | import com.unboundid.ldap.listener.InMemoryListenerConfig;
|
| 31 | +import org.junit.Before; |
31 | 32 | import org.junit.Test;
|
32 | 33 |
|
33 | 34 | import org.springframework.ldap.core.AttributesMapper;
|
|
40 | 41 |
|
41 | 42 | public class EmbeddedLdapServerTests {
|
42 | 43 |
|
| 44 | + private int port; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() throws IOException { |
| 48 | + this.port = getFreePort(); |
| 49 | + } |
| 50 | + |
43 | 51 | @Test
|
44 |
| - public void shouldStartAndCloseServer() throws Exception { |
45 |
| - int port = getFreePort(); |
46 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 52 | + public void shouldStartAndCloseServer() { |
| 53 | + assertPortIsFree(this.port); |
47 | 54 |
|
48 |
| - EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port); |
49 |
| - assertThat(isPortOpen(port)).isTrue(); |
| 55 | + EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port); |
| 56 | + assertPortIsUsed(this.port); |
50 | 57 |
|
51 | 58 | server.close();
|
52 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 59 | + assertPortIsFree(this.port); |
53 | 60 | }
|
54 | 61 |
|
55 | 62 | @Test
|
56 |
| - public void shouldStartAndAutoCloseServer() throws Exception { |
57 |
| - int port = getFreePort(); |
58 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 63 | + public void shouldStartAndAutoCloseServer() { |
| 64 | + assertPortIsFree(this.port); |
59 | 65 |
|
60 |
| - try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port)) { |
61 |
| - assertThat(isPortOpen(port)).isTrue(); |
| 66 | + try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", |
| 67 | + this.port)) { |
| 68 | + assertPortIsUsed(this.port); |
62 | 69 | }
|
63 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 70 | + assertPortIsFree(this.port); |
64 | 71 | }
|
65 | 72 |
|
66 | 73 | @Test
|
67 | 74 | public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
|
68 |
| - int port = getFreePort(); |
69 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 75 | + assertPortIsFree(this.port); |
70 | 76 |
|
71 |
| - LdapTestUtils.startEmbeddedServer(port, "dc=jayway,dc=se", "jayway"); |
72 |
| - assertThat(isPortOpen(port)).isTrue(); |
| 77 | + LdapTestUtils.startEmbeddedServer(this.port, "dc=jayway,dc=se", "jayway"); |
| 78 | + assertPortIsUsed(this.port); |
73 | 79 |
|
74 | 80 | LdapTestUtils.shutdownEmbeddedServer();
|
75 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 81 | + assertPortIsFree(this.port); |
76 | 82 | }
|
77 | 83 |
|
78 | 84 | @Test
|
79 |
| - public void startWhenNewEmbeddedServerThenException() throws Exception { |
80 |
| - int port = getFreePort(); |
81 |
| - EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port); |
| 85 | + public void startWhenNewEmbeddedServerThenException() { |
| 86 | + EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port); |
82 | 87 | assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
|
83 | 88 | }
|
84 | 89 |
|
85 | 90 | @Test
|
86 | 91 | public void startWhenUnstartedThenWorks() throws Exception {
|
87 |
| - int port = getFreePort(); |
88 | 92 | InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
|
89 |
| - config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); |
| 93 | + config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); |
90 | 94 | InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
|
91 | 95 | try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
|
92 | 96 | server.start();
|
93 |
| - assertThat(isPortOpen(port)).isTrue(); |
| 97 | + assertPortIsUsed(this.port); |
94 | 98 | }
|
95 | 99 | }
|
96 | 100 |
|
97 | 101 | @Test
|
98 | 102 | public void startWhenAlreadyStartedThenFails() throws Exception {
|
99 |
| - int port = getFreePort(); |
100 | 103 | InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
|
101 |
| - config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); |
| 104 | + config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); |
102 | 105 | InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
|
103 | 106 | try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
|
104 | 107 | server.start();
|
105 |
| - assertThat(isPortOpen(port)).isTrue(); |
| 108 | + assertPortIsUsed(this.port); |
106 | 109 | assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
|
107 | 110 | }
|
108 | 111 | }
|
109 | 112 |
|
110 | 113 | @Test
|
111 |
| - public void shouldBuildButNotStartTheServer() throws IOException { |
112 |
| - int port = getFreePort(); |
113 |
| - EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(port).build(); |
114 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 114 | + public void shouldBuildButNotStartTheServer() { |
| 115 | + EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(this.port).build(); |
| 116 | + assertPortIsFree(this.port); |
115 | 117 | }
|
116 | 118 |
|
117 | 119 | @Test
|
118 |
| - public void shouldBuildTheServerWithCustomPort() throws IOException { |
119 |
| - int port = getFreePort(); |
120 |
| - EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(port); |
| 120 | + public void shouldBuildTheServerWithCustomPort() { |
| 121 | + EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se") |
| 122 | + .port(this.port); |
121 | 123 |
|
122 | 124 | try (EmbeddedLdapServer server = serverBuilder.build()) {
|
123 | 125 | server.start();
|
124 |
| - assertThat(isPortOpen(port)).isTrue(); |
| 126 | + assertPortIsUsed(this.port); |
125 | 127 | }
|
126 |
| - assertThat(isPortOpen(port)).isFalse(); |
| 128 | + assertPortIsFree(this.port); |
127 | 129 | }
|
128 | 130 |
|
129 | 131 | @Test
|
130 | 132 | public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException {
|
131 |
| - int port = getFreePort(); |
132 | 133 | String tempLogFile = Files.createTempFile("ldap-log-", ".txt").toAbsolutePath().toString();
|
133 | 134 |
|
134 | 135 | EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
|
135 |
| - .port(port) |
| 136 | + .port(this.port) |
136 | 137 | .configurationCustomizer((config) -> config.setCodeLogDetails(tempLogFile, true));
|
137 | 138 |
|
138 | 139 | try (EmbeddedLdapServer server = serverBuilder.build()) {
|
139 | 140 | server.start();
|
140 | 141 |
|
141 |
| - ldapTemplate("dc=jayway,dc=se", port).search(LdapQueryBuilder.query().where("objectclass").is("person"), |
142 |
| - new AttributesMapper<>() { |
143 |
| - public String mapFromAttributes(Attributes attrs) throws NamingException { |
144 |
| - return (String) attrs.get("cn").get(); |
145 |
| - } |
146 |
| - }); |
| 142 | + ldapTemplate("dc=jayway,dc=se", this.port) |
| 143 | + .search(LdapQueryBuilder.query().where("objectclass").is("person"), new AttributesMapper<>() { |
| 144 | + public String mapFromAttributes(Attributes attrs) throws NamingException { |
| 145 | + return (String) attrs.get("cn").get(); |
| 146 | + } |
| 147 | + }); |
147 | 148 | }
|
148 | 149 |
|
149 | 150 | assertThat(Path.of(tempLogFile))
|
150 | 151 | .as("Applying the custom configuration should create a log file and populate it with the request")
|
151 | 152 | .isNotEmptyFile();
|
152 | 153 | }
|
153 | 154 |
|
| 155 | + static void assertPortIsFree(int port) { |
| 156 | + assertThat(isPortOpen(port)).isFalse(); |
| 157 | + } |
| 158 | + |
| 159 | + static void assertPortIsUsed(int port) { |
| 160 | + assertThat(isPortOpen(port)).isTrue(); |
| 161 | + } |
| 162 | + |
154 | 163 | static boolean isPortOpen(int port) {
|
155 | 164 | try (Socket ignored = new Socket("localhost", port)) {
|
156 | 165 | return true;
|
|
0 commit comments