Skip to content

Commit

Permalink
Test XML, Web, & @TestPropertySource AOT support
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 23, 2022
1 parent bb615e1 commit 2febfa9
Show file tree
Hide file tree
Showing 10 changed files with 540 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand All @@ -29,6 +28,7 @@
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.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.javapoet.ClassName;
Expand All @@ -38,8 +38,21 @@
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 org.springframework.test.context.aot.samples.web.WebSpringJupiterTests;
import org.springframework.test.context.aot.samples.web.WebSpringTestNGTests;
import org.springframework.test.context.aot.samples.web.WebSpringVintageTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringJupiterTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringTestNGTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringVintageTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.function.ThrowingConsumer;
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
* Tests for {@link TestContextAotGenerator}.
Expand Down Expand Up @@ -76,50 +89,95 @@ void generate() {
}

@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);
void processAheadOfTimeWithBasicTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class,
BasicSpringJupiterSharedConfigTests.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();
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class);

processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();

MessageService messageService = context.getBean(MessageService.class);
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
});
}

@Test
void processAheadOfTimeWithXmlTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
XmlSpringJupiterTests.class,
XmlSpringTestNGTests.class,
XmlSpringVintageTests.class);

processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();

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();
});
}

@Test
void processAheadOfTimeWithWebTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
WebSpringJupiterTests.class,
WebSpringTestNGTests.class,
WebSpringVintageTests.class);

@SuppressWarnings("unchecked")
private void compile(InMemoryGeneratedFiles generatedFiles, List<ClassName> classNames,
Consumer<GenericApplicationContext> result) {
processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();

MockMvc mockMvc = webAppContextSetup((WebApplicationContext) context).build();
mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
});
}


@SuppressWarnings("unchecked")
private void processAheadOfTime(Set<Class<?>> testClasses, ThrowingConsumer<ApplicationContext> result) {
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
List<Mapping> mappings = processAheadOfTime(generator, testClasses);
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled -> {
classNames.forEach(className -> {
GenericApplicationContext gac = new GenericApplicationContext();
mappings.forEach(mapping -> {
MergedContextConfiguration mergedConfig = mapping.mergedConfig();
ApplicationContextInitializer<GenericApplicationContext> contextInitializer =
compiled.getInstance(ApplicationContextInitializer.class, className.reflectionName());
contextInitializer.initialize(gac);
gac.refresh();
result.accept(gac);
compiled.getInstance(ApplicationContextInitializer.class, mapping.className().reflectionName());
AotRuntimeContextLoader aotRuntimeContextLoader = new AotRuntimeContextLoader();
GenericApplicationContext context = aotRuntimeContextLoader.loadContext(mergedConfig, contextInitializer);
result.accept(context);
});
});
}

private List<Mapping> processAheadOfTime(TestContextAotGenerator generator, Set<Class<?>> testClasses) {
List<Mapping> mappings = new ArrayList<>();
testClasses.forEach(testClass -> {
DefaultGenerationContext generationContext = generator.createGenerationContext(testClass);
MergedContextConfiguration mergedConfig = generator.buildMergedContextConfiguration(testClass);
ClassName className = generator.processAheadOfTime(mergedConfig, generationContext);
assertThat(className).isNotNull();
mappings.add(new Mapping(mergedConfig, className));
generationContext.writeGeneratedContent();
});
return mappings;
}


record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author Sam Brannen
* @since 6.0
*/
@RestController
class MessageController {

@GetMapping("/hello")
String hello() {
return "Hello, AOT!";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
* @author Sam Brannen
* @since 6.0
*/
@SpringJUnitWebConfig(WebTestConfiguration.class)
@TestPropertySource(properties = "test.engine = jupiter")
public class WebSpringJupiterTests {

MockMvc mockMvc;

@Autowired
WebApplicationContext wac;


@org.junit.jupiter.api.BeforeEach
void setUpMockMvc() {
this.mockMvc = webAppContextSetup(this.wac).build();
}

@org.junit.jupiter.api.Test
void test(@Value("${test.engine}") String testEngine) throws Exception {
assertThat(testEngine)
.as("@Value").isEqualTo("jupiter");
assertThat(wac.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("jupiter");

mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
* @author Sam Brannen
* @since 6.0
*/
@ContextConfiguration(classes = WebTestConfiguration.class)
@WebAppConfiguration
@TestPropertySource(properties = "test.engine = testng")
public class WebSpringTestNGTests extends AbstractTestNGSpringContextTests {

MockMvc mockMvc;

@Autowired
WebApplicationContext wac;

@Value("${test.engine}")
String testEngine;


@org.testng.annotations.BeforeMethod
public void setUpMockMvc() {
this.mockMvc = webAppContextSetup(this.wac).build();
}

@org.testng.annotations.Test
public void test() throws Exception {
assertThat(testEngine)
.as("@Value").isEqualTo("testng");
assertThat(wac.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("testng");

mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
}

}
Loading

0 comments on commit 2febfa9

Please sign in to comment.