Browse Source

Polishing

pull/29136/head
Sam Brannen 2 years ago
parent
commit
a085a1b6b6
  1. 25
      spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java
  2. 6
      spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java

25
spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java

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

6
spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java

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

Loading…
Cancel
Save