Browse Source

Throw exception if specified ldif does not exist

Closes gh-7791

Co-Authored-By: Shay Dratler <dratler@users.noreply.github.com>
Eleftheria Stein-Kousathana 5 years ago
parent
commit
7c4a706865

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

@@ -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();
+		}
+	}
 }

+ 4 - 1
ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java

@@ -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));
 					}