Browse Source
This commit introduces TestContextAotGenerator for processing Spring
integration test classes and generating AOT artifacts. Specifically,
this class performs the following.
- bootstraps the TCF for a given test class
- builds the MergedContextConfiguration for each test class and tracks
all test classes that share the same MergedContextConfiguration
- loads each test ApplicationContext without refreshing it
- passes the test ApplicationContext to ApplicationContextAotGenerator
to generate the AOT optimized ApplicationContextInitializer
- The GenerationContext passed to ApplicationContextAotGenerator uses
a feature name of the form "TestContext###_", where "###" is a
3-digit sequence ID left padded with zeros.
This commit also includes tests using the TestCompiler to verify that
each generated ApplicationContextInitializer can be used to populate a
GenericApplicationContext as expected.
See gh-28204
pull/28958/head
12 changed files with 618 additions and 27 deletions
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
/** |
||||
* Thrown if an error occurs during AOT build-time processing or AOT run-time |
||||
* execution in the <em>Spring TestContext Framework</em>. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
@SuppressWarnings("serial") |
||||
public class TestContextAotException extends RuntimeException { |
||||
|
||||
/** |
||||
* Create a new {@code TestContextAotException}. |
||||
* @param message the detail message |
||||
*/ |
||||
public TestContextAotException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code TestContextAotException}. |
||||
* @param message the detail message |
||||
* @param cause the root cause |
||||
*/ |
||||
public TestContextAotException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,206 @@
@@ -0,0 +1,206 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
import java.util.stream.Stream; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.aot.generate.ClassNameGenerator; |
||||
import org.springframework.aot.generate.DefaultGenerationContext; |
||||
import org.springframework.aot.generate.GeneratedFiles; |
||||
import org.springframework.aot.generate.GenerationContext; |
||||
import org.springframework.aot.hint.RuntimeHints; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ApplicationContextInitializer; |
||||
import org.springframework.context.aot.ApplicationContextAotGenerator; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.javapoet.ClassName; |
||||
import org.springframework.test.context.BootstrapUtils; |
||||
import org.springframework.test.context.ContextLoader; |
||||
import org.springframework.test.context.MergedContextConfiguration; |
||||
import org.springframework.test.context.SmartContextLoader; |
||||
import org.springframework.test.context.TestContextBootstrapper; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.LinkedMultiValueMap; |
||||
import org.springframework.util.MultiValueMap; |
||||
|
||||
/** |
||||
* {@code TestContextAotGenerator} generates AOT artifacts for integration tests |
||||
* that depend on support from the <em>Spring TestContext Framework</em>. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
* @see ApplicationContextAotGenerator |
||||
*/ |
||||
class TestContextAotGenerator { |
||||
|
||||
private static final Log logger = LogFactory.getLog(TestClassScanner.class); |
||||
|
||||
private final ApplicationContextAotGenerator aotGenerator = new ApplicationContextAotGenerator(); |
||||
|
||||
private final AtomicInteger sequence = new AtomicInteger(); |
||||
|
||||
private final GeneratedFiles generatedFiles; |
||||
|
||||
private final RuntimeHints runtimeHints; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@link TestContextAotGenerator} that uses the supplied |
||||
* {@link GeneratedFiles}. |
||||
* @param generatedFiles the {@code GeneratedFiles} to use |
||||
*/ |
||||
public TestContextAotGenerator(GeneratedFiles generatedFiles) { |
||||
this(generatedFiles, new RuntimeHints()); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@link TestContextAotGenerator} that uses the supplied |
||||
* {@link GeneratedFiles} and {@link RuntimeHints}. |
||||
* @param generatedFiles the {@code GeneratedFiles} to use |
||||
* @param runtimeHints the {@code RuntimeHints} to use |
||||
*/ |
||||
public TestContextAotGenerator(GeneratedFiles generatedFiles, RuntimeHints runtimeHints) { |
||||
this.generatedFiles = generatedFiles; |
||||
this.runtimeHints = runtimeHints; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get the {@link RuntimeHints} gathered during {@linkplain #processAheadOfTime(Stream) |
||||
* AOT processing}. |
||||
*/ |
||||
public final RuntimeHints getRuntimeHints() { |
||||
return this.runtimeHints; |
||||
} |
||||
|
||||
/** |
||||
* Process each of the supplied Spring integration test classes and generate |
||||
* AOT artifacts. |
||||
* @throws TestContextAotException if an error occurs during AOT processing |
||||
*/ |
||||
public void processAheadOfTime(Stream<Class<?>> testClasses) throws TestContextAotException { |
||||
MultiValueMap<MergedContextConfiguration, Class<?>> map = new LinkedMultiValueMap<>(); |
||||
testClasses.forEach(testClass -> map.add(buildMergedContextConfiguration(testClass), testClass)); |
||||
|
||||
map.forEach((mergedConfig, classes) -> { |
||||
// System.err.println(mergedConfig + " -> " + classes);
|
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Generating AOT artifacts for test classes [%s]" |
||||
.formatted(classes.stream().map(Class::getCanonicalName).toList())); |
||||
} |
||||
try { |
||||
// Use first test class discovered for a given unique MergedContextConfiguration.
|
||||
Class<?> testClass = classes.get(0); |
||||
DefaultGenerationContext generationContext = createGenerationContext(testClass); |
||||
ClassName className = processAheadOfTime(mergedConfig, generationContext); |
||||
// TODO Store ClassName in a map analogous to TestContextAotProcessor in Spring Native.
|
||||
generationContext.writeGeneratedContent(); |
||||
} |
||||
catch (Exception ex) { |
||||
if (logger.isWarnEnabled()) { |
||||
logger.warn("Failed to generate AOT artifacts for test classes [%s]" |
||||
.formatted(classes.stream().map(Class::getCanonicalName).toList()), ex); |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* Process the specified {@link MergedContextConfiguration} ahead-of-time |
||||
* using the specified {@link GenerationContext}. |
||||
* <p>Return the {@link ClassName} of the {@link ApplicationContextInitializer} |
||||
* to use to restore an optimized state of the test application context for |
||||
* the given {@code MergedContextConfiguration}. |
||||
* @param mergedConfig the {@code MergedContextConfiguration} to process |
||||
* @param generationContext the generation context to use |
||||
* @return the {@link ClassName} for the generated {@code ApplicationContextInitializer} |
||||
* @throws TestContextAotException if an error occurs during AOT processing |
||||
*/ |
||||
ClassName processAheadOfTime(MergedContextConfiguration mergedConfig, |
||||
GenerationContext generationContext) throws TestContextAotException { |
||||
|
||||
GenericApplicationContext gac = loadContextForAotProcessing(mergedConfig); |
||||
try { |
||||
return this.aotGenerator.processAheadOfTime(gac, generationContext); |
||||
} |
||||
catch (Throwable ex) { |
||||
throw new TestContextAotException("Failed to process test class [%s] for AOT" |
||||
.formatted(mergedConfig.getTestClass().getCanonicalName()), ex); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Load the {@code GenericApplicationContext} for the supplied merged context |
||||
* configuration for AOT processing. |
||||
* <p>Only supports {@link SmartContextLoader SmartContextLoaders} that |
||||
* create {@link GenericApplicationContext GenericApplicationContexts}. |
||||
* @throws TestContextAotException if an error occurs while loading the application |
||||
* context or if one of the prerequisites is not met |
||||
* @see SmartContextLoader#loadContextForAotProcessing(MergedContextConfiguration) |
||||
*/ |
||||
private GenericApplicationContext loadContextForAotProcessing( |
||||
MergedContextConfiguration mergedConfig) throws TestContextAotException { |
||||
|
||||
Class<?> testClass = mergedConfig.getTestClass(); |
||||
ContextLoader contextLoader = mergedConfig.getContextLoader(); |
||||
Assert.notNull(contextLoader, """ |
||||
Cannot load an ApplicationContext with a NULL 'contextLoader'. \ |
||||
Consider annotating test class [%s] with @ContextConfiguration or \ |
||||
@ContextHierarchy.""".formatted(testClass.getCanonicalName())); |
||||
|
||||
if (contextLoader instanceof SmartContextLoader smartContextLoader) { |
||||
try { |
||||
ApplicationContext context = smartContextLoader.loadContextForAotProcessing(mergedConfig); |
||||
if (context instanceof GenericApplicationContext gac) { |
||||
return gac; |
||||
} |
||||
} |
||||
catch (Exception ex) { |
||||
throw new TestContextAotException( |
||||
"Failed to load ApplicationContext for AOT processing for test class [%s]" |
||||
.formatted(testClass.getCanonicalName()), ex); |
||||
} |
||||
} |
||||
throw new TestContextAotException(""" |
||||
Cannot generate AOT artifacts for test class [%s]. The configured \ |
||||
ContextLoader [%s] must be a SmartContextLoader and must create a \ |
||||
GenericApplicationContext.""".formatted(testClass.getCanonicalName(), |
||||
contextLoader.getClass().getName())); |
||||
} |
||||
|
||||
MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) { |
||||
TestContextBootstrapper testContextBootstrapper = |
||||
BootstrapUtils.resolveTestContextBootstrapper(testClass); |
||||
return testContextBootstrapper.buildMergedContextConfiguration(); |
||||
} |
||||
|
||||
DefaultGenerationContext createGenerationContext(Class<?> testClass) { |
||||
ClassNameGenerator classNameGenerator = new ClassNameGenerator(testClass); |
||||
DefaultGenerationContext generationContext = |
||||
new DefaultGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints); |
||||
return generationContext.withName(nextTestContextId()); |
||||
} |
||||
|
||||
private String nextTestContextId() { |
||||
return "TestContext%03d_".formatted(this.sequence.incrementAndGet()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,80 @@
@@ -0,0 +1,80 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.nio.file.Path; |
||||
import java.nio.file.Paths; |
||||
import java.util.Set; |
||||
import java.util.stream.Stream; |
||||
|
||||
/** |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
abstract class AbstractAotTests { |
||||
|
||||
static final String[] expectedSourceFilesForBasicSpringTests = { |
||||
// BasicSpringJupiterSharedConfigTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java", |
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext001_BeanDefinitions.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ApplicationContextInitializer.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_BeanFactoryRegistrations.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext001_BeanDefinitions.java", |
||||
// BasicSpringJupiterTests -- not generated b/c already generated for BasicSpringJupiterSharedConfigTests.
|
||||
// "org/springframework/context/event/DefaultEventListenerFactory__TestContext00?_BeanDefinitions.java",
|
||||
// "org/springframework/context/event/EventListenerMethodProcessor__TestContext00?_BeanDefinitions.java",
|
||||
// "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests__TestContext00?_ApplicationContextInitializer.java",
|
||||
// "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests__TestContext00?_BeanFactoryRegistrations.java",
|
||||
// "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext00?_BeanDefinitions.java",
|
||||
// BasicSpringJupiterTests.NestedTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext002_BeanDefinitions.java", |
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext002_BeanDefinitions.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ApplicationContextInitializer.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_BeanFactoryRegistrations.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext002_BeanDefinitions.java", |
||||
// BasicSpringTestNGTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext003_BeanDefinitions.java", |
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext003_BeanDefinitions.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_ApplicationContextInitializer.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_BeanFactoryRegistrations.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext003_BeanDefinitions.java", |
||||
// BasicSpringVintageTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext004_BeanDefinitions.java", |
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext004_BeanDefinitions.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_ApplicationContextInitializer.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_BeanFactoryRegistrations.java", |
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java" |
||||
}; |
||||
|
||||
Stream<Class<?>> scan() { |
||||
return new TestClassScanner(classpathRoots()).scan(); |
||||
} |
||||
|
||||
Stream<Class<?>> scan(String... packageNames) { |
||||
return new TestClassScanner(classpathRoots()).scan(packageNames); |
||||
} |
||||
|
||||
Set<Path> classpathRoots() { |
||||
try { |
||||
return Set.of(Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI())); |
||||
} |
||||
catch (Exception ex) { |
||||
throw new RuntimeException(ex); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.List; |
||||
import java.util.stream.Stream; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.aot.generate.GeneratedFiles.Kind; |
||||
import org.springframework.aot.generate.InMemoryGeneratedFiles; |
||||
import org.springframework.aot.test.generator.compile.TestCompiler; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Smoke tests for AOT support in the TestContext framework. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
class AotSmokeTests extends AbstractAotTests { |
||||
|
||||
@Test |
||||
// Using @CompileWithTargetClassAccess results in the following exception in classpathRoots():
|
||||
// java.lang.NullPointerException: Cannot invoke "java.net.URL.toURI()" because the return
|
||||
// value of "java.security.CodeSource.getLocation()" is null
|
||||
void scanClassPathThenGenerateSourceFilesAndCompileThem() { |
||||
Stream<Class<?>> testClasses = scan("org.springframework.test.context.aot.samples.basic"); |
||||
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); |
||||
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); |
||||
|
||||
generator.processAheadOfTime(testClasses); |
||||
|
||||
List<String> sourceFiles = generatedFiles.getGeneratedFiles(Kind.SOURCE).keySet().stream().toList(); |
||||
assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFilesForBasicSpringTests); |
||||
|
||||
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled -> { |
||||
// just make sure compilation completes without errors
|
||||
}); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,125 @@
@@ -0,0 +1,125 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
import java.util.function.Consumer; |
||||
import java.util.stream.Stream; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.aot.generate.DefaultGenerationContext; |
||||
import org.springframework.aot.generate.GeneratedFiles.Kind; |
||||
import org.springframework.aot.generate.InMemoryGeneratedFiles; |
||||
import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess; |
||||
import org.springframework.aot.test.generator.compile.TestCompiler; |
||||
import org.springframework.context.ApplicationContextInitializer; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.javapoet.ClassName; |
||||
import org.springframework.test.context.MergedContextConfiguration; |
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests; |
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests; |
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests; |
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests; |
||||
import org.springframework.test.context.aot.samples.common.MessageService; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link TestContextAotGenerator}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
@CompileWithTargetClassAccess |
||||
class TestContextAotGeneratorTests extends AbstractAotTests { |
||||
|
||||
/** |
||||
* @see AotSmokeTests#scanClassPathThenGenerateSourceFilesAndCompileThem() |
||||
*/ |
||||
@Test |
||||
void generate() { |
||||
Stream<Class<?>> testClasses = Stream.of( |
||||
BasicSpringJupiterSharedConfigTests.class, |
||||
BasicSpringJupiterTests.class, |
||||
BasicSpringJupiterTests.NestedTests.class, |
||||
BasicSpringTestNGTests.class, |
||||
BasicSpringVintageTests.class); |
||||
|
||||
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); |
||||
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); |
||||
|
||||
generator.processAheadOfTime(testClasses); |
||||
|
||||
List<String> sourceFiles = generatedFiles.getGeneratedFiles(Kind.SOURCE).keySet().stream().toList(); |
||||
assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFilesForBasicSpringTests); |
||||
|
||||
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled -> { |
||||
// just make sure compilation completes without errors
|
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
|
||||
// cannot support @ParameterizedTest methods.
|
||||
void generateApplicationContextInitializer() { |
||||
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); |
||||
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); |
||||
Set<Class<?>> testClasses = Set.of( |
||||
BasicSpringTestNGTests.class, |
||||
BasicSpringVintageTests.class, |
||||
BasicSpringJupiterTests.class, |
||||
BasicSpringJupiterSharedConfigTests.class); |
||||
List<ClassName> classNames = new ArrayList<>(); |
||||
testClasses.forEach(testClass -> { |
||||
DefaultGenerationContext generationContext = generator.createGenerationContext(testClass); |
||||
MergedContextConfiguration mergedConfig = generator.buildMergedContextConfiguration(testClass); |
||||
ClassName className = generator.processAheadOfTime(mergedConfig, generationContext); |
||||
assertThat(className).isNotNull(); |
||||
classNames.add(className); |
||||
generationContext.writeGeneratedContent(); |
||||
}); |
||||
|
||||
compile(generatedFiles, classNames, context -> { |
||||
MessageService messageService = context.getBean(MessageService.class); |
||||
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); |
||||
// TODO Support @TestPropertySource in AOT testing mode.
|
||||
// assertThat(context.getEnvironment().getProperty("test.engine"))
|
||||
// .as("@TestPropertySource").isNotNull();
|
||||
}); |
||||
} |
||||
|
||||
|
||||
@SuppressWarnings("unchecked") |
||||
private void compile(InMemoryGeneratedFiles generatedFiles, List<ClassName> classNames, |
||||
Consumer<GenericApplicationContext> result) { |
||||
|
||||
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled -> { |
||||
classNames.forEach(className -> { |
||||
GenericApplicationContext gac = new GenericApplicationContext(); |
||||
ApplicationContextInitializer<GenericApplicationContext> contextInitializer = |
||||
compiled.getInstance(ApplicationContextInitializer.class, className.reflectionName()); |
||||
contextInitializer.initialize(gac); |
||||
gac.refresh(); |
||||
result.accept(gac); |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* 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.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.test.context.TestPropertySource; |
||||
import org.springframework.test.context.aot.samples.common.MessageService; |
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Uses configuration identical to {@link BasicSpringJupiterTests}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
@SpringJUnitConfig(BasicTestConfiguration.class) |
||||
@TestPropertySource(properties = "test.engine = jupiter") |
||||
public class BasicSpringJupiterSharedConfigTests { |
||||
|
||||
@Autowired |
||||
ApplicationContext context; |
||||
|
||||
@Autowired |
||||
MessageService messageService; |
||||
|
||||
@Value("${test.engine}") |
||||
String testEngine; |
||||
|
||||
@org.junit.jupiter.api.Test |
||||
void test() { |
||||
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); |
||||
assertThat(testEngine).isEqualTo("jupiter"); |
||||
assertThat(context.getEnvironment().getProperty("test.engine")) |
||||
.as("@TestPropertySource").isEqualTo("jupiter"); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue