From b0d65709ac54736d1912f7417bc3016a0a778b1a Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 18 Aug 2022 17:43:35 +0200 Subject: [PATCH] Test XML, Web, & @TestPropertySource AOT support See gh-28204, gh-28205 --- .../aot/TestContextAotGeneratorTests.java | 118 +++++++++++++----- .../aot/samples/web/MessageController.java | 34 +++++ .../samples/web/WebSpringJupiterTests.java | 62 +++++++++ .../aot/samples/web/WebSpringTestNGTests.java | 68 ++++++++++ .../samples/web/WebSpringVintageTests.java | 71 +++++++++++ .../aot/samples/web/WebTestConfiguration.java | 36 ++++++ .../samples/xml/XmlSpringJupiterTests.java | 56 +++++++++ .../aot/samples/xml/XmlSpringTestNGTests.java | 57 +++++++++ .../samples/xml/XmlSpringVintageTests.java | 60 +++++++++ .../context/aot/samples/xml/test-config.xml | 8 ++ 10 files changed, 540 insertions(+), 30 deletions(-) create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/web/MessageController.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringJupiterTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringTestNGTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringVintageTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebTestConfiguration.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringTestNGTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringVintageTests.java create mode 100644 spring-test/src/test/resources/org/springframework/test/context/aot/samples/xml/test-config.xml 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 1371daab153a..b15d89aa0a42 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 @@ -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; @@ -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; @@ -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}. @@ -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> testClasses = Set.of( - BasicSpringTestNGTests.class, - BasicSpringVintageTests.class, + BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class, - BasicSpringJupiterSharedConfigTests.class); - List 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> 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> testClasses = Set.of( + WebSpringJupiterTests.class, + WebSpringTestNGTests.class, + WebSpringVintageTests.class); - @SuppressWarnings("unchecked") - private void compile(InMemoryGeneratedFiles generatedFiles, List classNames, - Consumer 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> testClasses, ThrowingConsumer result) { + InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); + TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); + List 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 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 processAheadOfTime(TestContextAotGenerator generator, Set> testClasses) { + List 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) { + } + } diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/MessageController.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/MessageController.java new file mode 100644 index 000000000000..e8f5f5d5f80e --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/MessageController.java @@ -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!"; + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringJupiterTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringJupiterTests.java new file mode 100644 index 000000000000..fb46611987b3 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringJupiterTests.java @@ -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!")); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringTestNGTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringTestNGTests.java new file mode 100644 index 000000000000..0891a5262b57 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringTestNGTests.java @@ -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!")); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringVintageTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringVintageTests.java new file mode 100644 index 000000000000..d18c2068388e --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebSpringVintageTests.java @@ -0,0 +1,71 @@ +/* + * 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.junit.runner.RunWith; + +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.junit4.SpringRunner; +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 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = WebTestConfiguration.class) +@WebAppConfiguration +@TestPropertySource(properties = "test.engine = vintage") +public class WebSpringVintageTests { + + MockMvc mockMvc; + + @Autowired + WebApplicationContext wac; + + @Value("${test.engine}") + String testEngine; + + + @org.junit.Before + public void setUpMockMvc() { + this.mockMvc = webAppContextSetup(this.wac).build(); + } + + @org.junit.Test + public void test() throws Exception { + assertThat(testEngine) + .as("@Value").isEqualTo("vintage"); + assertThat(wac.getEnvironment().getProperty("test.engine")) + .as("Environment").isEqualTo("vintage"); + + mockMvc.perform(get("/hello")) + .andExpectAll(status().isOk(), content().string("Hello, AOT!")); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebTestConfiguration.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebTestConfiguration.java new file mode 100644 index 000000000000..d7256dc13c54 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/web/WebTestConfiguration.java @@ -0,0 +1,36 @@ +/* + * 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.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.config.EnableWebFlux; + +/** + * @author Sam Brannen + * @since 6.0 + */ +@Configuration(proxyBeanMethods = false) +@EnableWebFlux +class WebTestConfiguration { + + @Bean + MessageController messageController() { + return new MessageController(); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests.java new file mode 100644 index 000000000000..e85cfa8c64ff --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests.java @@ -0,0 +1,56 @@ +/* + * 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.xml; + +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; + +/** + * @author Sam Brannen + * @since 6.0 + */ +@SpringJUnitConfig(locations = "test-config.xml") +@TestPropertySource(properties = "test.engine = jupiter") +public class XmlSpringJupiterTests { + + @Autowired + ApplicationContext context; + + @Autowired + MessageService messageService; + + @Value("${test.engine}") + String testEngine; + + + @org.junit.jupiter.api.Test + void test() { + assertThat(testEngine) + .as("@Value").isEqualTo("jupiter"); + assertThat(context.getEnvironment().getProperty("test.engine")) + .as("Environment").isEqualTo("jupiter"); + + assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringTestNGTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringTestNGTests.java new file mode 100644 index 000000000000..42cddd6a1110 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringTestNGTests.java @@ -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.samples.xml; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.aot.samples.common.MessageService; +import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Sam Brannen + * @since 6.0 + */ +@ContextConfiguration("test-config.xml") +@TestPropertySource(properties = "test.engine = testng") +public class XmlSpringTestNGTests extends AbstractTestNGSpringContextTests { + + @Autowired + ApplicationContext context; + + @Autowired + MessageService messageService; + + @Value("${test.engine}") + String testEngine; + + + @org.testng.annotations.Test + public void test() { + assertThat(testEngine) + .as("@Value").isEqualTo("testng"); + assertThat(context.getEnvironment().getProperty("test.engine")) + .as("Environment").isEqualTo("testng"); + + assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringVintageTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringVintageTests.java new file mode 100644 index 000000000000..1e67f5dc320a --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/xml/XmlSpringVintageTests.java @@ -0,0 +1,60 @@ +/* + * 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.xml; + +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.aot.samples.common.MessageService; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Sam Brannen + * @since 6.0 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration("test-config.xml") +@TestPropertySource(properties = "test.engine = vintage") +public class XmlSpringVintageTests { + + @Autowired + ApplicationContext context; + + @Autowired + MessageService messageService; + + @Value("${test.engine}") + String testEngine; + + + @org.junit.Test + public void test() { + assertThat(testEngine) + .as("@Value").isEqualTo("vintage"); + assertThat(context.getEnvironment().getProperty("test.engine")) + .as("Environment").isEqualTo("vintage"); + + assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); + } + +} diff --git a/spring-test/src/test/resources/org/springframework/test/context/aot/samples/xml/test-config.xml b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/xml/test-config.xml new file mode 100644 index 000000000000..7488fb4fcbd9 --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/xml/test-config.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file