diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java index 4422b97548..4d0d043b44 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java @@ -162,25 +162,65 @@ abstract class AbstractNestedCondition extends SpringBootCondition for (Map.Entry> entry : this.memberConditions .entrySet()) { AnnotationMetadata metadata = entry.getKey(); - for (Condition condition : entry.getValue()) { - outcomes.add(getConditionOutcome(metadata, condition)); - } + List conditions = entry.getValue(); + outcomes.add(new MemberOutcomes(this.context, metadata, conditions) + .getUltimateOutcome()); } return Collections.unmodifiableList(outcomes); } + } + + private static class MemberOutcomes { + + private final ConditionContext context; + + private final AnnotationMetadata metadata; + + private final List outcomes; + + MemberOutcomes(ConditionContext context, AnnotationMetadata metadata, + List conditions) { + this.context = context; + this.metadata = metadata; + this.outcomes = new ArrayList(conditions.size()); + for (Condition condition : conditions) { + this.outcomes.add(getConditionOutcome(metadata, condition)); + } + } + private ConditionOutcome getConditionOutcome(AnnotationMetadata metadata, Condition condition) { - String messagePrefix = "member condition on " + metadata.getClassName(); if (condition instanceof SpringBootCondition) { - ConditionOutcome outcome = ((SpringBootCondition) condition) - .getMatchOutcome(this.context, metadata); - String message = outcome.getMessage(); - return new ConditionOutcome(outcome.isMatch(), messagePrefix - + (StringUtils.hasLength(message) ? " : " + message : "")); + return ((SpringBootCondition) condition).getMatchOutcome(this.context, + metadata); + } + return new ConditionOutcome(condition.matches(this.context, metadata), null); + } + + public ConditionOutcome getUltimateOutcome() { + if (this.outcomes.size() == 1) { + ConditionOutcome outcome = this.outcomes.get(0); + StringBuilder message = new StringBuilder( + "member condition on " + this.metadata.getClassName()); + if (StringUtils.hasLength(outcome.getMessage())) { + message.append(" " + outcome.getMessage()); + } + return new ConditionOutcome(outcome.isMatch(), message.toString()); + } + StringBuilder message = new StringBuilder( + "member conditions on " + this.metadata.getClassName()); + boolean match = true; + boolean hasMessage = false; + for (ConditionOutcome outcome : this.outcomes) { + match &= outcome.isMatch(); + if (StringUtils.hasLength(outcome.getMessage())) { + message.append(hasMessage ? ", " : " : "); + message.append(outcome.getMessage()); + hasMessage = true; + } } - boolean matches = condition.matches(this.context, metadata); - return new ConditionOutcome(matches, messagePrefix); + return new ConditionOutcome(match, message.toString()); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java index 69e8fa86bf..07ff223fb4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java @@ -31,12 +31,13 @@ import static org.junit.Assert.assertThat; * Tests for {@link AnyNestedCondition}. * * @author Phillip Webb + * @author Dave Syer */ public class AnyNestedConditionTests { @Test public void neither() throws Exception { - AnnotationConfigApplicationContext context = load(OnPropertyAorBCondition.class); + AnnotationConfigApplicationContext context = load(Config.class); assertThat(context.containsBean("myBean"), equalTo(false)); context.close(); } @@ -92,6 +93,7 @@ public class AnyNestedConditionTests { } + @ConditionalOnExpression("true") @ConditionalOnProperty("b") static class HasPropertyB {