diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java index 4baf9d22c14..74670b8aaee 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java @@ -195,7 +195,7 @@ abstract class AbstractNestedCondition extends SpringBootCondition metadata); } return new ConditionOutcome(condition.matches(this.context, metadata), - (ConditionMessage) null); + ConditionMessage.empty()); } public ConditionOutcome getUltimateOutcome() { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsCondition.java index e38ff4b7656..b78cc05fdb1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsCondition.java @@ -28,6 +28,8 @@ import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.bind.RelaxedNames; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySources; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.validation.DataBinder; @@ -41,20 +43,19 @@ class OnBootstrapHostsCondition extends SpringBootCondition { @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { - ConfigurableEnvironment environment = (ConfigurableEnvironment) context - .getEnvironment(); - PropertyResolver resolver = new PropertyResolver(environment, "spring.couchbase"); + Environment environment = context.getEnvironment(); + PropertyResolver resolver = new PropertyResolver( + ((ConfigurableEnvironment) environment).getPropertySources(), + "spring.couchbase"); Map.Entry entry = resolver.resolveProperty("bootstrap-hosts"); if (entry != null) { - return ConditionOutcome.match( - ConditionMessage.forCondition(OnBootstrapHostsCondition.class.getName()) - .found("property").items("spring.couchbase.bootstrap-hosts")); - } - else { - return ConditionOutcome.noMatch( - ConditionMessage.forCondition(OnBootstrapHostsCondition.class.getName()) - .didNotFind("property").items("spring.couchbase.bootstrap-hosts")); + return ConditionOutcome.match(ConditionMessage + .forCondition(OnBootstrapHostsCondition.class.getName()) + .found("property").items("spring.couchbase.bootstrap-hosts")); } + return ConditionOutcome.noMatch(ConditionMessage + .forCondition(OnBootstrapHostsCondition.class.getName()) + .didNotFind("property").items("spring.couchbase.bootstrap-hosts")); } private static class PropertyResolver { @@ -63,12 +64,11 @@ class OnBootstrapHostsCondition extends SpringBootCondition { private final Map content; - PropertyResolver(ConfigurableEnvironment environment, String prefix) { + PropertyResolver(PropertySources propertySources, String prefix) { this.prefix = prefix; this.content = new HashMap(); DataBinder binder = new RelaxedDataBinder(this.content, this.prefix); - binder.bind(new PropertySourcesPropertyValues( - environment.getPropertySources())); + binder.bind(new PropertySourcesPropertyValues(propertySources)); } Map.Entry resolveProperty(String name) { @@ -89,5 +89,3 @@ class OnBootstrapHostsCondition extends SpringBootCondition { } } - - diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java index 857228f2b46..eb7f995fb0c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java @@ -21,8 +21,11 @@ import org.junit.Test; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.AnnotatedTypeMetadata; import static org.assertj.core.api.Assertions.assertThat; @@ -94,6 +97,20 @@ public class AllNestedConditionsTests { } + @Conditional(NonSpringBootCondition.class) + static class SubclassC { + + } + + } + + static class NonSpringBootCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return true; + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java index f60defcb988..a11b93ac3c4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java @@ -21,8 +21,11 @@ import org.junit.Test; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.AnnotatedTypeMetadata; import static org.assertj.core.api.Assertions.assertThat; @@ -98,6 +101,20 @@ public class AnyNestedConditionTests { } + @Conditional(NonSpringBootCondition.class) + static class SubclassC { + + } + + } + + static class NonSpringBootCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return false; + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java index b94e7167a57..3d584834ad3 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java @@ -21,8 +21,11 @@ import org.junit.Test; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.AnnotatedTypeMetadata; import static org.assertj.core.api.Assertions.assertThat; @@ -94,6 +97,20 @@ public class NoneNestedConditionsTests { } + @Conditional(NonSpringBootCondition.class) + static class SubClassC { + + } + + } + + static class NonSpringBootCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return false; + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsConditionTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsConditionTests.java index a185d0d8e8e..5f0db8ddf73 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsConditionTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsConditionTests.java @@ -73,7 +73,6 @@ public class OnBootstrapHostsConditionTests { assertThat(this.context.containsBean("foo")).isTrue(); } - private void load(Class config, String... environment) { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, environment); diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java index 0b0000039bb..61ddf064830 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java @@ -276,45 +276,55 @@ class ImportsContextCustomizer implements ContextCustomizer { return (obj != null && getClass().equals(obj.getClass()) && this.annotations.equals(((ContextCustomizerKey) obj).annotations)); } + } - private interface AnnotationFilter { + /** + * Filter used to limit considered annotations. + */ + private interface AnnotationFilter { - boolean isIgnored(Annotation annotation); + boolean isIgnored(Annotation annotation); + } + + /** + * {@link AnnotationFilter} for {@literal java.lang} annotations. + */ + private static final class JavaLangAnnotationFilter implements AnnotationFilter { + + @Override + public boolean isIgnored(Annotation annotation) { + return AnnotationUtils.isInJavaLangAnnotationPackage(annotation); } - private static final class JavaLangAnnotationFilter implements AnnotationFilter { + } - @Override - public boolean isIgnored(Annotation annotation) { - return AnnotationUtils.isInJavaLangAnnotationPackage(annotation); - } + /** + * {@link AnnotationFilter} for Kotlin annotations. + */ + private static final class KotlinAnnotationFilter implements AnnotationFilter { + @Override + public boolean isIgnored(Annotation annotation) { + return "kotlin.Metadata".equals(annotation.annotationType().getName()) + || isInKotlinAnnotationPackage(annotation); } - private static final class KotlinAnnotationFilter implements AnnotationFilter { - - @Override - public boolean isIgnored(Annotation annotation) { - return "kotlin.Metadata".equals(annotation.annotationType().getName()) - || isInKotlinAnnotationPackage(annotation); - } - - private boolean isInKotlinAnnotationPackage(Annotation annotation) { - return annotation.annotationType().getName() - .startsWith("kotlin.annotation."); - } - + private boolean isInKotlinAnnotationPackage(Annotation annotation) { + return annotation.annotationType().getName().startsWith("kotlin.annotation."); } - private static final class SpockAnnotationFilter implements AnnotationFilter { + } - @Override - public boolean isIgnored(Annotation annotation) { - return annotation.annotationType().getName() - .startsWith("org.spockframework."); - } + /** + * {@link AnnotationFilter} for Spock annotations. + */ + private static final class SpockAnnotationFilter implements AnnotationFilter { + @Override + public boolean isIgnored(Annotation annotation) { + return annotation.annotationType().getName() + .startsWith("org.spockframework."); } } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java index df636e2120e..c2e3fb81499 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java @@ -128,10 +128,10 @@ public class ArtifactsLibrariesTests { this.libs = new ArtifactsLibraries(this.artifacts, null, mock(Log.class)); this.libs.doWithLibraries(this.callback); verify(this.callback, times(2)).library(this.libraryCaptor.capture()); - assertThat(this.libraryCaptor.getAllValues().get(0).getName()).isEqualTo( - "g1-artifact-1.0.jar"); - assertThat(this.libraryCaptor.getAllValues().get(1).getName()).isEqualTo( - "g2-artifact-1.0.jar"); + assertThat(this.libraryCaptor.getAllValues().get(0).getName()) + .isEqualTo("g1-artifact-1.0.jar"); + assertThat(this.libraryCaptor.getAllValues().get(1).getName()) + .isEqualTo("g2-artifact-1.0.jar"); } }