InvalidConfigurationPropertyValueFailureAnalyzerTests Class — spring-boot Architecture
Architecture documentation for the InvalidConfigurationPropertyValueFailureAnalyzerTests class in InvalidConfigurationPropertyValueFailureAnalyzerTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/InvalidConfigurationPropertyValueFailureAnalyzerTests.java lines 43–227
class InvalidConfigurationPropertyValueFailureAnalyzerTests {
private final MockEnvironment environment = new MockEnvironment();
@Test
void analysisWithNullEnvironment() {
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
"test.property", "invalid", "This is not valid.");
FailureAnalysis analysis = new InvalidConfigurationPropertyValueFailureAnalyzer(null).analyze(failure);
assertThat(analysis).isNotNull();
assertThat(analysis.getDescription())
.contains("Invalid value 'invalid' for configuration property 'test.property'.");
}
@Test
void analysisWithKnownProperty() {
MapPropertySource source = new MapPropertySource("test", Collections.singletonMap("test.property", "invalid"));
this.environment.getPropertySources().addFirst(OriginCapablePropertySource.get(source));
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
"test.property", "invalid", "This is not valid.");
FailureAnalysis analysis = performAnalysis(failure);
assertThat(analysis).isNotNull();
assertCommonParts(failure, analysis);
assertThat(analysis.getAction()).contains("Review the value of the property with the provided reason.");
assertThat(analysis.getDescription()).contains("Validation failed for the following reason")
.contains("This is not valid.")
.doesNotContain("Additionally, this property is also set");
}
@Test
void analysisWithKnownPropertyFromSystemEnvironment() {
MapPropertySource source = new SystemEnvironmentPropertySource("systemEnvironment",
Collections.singletonMap("COM_EXAMPLE_TESTPROPERTY", "invalid"));
this.environment.getPropertySources().addFirst(source);
ConfigurationPropertySources.attach(this.environment);
assertThat(this.environment.getProperty("com.example.test-property")).isEqualTo("invalid");
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
"com.example.test-property", "invalid", "This is not valid.");
FailureAnalysis analysis = performAnalysis(failure);
assertThat(analysis).isNotNull();
assertThat(analysis.getDescription()).contains("com.example.test-property")
.contains("invalid")
.contains("property source \"systemEnvironment\"");
assertThat(analysis.getCause()).isSameAs(failure);
assertThat(analysis.getAction()).contains("Review the value of the property with the provided reason.");
assertThat(analysis.getDescription()).contains("Validation failed for the following reason")
.contains("This is not valid.")
.doesNotContain("Additionally, this property is also set");
}
@Test
void analysisWithKnownPropertyAndNoReason() {
MapPropertySource source = new MapPropertySource("test", Collections.singletonMap("test.property", "invalid"));
this.environment.getPropertySources().addFirst(OriginCapablePropertySource.get(source));
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
"test.property", "invalid", null);
FailureAnalysis analysis = performAnalysis(failure);
assertThat(analysis).isNotNull();
assertThat(analysis.getAction()).contains("Review the value of the property.");
assertThat(analysis.getDescription()).contains("No reason was provided.")
.doesNotContain("Additionally, this property is also set");
}
@Test
void analysisWithKnownPropertyAndOtherCandidates() {
MapPropertySource source = new MapPropertySource("test", Collections.singletonMap("test.property", "invalid"));
MapPropertySource additional = new MapPropertySource("additional",
Collections.singletonMap("test.property", "valid"));
MapPropertySource another = new MapPropertySource("another", Collections.singletonMap("test.property", "test"));
this.environment.getPropertySources().addFirst(OriginCapablePropertySource.get(source));
this.environment.getPropertySources().addLast(additional);
this.environment.getPropertySources().addLast(OriginCapablePropertySource.get(another));
this.environment.getPropertySources().addLast(OriginCapablePropertySource.get("another-again", another));
ConfigurationPropertySources.attach(this.environment);
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
"test.property", "invalid", "This is not valid.");
FailureAnalysis analysis = performAnalysis(failure);
assertThat(analysis).isNotNull();
assertCommonParts(failure, analysis);
assertThat(analysis.getAction()).contains("Review the value of the property with the provided reason.");
assertThat(analysis.getDescription())
.contains("Additionally, this property is also set in the following property sources:")
.contains("In 'additional' with the value 'valid'")
.contains("In 'another' with the value 'test' (originating from 'TestOrigin test.property')")
.doesNotContain("another-again");
}
@Test
void analysisWithUnknownKey() {
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
"test.key.not.defined", "invalid", "This is not valid.");
FailureAnalysis analysis = performAnalysis(failure);
assertThat(analysis).isNotNull();
assertThat(analysis.getDescription())
.contains("Invalid value 'invalid' for configuration property 'test.key.not.defined'.");
}
private void assertCommonParts(InvalidConfigurationPropertyValueException failure, FailureAnalysis analysis) {
assertThat(analysis.getDescription()).contains("test.property")
.contains("invalid")
.contains("TestOrigin test.property");
assertThat(analysis.getCause()).isSameAs(failure);
}
private @Nullable FailureAnalysis performAnalysis(InvalidConfigurationPropertyValueException failure) {
InvalidConfigurationPropertyValueFailureAnalyzer analyzer = new InvalidConfigurationPropertyValueFailureAnalyzer(
this.environment);
return analyzer.analyze(failure);
}
static class OriginCapablePropertySource<T> extends EnumerablePropertySource<T> implements OriginLookup<String> {
private final EnumerablePropertySource<T> propertySource;
OriginCapablePropertySource(EnumerablePropertySource<T> propertySource) {
this(propertySource.getName(), propertySource);
}
OriginCapablePropertySource(String name, EnumerablePropertySource<T> propertySource) {
super(name, propertySource.getSource());
this.propertySource = propertySource;
}
@Override
public @Nullable Object getProperty(String name) {
return this.propertySource.getProperty(name);
}
@Override
public String[] getPropertyNames() {
return this.propertySource.getPropertyNames();
}
@Override
public Origin getOrigin(String name) {
return new TestOrigin(name, this.propertySource.getName());
}
static <T> OriginCapablePropertySource<T> get(EnumerablePropertySource<T> propertySource) {
return new OriginCapablePropertySource<>(propertySource);
}
static <T> OriginCapablePropertySource<T> get(String name, EnumerablePropertySource<T> propertySource) {
return new OriginCapablePropertySource<>(name, propertySource);
}
static final class TestOrigin implements Origin {
private final String name;
private final String sourceName;
private TestOrigin(String name, String sourceName) {
this.name = name;
this.sourceName = sourceName;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TestOrigin other = (TestOrigin) obj;
return ObjectUtils.nullSafeEquals(this.name, other.name)
&& ObjectUtils.nullSafeEquals(this.sourceName, other.sourceName);
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.name);
}
@Override
public String toString() {
return "TestOrigin " + this.name;
}
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free