Commit b79132ce authored by Maciej Walkowiak's avatar Maciej Walkowiak Committed by Phillip Webb

Update @ConditionalOnProperty to not match false

Update @ConditionalOnProperty so that properties that are present but
contain the value `false` are not considered a match.

Fixes gh-812
parent d9bf538e
......@@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;
......@@ -42,8 +43,11 @@ class OnPropertyCondition extends SpringBootCondition {
List<String> missingProperties = new ArrayList<String>();
Environment environment = context.getEnvironment();
for (String property : onProperties) {
if (!context.getEnvironment().containsProperty(property)) {
if (!environment.containsProperty(property)
|| StringUtils.endsWithIgnoreCase(environment.getProperty(property),
"false")) {
missingProperties.add(property);
}
}
......@@ -57,5 +61,4 @@ class OnPropertyCondition extends SpringBootCondition {
+ StringUtils.arrayToCommaDelimitedString(missingProperties
.toArray()) + " not found");
}
}
......@@ -52,6 +52,22 @@ public class ConditionalOnPropertyTests {
assertFalse(this.context.containsBean("foo"));
}
@Test
public void testBeanIsNotCreatedWhenPropertyValueEqualsFalse() {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
"property1=false", "property2=value2");
setupContext();
assertFalse(this.context.containsBean("foo"));
}
@Test
public void testBeanIsNotCreatedWhenPropertyValueEqualsFALSE() {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
"property1=FALSE", "property2=value2");
setupContext();
assertFalse(this.context.containsBean("foo"));
}
private void setupContext() {
this.context.register(MultiplePropertiesRequiredConfiguration.class);
this.context.refresh();
......
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