@@ -127,11 +127,11 @@ public void afterTestMethod(TestContext testContext) throws Exception {
127
127
private void executeSqlScripts (TestContext testContext , ExecutionPhase executionPhase ) throws Exception {
128
128
boolean classLevel = false ;
129
129
130
- Set <Sql > sqlAnnotations = AnnotatedElementUtils .getMergedRepeatableAnnotations (testContext . getTestMethod (), Sql . class ,
131
- SqlGroup .class );
130
+ Set <Sql > sqlAnnotations = AnnotatedElementUtils .getMergedRepeatableAnnotations (
131
+ testContext . getTestMethod (), Sql . class , SqlGroup .class );
132
132
if (sqlAnnotations .isEmpty ()) {
133
- sqlAnnotations = AnnotatedElementUtils .getMergedRepeatableAnnotations (testContext . getTestClass (), Sql . class ,
134
- SqlGroup .class );
133
+ sqlAnnotations = AnnotatedElementUtils .getMergedRepeatableAnnotations (
134
+ testContext . getTestClass (), Sql . class , SqlGroup .class );
135
135
if (!sqlAnnotations .isEmpty ()) {
136
136
classLevel = true ;
137
137
}
@@ -145,26 +145,24 @@ private void executeSqlScripts(TestContext testContext, ExecutionPhase execution
145
145
/**
146
146
* Execute the SQL scripts configured via the supplied {@link Sql @Sql}
147
147
* annotation for the given {@link ExecutionPhase} and {@link TestContext}.
148
- *
149
148
* <p>Special care must be taken in order to properly support the configured
150
149
* {@link SqlConfig#transactionMode}.
151
- *
152
150
* @param sql the {@code @Sql} annotation to parse
153
151
* @param executionPhase the current execution phase
154
152
* @param testContext the current {@code TestContext}
155
- * @param classLevel {@code true} if {@link Sql @Sql} was declared at the
156
- * class level
153
+ * @param classLevel {@code true} if {@link Sql @Sql} was declared at the class level
157
154
*/
158
155
private void executeSqlScripts (Sql sql , ExecutionPhase executionPhase , TestContext testContext , boolean classLevel )
159
156
throws Exception {
157
+
160
158
if (executionPhase != sql .executionPhase ()) {
161
159
return ;
162
160
}
163
161
164
162
MergedSqlConfig mergedSqlConfig = new MergedSqlConfig (sql .config (), testContext .getTestClass ());
165
163
if (logger .isDebugEnabled ()) {
166
- logger .debug (String .format ("Processing %s for execution phase [%s] and test context %s." , mergedSqlConfig ,
167
- executionPhase , testContext ));
164
+ logger .debug (String .format ("Processing %s for execution phase [%s] and test context %s." ,
165
+ mergedSqlConfig , executionPhase , testContext ));
168
166
}
169
167
170
168
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator ();
@@ -179,15 +177,13 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
179
177
String [] scripts = getScripts (sql , testContext , classLevel );
180
178
scripts = TestContextResourceUtils .convertToClasspathResourcePaths (testContext .getTestClass (), scripts );
181
179
List <Resource > scriptResources = TestContextResourceUtils .convertToResourceList (
182
- testContext .getApplicationContext (), scripts );
183
-
184
- for (String statement : sql .statements ()) {
185
- if (StringUtils .hasText (statement )) {
186
- statement = statement .trim ();
187
- scriptResources .add (new ByteArrayResource (statement .getBytes (), "from inlined SQL statement: " + statement ));
180
+ testContext .getApplicationContext (), scripts );
181
+ for (String stmt : sql .statements ()) {
182
+ if (StringUtils .hasText (stmt )) {
183
+ stmt = stmt .trim ();
184
+ scriptResources .add (new ByteArrayResource (stmt .getBytes (), "from inlined SQL statement: " + stmt ));
188
185
}
189
186
}
190
-
191
187
populator .setScripts (scriptResources .toArray (new Resource [scriptResources .size ()]));
192
188
if (logger .isDebugEnabled ()) {
193
189
logger .debug ("Executing SQL scripts: " + ObjectUtils .nullSafeToString (scriptResources ));
@@ -196,47 +192,39 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
196
192
String dsName = mergedSqlConfig .getDataSource ();
197
193
String tmName = mergedSqlConfig .getTransactionManager ();
198
194
DataSource dataSource = TestContextTransactionUtils .retrieveDataSource (testContext , dsName );
199
- final PlatformTransactionManager transactionManager = TestContextTransactionUtils .retrieveTransactionManager (
200
- testContext , tmName );
201
- final boolean newTxRequired = mergedSqlConfig .getTransactionMode () == TransactionMode .ISOLATED ;
195
+ PlatformTransactionManager txMgr = TestContextTransactionUtils .retrieveTransactionManager (testContext , tmName );
196
+ boolean newTxRequired = (mergedSqlConfig .getTransactionMode () == TransactionMode .ISOLATED );
202
197
203
- if (transactionManager == null ) {
198
+ if (txMgr == null ) {
204
199
Assert .state (!newTxRequired , () -> String .format ("Failed to execute SQL scripts for test context %s: " +
205
200
"cannot execute SQL scripts using Transaction Mode " +
206
201
"[%s] without a PlatformTransactionManager." , testContext , TransactionMode .ISOLATED ));
207
-
208
202
Assert .state (dataSource != null , () -> String .format ("Failed to execute SQL scripts for test context %s: " +
209
203
"supply at least a DataSource or PlatformTransactionManager." , testContext ));
210
-
211
204
// Execute scripts directly against the DataSource
212
205
populator .execute (dataSource );
213
206
}
214
207
else {
215
- DataSource dataSourceFromTxMgr = getDataSourceFromTransactionManager (transactionManager );
216
-
208
+ DataSource dataSourceFromTxMgr = getDataSourceFromTransactionManager (txMgr );
217
209
// Ensure user configured an appropriate DataSource/TransactionManager pair.
218
210
if (dataSource != null && dataSourceFromTxMgr != null && !dataSource .equals (dataSourceFromTxMgr )) {
219
211
throw new IllegalStateException (String .format ("Failed to execute SQL scripts for test context %s: " +
220
212
"the configured DataSource [%s] (named '%s') is not the one associated with " +
221
213
"transaction manager [%s] (named '%s')." , testContext , dataSource .getClass ().getName (),
222
- dsName , transactionManager .getClass ().getName (), tmName ));
214
+ dsName , txMgr .getClass ().getName (), tmName ));
223
215
}
224
-
225
216
if (dataSource == null ) {
226
217
dataSource = dataSourceFromTxMgr ;
227
- Assert .state (dataSource != null , () -> String .format ("Failed to execute SQL scripts for test context %s: " +
228
- "could not obtain DataSource from transaction manager [%s] (named '%s')." , testContext ,
229
- transactionManager .getClass ().getName (), tmName ));
218
+ Assert .state (dataSource != null , () -> String .format ("Failed to execute SQL scripts for " +
219
+ "test context %s: could not obtain DataSource from transaction manager [%s] (named '%s')." ,
220
+ testContext , txMgr .getClass ().getName (), tmName ));
230
221
}
231
-
232
222
final DataSource finalDataSource = dataSource ;
233
223
int propagation = (newTxRequired ? TransactionDefinition .PROPAGATION_REQUIRES_NEW :
234
224
TransactionDefinition .PROPAGATION_REQUIRED );
235
-
236
- TransactionAttribute transactionAttribute = TestContextTransactionUtils .createDelegatingTransactionAttribute (
237
- testContext , new DefaultTransactionAttribute (propagation ));
238
-
239
- new TransactionTemplate (transactionManager , transactionAttribute ).execute (status -> {
225
+ TransactionAttribute txAttr = TestContextTransactionUtils .createDelegatingTransactionAttribute (
226
+ testContext , new DefaultTransactionAttribute (propagation ));
227
+ new TransactionTemplate (txMgr , txAttr ).execute (status -> {
240
228
populator .execute (finalDataSource );
241
229
return null ;
242
230
});
@@ -260,7 +248,7 @@ private DataSource getDataSourceFromTransactionManager(PlatformTransactionManage
260
248
private String [] getScripts (Sql sql , TestContext testContext , boolean classLevel ) {
261
249
String [] scripts = sql .scripts ();
262
250
if (ObjectUtils .isEmpty (scripts ) && ObjectUtils .isEmpty (sql .statements ())) {
263
- scripts = new String [] { detectDefaultScript (testContext , classLevel ) };
251
+ scripts = new String [] {detectDefaultScript (testContext , classLevel )};
264
252
}
265
253
return scripts ;
266
254
}
@@ -286,8 +274,8 @@ private String detectDefaultScript(TestContext testContext, boolean classLevel)
286
274
287
275
if (classPathResource .exists ()) {
288
276
if (logger .isInfoEnabled ()) {
289
- logger .info (String .format ("Detected default SQL script \" %s\" for test %s [%s]" , prefixedResourcePath ,
290
- elementType , elementName ));
277
+ logger .info (String .format ("Detected default SQL script \" %s\" for test %s [%s]" ,
278
+ prefixedResourcePath , elementType , elementName ));
291
279
}
292
280
return prefixedResourcePath ;
293
281
}
0 commit comments