diff --git a/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java b/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java index 594cd9f93c..0e2664a9a2 100644 --- a/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java +++ b/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java @@ -88,17 +88,30 @@ public class ResourceBanner implements Banner { } private Map getVersionsMap(Class sourceClass) { - String applicationVersion = (sourceClass == null ? null : sourceClass - .getPackage().getImplementationVersion()); - String bootVersion = Banner.class.getPackage().getImplementationVersion(); + String appVersion = getApplicationVersion(sourceClass); + String bootVersion = getBootVersion(); Map versions = new HashMap(); - versions.put("application.version", getVersionString(applicationVersion)); - versions.put("spring-boot.version", getVersionString(bootVersion)); + versions.put("application.version", getVersionString(appVersion, false)); + versions.put("spring-boot.version", getVersionString(bootVersion, false)); + versions.put("spring-boot.formatted-version", getVersionString(bootVersion, true)); + versions.put("application.formatted-version", getVersionString(appVersion, true)); return versions; } - private String getVersionString(String version) { - return (version == null ? "" : " (v" + version + ")"); + protected String getApplicationVersion(Class sourceClass) { + return (sourceClass == null ? null : sourceClass.getPackage() + .getImplementationVersion()); + } + + protected String getBootVersion() { + return Banner.class.getPackage().getImplementationVersion(); + } + + private String getVersionString(String version, boolean format) { + if (version == null) { + return ""; + } + return (format ? " (v" + version + ")" : version); } } diff --git a/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java b/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java index 58641018e9..c3deebc70b 100644 --- a/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java @@ -28,9 +28,7 @@ import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; import org.springframework.mock.env.MockEnvironment; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.startsWith; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; /** @@ -41,17 +39,74 @@ import static org.junit.Assert.assertThat; public class ResourceBannerTests { @Test - public void renderWithReplacement() throws Exception { + public void renderVersions() throws Exception { Resource resource = new ByteArrayResource( "banner ${a} ${spring-boot.version} ${application.version}".getBytes()); - ResourceBanner banner = new ResourceBanner(resource); + String banner = printBanner(resource, "10.2", "2.0"); + assertThat(banner, equalTo("banner 1 10.2 2.0\n")); + } + + @Test + public void renderWithoutVersions() throws Exception { + Resource resource = new ByteArrayResource( + "banner ${a} ${spring-boot.version} ${application.version}".getBytes()); + String banner = printBanner(resource, null, null); + assertThat(banner, equalTo("banner 1 \n")); + } + + @Test + public void renderFormattedVersions() throws Exception { + Resource resource = new ByteArrayResource( + "banner ${a}${spring-boot.formatted-version}${application.formatted-version}" + .getBytes()); + String banner = printBanner(resource, "10.2", "2.0"); + assertThat(banner, equalTo("banner 1 (v10.2) (v2.0)\n")); + } + + @Test + public void renderWithoutFormattedVersions() throws Exception { + Resource resource = new ByteArrayResource( + "banner ${a}${spring-boot.formatted-version}${application.formatted-version}" + .getBytes()); + String banner = printBanner(resource, null, null); + assertThat(banner, equalTo("banner 1\n")); + } + + private String printBanner(Resource resource, String bootVersion, + String applicationVersion) { + ResourceBanner banner = new MockResourceBanner(resource, bootVersion, + applicationVersion); ConfigurableEnvironment environment = new MockEnvironment(); Map source = Collections. singletonMap("a", "1"); environment.getPropertySources().addLast(new MapPropertySource("map", source)); ByteArrayOutputStream out = new ByteArrayOutputStream(); banner.printBanner(environment, getClass(), new PrintStream(out)); - assertThat(out.toString(), startsWith("banner 1")); - assertThat(out.toString(), not(containsString("$"))); + return out.toString(); + } + + private static class MockResourceBanner extends ResourceBanner { + + private final String bootVersion; + + private final String applicationVersion; + + public MockResourceBanner(Resource resource, String bootVersion, + String applicationVersion) { + super(resource); + this.bootVersion = bootVersion; + this.applicationVersion = applicationVersion; + } + + @Override + protected String getBootVersion() { + return this.bootVersion; + } + + @Override + protected String getApplicationVersion(Class sourceClass) { + return this.applicationVersion; + } + } }