Merge branch '2.4.x'

Closes gh-26672
pull/26677/head
Phillip Webb 4 years ago
commit 249c675fff

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @param cause the exception cause
*/ */
public ConfigDataLocationNotFoundException(ConfigDataLocation location, Throwable 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"); Assert.notNull(location, "Location must not be null");
this.location = location; this.location = location;
} }

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

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

Loading…
Cancel
Save