Add formatted version numbers for banner expansion

Add `application.formatted-version` and `spring-boot.formatted-version`
properties for use in banner.txt files. The `application.version` and
`spring-boot.version` properties now return a simple version number
without any formatting.

Fixes gh-2090
pull/2091/head
Phillip Webb 10 years ago
parent 4bfabfcf29
commit 2b91471286

@ -88,17 +88,30 @@ public class ResourceBanner implements Banner {
} }
private Map<String, Object> getVersionsMap(Class<?> sourceClass) { private Map<String, Object> getVersionsMap(Class<?> sourceClass) {
String applicationVersion = (sourceClass == null ? null : sourceClass String appVersion = getApplicationVersion(sourceClass);
.getPackage().getImplementationVersion()); String bootVersion = getBootVersion();
String bootVersion = Banner.class.getPackage().getImplementationVersion();
Map<String, Object> versions = new HashMap<String, Object>(); Map<String, Object> versions = new HashMap<String, Object>();
versions.put("application.version", getVersionString(applicationVersion)); versions.put("application.version", getVersionString(appVersion, false));
versions.put("spring-boot.version", getVersionString(bootVersion)); 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; return versions;
} }
private String getVersionString(String version) { protected String getApplicationVersion(Class<?> sourceClass) {
return (version == null ? "" : " (v" + version + ")"); 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);
} }
} }

@ -28,9 +28,7 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
@ -41,17 +39,74 @@ import static org.junit.Assert.assertThat;
public class ResourceBannerTests { public class ResourceBannerTests {
@Test @Test
public void renderWithReplacement() throws Exception { public void renderVersions() throws Exception {
Resource resource = new ByteArrayResource( Resource resource = new ByteArrayResource(
"banner ${a} ${spring-boot.version} ${application.version}".getBytes()); "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(); ConfigurableEnvironment environment = new MockEnvironment();
Map<String, Object> source = Collections.<String, Object> singletonMap("a", "1"); Map<String, Object> source = Collections.<String, Object> singletonMap("a", "1");
environment.getPropertySources().addLast(new MapPropertySource("map", source)); environment.getPropertySources().addLast(new MapPropertySource("map", source));
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
banner.printBanner(environment, getClass(), new PrintStream(out)); banner.printBanner(environment, getClass(), new PrintStream(out));
assertThat(out.toString(), startsWith("banner 1")); return out.toString();
assertThat(out.toString(), not(containsString("$"))); }
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;
}
} }
} }

Loading…
Cancel
Save