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
This commit is contained in:
parent
4bfabfcf29
commit
2b91471286
|
|
@ -88,17 +88,30 @@ public class ResourceBanner implements Banner {
|
|||
}
|
||||
|
||||
private Map<String, Object> 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<String, Object> versions = new HashMap<String, Object>();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String, Object> source = Collections.<String, Object> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue