1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
import org .apache .commons .logging .Log ;
27
27
import org .apache .commons .logging .LogFactory ;
28
28
29
- import org .jetbrains .annotations .NotNull ;
30
29
import org .springframework .context .ApplicationContext ;
31
30
import org .springframework .core .annotation .AnnotatedElementUtils ;
32
31
import org .springframework .core .io .ByteArrayResource ;
33
32
import org .springframework .core .io .ClassPathResource ;
34
33
import org .springframework .core .io .Resource ;
35
34
import org .springframework .jdbc .datasource .init .ResourceDatabasePopulator ;
35
+ import org .springframework .lang .NonNull ;
36
36
import org .springframework .lang .Nullable ;
37
37
import org .springframework .test .context .TestContext ;
38
38
import org .springframework .test .context .jdbc .Sql .ExecutionPhase ;
84
84
* locate these beans.
85
85
*
86
86
* @author Sam Brannen
87
+ * @author Dmitry Semukhin
87
88
* @since 4.1
88
89
* @see Sql
89
90
* @see SqlConfig
@@ -111,7 +112,7 @@ public final int getOrder() {
111
112
* {@link TestContext} <em>before</em> the current test method.
112
113
*/
113
114
@ Override
114
- public void beforeTestMethod (TestContext testContext ) throws Exception {
115
+ public void beforeTestMethod (TestContext testContext ) {
115
116
executeSqlScripts (testContext , ExecutionPhase .BEFORE_TEST_METHOD );
116
117
}
117
118
@@ -120,43 +121,42 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
120
121
* {@link TestContext} <em>after</em> the current test method.
121
122
*/
122
123
@ Override
123
- public void afterTestMethod (TestContext testContext ) throws Exception {
124
+ public void afterTestMethod (TestContext testContext ) {
124
125
executeSqlScripts (testContext , ExecutionPhase .AFTER_TEST_METHOD );
125
126
}
126
127
127
128
/**
128
129
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
129
130
* {@link TestContext} and {@link ExecutionPhase}.
130
131
*/
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 ());
133
134
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 ());
137
138
if (methodLevelOverrides .isEmpty ()) {
138
- executeScripts (getScriptsFromElement (testContext .getTestClass ()), testContext , executionPhase , true );
139
+ executeScripts (getSqlAnnotationsFor (testContext .getTestClass ()), testContext , executionPhase , true );
139
140
executeScripts (methodLevelSqls , testContext , executionPhase , false );
140
141
} else {
141
142
executeScripts (methodLevelOverrides , testContext , executionPhase , false );
142
143
}
143
144
}
144
145
145
146
/**
146
- * Get SQL scripts configured via {@link Sql @Sql} for the supplied
147
+ * Get the {@link Sql @Sql} annotations declared on the supplied
147
148
* {@link AnnotatedElement}.
148
149
*/
149
- private Set <Sql > getScriptsFromElement (AnnotatedElement annotatedElement ) throws Exception {
150
+ private Set <Sql > getSqlAnnotationsFor (AnnotatedElement annotatedElement ) {
150
151
return AnnotatedElementUtils .getMergedRepeatableAnnotations (annotatedElement , Sql .class , SqlGroup .class );
151
152
}
152
153
153
154
/**
154
- * Execute given {@link Sql @Sql} scripts.
155
- * {@link AnnotatedElement}.
155
+ * Execute SQL scripts for the supplied {@link Sql @Sql} annotations.
156
156
*/
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
+
160
160
for (Sql sql : scripts ) {
161
161
executeSqlScripts (sql , executionPhase , testContext , classLevel );
162
162
}
@@ -172,8 +172,8 @@ private void executeScripts(Iterable<Sql> scripts, TestContext testContext, Exec
172
172
* @param testContext the current {@code TestContext}
173
173
* @param classLevel {@code true} if {@link Sql @Sql} was declared at the class level
174
174
*/
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 ) {
177
177
178
178
if (executionPhase != sql .executionPhase ()) {
179
179
return ;
@@ -185,8 +185,6 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
185
185
mergedSqlConfig , executionPhase , testContext ));
186
186
}
187
187
188
- final ResourceDatabasePopulator populator = configurePopulator (mergedSqlConfig );
189
-
190
188
String [] scripts = getScripts (sql , testContext , classLevel );
191
189
scripts = TestContextResourceUtils .convertToClasspathResourcePaths (testContext .getTestClass (), scripts );
192
190
List <Resource > scriptResources = TestContextResourceUtils .convertToResourceList (
@@ -197,6 +195,8 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
197
195
scriptResources .add (new ByteArrayResource (stmt .getBytes (), "from inlined SQL statement: " + stmt ));
198
196
}
199
197
}
198
+
199
+ ResourceDatabasePopulator populator = configurePopulator (mergedSqlConfig );
200
200
populator .setScripts (scriptResources .toArray (new Resource [0 ]));
201
201
if (logger .isDebugEnabled ()) {
202
202
logger .debug ("Executing SQL scripts: " + ObjectUtils .nullSafeToString (scriptResources ));
@@ -237,16 +237,13 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
237
237
TransactionDefinition .PROPAGATION_REQUIRED );
238
238
TransactionAttribute txAttr = TestContextTransactionUtils .createDelegatingTransactionAttribute (
239
239
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 ));
244
241
}
245
242
}
246
243
247
- @ NotNull
244
+ @ NonNull
248
245
private ResourceDatabasePopulator configurePopulator (MergedSqlConfig mergedSqlConfig ) {
249
- final ResourceDatabasePopulator populator = new ResourceDatabasePopulator ();
246
+ ResourceDatabasePopulator populator = new ResourceDatabasePopulator ();
250
247
populator .setSqlScriptEncoding (mergedSqlConfig .getEncoding ());
251
248
populator .setSeparator (mergedSqlConfig .getSeparator ());
252
249
populator .setCommentPrefix (mergedSqlConfig .getCommentPrefix ());
0 commit comments