Browse Source

Refactor GeneratedFilesTestCompilerUtils to CompilerFiles

See gh-29175
pull/29184/head
Phillip Webb 2 years ago
parent
commit
6802665177
  1. 73
      spring-core-test/src/main/java/org/springframework/aot/test/generate/CompilerFiles.java
  2. 56
      spring-core-test/src/main/java/org/springframework/aot/test/generate/GeneratedFilesTestCompilerUtils.java
  3. 5
      spring-core-test/src/main/java/org/springframework/aot/test/generate/TestGenerationContext.java
  4. 4
      spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java
  5. 11
      spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java
  6. 11
      spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java

73
spring-core-test/src/main/java/org/springframework/aot/test/generate/CompilerFiles.java

@ -0,0 +1,73 @@ @@ -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<TestCompiler> {
private final InMemoryGeneratedFiles generatedFiles;
private CompilerFiles(InMemoryGeneratedFiles generatedFiles) {
this.generatedFiles = generatedFiles;
}
public static UnaryOperator<TestCompiler> 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 <T> List<T> adapt(Kind kind,
BiFunction<String, InputStreamSource, T> adapter) {
List<T> result = new ArrayList<>();
this.generatedFiles.getGeneratedFiles(kind)
.forEach((k, v) -> result.add(adapter.apply(k, v)));
return result;
}
}

56
spring-core-test/src/main/java/org/springframework/aot/test/generate/GeneratedFilesTestCompilerUtils.java

@ -1,56 +0,0 @@ @@ -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<SourceFile> sourceFiles = new ArrayList<>();
generatedFiles.getGeneratedFiles(Kind.SOURCE).forEach(
(path, inputStreamSource) -> sourceFiles.add(SourceFile.of(inputStreamSource)));
List<ResourceFile> resourceFiles = new ArrayList<>();
generatedFiles.getGeneratedFiles(Kind.RESOURCE).forEach(
(path, inputStreamSource) -> resourceFiles.add(ResourceFile.of(path, inputStreamSource)));
List<ClassFile> 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);
}
}

5
spring-core-test/src/main/java/org/springframework/aot/test/generate/TestGenerationContext.java

@ -17,6 +17,7 @@ @@ -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; @@ -33,7 +34,7 @@ import org.springframework.core.test.tools.TestCompiler;
* @author Sam Brannen
* @since 6.0
*/
public class TestGenerationContext extends DefaultGenerationContext implements Function<TestCompiler, TestCompiler> {
public class TestGenerationContext extends DefaultGenerationContext implements UnaryOperator<TestCompiler> {
/**
* Create an instance using the specified {@link ClassNameGenerator}.
@ -72,7 +73,7 @@ public class TestGenerationContext extends DefaultGenerationContext implements F @@ -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);
}
}

4
spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java

@ -23,7 +23,7 @@ import java.util.Collections; @@ -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 { @@ -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<TestCompiler, TestCompiler> customizer) {
public TestCompiler with(UnaryOperator<TestCompiler> customizer) {
return customizer.apply(this);
}

11
spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java

@ -21,7 +21,6 @@ import java.nio.file.Path; @@ -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; @@ -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 { @@ -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 { @@ -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<TestCompiler, TestCompiler> setupGeneratedFiles(InMemoryGeneratedFiles generatedFiles) {
return testCompiler -> GeneratedFilesTestCompilerUtils.configure(testCompiler, generatedFiles);
}
private static void runTestsInAotMode(List<Class<?>> testClasses) {
runTestsInAotMode(-1, testClasses);
}

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

@ -20,7 +20,6 @@ import java.lang.annotation.Annotation; @@ -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; @@ -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 @@ -85,10 +84,6 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC
@CompileWithForkedClassLoader
class TestContextAotGeneratorTests extends AbstractAotTests {
private static Function<TestCompiler, TestCompiler> 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 { @@ -116,7 +111,7 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
List<String> 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 { @@ -327,7 +322,7 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
List<Mapping> 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<ConfigurableApplicationContext> contextInitializer =

Loading…
Cancel
Save