From 0f325f98b5f0ad07d404a41cc172ad33879a8ecf Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 20 Sep 2022 04:18:04 +0200 Subject: [PATCH] Disable ContextCustomizer implementations at runtime if necessary This commit disables ContextCustomizer implementations that tune a test configuration context at runtime. Previously, these ran again and required additional hints to work properly. Rather than contributing those hints, the customizer is skipped as its impact is irrelevant in an AOT-optimized context: the context is fully prepared and the updates on the MergedContextConfiguration are not taken into account. Closes gh-32422 --- .../OverrideAutoConfigurationContextCustomizerFactory.java | 6 +++++- .../filter/TypeExcludeFiltersContextCustomizerFactory.java | 6 +++++- .../boot/test/context/ImportsContextCustomizerFactory.java | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideAutoConfigurationContextCustomizerFactory.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideAutoConfigurationContextCustomizerFactory.java index cd66fcbd612..dd62a2d7705 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideAutoConfigurationContextCustomizerFactory.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideAutoConfigurationContextCustomizerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 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. @@ -18,6 +18,7 @@ package org.springframework.boot.test.autoconfigure; import java.util.List; +import org.springframework.aot.AotDetector; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ConfigurableApplicationContext; @@ -38,6 +39,9 @@ class OverrideAutoConfigurationContextCustomizerFactory implements ContextCustom @Override public ContextCustomizer createContextCustomizer(Class testClass, List configurationAttributes) { + if (AotDetector.useGeneratedArtifacts()) { + return null; + } OverrideAutoConfiguration overrideAutoConfiguration = TestContextAnnotationUtils.findMergedAnnotation(testClass, OverrideAutoConfiguration.class); boolean enabled = (overrideAutoConfiguration != null) ? overrideAutoConfiguration.enabled() : true; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/filter/TypeExcludeFiltersContextCustomizerFactory.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/filter/TypeExcludeFiltersContextCustomizerFactory.java index e0c748fd49d..407fb823e74 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/filter/TypeExcludeFiltersContextCustomizerFactory.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/filter/TypeExcludeFiltersContextCustomizerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 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. @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; +import org.springframework.aot.AotDetector; import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextCustomizer; @@ -42,6 +43,9 @@ class TypeExcludeFiltersContextCustomizerFactory implements ContextCustomizerFac @Override public ContextCustomizer createContextCustomizer(Class testClass, List configurationAttributes) { + if (AotDetector.useGeneratedArtifacts()) { + return null; + } AnnotationDescriptor descriptor = TestContextAnnotationUtils .findAnnotationDescriptor(testClass, TypeExcludeFilters.class); Class[] filterClasses = (descriptor != null) ? descriptor.getAnnotation().value() : NO_FILTERS; diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizerFactory.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizerFactory.java index 11c1889de8a..17520062ce0 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizerFactory.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 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. @@ -19,6 +19,7 @@ package org.springframework.boot.test.context; import java.lang.reflect.Method; import java.util.List; +import org.springframework.aot.AotDetector; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.core.annotation.MergedAnnotations; @@ -42,6 +43,9 @@ class ImportsContextCustomizerFactory implements ContextCustomizerFactory { @Override public ContextCustomizer createContextCustomizer(Class testClass, List configAttributes) { + if (AotDetector.useGeneratedArtifacts()) { + return null; + } AnnotationDescriptor descriptor = TestContextAnnotationUtils.findAnnotationDescriptor(testClass, Import.class); if (descriptor != null) {