Throw exception if specified ldif does not exist

Closes gh-7791

Co-Authored-By: Shay Dratler <dratler@users.noreply.github.com>
This commit is contained in:
Eleftheria Stein-Kousathana 2020-07-22 18:43:49 +02:00
parent b22c50c4a8
commit 7c4a706865
2 changed files with 54 additions and 1 deletions

View File

@ -141,4 +141,54 @@ public class UnboundIdContainerLdifTests {
this.container.stop();
}
}
@Test
public void unboundIdContainerWhenMissingLdifThenException() {
try {
appCtx = new AnnotationConfigApplicationContext(MissingLdifConfig.class);
failBecauseExceptionWasNotThrown(IllegalStateException.class);
} catch (Exception e) {
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
assertThat(e.getMessage()).contains("Unable to load LDIF classpath:does-not-exist.ldif");
}
}
@Configuration
static class MissingLdifConfig {
private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
"classpath:does-not-exist.ldif");
@Bean
UnboundIdContainer ldapContainer() {
this.container.setPort(0);
return this.container;
}
@PreDestroy
void shutdown() {
this.container.stop();
}
}
@Test
public void unboundIdContainerWhenWildcardLdifNotFoundThenProceeds() {
new AnnotationConfigApplicationContext(WildcardNoLdifConfig.class);
}
@Configuration
static class WildcardNoLdifConfig {
private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
"classpath*:*.test.ldif");
@Bean
UnboundIdContainer ldapContainer() {
this.container.setPort(0);
return this.container;
}
@PreDestroy
void shutdown() {
this.container.stop();
}
}
}

View File

@ -116,7 +116,10 @@ public class UnboundIdContainer implements InitializingBean, DisposableBean, Lif
if (StringUtils.hasText(this.ldif)) {
try {
Resource[] resources = this.context.getResources(this.ldif);
if (resources.length > 0 && resources[0].exists()) {
if (resources.length > 0) {
if (!resources[0].exists()) {
throw new IllegalArgumentException("Unable to find LDIF resource " + this.ldif);
}
try (InputStream inputStream = resources[0].getInputStream()) {
directoryServer.importFromLDIF(false, new LDIFReader(inputStream));
}