1616
1717package org .springframework .ldap .test .unboundid ;
1818
19+ import java .util .List ;
1920import java .util .function .Consumer ;
2021
22+ import javax .naming .InvalidNameException ;
23+ import javax .naming .ldap .LdapName ;
24+ import javax .naming .ldap .Rdn ;
25+
2126import com .unboundid .ldap .listener .InMemoryDirectoryServer ;
2227import com .unboundid .ldap .listener .InMemoryDirectoryServerConfig ;
2328import com .unboundid .ldap .listener .InMemoryListenerConfig ;
2631import com .unboundid .ldap .sdk .LDAPException ;
2732
2833import 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 );
0 commit comments