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 1fab19cdadf..dbe7ecc4691 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 @@ -22,6 +22,8 @@ import java.util.List; import java.util.Set; import java.util.stream.Stream; +import javax.sql.DataSource; + import org.junit.jupiter.api.Test; import org.springframework.aot.generate.DefaultGenerationContext; @@ -45,6 +47,7 @@ import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTest 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.jdbc.SqlScriptsSpringJupiterTests; 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; @@ -89,6 +92,7 @@ class TestContextAotGeneratorTests extends AbstractAotTests { BasicSpringJupiterTests.NestedTests.class, BasicSpringTestNGTests.class, BasicSpringVintageTests.class, + SqlScriptsSpringJupiterTests.class, XmlSpringJupiterTests.class, WebSpringJupiterTests.class); @@ -112,7 +116,10 @@ class TestContextAotGeneratorTests extends AbstractAotTests { ApplicationContext context = ((AotContextLoader) mergedConfig.getContextLoader()) .loadContextForAotRuntime(mergedConfig, contextInitializer); if (context instanceof WebApplicationContext wac) { - assertContextForBasicWebTests(wac); + assertContextForWebTests(wac); + } + else if (testClass.getPackageName().contains("jdbc")) { + assertContextForJdbcTests(context); } else { assertContextForBasicTests(context); @@ -227,7 +234,12 @@ class TestContextAotGeneratorTests extends AbstractAotTests { assertThat(messageService.generateMessage()).isEqualTo(expectedMessage); } - private void assertContextForBasicWebTests(WebApplicationContext wac) throws Exception { + private void assertContextForJdbcTests(ApplicationContext context) throws Exception { + assertThat(context.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull(); + assertThat(context.getBean(DataSource.class)).as("DataSource").isNotNull(); + } + + private void assertContextForWebTests(WebApplicationContext wac) throws Exception { assertThat(wac.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull(); MockMvc mockMvc = webAppContextSetup(wac).build(); @@ -339,21 +351,27 @@ class TestContextAotGeneratorTests extends AbstractAotTests { "org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_ApplicationContextInitializer.java", "org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_BeanFactoryRegistrations.java", "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java", - // WebSpringJupiterTests + // SqlScriptsSpringJupiterTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext005_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext005_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext005_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext005_BeanFactoryRegistrations.java", - "org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext005_BeanDefinitions.java", - "org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext005_Autowiring.java", - "org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext005_BeanDefinitions.java", - "org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport__TestContext005_BeanDefinitions.java", - // XmlSpringJupiterTests + "org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_BeanFactoryRegistrations.java", + "org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext005_BeanDefinitions.java", + // WebSpringJupiterTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext006_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext006_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext006_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext006_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext006_BeanFactoryRegistrations.java" + "org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext006_BeanDefinitions.java", + "org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_Autowiring.java", + "org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_BeanDefinitions.java", + "org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport__TestContext006_BeanDefinitions.java", + // XmlSpringJupiterTests + "org/springframework/context/event/DefaultEventListenerFactory__TestContext007_BeanDefinitions.java", + "org/springframework/context/event/EventListenerMethodProcessor__TestContext007_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext007_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_BeanFactoryRegistrations.java" }; } diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.java new file mode 100644 index 00000000000..d8bb4b88d57 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.java @@ -0,0 +1,53 @@ +/* + * 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.jdbc; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.jdbc.EmptyDatabaseConfig; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlMergeMode; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.transaction.annotation.Transactional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.MERGE; +import static org.springframework.test.jdbc.JdbcTestUtils.countRowsInTable; + +/** + * @author Sam Brannen + * @since 6.0 + */ +@SpringJUnitConfig(EmptyDatabaseConfig.class) +@Transactional +@SqlMergeMode(MERGE) +@Sql("/org/springframework/test/context/jdbc/schema.sql") +@DirtiesContext +@TestPropertySource(properties = "test.engine = jupiter") +public class SqlScriptsSpringJupiterTests { + + @Test + @Sql // default script --> org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql + void test(@Autowired JdbcTemplate jdbcTemplate) { + assertThat(countRowsInTable(jdbcTemplate, "user")).isEqualTo(1); + } + +} diff --git a/spring-test/src/test/resources/org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql new file mode 100644 index 00000000000..de81ec8bb73 --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql @@ -0,0 +1 @@ +INSERT INTO user VALUES('Daisy');