From 07415e160372b47ecd80c6b65f8c7c1f89ef8938 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 30 Apr 2020 18:17:29 -0700 Subject: [PATCH] Attempt to fix Windows CI test failure --- .../config/ConfigFileApplicationListener.java | 39 ++++++++++++------- .../ConfigFileApplicationListenerTests.java | 8 ++-- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index cdea50f4079..1b85a6622b8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -520,8 +520,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, } continue; } - String name = (location.contains("*")) ? "applicationConfig: [" + resource.toString() + "]" - : "applicationConfig: [" + location + "]"; + String name = "applicationConfig: [" + getLocationName(location, resource) + "]"; List documents = loadDocuments(loader, name, resource); if (CollectionUtils.isEmpty(documents)) { if (this.logger.isTraceEnabled()) { @@ -557,20 +556,20 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, } } + private String getLocationName(String location, Resource resource) { + if (!location.contains("*")) { + return location; + } + if (resource instanceof FileSystemResource) { + return ((FileSystemResource) resource).getPath(); + } + return resource.getDescription(); + } + private Resource[] getResources(String location) { try { if (location.contains("*")) { - String directoryPath = location.substring(0, location.indexOf("*/")); - String fileName = location.substring(location.lastIndexOf("/") + 1); - Resource resource = this.resourceLoader.getResource(directoryPath); - File[] files = resource.getFile().listFiles(File::isDirectory); - if (files != null) { - Arrays.sort(files, FILE_COMPARATOR); - return Arrays.stream(files).map((file) -> file.listFiles((dir, name) -> name.equals(fileName))) - .filter(Objects::nonNull).flatMap((Function>) Arrays::stream) - .map(FileSystemResource::new).toArray(Resource[]::new); - } - return EMPTY_RESOURCES; + return getResourcesFromPatternLocation(location); } return new Resource[] { this.resourceLoader.getResource(location) }; } @@ -579,6 +578,20 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, } } + private Resource[] getResourcesFromPatternLocation(String location) throws IOException { + String directoryPath = location.substring(0, location.indexOf("*/")); + String fileName = location.substring(location.lastIndexOf("/") + 1); + Resource resource = this.resourceLoader.getResource(directoryPath); + File[] files = resource.getFile().listFiles(File::isDirectory); + if (files != null) { + Arrays.sort(files, FILE_COMPARATOR); + return Arrays.stream(files).map((file) -> file.listFiles((dir, name) -> name.equals(fileName))) + .filter(Objects::nonNull).flatMap((Function>) Arrays::stream) + .map(FileSystemResource::new).toArray(Resource[]::new); + } + return EMPTY_RESOURCES; + } + private void addIncludedProfiles(Set includeProfiles) { LinkedList existingProfiles = new LinkedList<>(this.profiles); this.profiles.clear(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 65931c9ca73..911c3b77e78 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -1085,10 +1085,12 @@ class ConfigFileApplicationListenerTests { List sources = this.environment.getPropertySources().stream() .filter((source) -> source.getName().contains("applicationConfig")).map((source) -> { String name = source.getName(); - return name.substring(name.indexOf("src/test/resources")); + name = name.substring(name.indexOf("src/test/resources")); + name = name.substring(0, name.length() - 1); + return name; }).collect(Collectors.toList()); - assertThat(sources).containsExactly("src/test/resources/config/1-first/testproperties.properties]]", - "src/test/resources/config/2-second/testproperties.properties]]"); + assertThat(sources).containsExactly("src/test/resources/config/1-first/testproperties.properties", + "src/test/resources/config/2-second/testproperties.properties"); } @Test