Skip to content

Commit e85e769

Browse files
committed
Introduce @SQL tests for AOT processing
1 parent 35bbe9f commit e85e769

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Set;
2323
import java.util.stream.Stream;
2424

25+
import javax.sql.DataSource;
26+
2527
import org.junit.jupiter.api.Test;
2628

2729
import org.springframework.aot.generate.DefaultGenerationContext;
@@ -45,6 +47,7 @@
4547
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
4648
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
4749
import org.springframework.test.context.aot.samples.common.MessageService;
50+
import org.springframework.test.context.aot.samples.jdbc.SqlScriptsSpringJupiterTests;
4851
import org.springframework.test.context.aot.samples.web.WebSpringJupiterTests;
4952
import org.springframework.test.context.aot.samples.web.WebSpringTestNGTests;
5053
import org.springframework.test.context.aot.samples.web.WebSpringVintageTests;
@@ -89,6 +92,7 @@ void processAheadOfTimeAndGenerateAotTestMappings() {
8992
BasicSpringJupiterTests.NestedTests.class,
9093
BasicSpringTestNGTests.class,
9194
BasicSpringVintageTests.class,
95+
SqlScriptsSpringJupiterTests.class,
9296
XmlSpringJupiterTests.class,
9397
WebSpringJupiterTests.class);
9498

@@ -112,7 +116,10 @@ void processAheadOfTimeAndGenerateAotTestMappings() {
112116
ApplicationContext context = ((AotContextLoader) mergedConfig.getContextLoader())
113117
.loadContextForAotRuntime(mergedConfig, contextInitializer);
114118
if (context instanceof WebApplicationContext wac) {
115-
assertContextForBasicWebTests(wac);
119+
assertContextForWebTests(wac);
120+
}
121+
else if (testClass.getPackageName().contains("jdbc")) {
122+
assertContextForJdbcTests(context);
116123
}
117124
else {
118125
assertContextForBasicTests(context);
@@ -227,7 +234,12 @@ private void assertContextForBasicTests(ApplicationContext context) {
227234
assertThat(messageService.generateMessage()).isEqualTo(expectedMessage);
228235
}
229236

230-
private void assertContextForBasicWebTests(WebApplicationContext wac) throws Exception {
237+
private void assertContextForJdbcTests(ApplicationContext context) throws Exception {
238+
assertThat(context.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
239+
assertThat(context.getBean(DataSource.class)).as("DataSource").isNotNull();
240+
}
241+
242+
private void assertContextForWebTests(WebApplicationContext wac) throws Exception {
231243
assertThat(wac.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
232244

233245
MockMvc mockMvc = webAppContextSetup(wac).build();
@@ -339,21 +351,27 @@ record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
339351
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_ApplicationContextInitializer.java",
340352
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_BeanFactoryRegistrations.java",
341353
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java",
342-
// WebSpringJupiterTests
354+
// SqlScriptsSpringJupiterTests
343355
"org/springframework/context/event/DefaultEventListenerFactory__TestContext005_BeanDefinitions.java",
344356
"org/springframework/context/event/EventListenerMethodProcessor__TestContext005_BeanDefinitions.java",
345-
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext005_ApplicationContextInitializer.java",
346-
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext005_BeanFactoryRegistrations.java",
347-
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext005_BeanDefinitions.java",
348-
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext005_Autowiring.java",
349-
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext005_BeanDefinitions.java",
350-
"org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport__TestContext005_BeanDefinitions.java",
351-
// XmlSpringJupiterTests
357+
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_ApplicationContextInitializer.java",
358+
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_BeanFactoryRegistrations.java",
359+
"org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext005_BeanDefinitions.java",
360+
// WebSpringJupiterTests
352361
"org/springframework/context/event/DefaultEventListenerFactory__TestContext006_BeanDefinitions.java",
353362
"org/springframework/context/event/EventListenerMethodProcessor__TestContext006_BeanDefinitions.java",
354-
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext006_BeanDefinitions.java",
355-
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext006_ApplicationContextInitializer.java",
356-
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext006_BeanFactoryRegistrations.java"
363+
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_ApplicationContextInitializer.java",
364+
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_BeanFactoryRegistrations.java",
365+
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext006_BeanDefinitions.java",
366+
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_Autowiring.java",
367+
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_BeanDefinitions.java",
368+
"org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport__TestContext006_BeanDefinitions.java",
369+
// XmlSpringJupiterTests
370+
"org/springframework/context/event/DefaultEventListenerFactory__TestContext007_BeanDefinitions.java",
371+
"org/springframework/context/event/EventListenerMethodProcessor__TestContext007_BeanDefinitions.java",
372+
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext007_BeanDefinitions.java",
373+
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_ApplicationContextInitializer.java",
374+
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_BeanFactoryRegistrations.java"
357375
};
358376

359377
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.aot.samples.jdbc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.jdbc.core.JdbcTemplate;
23+
import org.springframework.test.annotation.DirtiesContext;
24+
import org.springframework.test.context.TestPropertySource;
25+
import org.springframework.test.context.jdbc.EmptyDatabaseConfig;
26+
import org.springframework.test.context.jdbc.Sql;
27+
import org.springframework.test.context.jdbc.SqlMergeMode;
28+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
29+
import org.springframework.transaction.annotation.Transactional;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.MERGE;
33+
import static org.springframework.test.jdbc.JdbcTestUtils.countRowsInTable;
34+
35+
/**
36+
* @author Sam Brannen
37+
* @since 6.0
38+
*/
39+
@SpringJUnitConfig(EmptyDatabaseConfig.class)
40+
@Transactional
41+
@SqlMergeMode(MERGE)
42+
@Sql("/org/springframework/test/context/jdbc/schema.sql")
43+
@DirtiesContext
44+
@TestPropertySource(properties = "test.engine = jupiter")
45+
public class SqlScriptsSpringJupiterTests {
46+
47+
@Test
48+
@Sql // default script --> org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql
49+
void test(@Autowired JdbcTemplate jdbcTemplate) {
50+
assertThat(countRowsInTable(jdbcTemplate, "user")).isEqualTo(1);
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO user VALUES('Daisy');

0 commit comments

Comments
 (0)