Skip to content

Commit abcad5d

Browse files
committed
Support property placeholders in @⁠Sql script paths
Prior to this commit, paths configured via the scripts attribute in @⁠Sql were required to be final paths without dynamic placeholders; however, being able to make script paths dependent on the current environment can be useful in certain testing scenarios. This commit introduces support for property placeholders (${...}) in @⁠Sql script paths which will be replaced by properties available in the Environment of the test's ApplicationContext. Closes gh-33114
1 parent 384d0e4 commit abcad5d

File tree

8 files changed

+100
-2
lines changed

8 files changed

+100
-2
lines changed

framework-docs/modules/ROOT/pages/testing/testcontext-framework/executing-sql.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ classpath resource (for example, `"/org/example/schema.sql"`). A path that refer
122122
URL (for example, a path prefixed with `classpath:`, `file:`, `http:`) is loaded by using
123123
the specified resource protocol.
124124

125+
As of Spring Framework 6.2, paths may contain property placeholders (`${...}`) that will
126+
be replaced by properties stored in the `Environment` of the test's `ApplicationContext`.
127+
125128
The following example shows how to use `@Sql` at the class level and at the method level
126129
within a JUnit Jupiter based integration test class:
127130

spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
* {@link org.springframework.util.ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
116116
* {@link org.springframework.util.ResourceUtils#FILE_URL_PREFIX file:},
117117
* {@code http:}, etc.) will be loaded using the specified resource protocol.
118+
* <p>As of Spring Framework 6.2, paths may contain property placeholders
119+
* (<code>${...}</code>) that will be replaced by properties stored in the
120+
* {@link org.springframework.core.env.Environment Environment} of the test's
121+
* {@code ApplicationContext}.
118122
* <h4>Default Script Detection</h4>
119123
* <p>If no SQL scripts or {@link #statements} are specified, an attempt will
120124
* be made to detect a <em>default</em> script depending on where this
@@ -131,6 +135,7 @@
131135
* </ul>
132136
* @see #value
133137
* @see #statements
138+
* @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String)
134139
*/
135140
@AliasFor("value")
136141
String[] scripts() default {};

spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,9 @@ else if (logger.isDebugEnabled()) {
308308
Method testMethod = (methodLevel ? testContext.getTestMethod() : null);
309309

310310
String[] scripts = getScripts(sql, testContext.getTestClass(), testMethod, classLevel);
311+
ApplicationContext applicationContext = testContext.getApplicationContext();
311312
List<Resource> scriptResources = TestContextResourceUtils.convertToResourceList(
312-
testContext.getApplicationContext(), scripts);
313+
applicationContext, applicationContext.getEnvironment(), scripts);
313314
for (String stmt : sql.statements()) {
314315
if (StringUtils.hasText(stmt)) {
315316
stmt = stmt.trim();

spring-test/src/main/java/org/springframework/test/context/util/TestContextResourceUtils.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.util.stream.Collectors;
2424
import java.util.stream.Stream;
2525

26+
import org.springframework.core.env.Environment;
2627
import org.springframework.core.io.Resource;
2728
import org.springframework.core.io.ResourceLoader;
2829
import org.springframework.core.io.support.ResourcePatternUtils;
@@ -145,6 +146,28 @@ public static List<Resource> convertToResourceList(ResourceLoader resourceLoader
145146
return stream(resourceLoader, paths).collect(Collectors.toCollection(ArrayList::new));
146147
}
147148

149+
/**
150+
* Convert the supplied paths to a list of {@link Resource} handles using the given
151+
* {@link ResourceLoader} and {@link Environment}.
152+
* @param resourceLoader the {@code ResourceLoader} to use to convert the paths
153+
* @param environment the {@code Environment} to use to resolve property placeholders
154+
* in the paths
155+
* @param paths the paths to be converted
156+
* @return a new, mutable list of resources
157+
* @since 6.2
158+
* @see #convertToResources(ResourceLoader, String...)
159+
* @see #convertToClasspathResourcePaths
160+
* @see Environment#resolveRequiredPlaceholders(String)
161+
*/
162+
public static List<Resource> convertToResourceList(
163+
ResourceLoader resourceLoader, Environment environment, String... paths) {
164+
165+
return Arrays.stream(paths)
166+
.map(environment::resolveRequiredPlaceholders)
167+
.map(resourceLoader::getResource)
168+
.collect(Collectors.toCollection(ArrayList::new));
169+
}
170+
148171
private static Stream<Resource> stream(ResourceLoader resourceLoader, String... paths) {
149172
return Arrays.stream(paths).map(resourceLoader::getResource);
150173
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2002-2024 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.jdbc;
18+
19+
import org.junit.jupiter.api.Nested;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.test.annotation.DirtiesContext;
23+
import org.springframework.test.context.ContextConfiguration;
24+
import org.springframework.test.context.TestPropertySource;
25+
26+
/**
27+
* Integration tests that verify support for property placeholders in SQL script locations.
28+
*
29+
* @author Sam Brannen
30+
* @since 6.2
31+
*/
32+
@ContextConfiguration(classes = PopulatedSchemaDatabaseConfig.class)
33+
class PropertyPlaceholderSqlScriptsTests {
34+
35+
private static final String SCRIPT_LOCATION = "classpath:org/springframework/test/context/jdbc/${vendor}/data.sql";
36+
37+
@Nested
38+
@TestPropertySource(properties = "vendor = db1")
39+
@DirtiesContext
40+
class DatabaseOneTests extends AbstractTransactionalTests {
41+
42+
@Test
43+
@Sql(SCRIPT_LOCATION)
44+
void placeholderIsResolvedInScriptLocation() {
45+
assertUsers("Dilbert 1");
46+
}
47+
}
48+
49+
@Nested
50+
@TestPropertySource(properties = "vendor = db2")
51+
@DirtiesContext
52+
class DatabaseTwoTests extends AbstractTransactionalTests {
53+
54+
@Test
55+
@Sql(SCRIPT_LOCATION)
56+
void placeholderIsResolvedInScriptLocation() {
57+
assertUsers("Dilbert 2");
58+
}
59+
}
60+
61+
}

spring-test/src/test/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListenerTests.java

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.context.ApplicationContext;
2323
import org.springframework.core.annotation.AnnotationConfigurationException;
24+
import org.springframework.mock.env.MockEnvironment;
2425
import org.springframework.test.context.TestContext;
2526

2627
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -84,6 +85,7 @@ void isolatedTxModeDeclaredWithoutTxMgr() throws Exception {
8485
ApplicationContext ctx = mock();
8586
given(ctx.getResource(anyString())).willReturn(mock());
8687
given(ctx.getAutowireCapableBeanFactory()).willReturn(mock());
88+
given(ctx.getEnvironment()).willReturn(new MockEnvironment());
8789

8890
Class<?> clazz = IsolatedWithoutTxMgr.class;
8991
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
@@ -98,6 +100,7 @@ void missingDataSourceAndTxMgr() throws Exception {
98100
ApplicationContext ctx = mock();
99101
given(ctx.getResource(anyString())).willReturn(mock());
100102
given(ctx.getAutowireCapableBeanFactory()).willReturn(mock());
103+
given(ctx.getEnvironment()).willReturn(new MockEnvironment());
101104

102105
Class<?> clazz = MissingDataSourceAndTxMgr.class;
103106
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO user VALUES('Dilbert 1');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO user VALUES('Dilbert 2');

0 commit comments

Comments
 (0)