diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/CompilerFiles.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/CompilerFiles.java new file mode 100644 index 0000000000..304241f1b5 --- /dev/null +++ b/spring-core-test/src/main/java/org/springframework/aot/test/generate/CompilerFiles.java @@ -0,0 +1,73 @@ +/* + * 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.aot.test.generate; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.UnaryOperator; + +import org.springframework.aot.generate.GeneratedFiles; +import org.springframework.aot.generate.GeneratedFiles.Kind; +import org.springframework.aot.generate.InMemoryGeneratedFiles; +import org.springframework.core.io.InputStreamSource; +import org.springframework.core.test.tools.ClassFile; +import org.springframework.core.test.tools.ResourceFile; +import org.springframework.core.test.tools.SourceFile; +import org.springframework.core.test.tools.TestCompiler; + +/** + * Adapter class that can be used to apply AOT {@link GeneratedFiles} to the + * {@link TestCompiler}. + * + * @author Stephane Nicoll + * @author Phillip Webb + * @since 6.0 + */ +public final class CompilerFiles implements UnaryOperator { + + private final InMemoryGeneratedFiles generatedFiles; + + private CompilerFiles(InMemoryGeneratedFiles generatedFiles) { + this.generatedFiles = generatedFiles; + } + + public static UnaryOperator from( + InMemoryGeneratedFiles generatedFiles) { + return new CompilerFiles(generatedFiles); + } + + @Override + public TestCompiler apply(TestCompiler testCompiler) { + return testCompiler + .withSources(adapt(Kind.SOURCE, (path, inputStreamSource) -> + SourceFile.of(inputStreamSource))) + .withResources(adapt(Kind.RESOURCE, (path, inputStreamSource) -> + ResourceFile.of(path, inputStreamSource))) + .withClasses(adapt(Kind.CLASS, (path, inputStreamSource) -> + ClassFile.of(ClassFile.toClassName(path), inputStreamSource))); + } + + private List adapt(Kind kind, + BiFunction adapter) { + List result = new ArrayList<>(); + this.generatedFiles.getGeneratedFiles(kind) + .forEach((k, v) -> result.add(adapter.apply(k, v))); + return result; + } + +} diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/GeneratedFilesTestCompilerUtils.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/GeneratedFilesTestCompilerUtils.java deleted file mode 100644 index 24c0a4dd0b..0000000000 --- a/spring-core-test/src/main/java/org/springframework/aot/test/generate/GeneratedFilesTestCompilerUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.aot.test.generate; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.aot.generate.GeneratedFiles.Kind; -import org.springframework.aot.generate.InMemoryGeneratedFiles; -import org.springframework.core.test.tools.ClassFile; -import org.springframework.core.test.tools.ResourceFile; -import org.springframework.core.test.tools.SourceFile; -import org.springframework.core.test.tools.TestCompiler; - -/** - * {@link TestCompiler} utilities for generated files. - * - * @author Stephane Nicoll - * @since 6.0 - */ -public abstract class GeneratedFilesTestCompilerUtils { - - /** - * Apply the specified {@link InMemoryGeneratedFiles} to the specified {@link TestCompiler}. - * @param testCompiler the compiler to configure - * @param generatedFiles the generated files to apply - * @return a new {@link TestCompiler} instance configured with the generated files - */ - public static TestCompiler configure(TestCompiler testCompiler, InMemoryGeneratedFiles generatedFiles) { - List sourceFiles = new ArrayList<>(); - generatedFiles.getGeneratedFiles(Kind.SOURCE).forEach( - (path, inputStreamSource) -> sourceFiles.add(SourceFile.of(inputStreamSource))); - List resourceFiles = new ArrayList<>(); - generatedFiles.getGeneratedFiles(Kind.RESOURCE).forEach( - (path, inputStreamSource) -> resourceFiles.add(ResourceFile.of(path, inputStreamSource))); - List classFiles = new ArrayList<>(); - generatedFiles.getGeneratedFiles(Kind.CLASS).forEach( - (path, inputStreamSource) -> classFiles.add(ClassFile.of( - ClassFile.toClassName(path), inputStreamSource))); - return testCompiler.withSources(sourceFiles).withResources(resourceFiles).withClasses(classFiles); - } -} diff --git a/spring-core-test/src/main/java/org/springframework/aot/test/generate/TestGenerationContext.java b/spring-core-test/src/main/java/org/springframework/aot/test/generate/TestGenerationContext.java index efbec7317b..cf00cb7ba5 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/test/generate/TestGenerationContext.java +++ b/spring-core-test/src/main/java/org/springframework/aot/test/generate/TestGenerationContext.java @@ -17,6 +17,7 @@ package org.springframework.aot.test.generate; import java.util.function.Function; +import java.util.function.UnaryOperator; import org.springframework.aot.generate.ClassNameGenerator; import org.springframework.aot.generate.DefaultGenerationContext; @@ -33,7 +34,7 @@ import org.springframework.core.test.tools.TestCompiler; * @author Sam Brannen * @since 6.0 */ -public class TestGenerationContext extends DefaultGenerationContext implements Function { +public class TestGenerationContext extends DefaultGenerationContext implements UnaryOperator { /** * Create an instance using the specified {@link ClassNameGenerator}. @@ -72,7 +73,7 @@ public class TestGenerationContext extends DefaultGenerationContext implements F */ @Override public TestCompiler apply(TestCompiler testCompiler) { - return GeneratedFilesTestCompilerUtils.configure(testCompiler, getGeneratedFiles()); + return CompilerFiles.from(getGeneratedFiles()).apply(testCompiler); } } diff --git a/spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java b/spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java index 2adc2d7999..308118002d 100644 --- a/spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java +++ b/spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java @@ -23,7 +23,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.function.Consumer; -import java.util.function.Function; +import java.util.function.UnaryOperator; import javax.annotation.processing.Processor; import javax.tools.Diagnostic; @@ -96,7 +96,7 @@ public final class TestCompiler { * @param customizer the customizer to call * @return a new {@code TestCompiler} instance with the customizations applied */ - public TestCompiler with(Function customizer) { + public TestCompiler with(UnaryOperator customizer) { return customizer.apply(this); } diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java index f65573f745..5e6a5ec73c 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java @@ -21,7 +21,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Set; -import java.util.function.Function; import java.util.stream.Stream; import org.junit.jupiter.api.Disabled; @@ -38,7 +37,7 @@ import org.opentest4j.MultipleFailuresError; import org.springframework.aot.AotDetector; import org.springframework.aot.generate.GeneratedFiles.Kind; import org.springframework.aot.generate.InMemoryGeneratedFiles; -import org.springframework.aot.test.generate.GeneratedFilesTestCompilerUtils; +import org.springframework.aot.test.generate.CompilerFiles; import org.springframework.core.test.tools.CompileWithForkedClassLoader; import org.springframework.core.test.tools.TestCompiler; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests; @@ -94,7 +93,7 @@ class AotIntegrationTests extends AbstractAotTests { assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFilesForBasicSpringTests); // AOT BUILD-TIME: COMPILATION - TestCompiler.forSystem().with(setupGeneratedFiles(generatedFiles)) + TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)) // .printFiles(System.out) .compile(compiled -> // AOT RUN-TIME: EXECUTION @@ -125,17 +124,13 @@ class AotIntegrationTests extends AbstractAotTests { generator.processAheadOfTime(testClasses.stream()); // AOT BUILD-TIME: COMPILATION - TestCompiler.forSystem().with(setupGeneratedFiles(generatedFiles)) + TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)) // .printFiles(System.out) .compile(compiled -> // AOT RUN-TIME: EXECUTION runTestsInAotMode(testClasses)); } - private static Function setupGeneratedFiles(InMemoryGeneratedFiles generatedFiles) { - return testCompiler -> GeneratedFilesTestCompilerUtils.configure(testCompiler, generatedFiles); - } - private static void runTestsInAotMode(List> testClasses) { runTestsInAotMode(-1, testClasses); } diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java index 3684c3fc6a..24e00b9c83 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java @@ -20,7 +20,6 @@ import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.List; import java.util.Set; -import java.util.function.Function; import java.util.stream.Stream; import javax.sql.DataSource; @@ -34,7 +33,7 @@ import org.springframework.aot.generate.InMemoryGeneratedFiles; import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.TypeReference; -import org.springframework.aot.test.generate.GeneratedFilesTestCompilerUtils; +import org.springframework.aot.test.generate.CompilerFiles; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -85,10 +84,6 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC @CompileWithForkedClassLoader class TestContextAotGeneratorTests extends AbstractAotTests { - private static Function setupGeneratedFiles(InMemoryGeneratedFiles generatedFiles) { - return testCompiler -> GeneratedFilesTestCompilerUtils.configure(testCompiler, generatedFiles); - } - /** * End-to-end tests within the scope of the {@link TestContextAotGenerator}. * @@ -116,7 +111,7 @@ class TestContextAotGeneratorTests extends AbstractAotTests { List sourceFiles = generatedFiles.getGeneratedFiles(Kind.SOURCE).keySet().stream().toList(); assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFiles); - TestCompiler.forSystem().with(setupGeneratedFiles(generatedFiles)).compile(ThrowingConsumer.of(compiled -> { + TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)).compile(ThrowingConsumer.of(compiled -> { try { System.setProperty(AotDetector.AOT_ENABLED, "true"); AotTestAttributesFactory.reset(); @@ -327,7 +322,7 @@ class TestContextAotGeneratorTests extends AbstractAotTests { InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); List mappings = processAheadOfTime(generator, testClasses); - TestCompiler.forSystem().with(setupGeneratedFiles(generatedFiles)).compile(ThrowingConsumer.of(compiled -> { + TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)).compile(ThrowingConsumer.of(compiled -> { for (Mapping mapping : mappings) { MergedContextConfiguration mergedConfig = mapping.mergedConfig(); ApplicationContextInitializer contextInitializer =