Merge pull request #34764 from krzyk

* gh-34764:
  Polish "Fix support for default values in banner placeholders"
  Fix support for default values in banner placeholders

Closes gh-34764
This commit is contained in:
Andy Wilkinson 2023-03-28 10:20:22 +01:00
commit a3135b49c3
2 changed files with 29 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,6 @@ package org.springframework.boot;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -29,6 +28,7 @@ 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;
@ -45,13 +45,14 @@ import org.springframework.util.StreamUtils;
* @author Phillip Webb * @author Phillip Webb
* @author Vedran Pavic * @author Vedran Pavic
* @author Toshiaki Maki * @author Toshiaki Maki
* @author Krzysztof Krason
* @since 1.2.0 * @since 1.2.0
*/ */
public class ResourceBanner implements Banner { public class ResourceBanner implements Banner {
private static final Log logger = LogFactory.getLog(ResourceBanner.class); private static final Log logger = LogFactory.getLog(ResourceBanner.class);
private Resource resource; private final Resource resource;
public ResourceBanner(Resource resource) { public ResourceBanner(Resource resource) {
Assert.notNull(resource, "Resource must not be null"); Assert.notNull(resource, "Resource must not be null");
@ -77,18 +78,18 @@ public class ResourceBanner implements Banner {
} }
protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) { protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) {
List<PropertyResolver> resolvers = new ArrayList<>(); MutablePropertySources propertySources = new MutablePropertySources();
resolvers.add(environment); if (environment instanceof ConfigurableEnvironment) {
resolvers.add(getVersionResolver(sourceClass)); ((ConfigurableEnvironment) environment).getPropertySources().forEach(propertySources::addLast);
resolvers.add(getAnsiResolver()); }
resolvers.add(getTitleResolver(sourceClass)); propertySources.addLast(getTitleSource(sourceClass));
return resolvers; propertySources.addLast(getAnsiSource());
propertySources.addLast(getVersionSource(sourceClass));
return Collections.singletonList(new PropertySourcesPropertyResolver(propertySources));
} }
private PropertyResolver getVersionResolver(Class<?> sourceClass) { private MapPropertySource getVersionSource(Class<?> sourceClass) {
MutablePropertySources propertySources = new MutablePropertySources(); return new MapPropertySource("version", getVersionsMap(sourceClass));
propertySources.addLast(new MapPropertySource("version", getVersionsMap(sourceClass)));
return new PropertySourcesPropertyResolver(propertySources);
} }
private Map<String, Object> getVersionsMap(Class<?> sourceClass) { private Map<String, Object> getVersionsMap(Class<?> sourceClass) {
@ -118,19 +119,15 @@ public class ResourceBanner implements Banner {
return format ? " (v" + version + ")" : version; return format ? " (v" + version + ")" : version;
} }
private PropertyResolver getAnsiResolver() { private AnsiPropertySource getAnsiSource() {
MutablePropertySources sources = new MutablePropertySources(); return new AnsiPropertySource("ansi", true);
sources.addFirst(new AnsiPropertySource("ansi", true));
return new PropertySourcesPropertyResolver(sources);
} }
private PropertyResolver getTitleResolver(Class<?> sourceClass) { private MapPropertySource getTitleSource(Class<?> sourceClass) {
MutablePropertySources sources = new MutablePropertySources();
String applicationTitle = getApplicationTitle(sourceClass); String applicationTitle = getApplicationTitle(sourceClass);
Map<String, Object> titleMap = Collections.singletonMap("application.title", Map<String, Object> titleMap = Collections.singletonMap("application.title",
(applicationTitle != null) ? applicationTitle : ""); (applicationTitle != null) ? applicationTitle : "");
sources.addFirst(new MapPropertySource("title", titleMap)); return new MapPropertySource("title", titleMap);
return new PropertySourcesPropertyResolver(sources);
} }
protected String getApplicationTitle(Class<?> sourceClass) { protected String getApplicationTitle(Class<?> sourceClass) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Phillip Webb * @author Phillip Webb
* @author Vedran Pavic * @author Vedran Pavic
* @author Toshiaki Maki * @author Toshiaki Maki
* @author Krzysztof Krason
*/ */
class ResourceBannerTests { class ResourceBannerTests {
@ -126,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);