Skip to content

Commit b4d118e

Browse files
committed
Merge pull request #19948 from jcordoba95
* pr/19948: Polish "Fix condition source in OnBeanCondition" Fix condition source in OnBeanCondition Closes gh-19948
2 parents d485708 + 66809c6 commit b4d118e

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -387,7 +387,7 @@ private static class Spec<A extends Annotation> {
387387

388388
private final ClassLoader classLoader;
389389

390-
private final Class<?> annotationType;
390+
private final Class<? extends Annotation> annotationType;
391391

392392
private final Set<String> names;
393393

@@ -581,11 +581,11 @@ Set<Class<?>> getParameterizedContainers() {
581581
}
582582

583583
ConditionMessage.Builder message() {
584-
return ConditionMessage.forCondition(ConditionalOnBean.class, this);
584+
return ConditionMessage.forCondition(this.annotationType, this);
585585
}
586586

587587
ConditionMessage.Builder message(ConditionMessage message) {
588-
return message.andCondition(ConditionalOnBean.class, this);
588+
return message.andCondition(this.annotationType, this);
589589
}
590590

591591
@Override

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.util.Collection;
2425
import java.util.Date;
2526
import java.util.function.Consumer;
2627

@@ -29,6 +30,7 @@
2930
import org.springframework.beans.factory.FactoryBean;
3031
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3132
import org.springframework.beans.factory.support.RootBeanDefinition;
33+
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
3234
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3335
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3436
import org.springframework.context.ConfigurableApplicationContext;
@@ -134,6 +136,17 @@ private void hasBarBean(AssertableApplicationContext context) {
134136
assertThat(context.getBean("bar")).isEqualTo("bar");
135137
}
136138

139+
@Test
140+
void onBeanConditionOutputShouldNotContainConditionalOnMissingBeanClassInMessage() {
141+
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class).run((context) -> {
142+
Collection<ConditionAndOutcomes> conditionAndOutcomes = ConditionEvaluationReport
143+
.get(context.getSourceApplicationContext().getBeanFactory()).getConditionAndOutcomesBySource()
144+
.values();
145+
String message = conditionAndOutcomes.iterator().next().iterator().next().getOutcome().getMessage();
146+
assertThat(message).doesNotContain("@ConditionalOnMissingBean");
147+
});
148+
}
149+
137150
@Test
138151
void conditionEvaluationConsidersChangeInTypeWhenBeanIsOverridden() {
139152
this.contextRunner.withUserConfiguration(OriginalDefinition.class, OverridingDefinition.class,

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.util.Collection;
2425
import java.util.Date;
2526
import java.util.function.Consumer;
2627

@@ -136,6 +137,17 @@ void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
136137
});
137138
}
138139

140+
@Test
141+
void testOnMissingBeanConditionOutputShouldNotContainConditionalOnBeanClassInMessage() {
142+
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class).run((context) -> {
143+
Collection<ConditionEvaluationReport.ConditionAndOutcomes> conditionAndOutcomes = ConditionEvaluationReport
144+
.get(context.getSourceApplicationContext().getBeanFactory()).getConditionAndOutcomesBySource()
145+
.values();
146+
String message = conditionAndOutcomes.iterator().next().iterator().next().getOutcome().getMessage();
147+
assertThat(message).doesNotContain("@ConditionalOnBean");
148+
});
149+
}
150+
139151
@Test
140152
void testOnMissingBeanConditionWithFactoryBean() {
141153
this.contextRunner

0 commit comments

Comments
 (0)