Skip to content

Commit

Permalink
Polish "Fix support for default values in banner placeholders"
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Mar 28, 2023
1 parent 5c01aa7 commit 2d280bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,22 +79,15 @@ public void printBanner(Environment environment, Class<?> sourceClass, PrintStre

protected List<PropertyResolver> 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<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) {
return new MapPropertySource("version", getVersionsMap(sourceClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2d280bb

Please sign in to comment.