Skip to content

Commit ab88762

Browse files
committed
Polish contribution
See gh-1835
1 parent d77b715 commit ab88762

File tree

7 files changed

+150
-108
lines changed

7 files changed

+150
-108
lines changed

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -32,7 +32,7 @@
3232
* database during integration tests.
3333
*
3434
* <p>Method-level declarations override class-level declarations by default.
35-
* This behaviour can be adjusted via {@link MergeMode}
35+
* This behavior can be adjusted by setting the {@link #mergeMode}.
3636
*
3737
* <p>Script execution is performed by the {@link SqlScriptsTestExecutionListener},
3838
* which is enabled by default.
@@ -54,6 +54,7 @@
5454
* <em>composed annotations</em> with attribute overrides.
5555
*
5656
* @author Sam Brannen
57+
* @author Dmitry Semukhin
5758
* @since 4.1
5859
* @see SqlConfig
5960
* @see SqlGroup
@@ -138,6 +139,16 @@
138139
*/
139140
ExecutionPhase executionPhase() default ExecutionPhase.BEFORE_TEST_METHOD;
140141

142+
/**
143+
* Indicates whether this {@code @Sql} annotation should be merged with
144+
* class-level {@code @Sql} annotations or override them.
145+
* <p>The merge mode is ignored if declared in a class-level {@code @Sql}
146+
* annotation.
147+
* <p>Defaults to {@link MergeMode#OVERRIDE OVERRIDE} for backwards compatibility.
148+
* @since 5.2
149+
*/
150+
MergeMode mergeMode() default MergeMode.OVERRIDE;
151+
141152
/**
142153
* Local configuration for the SQL scripts and statements declared within
143154
* this {@code @Sql} annotation.
@@ -147,13 +158,6 @@
147158
*/
148159
SqlConfig config() default @SqlConfig;
149160

150-
/**
151-
* Indicates whether this annotation should be merged with upper-level annotations
152-
* or override them.
153-
* <p>Defaults to {@link MergeMode#OVERRIDE}.
154-
*/
155-
MergeMode mergeMode() default MergeMode.OVERRIDE;
156-
157161

158162
/**
159163
* Enumeration of <em>phases</em> that dictate when SQL scripts are executed.
@@ -174,22 +178,23 @@ enum ExecutionPhase {
174178
}
175179

176180
/**
177-
* Enumeration of <em>modes</em> that dictate whether or not
178-
* declared SQL {@link #scripts} and {@link #statements} are merged
179-
* with the upper-level annotations.
181+
* Enumeration of <em>modes</em> that dictate whether method-level {@code @Sql}
182+
* declarations are merged with class-level {@code @Sql} declarations.
183+
* @since 5.2
180184
*/
181185
enum MergeMode {
182186

183187
/**
184-
* Indicates that locally declared SQL {@link #scripts} and {@link #statements}
185-
* should override the upper-level (e.g. Class-level) annotations.
188+
* Indicates that method-level {@code @Sql} declarations should override
189+
* class-level {@code @Sql} declarations.
186190
*/
187191
OVERRIDE,
188192

189193
/**
190-
* Indicates that locally declared SQL {@link #scripts} and {@link #statements}
191-
* should be merged the upper-level (e.g. Class-level) annotations.
194+
* Indicates that method-level {@code @Sql} declarations should be merged
195+
* with class-level {@code @Sql} declarations.
192196
*/
193197
MERGE
194198
}
199+
195200
}

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -26,13 +26,13 @@
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828

