Skip to content

Commit

Permalink
Merge pull request spring-projects#19948 from jcordoba95
Browse files Browse the repository at this point in the history
* pr/19948:
  Polish "Fix condition source in OnBeanCondition"
  Fix condition source in OnBeanCondition

Closes spring-projectsgh-19948
  • Loading branch information
snicoll committed Feb 11, 2020
2 parents d485708 + 66809c6 commit b4d118e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -387,7 +387,7 @@ private static class Spec<A extends Annotation> {

private final ClassLoader classLoader;

private final Class<?> annotationType;
private final Class<? extends Annotation> annotationType;

private final Set<String> names;

Expand Down Expand Up @@ -581,11 +581,11 @@ Set<Class<?>> getParameterizedContainers() {
}

ConditionMessage.Builder message() {
return ConditionMessage.forCondition(ConditionalOnBean.class, this);
return ConditionMessage.forCondition(this.annotationType, this);
}

ConditionMessage.Builder message(ConditionMessage message) {
return message.andCondition(ConditionalOnBean.class, this);
return message.andCondition(this.annotationType, this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Date;
import java.util.function.Consumer;

Expand All @@ -29,6 +30,7 @@
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ConfigurableApplicationContext;
Expand Down Expand Up @@ -134,6 +136,17 @@ private void hasBarBean(AssertableApplicationContext context) {
assertThat(context.getBean("bar")).isEqualTo("bar");
}

@Test
void onBeanConditionOutputShouldNotContainConditionalOnMissingBeanClassInMessage() {
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class).run((context) -> {
Collection<ConditionAndOutcomes> conditionAndOutcomes = ConditionEvaluationReport
.get(context.getSourceApplicationContext().getBeanFactory()).getConditionAndOutcomesBySource()
.values();
String message = conditionAndOutcomes.iterator().next().iterator().next().getOutcome().getMessage();
assertThat(message).doesNotContain("@ConditionalOnMissingBean");
});
}

@Test
void conditionEvaluationConsidersChangeInTypeWhenBeanIsOverridden() {
this.contextRunner.withUserConfiguration(OriginalDefinition.class, OverridingDefinition.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand All @@ -21,6 +21,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Date;
import java.util.function.Consumer;

Expand Down Expand Up @@ -136,6 +137,17 @@ void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
});
}

@Test
void testOnMissingBeanConditionOutputShouldNotContainConditionalOnBeanClassInMessage() {
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class).run((context) -> {
Collection<ConditionEvaluationReport.ConditionAndOutcomes> conditionAndOutcomes = ConditionEvaluationReport
.get(context.getSourceApplicationContext().getBeanFactory()).getConditionAndOutcomesBySource()
.values();
String message = conditionAndOutcomes.iterator().next().iterator().next().getOutcome().getMessage();
assertThat(message).doesNotContain("@ConditionalOnBean");
});
}

@Test
void testOnMissingBeanConditionWithFactoryBean() {
this.contextRunner
Expand Down

0 comments on commit b4d118e

Please sign in to comment.