Register runtime hints for ActiveProfilesResolvers
This commit introduces automatic registration of runtime hints for custom ActiveProfilesResolver implementations configured via the `resolver` attribute in @ActiveProfiles. Closes gh-29022
This commit is contained in:
@@ -35,6 +35,7 @@ import org.springframework.aot.test.generator.compile.TestCompiler;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.javapoet.ClassName;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests;
|
||||
@@ -162,13 +163,15 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
|
||||
Stream.of(
|
||||
// @BootstrapWith
|
||||
org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests.CustomXmlBootstrapper.class,
|
||||
// @ContextConfiguration(initializers=...)
|
||||
// @ContextConfiguration(initializers = ...)
|
||||
org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests.CustomInitializer.class,
|
||||
// @ContextConfiguration(loader=...)
|
||||
org.springframework.test.context.support.AnnotationConfigContextLoader.class
|
||||
// @ContextConfiguration(loader = ...)
|
||||
org.springframework.test.context.support.AnnotationConfigContextLoader.class,
|
||||
// @ActiveProfiles(resolver = ...)
|
||||
org.springframework.test.context.aot.samples.basic.SpanishActiveProfilesResolver.class
|
||||
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
|
||||
|
||||
// @ContextConfiguration(locations=...)
|
||||
// @ContextConfiguration(locations = ...)
|
||||
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/xml/test-config.xml"))
|
||||
.accepts(runtimeHints);
|
||||
|
||||
@@ -216,7 +219,10 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
|
||||
assertThat(context.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
|
||||
|
||||
MessageService messageService = context.getBean(MessageService.class);
|
||||
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
|
||||
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
|
||||
String expectedMessage = cac.getEnvironment().acceptsProfiles(Profiles.of("spanish")) ?
|
||||
"¡Hola, AOT!" : "Hello, AOT!";
|
||||
assertThat(messageService.generateMessage()).isEqualTo(expectedMessage);
|
||||
}
|
||||
|
||||
private void assertContextForBasicWebTests(WebApplicationContext wac) throws Exception {
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.extension.Extension;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests.DummyExtension;
|
||||
@@ -57,12 +58,13 @@ public class BasicSpringJupiterTests {
|
||||
|
||||
@Nested
|
||||
@TestPropertySource(properties = "foo=bar")
|
||||
@ActiveProfiles(resolver = SpanishActiveProfilesResolver.class)
|
||||
public class NestedTests {
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
void test(@Autowired ApplicationContext context, @Autowired MessageService messageService,
|
||||
@Value("${test.engine}") String testEngine, @Value("${foo}") String foo) {
|
||||
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
|
||||
assertThat(messageService.generateMessage()).isEqualTo("¡Hola, AOT!");
|
||||
assertThat(foo).isEqualTo("bar");
|
||||
assertThat(testEngine).isEqualTo("jupiter");
|
||||
assertThat(context.getEnvironment().getProperty("test.engine"))
|
||||
|
||||
@@ -18,8 +18,10 @@ package org.springframework.test.context.aot.samples.basic;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.test.context.aot.samples.common.DefaultMessageService;
|
||||
import org.springframework.test.context.aot.samples.common.MessageService;
|
||||
import org.springframework.test.context.aot.samples.common.SpanishMessageService;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
@@ -29,8 +31,15 @@ import org.springframework.test.context.aot.samples.common.MessageService;
|
||||
class BasicTestConfiguration {
|
||||
|
||||
@Bean
|
||||
MessageService messageService() {
|
||||
@Profile("default")
|
||||
MessageService defaultMessageService() {
|
||||
return new DefaultMessageService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Profile("spanish")
|
||||
MessageService spanishMessageService() {
|
||||
return new SpanishMessageService();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2002-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.aot.samples.basic;
|
||||
|
||||
import org.springframework.test.context.ActiveProfilesResolver;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
* @since 6.0
|
||||
*/
|
||||
public class SpanishActiveProfilesResolver implements ActiveProfilesResolver {
|
||||
|
||||
@Override
|
||||
public String[] resolve(Class<?> testClass) {
|
||||
return new String[] { "spanish" };
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2002-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.aot.samples.common;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
* @since 6.0
|
||||
*/
|
||||
public class SpanishMessageService implements MessageService {
|
||||
|
||||
@Override
|
||||
public String generateMessage() {
|
||||
return "¡Hola, AOT!";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user