From 270809783c43b9c76ea9dda9782cf56ba46bed74 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 17 Jul 2014 11:07:53 +0200 Subject: [PATCH] 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 --- .../condition/ConditionalOnProperty.java | 86 ++++++- .../condition/ConditionalOnPropertyValue.java | 80 ------ .../condition/OnPropertyCondition.java | 89 +++++-- .../condition/OnPropertyValueCondition.java | 61 ----- .../condition/PropertyHelper.java | 74 ------ .../condition/ConditionalOnPropertyTests.java | 229 ++++++++++++++++-- .../ConditionalOnPropertyValueTests.java | 196 --------------- 7 files changed, 360 insertions(+), 455 deletions(-) delete mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValue.java delete mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyValueCondition.java delete mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/PropertyHelper.java delete mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValueTests.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java index 26aa4a9ce9d..9f00db0a57e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java @@ -25,10 +25,66 @@ import org.springframework.context.annotation.Conditional; import org.springframework.core.env.Environment; /** - * {@link Conditional} that only matches when the specified properties are defined in - * {@link Environment} and not "false". + * {@link Conditional} that checks if the specified properties + * have the requested matching value. By default the properties + * must be present in the {@link Environment} ant not + * equal to {@code false}. The {@link #match()} and {@link #defaultMatch()} + * attributes allow to further customize the condition. + * + *

The {@link #match} attribute provides the value that the property + * should have. The {@link #defaultMatch()} flag specifies if the + * condition also matches if the property is not present + * at all. + * + *

The table below defines when a condition match according to the + * property value and the {@link #match()} value + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
+ * no {@code match} value{@code true}{@code false}{@code foo}
not set ({@code defaultMatch = false})nononono
not set ({@code defaultMatch = true})yesyesyesyes
{@code true}yesyesnono
{@code false}nonoyesno
{@code foo}yesnonoyes
* * @author Maciej Walkowiak + * @author Stephane Nicoll * @since 1.1.0 */ @Conditional(OnPropertyCondition.class) @@ -38,16 +94,38 @@ public @interface ConditionalOnProperty { /** * A prefix that should be applied to each property. + *

Defaults to no prefix. The prefix automatically + * ends with a dot if not specified. */ String prefix() default ""; /** - * One or more properties that must be present. If you are checking relaxed names you - * should specify the property in its dashed form. + * One or more properties to validate against the + * {@link #match} value. If a prefix has been defined, it + * is applied to compute the full key of each property. For + * instance if the prefix is {@code app.config} and one + * value is {@code my-value}, the fully key would be + * {@code app.config.my-value} + *

Use the dashed notation to specify each property, that + * is all lower case with a "-" to separate words (e.g. + * {@code my-long-property}). * @return the property names */ String[] value(); + /** + * The string representation of the expected value for the + * properties. If not specified, the property must + * not be equals to {@code false} + */ + String match() default ""; + + /** + * Specify if the condition should match if the property is not set. + * Defaults to {@code false} + */ + boolean defaultMatch() default false; + /** * If relaxed names should be checked. Defaults to {@code true}. */ diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValue.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValue.java deleted file mode 100644 index 8971b7e80d0..00000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValue.java +++ /dev/null @@ -1,80 +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 java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.springframework.context.annotation.Conditional; - -/** - * {@link Conditional} that only matches when a property - * has a given value. - * - *

If {@link #defaultMatch()} is {@code true} then the - * condition also matches if the property - * is not present at all. - * - * @author Stephane Nicoll - * @since 1.2.0 - */ -@Conditional(OnPropertyValueCondition.class) -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) -public @interface ConditionalOnPropertyValue { - - /** - * A prefix that should be applied to the property. - * Defaults to no prefix. The prefix automatically - * ends with a dot, it does not need to be added. - */ - String prefix() default ""; - - /** - * The property to check. If a prefix has been defined, it - * is applied to compute the full key of the property. For - * instance if the prefix is {@code app.config} and this - * property is {@code my-value}, the fully key would be - * {@code app.config.my-value} - *

Use the dashed notation to specify the property, that - * is all lower case with a "-" to separate words (e.g. - * {@code my-long-property}) - */ - String property(); - - /** - * If relaxed names should be checked. Defaults to {@code true}. - */ - boolean relaxedName() default true; - - /** - * The string representation of the expected value for the property. - */ - String value(); - - /** - * Specify if the condition should match if the property is not set. - * Defaults to {@code false} - *

This means that the specified {@link #value()} is actually - * the default one (i.e. the one that you expect if the property - * is not set). - */ - boolean defaultMatch() default false; - -} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java index 382599523cf..cdc8c820eb2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java @@ -20,8 +20,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.env.PropertyResolver; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.util.StringUtils; @@ -39,31 +41,86 @@ class OnPropertyCondition extends SpringBootCondition { @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { - Map attributes = metadata - .getAnnotationAttributes(ConditionalOnProperty.class.getName()); - String prefix = ((String) attributes.get("prefix")).trim(); - Boolean relaxedNames = (Boolean) attributes.get("relaxedNames"); - String[] names = (String[]) attributes.get("value"); + Map annotationAttributes = metadata.getAnnotationAttributes( + ConditionalOnProperty.class.getName()); + + String prefix = getPrefix(annotationAttributes); + String expectedValue = getExpectedValue(annotationAttributes); + String[] names = (String[]) annotationAttributes.get("value"); + boolean relaxedNames = (Boolean) annotationAttributes.get("relaxedNames"); + boolean matchDefault = (Boolean) annotationAttributes.get("defaultMatch"); - PropertyHelper helper = new PropertyHelper(context.getEnvironment(), prefix, relaxedNames); List missingProperties = new ArrayList(); - for (String name : names) { - String propertyKey = helper.createPropertyKey(name); - if (!helper.getPropertyResolver().containsProperty(propertyKey) - || "false".equalsIgnoreCase(helper.getPropertyResolver().getProperty(propertyKey))) { - missingProperties.add(propertyKey); + List nonMatchingProperties = new ArrayList(); + + PropertyResolver resolver = context.getEnvironment(); + if (relaxedNames) { + resolver = new RelaxedPropertyResolver(resolver, prefix); + prefix = ""; + } + for (String name : names) { + name = prefix + name; + boolean hasProperty = resolver.containsProperty(name); + if (!hasProperty) { // property not set + if (!matchDefault) { // property is mandatory + missingProperties.add(name); + } + } + else { + String actualValue = resolver.getProperty(name); + if (expectedValue == null) { + if ("false".equalsIgnoreCase(actualValue)) { + nonMatchingProperties.add(name); + } + } + else if (!expectedValue.equalsIgnoreCase(actualValue)) { + nonMatchingProperties.add(name); + } } } - if (missingProperties.isEmpty()) { + if (missingProperties.isEmpty() && nonMatchingProperties.isEmpty()) { return ConditionOutcome.match(); } - return ConditionOutcome.noMatch("@ConditionalOnProperty " - + "missing required properties: " - + StringUtils.arrayToCommaDelimitedString(missingProperties.toArray()) - + " not found"); + StringBuilder sb = new StringBuilder("@ConditionalOnProperty "); + if (!matchDefault && !missingProperties.isEmpty()) { + sb.append("missing required properties ") + .append(StringUtils.arrayToCommaDelimitedString(missingProperties.toArray())) + .append(" "); + } + if (!nonMatchingProperties.isEmpty()) { + String expected = expectedValue == null ? "!false" : expectedValue; + sb.append("expected '").append(expected).append("' for properties: ") + .append(StringUtils.arrayToCommaDelimitedString(nonMatchingProperties.toArray())); + } + + return ConditionOutcome.noMatch(sb.toString()); + } + + /** + * Return the prefix to use or an empty String if it's not set. + *

Add a dot at the end if it is not present already. + */ + private static String getPrefix(Map annotationAttributes) { + String prefix = ((String) annotationAttributes.get("prefix")).trim(); + if (StringUtils.hasText(prefix) && !prefix.endsWith(".")) { + prefix = prefix + "."; + } + return prefix; + } + + /** + * Return the expected value to match against or {@code null} if no + * match value is set. + */ + private static String getExpectedValue(Map annotationAttributes) { + String match = (String) annotationAttributes.get("match"); + if (StringUtils.hasText(match)) { + return match; + } + return null; } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyValueCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyValueCondition.java deleted file mode 100644 index 47d3224524e..00000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyValueCondition.java +++ /dev/null @@ -1,61 +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 java.util.Map; - -import org.springframework.context.annotation.ConditionContext; -import org.springframework.core.type.AnnotatedTypeMetadata; - -/** - * {@link org.springframework.context.annotation.Condition Condition} that - * checks if a property has a given value. Can also be configured so that - * the value to check is the default, hence the absence of property matches - * as well. - * - * @author Stephane Nicoll - * @since 1.2.0 - * @see ConditionalOnPropertyValue - */ -public class OnPropertyValueCondition extends SpringBootCondition { - - @Override - public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { - Map attributes = metadata - .getAnnotationAttributes(ConditionalOnPropertyValue.class.getName()); - String prefix = ((String) attributes.get("prefix")).trim(); - String property = (String) attributes.get("property"); - Boolean relaxedName = (Boolean) attributes.get("relaxedName"); - String value = (String) attributes.get("value"); - Boolean defaultMatch = (Boolean) attributes.get("defaultMatch"); - - PropertyHelper helper = new PropertyHelper(context.getEnvironment(), prefix, relaxedName); - String propertyKey = helper.createPropertyKey(property); - String actualValue = helper.getPropertyResolver().getProperty(propertyKey); - - if (actualValue == null && defaultMatch) { - return ConditionOutcome.match("@ConditionalOnProperty no property '" + property - + "' is set, assuming default value '" + value + "'"); - } - if (!value.equalsIgnoreCase(actualValue)) { - return ConditionOutcome.noMatch("@ConditionalOnProperty wrong value for property '" - + property + "': expected '" + value + "' but got '" + actualValue + "'"); - } - return ConditionOutcome.match(); - } - -} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/PropertyHelper.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/PropertyHelper.java deleted file mode 100644 index ab44da3a63d..00000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/PropertyHelper.java +++ /dev/null @@ -1,74 +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 org.springframework.boot.bind.RelaxedPropertyResolver; -import org.springframework.core.env.PropertyResolver; -import org.springframework.util.StringUtils; - -/** - * Helper for properties-related operations. - * - * @author Stephane Nicoll - * @since 1.2.0 - */ -class PropertyHelper { - - private final PropertyResolver propertyResolver; - - private final String prefix; - - private final boolean relaxedNames; - - /** - * Create a new instance with the base {@link PropertyResolver} to use. If - * a prefix is set and does not end with a dot, it is added. - * - * @param propertyResolver the base property resolver - * @param prefix the prefix to lookup keys, if any - * @param relaxedNames if relaxed names should be checked - */ - PropertyHelper(PropertyResolver propertyResolver, String prefix, boolean relaxedNames) { - this.prefix = cleanPrefix(prefix); - this.relaxedNames = relaxedNames; - if (relaxedNames) { - this.propertyResolver = new RelaxedPropertyResolver(propertyResolver, this.prefix); - } - else { - this.propertyResolver = propertyResolver; - } - } - - /** - * Return the {@link PropertyResolver} to use. - */ - public PropertyResolver getPropertyResolver() { - return propertyResolver; - } - - /** - * Create the full property key for the specified property. Applies the - * configured prefix, if necessary. - */ - public String createPropertyKey(String property) { - return (relaxedNames ? property : prefix + property); - } - - private static String cleanPrefix(String prefix) { - return (StringUtils.hasText(prefix) && !prefix.endsWith(".") ? prefix + "." : prefix); - } -} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java index 1599a0b7f2c..b8381c798c3 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java @@ -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"; + } + + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValueTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValueTests.java deleted file mode 100644 index da40bd39585..00000000000 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyValueTests.java +++ /dev/null @@ -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"; - } - } - -}