Skip to content

Commit 7c4a706

Browse files
eleftheriasdratler
andcommitted
Throw exception if specified ldif does not exist
Closes gh-7791 Co-Authored-By: Shay Dratler <[email protected]>
1 parent b22c50c commit 7c4a706

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerLdifTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,54 @@ void shutdown() {
141141
this.container.stop();
142142
}
143143
}
144+
145+
@Test
146+
public void unboundIdContainerWhenMissingLdifThenException() {
147+
try {
148+
appCtx = new AnnotationConfigApplicationContext(MissingLdifConfig.class);
149+
failBecauseExceptionWasNotThrown(IllegalStateException.class);
150+
} catch (Exception e) {
151+
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
152+
assertThat(e.getMessage()).contains("Unable to load LDIF classpath:does-not-exist.ldif");
153+
}
154+
}
155+
156+
@Configuration
157+
static class MissingLdifConfig {
158+
private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
159+
"classpath:does-not-exist.ldif");
160+
161+
@Bean
162+
UnboundIdContainer ldapContainer() {
163+
this.container.setPort(0);
164+
return this.container;
165+
}
166+
167+
@PreDestroy
168+
void shutdown() {
169+
this.container.stop();
170+
}
171+
}
172+
173+
@Test
174+
public void unboundIdContainerWhenWildcardLdifNotFoundThenProceeds() {
175+
new AnnotationConfigApplicationContext(WildcardNoLdifConfig.class);
176+
}
177+
178+
@Configuration
179+
static class WildcardNoLdifConfig {
180+
private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
181+
"classpath*:*.test.ldif");
182+
183+
@Bean
184+
UnboundIdContainer ldapContainer() {
185+
this.container.setPort(0);
186+
return this.container;
187+
}
188+
189+
@PreDestroy
190+
void shutdown() {
191+
this.container.stop();
192+
}
193+
}
144194
}

ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ private void importLdif(InMemoryDirectoryServer directoryServer) {
116116
if (StringUtils.hasText(this.ldif)) {
117117
try {
118118
Resource[] resources = this.context.getResources(this.ldif);
119-
if (resources.length > 0 && resources[0].exists()) {
119+
if (resources.length > 0) {
120+
if (!resources[0].exists()) {
121+
throw new IllegalArgumentException("Unable to find LDIF resource " + this.ldif);
122+
}
120123
try (InputStream inputStream = resources[0].getInputStream()) {
121124
directoryServer.importFromLDIF(false, new LDIFReader(inputStream));
122125
}

0 commit comments

Comments
 (0)