ConditionalOnBooleanPropertyTests Class — spring-boot Architecture
Architecture documentation for the ConditionalOnBooleanPropertyTests class in ConditionalOnBooleanPropertyTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBooleanPropertyTests.java lines 44–304
class ConditionalOnBooleanPropertyTests {
private @Nullable ConfigurableApplicationContext context;
private final ConfigurableEnvironment environment = new StandardEnvironment();
@AfterEach
void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
void defaultsWhenTrue() {
load(Defaults.class, "test=true");
assertThat(containsBean()).isTrue();
}
@Test
void defaultsWhenFalse() {
load(Defaults.class, "test=false");
assertThat(containsBean()).isFalse();
}
@Test
void defaultsWhenMissing() {
load(Defaults.class);
assertThat(containsBean()).isFalse();
}
@Test
void havingValueTrueMatchIfMissingFalseWhenTrue() {
load(HavingValueTrueMatchIfMissingFalse.class, "test=true");
assertThat(containsBean()).isTrue();
}
@Test
void havingValueTrueMatchIfMissingFalseWhenFalse() {
load(HavingValueTrueMatchIfMissingFalse.class, "test=false");
assertThat(containsBean()).isFalse();
}
@Test
void havingValueTrueMatchIfMissingFalseWhenMissing() {
load(HavingValueTrueMatchIfMissingFalse.class);
assertThat(containsBean()).isFalse();
}
@Test
void havingValueTrueMatchIfMissingTrueWhenTrue() {
load(HavingValueTrueMatchIfMissingTrue.class, "test=true");
assertThat(containsBean()).isTrue();
}
@Test
void havingValueTrueMatchIfMissingTrueWhenFalse() {
load(HavingValueTrueMatchIfMissingTrue.class, "test=false");
assertThat(containsBean()).isFalse();
}
@Test
void havingValueTrueMatchIfMissingTrueWhenMissing() {
load(HavingValueTrueMatchIfMissingTrue.class);
assertThat(containsBean()).isTrue();
}
@Test
void havingValueFalseMatchIfMissingFalseWhenTrue() {
load(HavingValueFalseMatchIfMissingFalse.class, "test=true");
assertThat(containsBean()).isFalse();
}
@Test
void havingValueFalseMatchIfMissingFalseWhenFalse() {
load(HavingValueFalseMatchIfMissingFalse.class, "test=false");
assertThat(containsBean()).isTrue();
}
@Test
void havingValueFalseMatchIfMissingFalseWhenMissing() {
load(HavingValueFalseMatchIfMissingFalse.class);
assertThat(containsBean()).isFalse();
}
@Test
void havingValueFalseMatchIfMissingTrueWhenTrue() {
load(HavingValueFalseMatchIfMissingTrue.class, "test=true");
assertThat(containsBean()).isFalse();
}
@Test
void havingValueFalseMatchIfMissingTrueWhenFalse() {
load(HavingValueFalseMatchIfMissingTrue.class, "test=false");
assertThat(containsBean()).isTrue();
}
@Test
void havingValueFalseMatchIfMissingTrueWhenMissing() {
load(HavingValueFalseMatchIfMissingTrue.class);
assertThat(containsBean()).isTrue();
}
@Test
void withPrefix() {
load(HavingValueFalseMatchIfMissingTrue.class, "foo.test=true");
assertThat(containsBean()).isTrue();
}
@Test
void nameOrValueMustBeSpecified() {
assertThatIllegalStateException().isThrownBy(() -> load(NoNameOrValueAttribute.class, "some.property"))
.satisfies(causeMessageContaining(
"The name or value attribute of @ConditionalOnBooleanProperty must be specified"));
}
@Test
void nameAndValueMustNotBeSpecified() {
assertThatIllegalStateException().isThrownBy(() -> load(NameAndValueAttribute.class, "some.property"))
.satisfies(causeMessageContaining(
"The name and value attributes of @ConditionalOnBooleanProperty are exclusive"));
}
@Test
void conditionReportWhenMatched() {
load(Defaults.class, "test=true");
assertThat(containsBean()).isTrue();
assertThat(getConditionEvaluationReport()).contains("@ConditionalOnBooleanProperty (test=true) matched");
}
@Test
void conditionReportWhenDoesNotMatch() {
load(Defaults.class, "test=false");
assertThat(containsBean()).isFalse();
assertThat(getConditionEvaluationReport())
.contains("@ConditionalOnBooleanProperty (test=true) found different value in property 'test'");
}
@Test
void repeatablePropertiesConditionReportWhenMatched() {
load(RepeatablePropertiesRequiredConfiguration.class, "property1=true", "property2=true");
assertThat(containsBean()).isTrue();
String report = getConditionEvaluationReport();
assertThat(report).contains("@ConditionalOnBooleanProperty (property1=true) matched");
assertThat(report).contains("@ConditionalOnBooleanProperty (property2=true) matched");
}
@Test
void repeatablePropertiesConditionReportWhenDoesNotMatch() {
load(RepeatablePropertiesRequiredConfiguration.class, "property1=true");
assertThat(getConditionEvaluationReport())
.contains("@ConditionalOnBooleanProperty (property2=true) did not find property 'property2'");
}
private <T extends Exception> Consumer<T> causeMessageContaining(String message) {
return (ex) -> assertThat(ex.getCause()).hasMessageContaining(message);
}
private String getConditionEvaluationReport() {
assertThat(this.context).isNotNull();
return ConditionEvaluationReport.get(this.context.getBeanFactory())
.getConditionAndOutcomesBySource()
.values()
.stream()
.flatMap(ConditionAndOutcomes::stream)
.map(Object::toString)
.collect(Collectors.joining("\n"));
}
private void load(Class<?> config, String... environment) {
TestPropertyValues.of(environment).applyTo(this.environment);
this.context = new SpringApplicationBuilder(config).environment(this.environment)
.web(WebApplicationType.NONE)
.run();
}
private boolean containsBean() {
assertThat(this.context).isNotNull();
return this.context.containsBean("foo");
}
abstract static class BeanConfiguration {
@Bean
String foo() {
return "foo";
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty("test")
static class Defaults extends BeanConfiguration {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(name = "test", havingValue = true, matchIfMissing = false)
static class HavingValueTrueMatchIfMissingFalse extends BeanConfiguration {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(name = "test", havingValue = true, matchIfMissing = true)
static class HavingValueTrueMatchIfMissingTrue extends BeanConfiguration {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(name = "test", havingValue = false, matchIfMissing = false)
static class HavingValueFalseMatchIfMissingFalse extends BeanConfiguration {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(name = "test", havingValue = false, matchIfMissing = true)
static class HavingValueFalseMatchIfMissingTrue extends BeanConfiguration {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(prefix = "foo", name = "test")
static class WithPrefix extends BeanConfiguration {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty
static class NoNameOrValueAttribute {
@Bean
String foo() {
return "foo";
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(value = "x", name = "y")
static class NameAndValueAttribute {
@Bean
String foo() {
return "foo";
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty("property1")
@ConditionalOnBooleanProperty("property2")
static class RepeatablePropertiesRequiredConfiguration {
@Bean
String foo() {
return "foo";
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free