Skip to content

Commit

Permalink
Merge branch '2.4.x'
Browse files Browse the repository at this point in the history
Closes gh-26672
  • Loading branch information
philwebb committed May 26, 2021
2 parents 6424e3d + 01dd68e commit 249c675
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
@@ -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.
Expand Down Expand Up @@ -44,7 +44,18 @@ public ConfigDataLocationNotFoundException(ConfigDataLocation location) {
* @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;
}
Expand Down
Expand Up @@ -43,6 +43,7 @@
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;

/**
Expand Down Expand Up @@ -265,10 +266,13 @@ private Set<StandardConfigDataResource> resolveNonPatternEmptyDirectories(Standa
}

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));
}
Expand Down
Expand Up @@ -713,10 +713,9 @@ void runWhenMandatoryWildcardLocationHasEmptyFileDirectory() {

@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
Expand All @@ -725,6 +724,18 @@ void runWhenHasMandatoryWildcardLocationThatDoesNotExist() {
.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
Expand Down

0 comments on commit 249c675

Please sign in to comment.