29-
import org.jetbrains.annotations.NotNull;
3029
import org.springframework.context.ApplicationContext;
3130
import org.springframework.core.annotation.AnnotatedElementUtils;
3231
import org.springframework.core.io.ByteArrayResource;
3332
import org.springframework.core.io.ClassPathResource;
3433
import org.springframework.core.io.Resource;
3534
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
35+
import org.springframework.lang.NonNull;
3636
import org.springframework.lang.Nullable;
3737
import org.springframework.test.context.TestContext;
3838
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
@@ -84,6 +84,7 @@
8484
* locate these beans.
8585
*
8686
* @author Sam Brannen
87+
* @author Dmitry Semukhin
8788
* @since 4.1
8889
* @see Sql
8990
* @see SqlConfig
@@ -111,7 +112,7 @@ public final int getOrder() {
111112
* {@link TestContext} <em>before</em> the current test method.
112113
*/
113114
@Override
114-
public void beforeTestMethod(TestContext testContext) throws Exception {
115+
public void beforeTestMethod(TestContext testContext) {
115116
executeSqlScripts(testContext, ExecutionPhase.BEFORE_TEST_METHOD);
116117
}
117118

@@ -120,43 +121,42 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
120121
* {@link TestContext} <em>after</em> the current test method.
121122
*/
122123
@Override
123-
public void afterTestMethod(TestContext testContext) throws Exception {
124+
public void afterTestMethod(TestContext testContext) {
124125
executeSqlScripts(testContext, ExecutionPhase.AFTER_TEST_METHOD);
125126
}
126127

127128
/**
128129
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
129130
* {@link TestContext} and {@link ExecutionPhase}.
130131
*/
131-
private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception {
132-
Set<Sql> methodLevelSqls = getScriptsFromElement(testContext.getTestMethod());
132+
private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) {
133+
Set<Sql> methodLevelSqls = getSqlAnnotationsFor(testContext.getTestMethod());
133134
List<Sql> methodLevelOverrides = methodLevelSqls.stream()
134-
.filter(s -> s.executionPhase() == executionPhase)
135-
.filter(s -> s.mergeMode() == Sql.MergeMode.OVERRIDE)
136-
.collect(Collectors.toList());
135+
.filter(s -> s.executionPhase() == executionPhase)
136+
.filter(s -> s.mergeMode() == Sql.MergeMode.OVERRIDE)
137+
.collect(Collectors.toList());
137138
if (methodLevelOverrides.isEmpty()) {
138-
executeScripts(getScriptsFromElement(testContext.getTestClass()), testContext, executionPhase, true);
139+
executeScripts(getSqlAnnotationsFor(testContext.getTestClass()), testContext, executionPhase, true);
139140
executeScripts(methodLevelSqls, testContext, executionPhase, false);
140141
} else {
141142
executeScripts(methodLevelOverrides, testContext, executionPhase, false);
142143
}
143144
}
144145

145146
/**
146-
* Get SQL scripts configured via {@link Sql @Sql} for the supplied
147+
* Get the {@link Sql @Sql} annotations declared on the supplied
147148
* {@link AnnotatedElement}.
148149
*/
149-
private Set<Sql> getScriptsFromElement(AnnotatedElement annotatedElement) throws Exception {
150+
private Set<Sql> getSqlAnnotationsFor(AnnotatedElement annotatedElement) {
150151
return AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement, Sql.class, SqlGroup.class);
151152
}
152153

153154
/**
154-
* Execute given {@link Sql @Sql} scripts.
155-
* {@link AnnotatedElement}.
155+
* Execute SQL scripts for the supplied {@link Sql @Sql} annotations.
156156
*/
157-
private void executeScripts(Iterable<Sql> scripts, TestContext testContext, ExecutionPhase executionPhase,
158-
boolean classLevel)
159-
throws Exception {
157+
private void executeScripts(
158+
Iterable<Sql> scripts, TestContext testContext, ExecutionPhase executionPhase, boolean classLevel) {
159+
160160
for (Sql sql : scripts) {
161161
executeSqlScripts(sql, executionPhase, testContext, classLevel);
162162
}
@@ -172,8 +172,8 @@ private void executeScripts(Iterable<Sql> scripts, TestContext testContext, Exec
172172
* @param testContext the current {@code TestContext}
173173
* @param classLevel {@code true} if {@link Sql @Sql} was declared at the class level
174174
*/
175-
private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel)
176-
throws Exception {
175+
private void executeSqlScripts(
176+
Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel) {
177177

178178
if (executionPhase != sql.executionPhase()) {
179179
return;
@@ -185,8 +185,6 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
185185
mergedSqlConfig, executionPhase, testContext));
186186
}
187187

188-
final ResourceDatabasePopulator populator = configurePopulator(mergedSqlConfig);
189-
190188
String[] scripts = getScripts(sql, testContext, classLevel);
191189
scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
192190
List<Resource> scriptResources = TestContextResourceUtils.convertToResourceList(
@@ -197,6 +195,8 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
197195
scriptResources.add(new ByteArrayResource(stmt.getBytes(), "from inlined SQL statement: " + stmt));
198196
}
199197
}
198+
199+
ResourceDatabasePopulator populator = configurePopulator(mergedSqlConfig);
200200
populator.setScripts(scriptResources.toArray(new Resource[0]));
201201
if (logger.isDebugEnabled()) {
202202
logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scriptResources));
@@ -237,16 +237,13 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
237237
TransactionDefinition.PROPAGATION_REQUIRED);
238238
TransactionAttribute txAttr = TestContextTransactionUtils.createDelegatingTransactionAttribute(
239239
testContext, new DefaultTransactionAttribute(propagation));
240-
new TransactionTemplate(txMgr, txAttr).execute(status -> {
241-
populator.execute(finalDataSource);
242-
return null;
243-
});
240+
new TransactionTemplate(txMgr, txAttr).execute(() -> populator.execute(finalDataSource));
244241
}
245242
}
246243

