Allow optional directories without sub-directories

Update `StandardConfigDataLocationResolver` so that directory resources
are only required when the location is not optional.

Closes gh-26627

Co-authored-by: Phillip Webb <pwebb@vmware.com>
pull/26691/head
Andy Wilkinson 4 years ago committed by Phillip Webb
parent 14f578ed80
commit 01dd68e29e

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,7 +44,18 @@ public class ConfigDataLocationNotFoundException extends ConfigDataNotFoundExcep
* @param cause the exception cause
*/
public ConfigDataLocationNotFoundException(ConfigDataLocation location, Throwable cause) {
super(getMessage(location), cause);
this(location, getMessage(location), cause);
}
/**
* Create a new {@link ConfigDataLocationNotFoundException} instance.
* @param location the location that could not be found
* @param message the exception message
* @param cause the exception cause
* @since 2.4.7
*/
public ConfigDataLocationNotFoundException(ConfigDataLocation location, String message, Throwable cause) {
super(message, cause);
Assert.notNull(location, "Location must not be null");
this.location = location;
}

@ -42,6 +42,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
@ -264,10 +265,13 @@ public class StandardConfigDataLocationResolver
}
private Set<StandardConfigDataResource> resolvePatternEmptyDirectories(StandardConfigDataReference reference) {
Resource[] resources = this.resourceLoader.getResources(reference.getDirectory(), ResourceType.DIRECTORY);
Assert.state(resources.length > 0,
"No subdirectories found for mandatory directory location '" + reference.getDirectory() + "'.");
return Arrays.stream(resources).filter(Resource::exists)
Resource[] subdirectories = this.resourceLoader.getResources(reference.getDirectory(), ResourceType.DIRECTORY);
ConfigDataLocation location = reference.getConfigDataLocation();
if (location.isOptional() && ObjectUtils.isEmpty(subdirectories)) {
String message = String.format("Config data location '%s' contains no subdirectories", location);
throw new ConfigDataLocationNotFoundException(location, message, null);
}
return Arrays.stream(subdirectories).filter(Resource::exists)
.map((resource) -> new StandardConfigDataResource(reference, resource, true))
.collect(Collectors.toCollection(LinkedHashSet::new));
}

@ -713,10 +713,9 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
@Test
void runWhenMandatoryWildcardLocationHasNoSubdirectories() {
assertThatIllegalStateException().isThrownBy(
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy(
() -> this.application.run("--spring.config.location=file:src/test/resources/config/0-empty/*/"))
.withMessage(
"No subdirectories found for mandatory directory location 'file:src/test/resources/config/0-empty/*/'.");
.withMessage("Config data location 'file:src/test/resources/config/0-empty/*/' cannot be found");
}
@Test
@ -725,6 +724,18 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
.isThrownBy(() -> this.application.run("--spring.config.location=file:invalid/*/"));
}
@Test
void runWhenHasOptionalWildcardLocationThatDoesNotExistDoesNotThrow() {
assertThatNoException()
.isThrownBy(() -> this.application.run("--spring.config.location=optional:file:invalid/*/"));
}
@Test
void runWhenOptionalWildcardLocationHasNoSubdirectoriesDoesNotThrow() {
assertThatNoException().isThrownBy(() -> this.application
.run("--spring.config.location=optional:file:src/test/resources/config/0-empty/*/"));
}
@Test // gh-24990
void runWhenHasProfileSpecificFileWithActiveOnProfileProperty() {
ConfigurableApplicationContext context = this.application

Loading…
Cancel
Save