Commit c91d9bfd authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Add encoding support for git and build properties"

Closes gh-10771
parent f7a4a56f
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.info; package org.springframework.boot.autoconfigure.info;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties; import java.util.Properties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
...@@ -39,7 +40,6 @@ import org.springframework.core.io.ResourceLoader; ...@@ -39,7 +40,6 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for various project information. * {@link EnableAutoConfiguration Auto-configuration} for various project information.
...@@ -70,19 +70,14 @@ public class ProjectInfoAutoConfiguration { ...@@ -70,19 +70,14 @@ public class ProjectInfoAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
@Bean @Bean
public BuildProperties buildProperties() throws Exception { public BuildProperties buildProperties() throws Exception {
return new BuildProperties( return new BuildProperties(loadFrom(this.properties.getBuild().getLocation(),
loadFrom(this.properties.getBuild().getLocation(), "build", "build", this.properties.getBuild().getEncoding()));
this.properties.getBuild().getEncoding()));
} }
protected Properties loadFrom(Resource location, String prefix, String encoding) throws IOException { protected Properties loadFrom(Resource location, String prefix, Charset encoding)
throws IOException {
String p = prefix.endsWith(".") ? prefix : prefix + "."; String p = prefix.endsWith(".") ? prefix : prefix + ".";
Properties source = null; Properties source = loadSource(location, encoding);
if (StringUtils.isEmpty(encoding)) {
source = PropertiesLoaderUtils.loadProperties(location);
} else {
source = PropertiesLoaderUtils.loadProperties(new EncodedResource(location, encoding));
}
Properties target = new Properties(); Properties target = new Properties();
for (String key : source.stringPropertyNames()) { for (String key : source.stringPropertyNames()) {
if (key.startsWith(p)) { if (key.startsWith(p)) {
...@@ -92,6 +87,17 @@ public class ProjectInfoAutoConfiguration { ...@@ -92,6 +87,17 @@ public class ProjectInfoAutoConfiguration {
return target; return target;
} }
private Properties loadSource(Resource location, Charset encoding)
throws IOException {
if (encoding != null) {
return PropertiesLoaderUtils
.loadProperties(new EncodedResource(location, encoding));
}
else {
return PropertiesLoaderUtils.loadProperties(location);
}
}
static class GitResourceAvailableCondition extends SpringBootCondition { static class GitResourceAvailableCondition extends SpringBootCondition {
private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader(); private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader();
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 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.
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure.info; package org.springframework.boot.autoconfigure.info;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
...@@ -53,9 +56,9 @@ public class ProjectInfoProperties { ...@@ -53,9 +56,9 @@ public class ProjectInfoProperties {
"META-INF/build-info.properties"); "META-INF/build-info.properties");
/** /**
* build-info.properties file encoding. * File encoding.
*/ */
private String encoding; private Charset encoding = StandardCharsets.UTF_8;
public Resource getLocation() { public Resource getLocation() {
return this.location; return this.location;
...@@ -65,11 +68,11 @@ public class ProjectInfoProperties { ...@@ -65,11 +68,11 @@ public class ProjectInfoProperties {
this.location = location; this.location = location;
} }
public String getEncoding() { public Charset getEncoding() {
return this.encoding; return this.encoding;
} }
public void setEncoding(String encoding) { public void setEncoding(Charset encoding) {
this.encoding = encoding; this.encoding = encoding;
} }
...@@ -86,9 +89,9 @@ public class ProjectInfoProperties { ...@@ -86,9 +89,9 @@ public class ProjectInfoProperties {
private Resource location = new ClassPathResource("git.properties"); private Resource location = new ClassPathResource("git.properties");
/** /**
* git.properties file encoding. * File encoding.
*/ */
private String encoding; private Charset encoding = StandardCharsets.UTF_8;
public Resource getLocation() { public Resource getLocation() {
return this.location; return this.location;
...@@ -98,11 +101,11 @@ public class ProjectInfoProperties { ...@@ -98,11 +101,11 @@ public class ProjectInfoProperties {
this.location = location; this.location = location;
} }
public String getEncoding() { public Charset getEncoding() {
return this.encoding; return this.encoding;
} }
public void setEncoding(String encoding) { public void setEncoding(Charset encoding) {
this.encoding = encoding; this.encoding = encoding;
} }
......
...@@ -72,11 +72,23 @@ public class ProjectInfoAutoConfigurationTests { ...@@ -72,11 +72,23 @@ public class ProjectInfoAutoConfigurationTests {
} }
@Test @Test
public void gitPropertiesWithUnicode() { public void gitPropertiesUsesUtf8ByDefault() {
load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties", this.contextRunner.withPropertyValues(
"spring.info.git.encoding=utf-8"); "spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties")
GitProperties gitProperties = this.context.getBean(GitProperties.class); .run((context) -> {
assertThat(gitProperties.get("commit.unicode")).isEqualTo("中文"); GitProperties gitProperties = context.getBean(GitProperties.class);
assertThat(gitProperties.get("commit.charset")).isEqualTo("test™");
});
}
@Test
public void gitPropertiesEncodingCanBeConfigured() {
this.contextRunner.withPropertyValues("spring.info.git.encoding=US-ASCII",
"spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties")
.run((context) -> {
GitProperties gitProperties = context.getBean(GitProperties.class);
assertThat(gitProperties.get("commit.charset")).isNotEqualTo("test™");
});
} }
@Test @Test
...@@ -128,6 +140,28 @@ public class ProjectInfoAutoConfigurationTests { ...@@ -128,6 +140,28 @@ public class ProjectInfoAutoConfigurationTests {
}); });
} }
@Test
public void buildPropertiesUsesUtf8ByDefault() {
this.contextRunner.withPropertyValues(
"spring.info.build.location=classpath:/org/springframework/boot/autoconfigure/info/build-info.properties")
.run((context) -> {
BuildProperties buildProperties = context
.getBean(BuildProperties.class);
assertThat(buildProperties.get("charset")).isEqualTo("test™");
});
}
@Test
public void buildPropertiesEncodingCanBeConfigured() {
this.contextRunner.withPropertyValues("spring.info.build.encoding=US-ASCII",
"spring.info.build.location=classpath:/org/springframework/boot/autoconfigure/info/build-info.properties")
.run((context) -> {
BuildProperties buildProperties = context
.getBean(BuildProperties.class);
assertThat(buildProperties.get("charset")).isNotEqualTo("test™");
});
}
@Configuration @Configuration
static class CustomInfoPropertiesConfiguration { static class CustomInfoPropertiesConfiguration {
......
...@@ -3,3 +3,4 @@ build.artifact=acme ...@@ -3,3 +3,4 @@ build.artifact=acme
build.name=acme build.name=acme
build.version=1.0.1-SNAPSHOT build.version=1.0.1-SNAPSHOT
build.time=2016-03-04T10:42:00.000Z build.time=2016-03-04T10:42:00.000Z
build.charset=test™
...@@ -2,4 +2,4 @@ git.commit.user.email=john@example.com ...@@ -2,4 +2,4 @@ git.commit.user.email=john@example.com
git.commit.id=f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a git.commit.id=f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a
git.commit.user.name=John Smith git.commit.user.name=John Smith
git.commit.time=2016-03-03T10\:02\:00+0100 git.commit.time=2016-03-03T10\:02\:00+0100
git.commit.unicode=中文 git.commit.charset=test™
...@@ -98,7 +98,9 @@ content into your application. Rather, pick only the properties that you need. ...@@ -98,7 +98,9 @@ content into your application. Rather, pick only the properties that you need.
spring.hazelcast.config= # The location of the configuration file to use to initialize Hazelcast. spring.hazelcast.config= # The location of the configuration file to use to initialize Hazelcast.
# PROJECT INFORMATION ({sc-spring-boot-autoconfigure}/info/ProjectInfoProperties.{sc-ext}[ProjectInfoProperties]) # PROJECT INFORMATION ({sc-spring-boot-autoconfigure}/info/ProjectInfoProperties.{sc-ext}[ProjectInfoProperties])
spring.info.build.encoding=UTF-8 # File encoding.
spring.info.build.location=classpath:META-INF/build-info.properties # Location of the generated build-info.properties file. spring.info.build.location=classpath:META-INF/build-info.properties # Location of the generated build-info.properties file.
spring.info.git.encoding=UTF-8 # File encoding.
spring.info.git.location=classpath:git.properties # Location of the generated git.properties file. spring.info.git.location=classpath:git.properties # Location of the generated git.properties file.
# JMX # JMX
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment