Polish "Fix support for default values in banner placeholders"

See gh-34764
pull/34975/head
Andy Wilkinson 2 years ago
parent 5c01aa719c
commit 2d280bb967

@ -28,11 +28,11 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.ansi.AnsiPropertySource; import org.springframework.boot.ansi.AnsiPropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySourcesPropertyResolver; import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.log.LogMessage; import org.springframework.core.log.LogMessage;
@ -79,22 +79,15 @@ public class ResourceBanner implements Banner {
protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) { protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) {
MutablePropertySources propertySources = new MutablePropertySources(); MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(getEnvironmentSource(environment)); if (environment instanceof ConfigurableEnvironment) {
((ConfigurableEnvironment) environment).getPropertySources().forEach(propertySources::addLast);
}
propertySources.addLast(getTitleSource(sourceClass)); propertySources.addLast(getTitleSource(sourceClass));
propertySources.addLast(getAnsiSource()); propertySources.addLast(getAnsiSource());
propertySources.addLast(getVersionSource(sourceClass)); propertySources.addLast(getVersionSource(sourceClass));
return Collections.singletonList(new PropertySourcesPropertyResolver(propertySources)); return Collections.singletonList(new PropertySourcesPropertyResolver(propertySources));
} }
private PropertySource<Environment> getEnvironmentSource(Environment environment) {
return new PropertySource<Environment>("environment", environment) {
@Override
public Object getProperty(String name) {
return environment.getProperty(name);
}
};
}
private MapPropertySource getVersionSource(Class<?> sourceClass) { private MapPropertySource getVersionSource(Class<?> sourceClass) {
return new MapPropertySource("version", getVersionsMap(sourceClass)); return new MapPropertySource("version", getVersionsMap(sourceClass));
} }

@ -49,21 +49,6 @@ class ResourceBannerTests {
AnsiOutput.setEnabled(Enabled.DETECT); 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 @Test
void renderVersions() { void renderVersions() {
Resource resource = new ByteArrayResource( Resource resource = new ByteArrayResource(
@ -142,6 +127,15 @@ class ResourceBannerTests {
assertThat(banner).startsWith("banner 1"); 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, private String printBanner(Resource resource, String bootVersion, String applicationVersion,
String applicationTitle) { String applicationTitle) {
ResourceBanner banner = new MockResourceBanner(resource, bootVersion, applicationVersion, applicationTitle); ResourceBanner banner = new MockResourceBanner(resource, bootVersion, applicationVersion, applicationTitle);

Loading…
Cancel
Save