99import com .unboundid .ldap .sdk .schema .Schema ;
1010import com .unboundid .ldif .LDIFException ;
1111import org .zapodot .junit .ldap .internal .AuthenticationConfiguration ;
12- import org .zapodot .junit .ldap .internal .EmbeddedLdapRuleImpl ;
1312
1413import javax .net .ssl .SSLSocketFactory ;
1514import java .io .File ;
2221import java .util .List ;
2322import java .util .Objects ;
2423
25- /**
26- * A builder providing a fluent way of defining EmbeddedLdapRule instances
27- */
28- public class EmbeddedLdapRuleBuilder {
29-
24+ public abstract class AbstractEmbeddedLdapSupportBuilder <T > {
3025 public static final String DEFAULT_DOMAIN = "dc=example,dc=com" ;
3126 public static final String DEFAULT_BIND_DSN = "cn=Directory manager" ;
3227 public static final String DEFAULT_BIND_CREDENTIALS = "password" ;
3328 public static final String LDAP_SERVER_LISTENER_NAME = "test-listener" ;
3429 public static final int MIN_PORT_EXCLUSIVE = 0 ;
3530 public static final int MAX_PORT_EXCLUSIVE = 65535 ;
36- private List <String > domainDsn = new LinkedList <>();
37-
38- private String bindDSN = DEFAULT_BIND_DSN ;
31+ protected List <String > domainDsn = new LinkedList <>();
3932
40- private String bindCredentials = DEFAULT_BIND_CREDENTIALS ;
33+ protected String bindDSN = DEFAULT_BIND_DSN ;
4134
42- private List < String > ldifsToImport = new LinkedList <>() ;
35+ protected String bindCredentials = DEFAULT_BIND_CREDENTIALS ;
4336
44- private List <String > schemaLdifs = new LinkedList <>();
37+ protected List <String > ldifsToImport = new LinkedList <>();
4538
46- private boolean addDefaultSchema = true ;
39+ protected List < String > schemaLdifs = new LinkedList <>() ;
4740
48- private Integer bindPort = 0 ;
41+ protected boolean addDefaultSchema = true ;
4942
50- private InetAddress bindAddress = InetAddress . getLoopbackAddress () ;
43+ protected Integer bindPort = 0 ;
5144
52- private AuthenticationConfiguration authenticationConfiguration ;
45+ protected InetAddress bindAddress = InetAddress . getLoopbackAddress () ;
5346
54- private InMemoryListenerConfig listenerConfig = null ;
47+ protected AuthenticationConfiguration authenticationConfiguration ;
5548
56- private boolean useTls = false ;
57- private SSLSocketFactory socketFactory = null ;
49+ protected InMemoryListenerConfig listenerConfig = null ;
5850
59- private Integer maxSizeLimit = null ;
51+ protected boolean useTls = false ;
52+ protected SSLSocketFactory socketFactory = null ;
6053
61- public EmbeddedLdapRuleBuilder () {
62- }
54+ protected Integer maxSizeLimit = null ;
6355
64- /**
65- * Creates a new builder
66- *
67- * @return a new EmbeddedLdapRuleBuilder instance
68- */
69- public static EmbeddedLdapRuleBuilder newInstance () {
70- return new EmbeddedLdapRuleBuilder ();
71- }
7256
7357 /**
7458 * Sets a domainDsn to be used. May be multiple values. If not set, it will default to the value of the {@link #DEFAULT_DOMAIN DEFAULT_DOMAIN} field
7559 *
7660 * @param domainDsn a valid DSN string
7761 * @return same EmbeddedLdapRuleBuilder instance with the domainDsn field set
7862 */
79- public EmbeddedLdapRuleBuilder usingDomainDsn (final String domainDsn ) {
63+ public AbstractEmbeddedLdapSupportBuilder < T > usingDomainDsn (final String domainDsn ) {
8064 this .domainDsn .add (domainDsn );
8165 return this ;
8266 }
@@ -87,7 +71,7 @@ public EmbeddedLdapRuleBuilder usingDomainDsn(final String domainDsn) {
8771 * @param bindDSN a valid DSN string
8872 * @return same EmbeddedLdapRuleBuilder instance with the bindDSN field set
8973 */
90- public EmbeddedLdapRuleBuilder usingBindDSN (final String bindDSN ) {
74+ public AbstractEmbeddedLdapSupportBuilder < T > usingBindDSN (final String bindDSN ) {
9175 this .bindDSN = bindDSN ;
9276 return this ;
9377 }
@@ -98,7 +82,7 @@ public EmbeddedLdapRuleBuilder usingBindDSN(final String bindDSN) {
9882 * @param bindCredentials a password string
9983 * @return same EmbeddedLdapRuleBuilder instance with the bindCredentials field set
10084 */
101- public EmbeddedLdapRuleBuilder usingBindCredentials (final String bindCredentials ) {
85+ public AbstractEmbeddedLdapSupportBuilder < T > usingBindCredentials (final String bindCredentials ) {
10286 this .bindCredentials = bindCredentials ;
10387 return this ;
10488 }
@@ -111,7 +95,7 @@ public EmbeddedLdapRuleBuilder usingBindCredentials(final String bindCredentials
11195 * @throws IllegalArgumentException if the provided value for port is not between @{link MIN_PORT_EXCLUSIVE}
11296 * and @{MAX_PORT_EXCLUSIVE} (exclusive)
11397 */
114- public EmbeddedLdapRuleBuilder bindingToPort (final int port ) {
98+ public AbstractEmbeddedLdapSupportBuilder < T > bindingToPort (final int port ) {
11599 if ((port < MIN_PORT_EXCLUSIVE ) || (port > MAX_PORT_EXCLUSIVE )) {
116100 throw new IllegalArgumentException (String .format ("Value \" %s\" is not a valid port number" , port ));
117101 }
@@ -126,7 +110,7 @@ public EmbeddedLdapRuleBuilder bindingToPort(final int port) {
126110 * @return same EmbeddedLdapRuleBuilder instance with the bindAddress field set
127111 * @throws IllegalArgumentException if the value provided for \"address\" is invalid
128112 */
129- public EmbeddedLdapRuleBuilder bindingToAddress (final String address ) {
113+ public AbstractEmbeddedLdapSupportBuilder < T > bindingToAddress (final String address ) {
130114 Objects .requireNonNull (address );
131115 try {
132116 final InetAddress addressByName = InetAddress .getByName (address );
@@ -137,7 +121,7 @@ public EmbeddedLdapRuleBuilder bindingToAddress(final String address) {
137121 return this ;
138122 }
139123
140- public EmbeddedLdapRuleBuilder withMaxSizeLimit (final int maxSizeLimit ) {
124+ public AbstractEmbeddedLdapSupportBuilder < T > withMaxSizeLimit (final int maxSizeLimit ) {
141125 this .maxSizeLimit = Integer .valueOf (maxSizeLimit );
142126 return this ;
143127 }
@@ -147,7 +131,7 @@ public EmbeddedLdapRuleBuilder withMaxSizeLimit(final int maxSizeLimit) {
147131 *
148132 * @return same EmbeddedLdapRuleBuilder instance with the withoutDefaultSchema field set to FALSE
149133 */
150- public EmbeddedLdapRuleBuilder withoutDefaultSchema () {
134+ public AbstractEmbeddedLdapSupportBuilder < T > withoutDefaultSchema () {
151135 this .addDefaultSchema = false ;
152136 return this ;
153137 }
@@ -158,7 +142,7 @@ public EmbeddedLdapRuleBuilder withoutDefaultSchema() {
158142 * @param ldifSchemaFiles LDIF-files containing schema element definitions
159143 * @return same EmbeddedLdapRuleBuilder with the given LDIF-files added to the internal schema file collection.
160144 */
161- public EmbeddedLdapRuleBuilder withSchema (final String ... ldifSchemaFiles ) {
145+ public AbstractEmbeddedLdapSupportBuilder < T > withSchema (final String ... ldifSchemaFiles ) {
162146 this .schemaLdifs .addAll (Arrays .asList (ldifSchemaFiles ));
163147 return this ;
164148 }
@@ -169,19 +153,19 @@ public EmbeddedLdapRuleBuilder withSchema(final String... ldifSchemaFiles) {
169153 * @param ldifFiles LDIF-files to import
170154 * @return same EmbeddedLdapRuleBuilder instance with the provided ldifFiles added to the list of LDIF files to import
171155 */
172- public EmbeddedLdapRuleBuilder importingLdifs (final String ... ldifFiles ) {
156+ public AbstractEmbeddedLdapSupportBuilder < T > importingLdifs (final String ... ldifFiles ) {
173157 if (ldifFiles != null ) {
174158 ldifsToImport .addAll (Arrays .asList (ldifFiles ));
175159 }
176160 return this ;
177161 }
178162
179- public EmbeddedLdapRuleBuilder withListener (InMemoryListenerConfig listenerConfig ) {
163+ public AbstractEmbeddedLdapSupportBuilder < T > withListener (InMemoryListenerConfig listenerConfig ) {
180164 this .listenerConfig = listenerConfig ;
181165 return this ;
182166 }
183167
184- public EmbeddedLdapRuleBuilder useTls (boolean useTls ) {
168+ public AbstractEmbeddedLdapSupportBuilder < T > useTls (boolean useTls ) {
185169 this .useTls = useTls ;
186170 return this ;
187171 }
@@ -191,14 +175,10 @@ public EmbeddedLdapRuleBuilder useTls(boolean useTls) {
191175 *
192176 * @return a new EmbeddedLdapRule instance
193177 */
194- public EmbeddedLdapRule build () {
195- Objects .requireNonNull (bindDSN , "\" bindDSN\" can not be null" );
196- return EmbeddedLdapRuleImpl .createForConfiguration (createInMemoryServerConfiguration (),
197- authenticationConfiguration ,
198- ldifsToImport , useTls , socketFactory );
199- }
178+ abstract public T build ();
200179
201- private InMemoryDirectoryServerConfig createInMemoryServerConfiguration () {
180+
181+ protected InMemoryDirectoryServerConfig createInMemoryServerConfiguration () {
202182 try {
203183 final InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig =
204184 new InMemoryDirectoryServerConfig (domainDsnArray ());
@@ -210,10 +190,10 @@ private InMemoryDirectoryServerConfig createInMemoryServerConfiguration() {
210190
211191 if (listenerConfig == null ) {
212192 listenerConfig = InMemoryListenerConfig .createLDAPConfig (
213- LDAP_SERVER_LISTENER_NAME ,
214- bindAddress ,
215- bindPort ,
216- null );
193+ LDAP_SERVER_LISTENER_NAME ,
194+ bindAddress ,
195+ bindPort ,
196+ null );
217197 }
218198 inMemoryDirectoryServerConfig .setListenerConfigs (listenerConfig );
219199 inMemoryDirectoryServerConfig .setSchema (customSchema ());
@@ -243,8 +223,8 @@ private Schema customSchema() {
243223 final Schema initialSchema = (addDefaultSchema ? Schema .getDefaultStandardSchema () : null );
244224 if (!schemaFiles .isEmpty ()) {
245225 final Schema customSchema = initialSchema == null
246- ? Schema .getSchema (schemaFiles )
247- : Schema .mergeSchemas (initialSchema , Schema .getSchema (schemaFiles ));
226+ ? Schema .getSchema (schemaFiles )
227+ : Schema .mergeSchemas (initialSchema , Schema .getSchema (schemaFiles ));
248228 return customSchema ;
249229 } else {
250230 return null ;
@@ -278,8 +258,9 @@ public File apply(final String input) {
278258 }));
279259 }
280260
281- public EmbeddedLdapRuleBuilder withSocketFactory (SSLSocketFactory socketFactory ) {
261+ public AbstractEmbeddedLdapSupportBuilder < T > withSocketFactory (SSLSocketFactory socketFactory ) {
282262 this .socketFactory = socketFactory ;
283263 return this ;
284264 }
265+
285266}
0 commit comments