Merge @ConditionalOnPropertyValue

This commit merges the features of @ConditionalOnPropertyValue
to the existing @ConditionalOnProperty.

The "match" attribute provides the value to match against. By default,
the value should not be equal to "false" which is the existing default
of @ConditionalOnProperty. "defaultMatch" specifies if the value
should be present. The default matches also the existing behavior of
@ConditionalOnProperty.

Fixes gh-1000
This commit is contained in:
Stephane Nicoll
2014-07-17 11:07:53 +02:00
parent fc9b160a72
commit 270809783c
7 changed files with 360 additions and 455 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.condition;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -29,75 +30,178 @@ import static org.junit.Assert.assertTrue;
* Tests for {@link ConditionalOnProperty}.
*
* @author Maciej Walkowiak
* @author Stephane Nicoll
*/
public class ConditionalOnPropertyTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private AnnotationConfigApplicationContext context;
@After
public void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void allPropertiesAreDefined() {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
load(MultiplePropertiesRequiredConfiguration.class,
"property1=value1", "property2=value2");
this.context.register(MultiplePropertiesRequiredConfiguration.class);
this.context.refresh();
assertTrue(this.context.containsBean("foo"));
}
@Test
public void notAllPropertiesAreDefined() {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
load(MultiplePropertiesRequiredConfiguration.class,
"property1=value1");
this.context.register(MultiplePropertiesRequiredConfiguration.class);
this.context.refresh();
assertFalse(this.context.containsBean("foo"));
}
@Test
public void propertyValueEqualsFalse() {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
load(MultiplePropertiesRequiredConfiguration.class,
"property1=false", "property2=value2");
this.context.register(MultiplePropertiesRequiredConfiguration.class);
this.context.refresh();
assertFalse(this.context.containsBean("foo"));
}
@Test
public void propertyValueEqualsFALSE() {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
load(MultiplePropertiesRequiredConfiguration.class,
"property1=FALSE", "property2=value2");
this.context.register(MultiplePropertiesRequiredConfiguration.class);
this.context.refresh();
assertFalse(this.context.containsBean("foo"));
}
@Test
public void relaxedName() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
public void relaxedName() {
load(RelaxedPropertiesRequiredConfiguration.class,
"spring.theRelaxedProperty=value1");
this.context.register(RelaxedPropertiesRequiredConfiguration.class);
this.context.refresh();
assertTrue(this.context.containsBean("foo"));
}
@Test
public void prefixWithoutPeriod() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
load(RelaxedPropertiesRequiredConfigurationWithShortPrefix.class,
"spring.property=value1");
this.context
.register(RelaxedPropertiesRequiredConfigurationWithShortPrefix.class);
this.context.refresh();
assertTrue(this.context.containsBean("foo"));
}
@Test
public void nonRelaxedName() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
load(NonRelaxedPropertiesRequiredConfiguration.class,
"theRelaxedProperty=value1");
this.context.register(NonRelaxedPropertiesRequiredConfiguration.class);
this.context.refresh();
assertFalse(this.context.containsBean("foo"));
}
@Test // Enabled by default
public void enabledIfNotConfiguredOtherwise() {
load(EnabledIfNotConfiguredOtherwiseConfig.class);
assertTrue(this.context.containsBean("foo"));
}
@Test
public void enabledIfNotConfiguredOtherwiseWithConfig() {
load(EnabledIfNotConfiguredOtherwiseConfig.class, "simple.myProperty:false");
assertFalse(this.context.containsBean("foo"));
}
@Test
public void enabledIfNotConfiguredOtherwiseWithConfigDifferentCase() {
load(EnabledIfNotConfiguredOtherwiseConfig.class, "simple.my-property:FALSE");
assertFalse(this.context.containsBean("foo"));
}
@Test // Disabled by default
public void disableIfNotConfiguredOtherwise() {
load(DisabledIfNotConfiguredOtherwiseConfig.class);
assertFalse(this.context.containsBean("foo"));
}
@Test
public void disableIfNotConfiguredOtherwiseWithConfig() {
load(DisabledIfNotConfiguredOtherwiseConfig.class, "simple.myProperty:true");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void disableIfNotConfiguredOtherwiseWithConfigDifferentCase() {
load(DisabledIfNotConfiguredOtherwiseConfig.class, "simple.myproperty:TrUe");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void simpleValueIsSet() {
load(SimpleValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void caseInsensitive() {
load(SimpleValueConfig.class, "simple.myProperty:BaR");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void defaultValueIsSet() {
load(DefaultValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void defaultValueIsNotSet() {
load(DefaultValueConfig.class);
assertTrue(this.context.containsBean("foo"));
}
@Test
public void defaultValueIsSetDifferentValue() {
load(DefaultValueConfig.class, "simple.myProperty:another");
assertFalse(this.context.containsBean("foo"));
}
@Test
public void prefix() {
load(PrefixValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void relaxedEnabledByDefault() {
load(PrefixValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void strictNameMatch() {
load(StrictNameConfig.class, "simple.my-property:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void strictNameNoMatch() {
load(StrictNameConfig.class, "simple.myProperty:bar");
assertFalse(this.context.containsBean("foo"));
}
@Test
public void multiValuesAllSet() {
load(MultiValuesConfig.class, "simple.my-property:bar", "simple.my-another-property:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void multiValuesOnlyOneSet() {
load(MultiValuesConfig.class, "simple.my-property:bar");
assertFalse(this.context.containsBean("foo"));
}
private void load(Class<?> config, String... environment) {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment);
this.context.register(config);
this.context.refresh();
}
@Configuration
@ConditionalOnProperty({ "property1", "property2" })
protected static class MultiplePropertiesRequiredConfiguration {
@@ -142,4 +246,81 @@ public class ConditionalOnPropertyTests {
}
@Configuration // ${simple.myProperty:true}
@ConditionalOnProperty(prefix = "simple", value = "my-property", match = "true", defaultMatch = true)
static class EnabledIfNotConfiguredOtherwiseConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration // ${simple.myProperty:false}
@ConditionalOnProperty(prefix = "simple", value = "my-property", match = "true", defaultMatch = false)
static class DisabledIfNotConfiguredOtherwiseConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnProperty(prefix = "simple", value = "my-property", match = "bar")
static class SimpleValueConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnProperty(value = "simple.myProperty", match = "bar", defaultMatch = true)
static class DefaultValueConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnProperty(prefix = "simple", value = "my-property", match = "bar")
static class PrefixValueConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnProperty(prefix = "simple", value = "my-property", match = "bar", relaxedNames = false)
static class StrictNameConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnProperty(prefix = "simple", value = {"my-property", "my-another-property"}, match = "bar")
static class MultiValuesConfig {
@Bean
public String foo() {
return "foo";
}
}
}

View File

@@ -1,196 +0,0 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.condition;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Tests for {@link ConditionalOnPropertyValue}
*
* @author Stephane Nicoll
*/
public class ConditionalOnPropertyValueTests {
private AnnotationConfigApplicationContext context;
@After
public void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test // Enabled by default
public void enabledIfNotConfiguredOtherwise() {
load(EnabledIfNotConfiguredOtherwiseConfig.class);
assertTrue(this.context.containsBean("foo"));
}
@Test
public void enabledIfNotConfiguredOtherwiseWithConfig() {
load(EnabledIfNotConfiguredOtherwiseConfig.class, "simple.myProperty:false");
assertFalse(this.context.containsBean("foo"));
}
@Test
public void enabledIfNotConfiguredOtherwiseWithConfigDifferentCase() {
load(EnabledIfNotConfiguredOtherwiseConfig.class, "simple.my-property:FALSE");
assertFalse(this.context.containsBean("foo"));
}
@Test // Disabled by default
public void disableIfNotConfiguredOtherwise() {
load(DisabledIfNotConfiguredOtherwiseConfig.class);
assertFalse(this.context.containsBean("foo"));
}
@Test
public void disableIfNotConfiguredOtherwiseWithConfig() {
load(DisabledIfNotConfiguredOtherwiseConfig.class, "simple.myProperty:true");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void disableIfNotConfiguredOtherwiseWithConfigDifferentCase() {
load(DisabledIfNotConfiguredOtherwiseConfig.class, "simple.myproperty:TrUe");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void simpleValueIsSet() {
load(SimpleValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void caseInsensitive() {
load(SimpleValueConfig.class, "simple.myProperty:BaR");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void defaultValueIsSet() {
load(DefaultValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void defaultValueIsNotSet() {
load(DefaultValueConfig.class);
assertTrue(this.context.containsBean("foo"));
}
@Test
public void defaultValueIsSetDifferentValue() {
load(DefaultValueConfig.class, "simple.myProperty:another");
assertFalse(this.context.containsBean("foo"));
}
@Test
public void prefix() {
load(PrefixValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void relaxedEnabledByDefault() {
load(PrefixValueConfig.class, "simple.myProperty:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void strictNameMatch() {
load(StrictNameConfig.class, "simple.my-property:bar");
assertTrue(this.context.containsBean("foo"));
}
@Test
public void strictNameNoMatch() {
load(StrictNameConfig.class, "simple.myProperty:bar");
assertFalse(this.context.containsBean("foo"));
}
private void load(Class<?> config, String... environment) {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment);
this.context.register(config);
this.context.refresh();
}
@Configuration // ${simple.myProperty:true}
@ConditionalOnPropertyValue(prefix = "simple", property = "my-property", value = "true", defaultMatch = true)
static class EnabledIfNotConfiguredOtherwiseConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration // ${simple.myProperty:false}
@ConditionalOnPropertyValue(prefix = "simple", property = "my-property", value = "true", defaultMatch = false)
static class DisabledIfNotConfiguredOtherwiseConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnPropertyValue(prefix = "simple", property = "my-property", value = "bar")
static class SimpleValueConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnPropertyValue(property = "simple.myProperty", value = "bar", defaultMatch = true)
static class DefaultValueConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnPropertyValue(prefix = "simple", property = "my-property", value = "bar")
static class PrefixValueConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnPropertyValue(prefix = "simple", property = "my-property", value = "bar", relaxedName = false)
static class StrictNameConfig {
@Bean
public String foo() {
return "foo";
}
}
}