Skip to content

Commit c18de67

Browse files
committed
Add default standard schema and add the option to avoid it from happening which resloves #4
1 parent 9e7c65d commit c18de67

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/main/java/org/zapodot/junit/ldap/EmbeddedLdapRuleBuilder.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class EmbeddedLdapRuleBuilder {
4343

4444
private List<String> schemaLdifs = new LinkedList<>();
4545

46+
private boolean addDefaultSchema = true;
47+
4648
private Integer bindPort = 0;
4749

4850
private InetAddress bindAddress = InetAddress.getLoopbackAddress();
@@ -128,6 +130,16 @@ public EmbeddedLdapRuleBuilder bindingToAddress(final String address) {
128130
return this;
129131
}
130132

133+
/**
134+
* Avoid adding UnboundID's default schema that contains the most common LDAP elements defined through various RFC's.
135+
*
136+
* @return same EmbeddedLdapRuleBuilder instance with the withoutDefaultSchema field set to FALSE
137+
*/
138+
public EmbeddedLdapRuleBuilder withoutDefaultSchema() {
139+
this.addDefaultSchema = true;
140+
return this;
141+
}
142+
131143
/**
132144
* Define schemas to be used for the server. If not defined, UnboundID will set up a default schema.
133145
*
@@ -201,16 +213,22 @@ private String[] domainDsnArray() {
201213

202214
private Optional<Schema> customSchema() {
203215
final List<File> schemaFiles = schemaFiles();
204-
if (!schemaFiles.isEmpty()) {
205-
try {
206-
return Optional.fromNullable(Schema.getSchema(schemaFiles));
207-
} catch (IOException | LDIFException e) {
208-
throw new IllegalArgumentException(
209-
"Could not create custom LDAP schema due, probably caused by an incorrectly formatted schema",
210-
e);
216+
217+
try {
218+
final Schema initialSchema = (addDefaultSchema ? Schema.getDefaultStandardSchema() : null);
219+
if (!schemaFiles.isEmpty()) {
220+
final Schema customSchema = initialSchema == null
221+
? Schema.getSchema(schemaFiles)
222+
: initialSchema.mergeSchemas(Schema.getSchema(schemaFiles));
223+
return Optional.fromNullable(customSchema);
224+
} else {
225+
return Optional.absent();
211226
}
212-
} else {
213-
return Optional.absent();
227+
228+
} catch (IOException | LDIFException | LDAPException e) {
229+
throw new IllegalArgumentException(
230+
"Could not create custom LDAP schema due, probably caused by an incorrectly formatted schema",
231+
e);
214232
}
215233
}
216234

src/test/java/org/zapodot/junit/ldap/EmbeddedLdapRuleCustomSchemaTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
public class EmbeddedLdapRuleCustomSchemaTest {
1010

1111
@Rule
12-
public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance().withSchema("standard-schema.ldif").build();
12+
public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance()
13+
.withoutDefaultSchema()
14+
.withSchema("standard-schema.ldif")
15+
.build();
1316

1417
@Test
1518
public void testFindCustomAttribute() throws Exception {

0 commit comments

Comments
 (0)