Browse Source

Polish OnPropertyCondition

See gh-43754
pull/43773/head
Phillip Webb 11 months ago
parent
commit
4812328be2
  1. 11
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionEvaluationReport.java
  2. 10
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java
  3. 13
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBooleanPropertyTests.java
  4. 13
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java

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

@ -27,6 +27,8 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@ -237,6 +239,15 @@ public final class ConditionEvaluationReport {
return true; return true;
} }
/**
* Return a {@link Stream} of the {@link ConditionAndOutcome} items.
* @return a stream of the {@link ConditionAndOutcome} items.
* @since 3.5.0
*/
public Stream<ConditionAndOutcome> stream() {
return StreamSupport.stream(spliterator(), false);
}
@Override @Override
public Iterator<ConditionAndOutcome> iterator() { public Iterator<ConditionAndOutcome> iterator() {
return Collections.unmodifiableSet(this.outcomes).iterator(); return Collections.unmodifiableSet(this.outcomes).iterator();

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

@ -28,7 +28,6 @@ import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotationPredicates; import org.springframework.core.annotation.MergedAnnotationPredicates;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertyResolver;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
@ -51,15 +50,14 @@ class OnPropertyCondition extends SpringBootCondition {
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
MergedAnnotations annotations = metadata.getAnnotations(); List<MergedAnnotation<Annotation>> annotations = Stream
List<MergedAnnotation<Annotation>> allAnnotations = Stream .concat(metadata.getAnnotations().stream(ConditionalOnProperty.class.getName()),
.concat(annotations.stream(ConditionalOnProperty.class.getName()), metadata.getAnnotations().stream(ConditionalOnBooleanProperty.class.getName()))
annotations.stream(ConditionalOnBooleanProperty.class.getName()))
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes)) .filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes))
.toList(); .toList();
List<ConditionMessage> noMatch = new ArrayList<>(); List<ConditionMessage> noMatch = new ArrayList<>();
List<ConditionMessage> match = new ArrayList<>(); List<ConditionMessage> match = new ArrayList<>();
for (MergedAnnotation<Annotation> annotation : allAnnotations) { for (MergedAnnotation<Annotation> annotation : annotations) {
ConditionOutcome outcome = determineOutcome(annotation, context.getEnvironment()); ConditionOutcome outcome = determineOutcome(annotation, context.getEnvironment());
(outcome.isMatch() ? match : noMatch).add(outcome.getConditionMessage()); (outcome.isMatch() ? match : noMatch).add(outcome.getConditionMessage());
} }

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

@ -17,11 +17,13 @@
package org.springframework.boot.autoconfigure.condition; package org.springframework.boot.autoconfigure.condition;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@ -181,12 +183,13 @@ class ConditionalOnBooleanPropertyTests {
} }
private String getConditionEvaluationReport() { private String getConditionEvaluationReport() {
ConditionEvaluationReport report = ConditionEvaluationReport.get(this.context.getBeanFactory()); return ConditionEvaluationReport.get(this.context.getBeanFactory())
StringBuilder builder = new StringBuilder(); .getConditionAndOutcomesBySource()
report.getConditionAndOutcomesBySource()
.values() .values()
.forEach((outcomes) -> outcomes.forEach((outcome) -> builder.append(outcome.toString()).append('\n'))); .stream()
return builder.toString(); .flatMap(ConditionAndOutcomes::stream)
.map(Object::toString)
.collect(Collectors.joining("\n"));
} }
private void load(Class<?> config, String... environment) { private void load(Class<?> config, String... environment) {

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

@ -21,11 +21,13 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@ -293,12 +295,13 @@ class ConditionalOnPropertyTests {
} }
private String getConditionEvaluationReport() { private String getConditionEvaluationReport() {
ConditionEvaluationReport report = ConditionEvaluationReport.get(this.context.getBeanFactory()); return ConditionEvaluationReport.get(this.context.getBeanFactory())
StringBuilder builder = new StringBuilder(); .getConditionAndOutcomesBySource()
report.getConditionAndOutcomesBySource()
.values() .values()
.forEach((outcomes) -> outcomes.forEach((outcome) -> builder.append(outcome.toString()).append('\n'))); .stream()
return builder.toString(); .flatMap(ConditionAndOutcomes::stream)
.map(Object::toString)
.collect(Collectors.joining("\n"));
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save