Retain active profiles used during AOT processing

This commit makes sure that profiles that have been explicitly enabled
during AOT optimizations are automatically enabled when using those
optimizations.

If other profiles are set at runtime, they take precedence over the ones
defined during AOT processing.

Closes gh-30421
This commit is contained in:
Stephane Nicoll
2023-06-12 16:33:35 +02:00
parent 446b90172b
commit 61c9cbc3f5
3 changed files with 58 additions and 6 deletions

View File

@@ -22,9 +22,13 @@ import java.lang.reflect.Proxy;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.aot.generate.GeneratedFiles.Kind;
import org.springframework.aot.generate.GenerationContext;
@@ -384,6 +388,37 @@ class ApplicationContextAotGeneratorTests {
}
@Nested
static class ActiveProfile {
@ParameterizedTest
@MethodSource("activeProfilesParameters")
void processAheadOfTimeWhenHasActiveProfiles(String[] aotProfiles, String[] runtimeProfiles, String[] expectedActiveProfiles) {
GenericApplicationContext applicationContext = new GenericApplicationContext();
if (aotProfiles.length != 0) {
applicationContext.getEnvironment().setActiveProfiles(aotProfiles);
}
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
if (runtimeProfiles.length != 0) {
freshApplicationContext.getEnvironment().setActiveProfiles(runtimeProfiles);
}
initializer.initialize(freshApplicationContext);
freshApplicationContext.refresh();
assertThat(freshApplicationContext.getEnvironment().getActiveProfiles()).containsExactly(expectedActiveProfiles);
});
}
static Stream<Arguments> activeProfilesParameters() {
return Stream.of(Arguments.of(new String[] { "aot", "prod" }, new String[] {}, new String[] { "aot", "prod" }),
Arguments.of(new String[] {}, new String[] { "aot", "prod" }, new String[] { "aot", "prod" }),
Arguments.of(new String[] { "aot" }, new String[] { "prod" }, new String[] { "prod", "aot" }),
Arguments.of(new String[] { "aot", "prod" }, new String[] { "aot", "prod" }, new String[] { "aot", "prod" }),
Arguments.of(new String[] { "default" }, new String[] {}, new String[] {}));
}
}
private Consumer<List<? extends JdkProxyHint>> doesNotHaveProxyFor(Class<?> target) {
return hints -> assertThat(hints).noneMatch(hint ->
hint.getProxiedInterfaces().get(0).equals(TypeReference.of(target)));