Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d1c520c

Browse files
committedMar 27, 2025·
Polish EmbeddedLdapServer Tests
Issue #938 Signed-off-by: Emanuel Trandafir <emanueltrandafir1993@gmail.com> Signed-off-by: etrandafir93 <emanueltrandafir1993@gmail.com>
1 parent 7435738 commit d1c520c

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed
 

‎test-support/src/test/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerTests.java

+52-43
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
2929
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
3030
import com.unboundid.ldap.listener.InMemoryListenerConfig;
31+
import org.junit.Before;
3132
import org.junit.Test;
3233

3334
import org.springframework.ldap.core.AttributesMapper;
@@ -40,117 +41,125 @@
4041

4142
public class EmbeddedLdapServerTests {
4243

44+
private int port;
45+
46+
@Before
47+
public void setUp() throws IOException {
48+
this.port = getFreePort();
49+
}
50+
4351
@Test
44-
public void shouldStartAndCloseServer() throws Exception {
45-
int port = getFreePort();
46-
assertThat(isPortOpen(port)).isFalse();
52+
public void shouldStartAndCloseServer() {
53+
assertPortIsFree(this.port);
4754

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);
5057

5158
server.close();
52-
assertThat(isPortOpen(port)).isFalse();
59+
assertPortIsFree(this.port);
5360
}
5461

5562
@Test
56-
public void shouldStartAndAutoCloseServer() throws Exception {
57-
int port = getFreePort();
58-
assertThat(isPortOpen(port)).isFalse();
63+
public void shouldStartAndAutoCloseServer() {
64+
assertPortIsFree(this.port);
5965

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);
6269
}
63-
assertThat(isPortOpen(port)).isFalse();
70+
assertPortIsFree(this.port);
6471
}
6572

6673
@Test
6774
public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
68-
int port = getFreePort();
69-
assertThat(isPortOpen(port)).isFalse();
75+
assertPortIsFree(this.port);
7076

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);
7379

7480
LdapTestUtils.shutdownEmbeddedServer();
75-
assertThat(isPortOpen(port)).isFalse();
81+
assertPortIsFree(this.port);
7682
}
7783

7884
@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);
8287
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
8388
}
8489

8590
@Test
8691
public void startWhenUnstartedThenWorks() throws Exception {
87-
int port = getFreePort();
8892
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
89-
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
93+
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
9094
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
9195
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
9296
server.start();
93-
assertThat(isPortOpen(port)).isTrue();
97+
assertPortIsUsed(this.port);
9498
}
9599
}
96100

97101
@Test
98102
public void startWhenAlreadyStartedThenFails() throws Exception {
99-
int port = getFreePort();
100103
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
101-
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
104+
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
102105
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
103106
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
104107
server.start();
105-
assertThat(isPortOpen(port)).isTrue();
108+
assertPortIsUsed(this.port);
106109
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
107110
}
108111
}
109112

110113
@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);
115117
}
116118

117119
@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);
121123

122124
try (EmbeddedLdapServer server = serverBuilder.build()) {
123125
server.start();
124-
assertThat(isPortOpen(port)).isTrue();
126+
assertPortIsUsed(this.port);
125127
}
126-
assertThat(isPortOpen(port)).isFalse();
128+
assertPortIsFree(this.port);
127129
}
128130

129131
@Test
130132
public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException {
131-
int port = getFreePort();
132133
String tempLogFile = Files.createTempFile("ldap-log-", ".txt").toAbsolutePath().toString();
133134

134135
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
135-
.port(port)
136+
.port(this.port)
136137
.configurationCustomizer((config) -> config.setCodeLogDetails(tempLogFile, true));
137138

138139
try (EmbeddedLdapServer server = serverBuilder.build()) {
139140
server.start();
140141

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+
});
147148
}
148149

149150
assertThat(Path.of(tempLogFile))
150151
.as("Applying the custom configuration should create a log file and populate it with the request")
151152
.isNotEmptyFile();
152153
}
153154

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+
154163
static boolean isPortOpen(int port) {
155164
try (Socket ignored = new Socket("localhost", port)) {
156165
return true;

0 commit comments

Comments
 (0)
Please sign in to comment.