Skip to content

Commit

Permalink
Merge branch '2.5.x' into main
Browse files Browse the repository at this point in the history
Closes gh-27212
Closes gh-27213
  • Loading branch information
mbhave committed Jul 8, 2021
2 parents cf5fc04 + 0f70c46 commit d82b46b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ Resource[] getResources(String location, ResourceType type) {
validatePattern(location, type);
String directoryPath = location.substring(0, location.indexOf("*/"));
String fileName = location.substring(location.lastIndexOf("/") + 1);
Resource directoryResource = getResource(directoryPath);
if (!directoryResource.exists()) {
return new Resource[] { directoryResource };
Resource resource = getResource(directoryPath);
if (!resource.exists()) {
return EMPTY_RESOURCES;
}
File file = getFile(location, resource);
if (!file.isDirectory()) {
return EMPTY_RESOURCES;
}
File directory = getDirectory(location, directoryResource);
File[] subDirectories = directory.listFiles(this::isVisibleDirectory);
File[] subDirectories = file.listFiles(this::isVisibleDirectory);
if (subDirectories == null) {
return EMPTY_RESOURCES;
}
Expand Down Expand Up @@ -131,11 +134,9 @@ private void validatePattern(String location, ResourceType type) {
Assert.state(directoryPath.endsWith("*/"), () -> String.format("Location '%s' must end with '*/'", location));
}

private File getDirectory(String patternLocation, Resource resource) {
private File getFile(String patternLocation, Resource resource) {
try {
File directory = resource.getFile();
Assert.state(directory.isDirectory(), () -> "'" + directory + "' is not a directory");
return directory;
return resource.getFile();
}
catch (Exception ex) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ private Collection<StandardConfigDataResource> resolveEmptyDirectories(
Set<StandardConfigDataReference> references) {
Set<StandardConfigDataResource> empty = new LinkedHashSet<>();
for (StandardConfigDataReference reference : references) {
empty.addAll(resolveEmptyDirectories(reference));
if (reference.getDirectory() != null) {
empty.addAll(resolveEmptyDirectories(reference));
}
}
return empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,18 @@ void runWhenHasWildcardLocationLoadsFromAllMatchingLocations() {
assertThat(environment.getProperty("second.property")).isEqualTo("ball");
}

@Test
void runWhenOptionalWildcardLocationDoesNotExistDoesNotThrowException() {
assertThatNoException().isThrownBy(() -> this.application.run(
"--spring.config.location=optional:file:src/test/resources/nonexistent/*/testproperties.properties"));
}

@Test
void runWhenMandatoryWildcardLocationDoesNotExistThrowsException() {
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy(() -> this.application
.run("--spring.config.location=file:src/test/resources/nonexistent/*/testproperties.properties"));
}

@Test
void runWhenMandatoryWildcardLocationHasEmptyFileDirectory() {
assertThatNoException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -151,6 +152,12 @@ void resolveWhenLocationIsWildcardDirectoriesSortsAlphabeticallyBasedOnAbsoluteP
filePath("src", "test", "resources", "config", "2-second", "testproperties.properties"));
}

@Test
void resolveWhenLocationIsWildcardAndMatchingFilePresentShouldNotFail() {
ConfigDataLocation location = ConfigDataLocation.of("optional:file:src/test/resources/a-file/*/");
assertThatNoException().isThrownBy(() -> this.resolver.resolve(this.context, location));
}

@Test
void resolveWhenLocationIsWildcardFilesLoadsAllFilesThatMatch() {
ConfigDataLocation location = ConfigDataLocation
Expand Down
Empty file.

0 comments on commit d82b46b

Please sign in to comment.