Rename configuration-analyzer

Rename `spring-boot-configuration-analyzer` to
`spring-boot-deprecated-properties-support`.

Also renamed classes to match and polished some of the code.

See gh-11301
This commit is contained in:
Phillip Webb 2018-01-22 14:06:00 -08:00
parent 66f8279f65
commit d0ca1a3eea
15 changed files with 190 additions and 194 deletions

View File

@ -21,7 +21,7 @@
<module>spring-boot-actuator</module>
<module>spring-boot-actuator-autoconfigure</module>
<module>spring-boot-autoconfigure</module>
<module>spring-boot-configuration-analyzer</module>
<module>spring-boot-deprecated-properties-support</module>
<module>spring-boot-devtools</module>
<module>spring-boot-test</module>
<module>spring-boot-test-autoconfigure</module>

View File

@ -1,2 +0,0 @@
org.springframework.context.ApplicationListener=\
org.springframework.boot.configurationalayzer.LegacyPropertiesAnalyzerListener

View File

@ -8,9 +8,9 @@
<version>${revision}</version>
<relativePath>../spring-boot-parent</relativePath>
</parent>
<artifactId>spring-boot-configuration-analyzer</artifactId>
<name>Spring Boot Configuration Analyzer</name>
<description>Spring Boot Configuration Analyzer</description>
<artifactId>spring-boot-deprecated-properties-support</artifactId>
<name>Spring Boot Deprecated Properties Support</name>
<description>Spring Boot Deprecated Properties Support</description>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>

View File

