Merge branch '2.5.x' into main
Closes gh-27212 Closes gh-27213
This commit is contained in:
commit
d82b46b718
@ -95,12 +95,15 @@ class LocationResourceLoader {
|
|||||||
validatePattern(location, type);
|
validatePattern(location, type);
|
||||||
String directoryPath = location.substring(0, location.indexOf("*/"));
|
String directoryPath = location.substring(0, location.indexOf("*/"));
|
||||||
String fileName = location.substring(location.lastIndexOf("/") + 1);
|
String fileName = location.substring(location.lastIndexOf("/") + 1);
|
||||||
Resource directoryResource = getResource(directoryPath);
|
Resource resource = getResource(directoryPath);
|
||||||
if (!directoryResource.exists()) {
|
if (!resource.exists()) {
|
||||||
return new Resource[] { directoryResource };
|
return EMPTY_RESOURCES;
|
||||||
}
|
}
|
||||||
File directory = getDirectory(location, directoryResource);
|
File file = getFile(location, resource);
|
||||||
File[] subDirectories = directory.listFiles(this::isVisibleDirectory);
|
if (!file.isDirectory()) {
|
||||||
|
return EMPTY_RESOURCES;
|
||||||
|
}
|
||||||
|
File[] subDirectories = file.listFiles(this::isVisibleDirectory);
|
||||||
if (subDirectories == null) {
|
if (subDirectories == null) {
|
||||||
return EMPTY_RESOURCES;
|
return EMPTY_RESOURCES;
|
||||||
}
|
}
|
||||||
@ -131,11 +134,9 @@ class LocationResourceLoader {
|
|||||||
Assert.state(directoryPath.endsWith("*/"), () -> String.format("Location '%s' must end with '*/'", location));
|
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 {
|
try {
|
||||||
File directory = resource.getFile();
|
return resource.getFile();
|
||||||
Assert.state(directory.isDirectory(), () -> "'" + directory + "' is not a directory");
|
|
||||||
return directory;
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
|
@ -258,7 +258,9 @@ public class StandardConfigDataLocationResolver
|
|||||||
Set<StandardConfigDataReference> references) {
|
Set<StandardConfigDataReference> references) {
|
||||||
Set<StandardConfigDataResource> empty = new LinkedHashSet<>();
|
Set<StandardConfigDataResource> empty = new LinkedHashSet<>();
|
||||||
for (StandardConfigDataReference reference : references) {
|
for (StandardConfigDataReference reference : references) {
|
||||||
empty.addAll(resolveEmptyDirectories(reference));
|
if (reference.getDirectory() != null) {
|
||||||
|
empty.addAll(resolveEmptyDirectories(reference));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return empty;
|
return empty;
|
||||||
}
|
}
|
||||||
|
@ -709,6 +709,18 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
|
|||||||
assertThat(environment.getProperty("second.property")).isEqualTo("ball");
|
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
|
@Test
|
||||||
void runWhenMandatoryWildcardLocationHasEmptyFileDirectory() {
|
void runWhenMandatoryWildcardLocationHasEmptyFileDirectory() {
|
||||||
assertThatNoException()
|
assertThatNoException()
|
||||||
|
@ -34,6 +34,7 @@ import org.springframework.mock.env.MockEnvironment;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
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.BDDMockito.given;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
@ -151,6 +152,12 @@ public class StandardConfigDataLocationResolverTests {
|
|||||||
filePath("src", "test", "resources", "config", "2-second", "testproperties.properties"));
|
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
|
@Test
|
||||||
void resolveWhenLocationIsWildcardFilesLoadsAllFilesThatMatch() {
|
void resolveWhenLocationIsWildcardFilesLoadsAllFilesThatMatch() {
|
||||||
ConfigDataLocation location = ConfigDataLocation
|
ConfigDataLocation location = ConfigDataLocation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user