From 2d280bb9674e15aba78987ed0f616fd161785889 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 28 Mar 2023 09:57:22 +0100 Subject: [PATCH] Polish "Fix support for default values in banner placeholders" See gh-34764 --- .../springframework/boot/ResourceBanner.java | 15 ++++-------- .../boot/ResourceBannerTests.java | 24 +++++++------------ 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java index ec8f6c591129..46108497dfaf 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java @@ -28,11 +28,11 @@ import org.apache.commons.logging.LogFactory; import org.springframework.boot.ansi.AnsiPropertySource; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertyResolver; -import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySourcesPropertyResolver; import org.springframework.core.io.Resource; import org.springframework.core.log.LogMessage; @@ -79,22 +79,15 @@ public void printBanner(Environment environment, Class sourceClass, PrintStre protected List getPropertyResolvers(Environment environment, Class sourceClass) { MutablePropertySources propertySources = new MutablePropertySources(); - propertySources.addLast(getEnvironmentSource(environment)); + if (environment instanceof ConfigurableEnvironment) { + ((ConfigurableEnvironment) environment).getPropertySources().forEach(propertySources::addLast); + } propertySources.addLast(getTitleSource(sourceClass)); propertySources.addLast(getAnsiSource()); propertySources.addLast(getVersionSource(sourceClass)); return Collections.singletonList(new PropertySourcesPropertyResolver(propertySources)); } - private PropertySource getEnvironmentSource(Environment environment) { - return new PropertySource("environment", environment) { - @Override - public Object getProperty(String name) { - return environment.getProperty(name); - } - }; - } - private MapPropertySource getVersionSource(Class sourceClass) { return new MapPropertySource("version", getVersionsMap(sourceClass)); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java index dcd95a13eaf6..9960e78c9b88 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java @@ -49,21 +49,6 @@ void reset() { AnsiOutput.setEnabled(Enabled.DETECT); } - @Test - void doNotUseDefaultsIfValueExists() { - Resource resource = new ByteArrayResource( - "banner ${a:def} ${spring-boot.version:def} ${application.version:def}".getBytes()); - String banner = printBanner(resource, "10.2", "1.0", null); - assertThat(banner).startsWith("banner 1 10.2 1.0"); - } - - @Test - void useDefaults() { - Resource resource = new ByteArrayResource("banner ${b:def1} ${c:def2} ${d:def3}".getBytes()); - String banner = printBanner(resource, null, null, null); - assertThat(banner).startsWith("banner def1 def2 def3"); - } - @Test void renderVersions() { Resource resource = new ByteArrayResource( @@ -142,6 +127,15 @@ void renderWithoutTitle() { assertThat(banner).startsWith("banner 1"); } + @Test + void renderWithDefaultValues() { + Resource resource = new ByteArrayResource( + "banner ${a:default-a} ${b:default-b} ${spring-boot.version:default-boot-version} ${application.version:default-application-version}" + .getBytes()); + String banner = printBanner(resource, "10.2", "1.0", null); + assertThat(banner).startsWith("banner 1 default-b 10.2 1.0"); + } + private String printBanner(Resource resource, String bootVersion, String applicationVersion, String applicationTitle) { ResourceBanner banner = new MockResourceBanner(resource, bootVersion, applicationVersion, applicationTitle);