@ -14,12 +14,10 @@
* limitations under the License.
*/
package org.springframework.boot.configurationalayzer;
package org.springframework.boot.deprecatedproperties;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -37,20 +35,21 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/**
* An {@link ApplicationListener} that inspects the {@link ConfigurableEnvironment
* environment} for legacy configuration keys. Automatically renames the keys that
* have a matching replacement and log a report of what was discovered.
* environment} for deprecated configuration keys. Automatically renames the keys that
* have a matching replacement and log a report of what was discovered.
*
* @author Stephane Nicoll
* @since 2.0.0
*/
public class LegacyPropertiesAnalyzerListener
public class DeprecatedPropertiesListener
implements ApplicationListener<SpringApplicationEvent> {
private static final Log logger = LogFactory.getLog(LegacyPropertiesAnalyzerListener.class);
private static final Log logger = LogFactory
.getLog(DeprecatedPropertiesListener.class);
private LegacyPropertiesAnalysis analysis;
private DeprecatedPropertiesReport report;
private boolean analysisLogged;
private boolean reported;
@Override
public void onApplicationEvent(SpringApplicationEvent event) {
@ -65,49 +64,45 @@ public class LegacyPropertiesAnalyzerListener
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
ConfigurationMetadataRepository repository = loadRepository();
ConfigurableEnvironment environment =
event.getApplicationContext().getEnvironment();
LegacyPropertiesAnalyzer validator = new LegacyPropertiesAnalyzer(
repository, environment);
this.analysis = validator.analyseLegacyProperties();
}
private void logLegacyPropertiesAnalysis() {
if (this.analysis == null || this.analysisLogged) {
return;
}
String warningReport = this.analysis.createWarningReport();
String errorReport = this.analysis.createErrorReport();
if (warningReport != null) {
logger.warn(warningReport);
}
if (errorReport != null) {
logger.error(errorReport);
}
this.analysisLogged = true;
DeprecatedPropertiesReporter reporter = new DeprecatedPropertiesReporter(repository,
event.getApplicationContext().getEnvironment());
this.report = reporter.getReport();
}
private ConfigurationMetadataRepository loadRepository() {
try {
ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder.create();
for (InputStream inputStream : getResources()) {
builder.withJsonResource(inputStream);
}
return builder.build();
return loadRepository(ConfigurationMetadataRepositoryJsonBuilder.create());
}
catch (IOException ex) {
throw new IllegalStateException("Failed to load metadata", ex);
}
}
private List<InputStream> getResources() throws IOException {
private ConfigurationMetadataRepository loadRepository(
ConfigurationMetadataRepositoryJsonBuilder builder) throws IOException {
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath*:/META-INF/spring-configuration-metadata.json");
List<InputStream> result = new ArrayList<>();
for (Resource resource : resources) {
result.add(resource.getInputStream());
try (InputStream inputStream = resource.getInputStream()) {
builder.withJsonResource(inputStream);
}
}
return result;
return builder.build();
}
private void logLegacyPropertiesAnalysis() {
if (this.report == null || this.reported) {
return;
}
String warningReport = this.report.getWarningReport();
if (warningReport != null) {
logger.warn(warningReport);
}
String errorReport = this.report.getErrorReport();
if (errorReport != null) {
logger.error(errorReport);
}
this.reported = true;
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.configurationalayzer;
package org.springframework.boot.deprecatedproperties;
import java.util.ArrayList;
import java.util.Collections;
@ -28,32 +28,30 @@ import org.springframework.boot.configurationmetadata.ConfigurationMetadataPrope
import org.springframework.util.StringUtils;
/**
* Describes the outcome of the environment analysis.
* Provides a deprecated properties report.
*
* @author Stephane Nicoll
*/
class LegacyPropertiesAnalysis {
class DeprecatedPropertiesReport {
private final Map<String, PropertySourceAnalysis> content = new LinkedHashMap<>();
private final Map<String, DeprecatedProperties> content = new LinkedHashMap<>();
/**
* Create a report for all the legacy properties that were automatically renamed. If
* Return a report for all the legacy properties that were automatically renamed. If
* no such legacy properties were found, return {@code null}.
* @return a report with the configurations keys that should be renamed
*/
public String createWarningReport() {
Map<String, List<LegacyProperty>> content = this.content.entrySet().stream()
.filter(e -> !e.getValue().handledProperties.isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
e -> new ArrayList<>(e.getValue().handledProperties)));
public String getWarningReport() {
Map<String, List<DeprecatedProperty>> content = getContent(
DeprecatedProperties::getRenamed);
if (content.isEmpty()) {
return null;
}
StringBuilder report = new StringBuilder();
report.append(String.format("%nThe use of configuration keys that have been "
+ "renamed was found in the environment:%n%n"));
appendProperties(report, content, metadata ->
"Replacement: " + metadata.getDeprecation().getReplacement());
append(report, content, (metadata) -> "Replacement: "
+ metadata.getDeprecation().getReplacement());
report.append(String.format("%n"));
report.append("Each configuration key has been temporarily mapped to its "
+ "replacement for your convenience. To silence this warning, please "
@ -63,24 +61,23 @@ class LegacyPropertiesAnalysis {
}
/**
* Create a report for all the legacy properties that are no longer supported. If
* no such legacy properties were found, return {@code null}.
* Return a report for all the legacy properties that are no longer supported. If no
* such legacy properties were found, return {@code null}.
* @return a report with the configurations keys that are no longer supported
*/
public String createErrorReport() {
Map<String, List<LegacyProperty>> content = this.content.entrySet().stream()
.filter(e -> !e.getValue().notHandledProperties.isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
e -> new ArrayList<>(e.getValue().notHandledProperties)));
public String getErrorReport() {
Map<String, List<DeprecatedProperty>> content = getContent(
DeprecatedProperties::getUnsupported);
if (content.isEmpty()) {
return null;
}
StringBuilder report = new StringBuilder();
report.append(String.format("%nThe use of configuration keys that are no longer "
+ "supported was found in the environment:%n%n"));
appendProperties(report, content, metadata ->
"Reason: " + (StringUtils.hasText(metadata.getDeprecation().getReason())
? metadata.getDeprecation().getReason() : "none"));
append(report, content,
metadata -> "Reason: "
+ (StringUtils.hasText(metadata.getDeprecation().getReason())
? metadata.getDeprecation().getReason() : "none"));
report.append(String.format("%n"));
report.append("Please refer to the migration guide or reference guide for "
+ "potential alternatives.");
@ -88,21 +85,29 @@ class LegacyPropertiesAnalysis {
return report.toString();
}
private void appendProperties(StringBuilder report,
Map<String, List<LegacyProperty>> content,
private Map<String, List<DeprecatedProperty>> getContent(
Function<DeprecatedProperties, List<DeprecatedProperty>> extractor) {
return this.content.entrySet().stream()
.filter((entry) -> !extractor.apply(entry.getValue()).isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
(entry) -> new ArrayList<>(extractor.apply(entry.getValue()))));
}
private void append(StringBuilder report,
Map<String, List<DeprecatedProperty>> content,
Function<ConfigurationMetadataProperty, String> deprecationMessage) {
content.forEach((name, properties) -> {
report.append(String.format("Property source '%s':%n", name));
properties.sort(LegacyProperty.COMPARATOR);
properties.sort(DeprecatedProperty.COMPARATOR);
properties.forEach((property) -> {
ConfigurationMetadataProperty metadata = property.getMetadata();
report.append(String.format("\tKey: %s%n", metadata.getId()));
if (property.getLineNumber() != null) {
report.append(String.format("\t\tLine: %d%n",
property.getLineNumber()));
report.append(
String.format("\t\tLine: %d%n", property.getLineNumber()));
}
report.append(String.format("\t\t%s%n",
deprecationMessage.apply(metadata)));
report.append(
String.format("\t\t%s%n", deprecationMessage.apply(metadata)));
});
report.append(String.format("%n"));
});
@ -111,29 +116,36 @@ class LegacyPropertiesAnalysis {
/**
* Register a new property source.
* @param name the name of the property source
* @param handledProperties the properties that were renamed
* @param notHandledProperties the properties that are no longer supported
* @param renamed the properties that were renamed
* @param unsupported the properties that are no longer supported
*/
void register(String name, List<LegacyProperty> handledProperties,
List<LegacyProperty> notHandledProperties) {
List<LegacyProperty> handled = (handledProperties != null
? new ArrayList<>(handledProperties) : Collections.emptyList());
List<LegacyProperty> notHandled = (notHandledProperties != null
? new ArrayList<>(notHandledProperties) : Collections.emptyList());
this.content.put(name, new PropertySourceAnalysis(handled, notHandled));
void add(String name, List<DeprecatedProperty> renamed,
List<DeprecatedProperty> unsupported) {
this.content.put(name, new DeprecatedProperties(renamed, unsupported));
}
private static class DeprecatedProperties {
private static class PropertySourceAnalysis {
private final List<DeprecatedProperty> renamed;
private final List<LegacyProperty> handledProperties;
private final List<DeprecatedProperty> unsupported;
private final List<LegacyProperty> notHandledProperties;
DeprecatedProperties(List<DeprecatedProperty> renamed,
List<DeprecatedProperty> unsupported) {
this.renamed = asNewList(renamed);
this.unsupported = asNewList(unsupported);
}
PropertySourceAnalysis(List<LegacyProperty> handledProperties,
List<LegacyProperty> notHandledProperties) {
this.handledProperties = handledProperties;
this.notHandledProperties = notHandledProperties;
private <T> List<T> asNewList(List<T> source) {
return (source == null ? Collections.emptyList() : new ArrayList<T>(source));
}
public List<DeprecatedProperty> getRenamed() {
return this.renamed;
}
public List<DeprecatedProperty> getUnsupported() {
return this.unsupported;
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.configurationalayzer;
package org.springframework.boot.deprecatedproperties;
import java.util.ArrayList;
import java.util.Collections;
@ -40,19 +40,20 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
/**
* Analyse {@link LegacyProperty legacy properties}.
* Report on {@link DeprecatedProperty deprecated properties}.
*
* @author Stephane Nicoll
*/
class LegacyPropertiesAnalyzer {
class DeprecatedPropertiesReporter {
private final Map<String, ConfigurationMetadataProperty> allProperties;
private final ConfigurableEnvironment environment;
LegacyPropertiesAnalyzer(ConfigurationMetadataRepository metadataRepository,
DeprecatedPropertiesReporter(ConfigurationMetadataRepository metadataRepository,
ConfigurableEnvironment environment) {
this.allProperties = Collections.unmodifiableMap(metadataRepository.getAllProperties());
this.allProperties = Collections
.unmodifiableMap(metadataRepository.getAllProperties());
this.environment = environment;
}
@ -61,60 +62,59 @@ class LegacyPropertiesAnalyzer {
* legacy properties if a replacement exists.
* @return the analysis
*/
public LegacyPropertiesAnalysis analyseLegacyProperties() {
LegacyPropertiesAnalysis analysis = new LegacyPropertiesAnalysis();
Map<String, List<LegacyProperty>> properties = getMatchingProperties(deprecatedFilter());
public DeprecatedPropertiesReport getReport() {
DeprecatedPropertiesReport report = new DeprecatedPropertiesReport();
Map<String, List<DeprecatedProperty>> properties = getMatchingProperties(
deprecatedFilter());
if (properties.isEmpty()) {
return analysis;
return report;
}
properties.forEach((name, candidates) -> {
PropertySource<?> propertySource = mapPropertiesWithReplacement(analysis,
PropertySource<?> propertySource = mapPropertiesWithReplacement(report,
name, candidates);
if (propertySource != null) {
this.environment.getPropertySources().addBefore(name, propertySource);
}
});
return analysis;
return report;
}
private PropertySource<?> mapPropertiesWithReplacement(
LegacyPropertiesAnalysis analysis, String name,
List<LegacyProperty> properties) {
List<LegacyProperty> matches = new ArrayList<>();
List<LegacyProperty> unhandled = new ArrayList<>();
for (LegacyProperty property : properties) {
if (hasValidReplacement(property)) {
matches.add(property);
}
else {
unhandled.add(property);
}
}
analysis.register(name, matches, unhandled);
if (matches.isEmpty()) {
DeprecatedPropertiesReport report, String name,
List<DeprecatedProperty> properties) {
List<DeprecatedProperty> renamed = new ArrayList<>();
List<DeprecatedProperty> unsupported = new ArrayList<>();
properties.forEach((property) -> {
(isRenamed(property) ? renamed : unsupported).add(property);
});
report.add(name, renamed, unsupported);
if (renamed.isEmpty()) {
return null;
}
String target = "migrate-" + name;
Map<String, OriginTrackedValue> content = new LinkedHashMap<>();
for (LegacyProperty candidate : matches) {
for (DeprecatedProperty candidate : renamed) {
OriginTrackedValue value = OriginTrackedValue.of(
candidate.getProperty().getValue(), candidate.getProperty().getOrigin());
candidate.getProperty().getValue(),
candidate.getProperty().getOrigin());
content.put(candidate.getMetadata().getDeprecation().getReplacement(), value);
}
return new OriginTrackedMapPropertySource(target, content);
}
private boolean hasValidReplacement(LegacyProperty property) {
String replacementId = property.getMetadata().getDeprecation().getReplacement();
private boolean isRenamed(DeprecatedProperty property) {
ConfigurationMetadataProperty metadata = property.getMetadata();
String replacementId = metadata.getDeprecation().getReplacement();
if (StringUtils.hasText(replacementId)) {
ConfigurationMetadataProperty replacement = this.allProperties.get(replacementId);
ConfigurationMetadataProperty replacement = this.allProperties
.get(replacementId);
if (replacement != null) {
return replacement.getType().equals(property.getMetadata().getType());
return replacement.getType().equals(metadata.getType());
}
replacement = getMapProperty(replacementId);
if (replacement != null) {
return replacement.getType().startsWith("java.util.Map")
&& replacement.getType().endsWith(property.getMetadata().getType() + ">");
&& replacement.getType().endsWith(metadata.getType() + ">");
}
}
return false;
@ -128,17 +128,19 @@ class LegacyPropertiesAnalyzer {
return null;
}
private Map<String, List<LegacyProperty>> getMatchingProperties(
private Map<String, List<DeprecatedProperty>> getMatchingProperties(
Predicate<ConfigurationMetadataProperty> filter) {
MultiValueMap<String, LegacyProperty> result = new LinkedMultiValueMap<>();
MultiValueMap<String, DeprecatedProperty> result = new LinkedMultiValueMap<>();
List<ConfigurationMetadataProperty> candidates = this.allProperties.values()
.stream().filter(filter).collect(Collectors.toList());
getPropertySourcesAsMap().forEach((name, source) -> {
candidates.forEach(metadata -> {
ConfigurationProperty configurationProperty = source.getConfigurationProperty(
ConfigurationPropertyName.of(metadata.getId()));
ConfigurationProperty configurationProperty = source
.getConfigurationProperty(
ConfigurationPropertyName.of(metadata.getId()));
if (configurationProperty != null) {
result.add(name, new LegacyProperty(metadata, configurationProperty));
result.add(name,
new DeprecatedProperty(metadata, configurationProperty));
}
});
});
@ -146,14 +148,15 @@ class LegacyPropertiesAnalyzer {
}
private Predicate<ConfigurationMetadataProperty> deprecatedFilter() {
return p -> p.getDeprecation() != null
&& p.getDeprecation().getLevel() == Deprecation.Level.ERROR;
return (property) -> property.getDeprecation() != null
&& property.getDeprecation().getLevel() == Deprecation.Level.ERROR;
}
private Map<String, ConfigurationPropertySource> getPropertySourcesAsMap() {
Map<String, ConfigurationPropertySource> map = new LinkedHashMap<>();
ConfigurationPropertySources.get(this.environment);
for (ConfigurationPropertySource source : ConfigurationPropertySources.get(this.environment)) {
for (ConfigurationPropertySource source : ConfigurationPropertySources
.get(this.environment)) {
map.put(determinePropertySourceName(source), source);
}
return map;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.configurationalayzer;
package org.springframework.boot.deprecatedproperties;
import java.util.Comparator;
@ -28,9 +28,10 @@ import org.springframework.boot.origin.TextResourceOrigin;
*
* @author Stephane Nicoll
*/
class LegacyProperty {
class DeprecatedProperty {
static final LegacyPropertyComparator COMPARATOR = new LegacyPropertyComparator();
public static final Comparator<DeprecatedProperty> COMPARATOR = Comparator
.comparing((property) -> property.getMetadata().getId());
private final ConfigurationMetadataProperty metadata;
@ -38,7 +39,7 @@ class LegacyProperty {
private final Integer lineNumber;
LegacyProperty(ConfigurationMetadataProperty metadata,
DeprecatedProperty(ConfigurationMetadataProperty metadata,
ConfigurationProperty property) {
this.metadata = metadata;
this.property = property;
@ -68,13 +69,4 @@ class LegacyProperty {
return this.lineNumber;
}
private static class LegacyPropertyComparator implements Comparator<LegacyProperty> {
@Override
public int compare(LegacyProperty p1, LegacyProperty p2) {
return p1.getMetadata().getId().compareTo(p2.getMetadata().getId());
}
}
}

View File

@ -15,6 +15,6 @@
*/
/**
* Support for analyzing the environment.
* Support for migrating deprecated Spring Boot properties.
*/
package org.springframework.boot.configurationalayzer;
package org.springframework.boot.deprecatedproperties;

View File

@ -0,0 +1,2 @@
org.springframework.context.ApplicationListener=\
org.springframework.boot.deprecatedproperties.DeprecatedPropertiesListener

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.configurationalayzer;
package org.springframework.boot.deprecatedproperties;
import org.junit.After;
import org.junit.Rule;
@ -28,11 +28,11 @@ import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link LegacyPropertiesAnalyzerListener}.
* Tests for {@link DeprecatedPropertiesListener}.
*
* @author Stephane Nicoll
*/
public class LegacyPropertiesAnalyzerListenerTests {
public class DeprecatedPropertiesListenerTests {
@Rule
public final OutputCapture output = new OutputCapture();
@ -48,8 +48,7 @@ public class LegacyPropertiesAnalyzerListenerTests {
@Test
public void sampleReport() {
this.context = createSampleApplication()
.run("--banner.charset=UTF8");
this.context = createSampleApplication().run("--banner.charset=UTF8");
assertThat(this.output.toString()).contains("commandLineArgs")
.contains("spring.banner.charset")
.contains("Each configuration key has been temporarily mapped")
@ -60,7 +59,6 @@ public class LegacyPropertiesAnalyzerListenerTests {
return new SpringApplication(TestApplication.class);
}
@Configuration
public static class TestApplication {

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.configurationalayzer;
package org.springframework.boot.deprecatedproperties;
import java.io.IOException;
import java.util.ArrayList;
@ -38,13 +38,12 @@ import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link LegacyPropertiesAnalyzer}.
* Tests for {@link DeprecatedPropertiesReporter}.
*
* @author Stephane Nicoll
*/
public class LegacyPropertiesAnalyzerTests {
public class DeprecatedPropertiesReporterTests {
private ConfigurableEnvironment environment = new MockEnvironment();
@ -64,56 +63,53 @@ public class LegacyPropertiesAnalyzerTests {
propertySources.addFirst(one);
propertySources.addAfter("one", two);
assertThat(propertySources).hasSize(3);
createAnalyzer(loadRepository("metadata/sample-metadata.json"))
.analyseLegacyProperties();
assertThat(mapToNames(propertySources)).containsExactly("one",
"migrate-two", "two", "mockProperties");
assertMappedProperty(propertySources.get("migrate-two"),
"test.two", "another", getOrigin(two, "wrong.two"));
createAnalyzer(loadRepository("metadata/sample-metadata.json")).getReport();
assertThat(mapToNames(propertySources)).containsExactly("one", "migrate-two",
"two", "mockProperties");
assertMappedProperty(propertySources.get("migrate-two"), "test.two", "another",
getOrigin(two, "wrong.two"));
}
@Test
public void warningReport() throws IOException {
this.environment.getPropertySources().addFirst(loadPropertySource("test",
"config/config-warnings.properties"));
this.environment.getPropertySources().addFirst(loadPropertySource("ignore",
"config/config-error.properties"));
String report = createWarningReport(loadRepository(
"metadata/sample-metadata.json"));
this.environment.getPropertySources().addFirst(
loadPropertySource("test", "config/config-warnings.properties"));
this.environment.getPropertySources()
.addFirst(loadPropertySource("ignore", "config/config-error.properties"));
String report = createWarningReport(
loadRepository("metadata/sample-metadata.json"));
assertThat(report).isNotNull();
assertThat(report).containsSubsequence("Property source 'test'",
"wrong.four.test", "Line: 5", "test.four.test",
"wrong.two", "Line: 2", "test.two");
"wrong.four.test", "Line: 5", "test.four.test", "wrong.two", "Line: 2",
"test.two");
assertThat(report).doesNotContain("wrong.one");
}
@Test
public void errorReport() throws IOException {
this.environment.getPropertySources().addFirst(loadPropertySource("test1",
"config/config-warnings.properties"));
this.environment.getPropertySources().addFirst(loadPropertySource("test2",
"config/config-error.properties"));
String report = createErrorReport(loadRepository(
"metadata/sample-metadata.json"));
this.environment.getPropertySources().addFirst(
loadPropertySource("test1", "config/config-warnings.properties"));
this.environment.getPropertySources()
.addFirst(loadPropertySource("test2", "config/config-error.properties"));
String report = createErrorReport(
loadRepository("metadata/sample-metadata.json"));
assertThat(report).isNotNull();
assertThat(report).containsSubsequence("Property source 'test2'",
"wrong.one", "Line: 2", "This is no longer supported.");
assertThat(report).doesNotContain("wrong.four.test")
.doesNotContain("wrong.two");
assertThat(report).containsSubsequence("Property source 'test2'", "wrong.one",
"Line: 2", "This is no longer supported.");
assertThat(report).doesNotContain("wrong.four.test").doesNotContain("wrong.two");
}
@Test
public void errorReportNoReplacement() throws IOException {
this.environment.getPropertySources().addFirst(loadPropertySource("first",
"config/config-error-no-replacement.properties"));
this.environment.getPropertySources().addFirst(loadPropertySource("second",
"config/config-error.properties"));
String report = createErrorReport(loadRepository(
"metadata/sample-metadata.json"));
this.environment.getPropertySources()
.addFirst(loadPropertySource("second", "config/config-error.properties"));
String report = createErrorReport(
loadRepository("metadata/sample-metadata.json"));
assertThat(report).isNotNull();
assertThat(report).containsSubsequence(
"Property source 'first'", "wrong.three", "Line: 6", "none",
"Property source 'second'", "wrong.one", "Line: 2",
assertThat(report).containsSubsequence("Property source 'first'", "wrong.three",
"Line: 6", "none", "Property source 'second'", "wrong.one", "Line: 2",
"This is no longer supported.");
assertThat(report).doesNotContain("null").doesNotContain("server.port")
.doesNotContain("debug");
@ -132,6 +128,7 @@ public class LegacyPropertiesAnalyzerTests {
return ((OriginLookup<String>) propertySource).getOrigin(name);
}
@SuppressWarnings("unchecked")
private void assertMappedProperty(PropertySource<?> propertySource, String name,
Object value, Origin origin) {
assertThat(propertySource.containsProperty(name)).isTrue();
@ -146,15 +143,16 @@ public class LegacyPropertiesAnalyzerTests {
private PropertySource<?> loadPropertySource(String name, String path)
throws IOException {
ClassPathResource resource = new ClassPathResource(path);
PropertySource<?> propertySource = new PropertiesPropertySourceLoader()
.load(name, resource, null);
PropertySource<?> propertySource = new PropertiesPropertySourceLoader().load(name,
resource, null);
assertThat(propertySource).isNotNull();
return propertySource;
}
private ConfigurationMetadataRepository loadRepository(String... content) {
try {
ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder.create();
ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder
.create();
for (String path : content) {
Resource resource = new ClassPathResource(path);
builder.withJsonResource(resource.getInputStream());
@ -167,18 +165,16 @@ public class LegacyPropertiesAnalyzerTests {
}
private String createWarningReport(ConfigurationMetadataRepository repository) {
return createAnalyzer(repository).analyseLegacyProperties()
.createWarningReport();
return createAnalyzer(repository).getReport().getWarningReport();
}
private String createErrorReport(ConfigurationMetadataRepository repository) {
return createAnalyzer(repository).analyseLegacyProperties()
.createErrorReport();
return createAnalyzer(repository).getReport().getErrorReport();
}
private LegacyPropertiesAnalyzer createAnalyzer(
private DeprecatedPropertiesReporter createAnalyzer(
ConfigurationMetadataRepository repository) {
return new LegacyPropertiesAnalyzer(repository, this.environment);
return new DeprecatedPropertiesReporter(repository, this.environment);
}
}