Skip to content

Commit 3c0583d

Browse files
committed
code review
Closes #938 Signed-off-by: emanueltrandafir1993 <[email protected]>
1 parent 543e15a commit 3c0583d

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

test-support/src/main/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServer.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
package org.springframework.ldap.test.unboundid;
1818

19+
import java.util.List;
1920
import java.util.function.Consumer;
2021

22+
import javax.naming.InvalidNameException;
23+
import javax.naming.ldap.LdapName;
24+
import javax.naming.ldap.Rdn;
25+
2126
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
2227
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
2328
import com.unboundid.ldap.listener.InMemoryListenerConfig;
@@ -26,6 +31,7 @@
2631
import com.unboundid.ldap.sdk.LDAPException;
2732

2833
import org.springframework.util.Assert;
34+
import org.springframework.util.CollectionUtils;
2935

3036
/**
3137
* Helper class for embedded Unboundid ldap server.
@@ -115,31 +121,52 @@ public void shutdown() {
115121
*/
116122
public static final class Builder {
117123

124+
private final String partitionSuffix;
125+
126+
private String partitionName;
127+
118128
private int port = 0;
119129

120130
private Consumer<InMemoryDirectoryServerConfig> configurationCustomizer = (__) -> {
121131
};
122132

123-
private String partitionName = "jayway";
124-
125-
private final String partitionSuffix;
126-
127133
private Builder(String partitionSuffix) {
128134
this.partitionSuffix = partitionSuffix;
135+
this.partitionName = leftMostElement(partitionSuffix);
129136
}
130137

138+
/**
139+
* Sets the port for the embedded LDAP server.
140+
* @param port the port for the embedded LDAP server. Defaults to 0 in which case
141+
* the server should automatically choose an available port.
142+
* @return this {@link Builder} instance.
143+
*/
131144
public Builder port(int port) {
132145
this.port = port;
133146
return this;
134147
}
135148

149+
/**
150+
* Sets a customizer for the {@link InMemoryDirectoryServerConfig}.
151+
* @param configurationCustomizer a {@link Consumer} function that will be applied
152+
* to the {@link InMemoryDirectoryServerConfig} before creating the
153+
* {@link InMemoryDirectoryServer}. The default values, it a Consumer function
154+
* that does nothing: (config) -> {}
155+
* @return this {@link Builder} instance.
156+
*/
136157
public Builder configurationCustomizer(Consumer<InMemoryDirectoryServerConfig> configurationCustomizer) {
137158
this.configurationCustomizer = configurationCustomizer;
138159
return this;
139160
}
140161

141-
public Builder partitionName(String defaultPartitionName) {
142-
this.partitionName = defaultPartitionName;
162+
/**
163+
* Sets the partition name for the embedded LDAP server.
164+
* @param partitionName the partition name for the embedded LDAP server. Defaults
165+
* to the left most element of the partition suffix.
166+
* @return this {@link Builder} instance.
167+
*/
168+
public Builder partitionName(String partitionName) {
169+
this.partitionName = partitionName;
143170
return this;
144171
}
145172

@@ -164,6 +191,16 @@ public EmbeddedLdapServer build() {
164191
}
165192
}
166193

194+
static String leftMostElement(String partitionSuffix) {
195+
try {
196+
List<Rdn> rdns = new LdapName(partitionSuffix).getRdns();
197+
return CollectionUtils.lastElement(rdns).getValue().toString();
198+
}
199+
catch (InvalidNameException ex) {
200+
throw new RuntimeException(ex);
201+
}
202+
}
203+
167204
private static InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig(String partitionSuffix, int port)
168205
throws LDAPException {
169206
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(partitionSuffix);

test-support/src/main/java/org/springframework/ldap/test/unboundid/EmbeddedLdapServerFactoryBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void setPort(int port) {
4747
}
4848

4949
@Override
50-
protected EmbeddedLdapServer createInstance() {
50+
protected EmbeddedLdapServer createInstance() throws Exception {
5151
EmbeddedLdapServer server = EmbeddedLdapServer.withPartitionSuffix(this.partitionSuffix)
5252
.partitionName(this.partitionName)
5353
.port(this.port)

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ public void setUp() {
5050

5151
@Test
5252
public void shouldBuildButNotStartTheServer() {
53-
EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").partitionName("jayway").port(this.port).build();
53+
EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(this.port).build();
5454

5555
assertPortIsFree(this.port);
5656
}
5757

5858
@Test
5959
public void shouldBuildTheServerWithCustomPort() {
6060
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
61-
.partitionName("jayway")
6261
.port(this.port);
6362

6463
try (EmbeddedLdapServer server = serverBuilder.build()) {
@@ -74,7 +73,6 @@ public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOExceptio
7473

7574
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
7675
.port(this.port)
77-
.partitionName("jayway")
7876
.configurationCustomizer((config) -> config.setCodeLogDetails(tempLogFile, true));
7977

8078
try (EmbeddedLdapServer server = serverBuilder.build()) {
@@ -155,6 +153,15 @@ public void startWhenAlreadyStartedThenFails() throws Exception {
155153
}
156154
}
157155

156+
@Test
157+
public void shouldParseLdapName() {
158+
String actual = EmbeddedLdapServer.Builder.leftMostElement("dc=jayway,dc=se");
159+
assertThat(actual).isEqualTo("jayway");
160+
161+
String actual2 = EmbeddedLdapServer.Builder.leftMostElement("dc=test,dc=jayway,dc=se");
162+
assertThat(actual2).isEqualTo("test");
163+
}
164+
158165
private static LdapTemplate ldapTemplate(String base, int port) {
159166
LdapContextSource ctx = new LdapContextSource();
160167
ctx.setBase(base);

0 commit comments

Comments
 (0)