Polishing

This commit is contained in:
Sam Brannen 2022-09-12 12:37:46 +02:00
parent 82e47db28f
commit a085a1b6b6
2 changed files with 16 additions and 15 deletions

View File

@ -28,7 +28,6 @@ import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GeneratedClasses; import org.springframework.aot.generate.GeneratedClasses;
import org.springframework.aot.generate.GeneratedFiles; import org.springframework.aot.generate.GeneratedFiles;
import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.beans.factory.aot.AotServices; import org.springframework.beans.factory.aot.AotServices;
@ -111,10 +110,11 @@ public class TestContextAotGenerator {
public void processAheadOfTime(Stream<Class<?>> testClasses) throws TestContextAotException { public void processAheadOfTime(Stream<Class<?>> testClasses) throws TestContextAotException {
MultiValueMap<MergedContextConfiguration, Class<?>> mergedConfigMappings = new LinkedMultiValueMap<>(); MultiValueMap<MergedContextConfiguration, Class<?>> mergedConfigMappings = new LinkedMultiValueMap<>();
testClasses.forEach(testClass -> mergedConfigMappings.add(buildMergedContextConfiguration(testClass), testClass)); testClasses.forEach(testClass -> mergedConfigMappings.add(buildMergedContextConfiguration(testClass), testClass));
processAheadOfTime(mergedConfigMappings); MultiValueMap<ClassName, Class<?>> initializerClassMappings = processAheadOfTime(mergedConfigMappings);
generateTestAotMappings(initializerClassMappings);
} }
private void processAheadOfTime(MultiValueMap<MergedContextConfiguration, Class<?>> mergedConfigMappings) { private MultiValueMap<ClassName, Class<?>> processAheadOfTime(MultiValueMap<MergedContextConfiguration, Class<?>> mergedConfigMappings) {
MultiValueMap<ClassName, Class<?>> initializerClassMappings = new LinkedMultiValueMap<>(); MultiValueMap<ClassName, Class<?>> initializerClassMappings = new LinkedMultiValueMap<>();
mergedConfigMappings.forEach((mergedConfig, testClasses) -> { mergedConfigMappings.forEach((mergedConfig, testClasses) -> {
logger.debug(LogMessage.format("Generating AOT artifacts for test classes %s", logger.debug(LogMessage.format("Generating AOT artifacts for test classes %s",
@ -137,8 +137,7 @@ public class TestContextAotGenerator {
testClasses.stream().map(Class::getName).toList()), ex); testClasses.stream().map(Class::getName).toList()), ex);
} }
}); });
return initializerClassMappings;
generateTestAotMappings(initializerClassMappings);
} }
/** /**
@ -207,11 +206,9 @@ public class TestContextAotGenerator {
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) { private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
TestContextBootstrapper testContextBootstrapper = TestContextBootstrapper testContextBootstrapper =
BootstrapUtils.resolveTestContextBootstrapper(testClass); BootstrapUtils.resolveTestContextBootstrapper(testClass);
// @BootstrapWith registerDeclaredConstructors(testContextBootstrapper.getClass()); // @BootstrapWith
registerDeclaredConstructors(testContextBootstrapper.getClass());
// @TestExecutionListeners
testContextBootstrapper.getTestExecutionListeners().forEach(listener -> { testContextBootstrapper.getTestExecutionListeners().forEach(listener -> {
registerDeclaredConstructors(listener.getClass()); registerDeclaredConstructors(listener.getClass()); // @TestExecutionListeners
if (listener instanceof AotTestExecutionListener aotListener) { if (listener instanceof AotTestExecutionListener aotListener) {
aotListener.processAheadOfTime(testClass, this.runtimeHints, getClass().getClassLoader()); aotListener.processAheadOfTime(testClass, this.runtimeHints, getClass().getClassLoader());
} }
@ -240,13 +237,15 @@ public class TestContextAotGenerator {
new TestAotMappingsCodeGenerator(initializerClassMappings, generatedClasses); new TestAotMappingsCodeGenerator(initializerClassMappings, generatedClasses);
generationContext.writeGeneratedContent(); generationContext.writeGeneratedContent();
String className = codeGenerator.getGeneratedClass().getName().reflectionName(); String className = codeGenerator.getGeneratedClass().getName().reflectionName();
this.runtimeHints.reflection() registerPublicMethods(className);
.registerType(TypeReference.of(className), INVOKE_PUBLIC_METHODS); }
private void registerPublicMethods(String className) {
this.runtimeHints.reflection().registerType(TypeReference.of(className), INVOKE_PUBLIC_METHODS);
} }
private void registerDeclaredConstructors(Class<?> type) { private void registerDeclaredConstructors(Class<?> type) {
ReflectionHints reflectionHints = this.runtimeHints.reflection(); this.runtimeHints.reflection().registerType(type, INVOKE_DECLARED_CONSTRUCTORS);
reflectionHints.registerType(type, INVOKE_DECLARED_CONSTRUCTORS);
} }
} }

View File

@ -82,10 +82,12 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC
class TestContextAotGeneratorTests extends AbstractAotTests { class TestContextAotGeneratorTests extends AbstractAotTests {
/** /**
* @see AotIntegrationTests#endToEndTests() * End-to-end tests within the scope of the {@link TestContextAotGenerator}.
*
* @see AotIntegrationTests
*/ */
@Test @Test
void processAheadOfTimeAndGenerateTestAotMappings() { void endToEndTests() {
Set<Class<?>> testClasses = Set.of( Set<Class<?>> testClasses = Set.of(
BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class, BasicSpringJupiterTests.class,