Merge branch '6.0.x'

This commit is contained in:
Sam Brannen
2023-07-15 13:09:12 +02:00
13 changed files with 307 additions and 18 deletions

View File

@@ -85,6 +85,8 @@ public class TestContextAotGenerator {
private final RuntimeHints runtimeHints;
private final boolean failOnError;
/**
* Create a new {@link TestContextAotGenerator} that uses the supplied
@@ -102,9 +104,23 @@ public class TestContextAotGenerator {
* @param runtimeHints the {@code RuntimeHints} to use
*/
public TestContextAotGenerator(GeneratedFiles generatedFiles, RuntimeHints runtimeHints) {
this(generatedFiles, runtimeHints, false);
}
/**
* Create a new {@link TestContextAotGenerator} that uses the supplied
* {@link GeneratedFiles}, {@link RuntimeHints}, and {@code failOnError} flag.
* @param generatedFiles the {@code GeneratedFiles} to use
* @param runtimeHints the {@code RuntimeHints} to use
* @param failOnError {@code true} if errors encountered during AOT processing
* should result in an exception that fails the overall process
* @since 6.0.12
*/
TestContextAotGenerator(GeneratedFiles generatedFiles, RuntimeHints runtimeHints, boolean failOnError) {
this.testRuntimeHintsRegistrars = AotServices.factories().load(TestRuntimeHintsRegistrar.class);
this.generatedFiles = generatedFiles;
this.runtimeHints = runtimeHints;
this.failOnError = failOnError;
}
@@ -210,6 +226,10 @@ public class TestContextAotGenerator {
generationContext.writeGeneratedContent();
}
catch (Exception ex) {
if (this.failOnError) {
throw new IllegalStateException("Failed to generate AOT artifacts for test classes " +
testClasses.stream().map(Class::getName).toList(), ex);
}
if (logger.isDebugEnabled()) {
logger.debug("Failed to generate AOT artifacts for test classes " +
testClasses.stream().map(Class::getName).toList(), ex);
@@ -304,8 +324,8 @@ public class TestContextAotGenerator {
DefaultGenerationContext createGenerationContext(Class<?> testClass) {
ClassNameGenerator classNameGenerator = new ClassNameGenerator(ClassName.get(testClass));
DefaultGenerationContext generationContext =
new DefaultGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints);
TestContextGenerationContext generationContext =
new TestContextGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints);
return generationContext.withName(nextTestContextId());
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright 2002-2023 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 org.springframework.aot.generate.ClassNameGenerator;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GeneratedClasses;
import org.springframework.aot.generate.GeneratedFiles;
import org.springframework.aot.hint.RuntimeHints;
/**
* Extension of {@link DefaultGenerationContext} with a custom implementation of
* {@link #withName(String)} that is specific to the <em>Spring TestContext Framework</em>.
*
* @author Sam Brannen
* @since 6.0.12
*/
class TestContextGenerationContext extends DefaultGenerationContext {
private final String featureName;
/**
* Create a new {@link TestContextGenerationContext} instance backed by the
* specified {@link ClassNameGenerator}, {@link GeneratedFiles}, and
* {@link RuntimeHints}.
* @param classNameGenerator the naming convention to use for generated class names
* @param generatedFiles the generated files
* @param runtimeHints the runtime hints
*/
TestContextGenerationContext(ClassNameGenerator classNameGenerator, GeneratedFiles generatedFiles,
RuntimeHints runtimeHints) {
super(classNameGenerator, generatedFiles, runtimeHints);
this.featureName = null;
}
/**
* Create a new {@link TestContextGenerationContext} instance backed by the
* specified {@link GeneratedClasses}, {@link GeneratedFiles}, and
* {@link RuntimeHints}.
* @param generatedClasses the generated classes
* @param generatedFiles the generated files
* @param runtimeHints the runtime hints
*/
private TestContextGenerationContext(GeneratedClasses generatedClasses, GeneratedFiles generatedFiles,
RuntimeHints runtimeHints, String featureName) {
super(generatedClasses, generatedFiles, runtimeHints);
this.featureName = featureName;
}
/**
* Create a new {@link TestContextGenerationContext} instance using the specified
* feature name to qualify generated assets for a dedicated round of code generation.
* <p>If <em>this</em> {@code TestContextGenerationContext} has a configured feature
* name, the supplied feature name will be appended to the existing feature name
* in order to avoid naming collisions.
* @param featureName the feature name to use
* @return a specialized {@link TestContextGenerationContext} for the specified
* feature name
*/
@Override
public TestContextGenerationContext withName(String featureName) {
if (this.featureName != null) {
featureName = this.featureName + featureName;
}
GeneratedClasses generatedClasses = getGeneratedClasses().withFeatureNamePrefix(featureName);
return new TestContextGenerationContext(generatedClasses, getGeneratedFiles(), getRuntimeHints(), featureName);
}
}