parent
9e571a2827
commit
bbad720e23
|
|
@ -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<AnnotationMetadata, List<Condition>> 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<AnnotationMetadata, List<Condition>> getMemberConditions(
|
||||
String[] members) {
|
||||
String[] members, ConfigurationPhase phase, String className) {
|
||||
MultiValueMap<AnnotationMetadata, Condition> 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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
Loading…
Reference in New Issue