Skip to content

Commit

Permalink
Register runtime hints for ActiveProfilesResolvers
Browse files Browse the repository at this point in the history
This commit introduces automatic registration of runtime hints for
custom ActiveProfilesResolver implementations configured via the
`resolver` attribute in @activeprofiles.

Closes gh-29022
  • Loading branch information
sbrannen committed Sep 4, 2022
1 parent 8d6d3f2 commit a68d4ae
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
import java.util.List;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ActiveProfilesResolver;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.aot.TestRuntimeHintsRegistrar;
import org.springframework.test.context.web.WebMergedContextConfiguration;

import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_CONSTRUCTORS;
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY;
import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;

/**
Expand All @@ -46,6 +51,7 @@ public void registerHints(RuntimeHints runtimeHints, MergedContextConfiguration
List<Class<?>> testClasses, ClassLoader classLoader) {

registerHintsForMergedContextConfiguration(runtimeHints, mergedConfig);
testClasses.forEach(testClass -> registerHintsForActiveProfilesResolvers(runtimeHints, testClass));
}

private void registerHintsForMergedContextConfiguration(
Expand Down Expand Up @@ -73,6 +79,17 @@ private void registerHintsForMergedContextConfiguration(
}
}

private void registerHintsForActiveProfilesResolvers(RuntimeHints runtimeHints, Class<?> testClass) {
// @ActiveProfiles(resolver = ...)
MergedAnnotations.search(TYPE_HIERARCHY)
.withEnclosingClasses(TestContextAnnotationUtils::searchEnclosingClass)
.from(testClass)
.stream(ActiveProfiles.class)
.map(mergedAnnotation -> mergedAnnotation.getClass("resolver"))
.filter(type -> type != ActiveProfilesResolver.class)
.forEach(resolverClass -> registerDeclaredConstructors(runtimeHints, resolverClass));
}

private void registerDeclaredConstructors(RuntimeHints runtimeHints, Class<?> type) {
runtimeHints.reflection().registerType(type, INVOKE_DECLARED_CONSTRUCTORS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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;
Expand Down Expand Up @@ -162,13 +163,15 @@ private static void assertRuntimeHints(RuntimeHints runtimeHints) {
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);

Expand Down Expand Up @@ -216,7 +219,10 @@ private void assertContextForBasicTests(ApplicationContext context) {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -57,12 +58,13 @@ void test(@Autowired ApplicationContext context, @Autowired MessageService messa

@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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

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
Expand All @@ -29,8 +31,15 @@
class BasicTestConfiguration {

@Bean
MessageService messageService() {
@Profile("default")
MessageService defaultMessageService() {
return new DefaultMessageService();
}

@Bean
@Profile("spanish")
MessageService spanishMessageService() {
return new SpanishMessageService();
}

}
Original file line number Diff line number Diff line change
@@ -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" };
}

}
Original file line number Diff line number Diff line change
@@ -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!";
}

}

0 comments on commit a68d4ae

Please sign in to comment.