From bbad720e2343997922a12f061289241f543c0fee Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 3 Dec 2018 14:12:30 +0100 Subject: [PATCH] Polish contribution Closes gh-15086 --- .../condition/AbstractNestedCondition.java | 23 ++++++++----------- ...java => AbstractNestedConditionTests.java} | 16 ++++--------- 2 files changed, 13 insertions(+), 26 deletions(-) rename spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/{AbstractNestedConditionTest.java => AbstractNestedConditionTests.java} (88%) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java index 6c1f6d0dde..da6fa3aa5b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java @@ -109,32 +109,26 @@ public abstract class AbstractNestedCondition extends SpringBootCondition private final MetadataReaderFactory readerFactory; - private final ConfigurationPhase nestedPhase; - - private final String nestedClassName; - private final Map> memberConditions; - MemberConditions(ConditionContext context, ConfigurationPhase nestedPhase, + MemberConditions(ConditionContext context, ConfigurationPhase phase, String className) { this.context = context; this.readerFactory = new SimpleMetadataReaderFactory( context.getResourceLoader()); - this.nestedPhase = nestedPhase; - this.nestedClassName = className; String[] members = getMetadata(className).getMemberClassNames(); - this.memberConditions = getMemberConditions(members); + this.memberConditions = getMemberConditions(members, phase, className); } private Map> getMemberConditions( - String[] members) { + String[] members, ConfigurationPhase phase, String className) { MultiValueMap memberConditions = new LinkedMultiValueMap<>(); for (String member : members) { AnnotationMetadata metadata = getMetadata(member); for (String[] conditionClasses : getConditionClasses(metadata)) { for (String conditionClass : conditionClasses) { Condition condition = getCondition(conditionClass); - validateMemberCondition(condition); + validateMemberCondition(condition, phase, className); memberConditions.add(metadata, condition); } } @@ -142,14 +136,15 @@ public abstract class AbstractNestedCondition extends SpringBootCondition return Collections.unmodifiableMap(memberConditions); } - private void validateMemberCondition(Condition condition) { - if (this.nestedPhase == ConfigurationPhase.PARSE_CONFIGURATION + private void validateMemberCondition(Condition condition, + ConfigurationPhase nestedPhase, String nestedClassName) { + if (nestedPhase == ConfigurationPhase.PARSE_CONFIGURATION && condition instanceof ConfigurationCondition) { ConfigurationPhase memberPhase = ((ConfigurationCondition) condition) .getConfigurationPhase(); if (memberPhase == ConfigurationPhase.REGISTER_BEAN) { - throw new IllegalStateException("Nested condition " - + this.nestedClassName + " uses a configuration " + throw new IllegalStateException("Nested condition " + nestedClassName + + " uses a configuration " + "phase that is inappropriate for " + condition.getClass()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AbstractNestedConditionTest.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AbstractNestedConditionTests.java similarity index 88% rename from spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AbstractNestedConditionTest.java rename to spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AbstractNestedConditionTests.java index b0aa1cda72..98f15a0832 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AbstractNestedConditionTest.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AbstractNestedConditionTests.java @@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.condition; import org.junit.Test; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @@ -31,18 +30,18 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Razib Shahriar */ -public class AbstractNestedConditionTest { +public class AbstractNestedConditionTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); @Test - public void validMemberPhaseEvaluatesCorrectly() { + public void validPhase() { this.contextRunner.withUserConfiguration(ValidConfig.class) .run((context) -> assertThat(context).hasBean("myBean")); } @Test - public void invalidMemberPhaseThrowsIllegalState() { + public void invalidMemberPhase() { this.contextRunner.withUserConfiguration(InvalidConfig.class).run((context) -> { assertThat(context).hasFailed(); assertThat(context.getStartupFailure().getCause()) @@ -55,7 +54,7 @@ public class AbstractNestedConditionTest { } @Test - public void invalidNestedMemberPhaseThrowsIllegalState() { + public void invalidNestedMemberPhase() { this.contextRunner.withUserConfiguration(DoubleNestedConfig.class) .run((context) -> { assertThat(context).hasFailed(); @@ -68,13 +67,6 @@ public class AbstractNestedConditionTest { }); } - private AnnotationConfigApplicationContext load(Class config) { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - context.register(config); - context.refresh(); - return context; - } - @Configuration @Conditional(ValidNestedCondition.class) public static class ValidConfig {