247-
@NotNull
244+
@NonNull
248245
private ResourceDatabasePopulator configurePopulator(MergedSqlConfig mergedSqlConfig) {
249-
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
246+
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
250247
populator.setSqlScriptEncoding(mergedSqlConfig.getEncoding());
251248
populator.setSeparator(mergedSqlConfig.getSeparator());
252249
populator.setCommentPrefix(mergedSqlConfig.getCommentPrefix());

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.test.annotation.DirtiesContext;
2626
import org.springframework.test.context.ContextConfiguration;
2727
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
28-
import org.springframework.test.jdbc.JdbcTestUtils;
2928

3029
import static org.assertj.core.api.Assertions.assertThat;
3130

@@ -59,10 +58,6 @@ public void test02_methodLevelScripts() {
5958
assertNumUsers(2);
6059
}
6160

62-
protected int countRowsInTable(String tableName) {
63-
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
64-
}
65-
6661
protected void assertNumUsers(int expected) {
6762
assertThat(countRowsInTable("user")).as("Number of rows in the 'user' table.").isEqualTo(expected);
6863
}

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

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2019 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.Test;
20+
21+
import org.springframework.test.annotation.DirtiesContext;
22+
import org.springframework.test.context.ContextConfiguration;
23+
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.springframework.test.context.jdbc.Sql.MergeMode.MERGE;
27+
28+
/**
29+
* Transactional integration tests for {@link Sql @Sql} that verify proper
30+
* merging support for class-level and method-level declarations.
31+
*
32+
* @author Dmitry Semukhin
33+
* @author Sam Brannen
34+
* @since 5.2
35+
*/
36+
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
37+
@Sql({ "schema.sql", "data-add-catbert.sql" })
38+
@DirtiesContext
39+
public class SqlMethodMergeTests extends AbstractTransactionalJUnit4SpringContextTests {
40+
41+
@Test
42+
@Sql(scripts = "data-add-dogbert.sql", mergeMode = MERGE)
43+
public void testMerge() {
44+
assertNumUsers(2);
45+
}
46+
47+
protected void assertNumUsers(int expected) {
48+
assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user"));
49+
}
50+
51+
}

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

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)