1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2011 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.
27
27
28
28
import org .apache .commons .logging .Log ;
29
29
import org .apache .commons .logging .LogFactory ;
30
-
31
30
import org .springframework .beans .BeansException ;
32
31
import org .springframework .beans .factory .BeanFactory ;
33
32
import org .springframework .core .annotation .AnnotationUtils ;
50
49
51
50
/**
52
51
* <p>
53
- * <code>TestExecutionListener</code> which provides support for executing
54
- * tests within transactions by using
52
+ * <code>TestExecutionListener</code> that provides support for executing
53
+ * tests within transactions by using the
55
54
* {@link org.springframework.transaction.annotation.Transactional @Transactional}
56
55
* and {@link NotTransactional @NotTransactional} annotations.
57
56
* </p>
58
57
* <p>
59
- * Changes to the database during a test run with @Transactional will be
58
+ * Changes to the database during a test that is run with @Transactional will be
60
59
* run within a transaction that will, by default, be automatically
61
60
* <em>rolled back</em> after completion of the test; whereas, changes to the
62
- * database during a test run with @NotTransactional will <strong>not</strong>
63
- * be run within a transaction. Similarly, test methods that are not annotated
64
- * with either @Transactional (at the class or method level) or
65
- * @NotTransactional will not be run within a transaction.
61
+ * database during a test that is run with @NotTransactional will <strong>not</strong>
62
+ * be run within a transaction. Test methods that are not annotated with either
63
+ * @Transactional (at the class or method level) or @NotTransactional
64
+ * will not be run within a transaction.
66
65
* </p>
67
66
* <p>
68
67
* Transactional commit and rollback behavior can be configured via the
73
72
* is to be used to drive transactions.
74
73
* </p>
75
74
* <p>
76
- * When executing transactional tests, it is sometimes useful to be able execute
75
+ * When executing transactional tests, it is sometimes useful to be able to execute
77
76
* certain <em>set up</em> or <em>tear down</em> code outside of a
78
77
* transaction. <code>TransactionalTestExecutionListener</code> provides such
79
78
* support for methods annotated with
@@ -102,8 +101,8 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
102
101
103
102
private volatile int transactionsStarted = 0 ;
104
103
105
- private final Map <Method , TransactionContext > transactionContextCache =
106
- Collections . synchronizedMap ( new IdentityHashMap < Method , TransactionContext >());
104
+ private final Map <Method , TransactionContext > transactionContextCache = Collections . synchronizedMap ( new IdentityHashMap < Method , TransactionContext >());
105
+
107
106
108
107
/**
109
108
* If the test method of the supplied {@link TestContext test context} is
@@ -123,19 +122,20 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
123
122
Assert .notNull (testMethod , "The test method of the supplied TestContext must not be null" );
124
123
125
124
if (this .transactionContextCache .remove (testMethod ) != null ) {
126
- throw new IllegalStateException ("Cannot start new transaction without ending existing transaction: " +
127
- "Invoke endTransaction() before startNewTransaction()." );
125
+ throw new IllegalStateException ("Cannot start new transaction without ending existing transaction: "
126
+ + "Invoke endTransaction() before startNewTransaction()." );
128
127
}
129
128
130
129
if (testMethod .isAnnotationPresent (NotTransactional .class )) {
131
130
return ;
132
131
}
133
132
134
- TransactionAttribute transactionAttribute =
135
- this . attributeSource . getTransactionAttribute ( testMethod , testContext .getTestClass ());
133
+ TransactionAttribute transactionAttribute = this . attributeSource . getTransactionAttribute ( testMethod ,
134
+ testContext .getTestClass ());
136
135
TransactionDefinition transactionDefinition = null ;
137
136
if (transactionAttribute != null ) {
138
137
transactionDefinition = new DelegatingTransactionAttribute (transactionAttribute ) {
138
+
139
139
public String getName () {
140
140
return testMethod .getName ();
141
141
}
@@ -144,14 +144,15 @@ public String getName() {
144
144
145
145
if (transactionDefinition != null ) {
146
146
if (logger .isDebugEnabled ()) {
147
- logger .debug ("Explicit transaction definition [" + transactionDefinition +
148
- "] found for test context [" + testContext + "]" );
147
+ logger .debug ("Explicit transaction definition [" + transactionDefinition + "] found for test context ["
148
+ + testContext + "]" );
149
149
}
150
150
String qualifier = transactionAttribute .getQualifier ();
151
151
PlatformTransactionManager tm ;
152
152
if (StringUtils .hasLength (qualifier )) {
153
- // Use autowire-capable factory in order to support extended qualifier matching
154
- // (only exposed on the internal BeanFactory, not on the ApplicationContext).
153
+ // Use autowire-capable factory in order to support extended
154
+ // qualifier matching (only exposed on the internal BeanFactory,
155
+ // not on the ApplicationContext).
155
156
BeanFactory bf = testContext .getApplicationContext ().getAutowireCapableBeanFactory ();
156
157
tm = TransactionAspectUtils .getTransactionManager (bf , qualifier );
157
158
}
@@ -231,8 +232,8 @@ protected void runAfterTransactionMethods(TestContext testContext) throws Except
231
232
for (Method method : methods ) {
232
233
try {
233
234
if (logger .isDebugEnabled ()) {
234
- logger .debug ("Executing @AfterTransaction method [" + method + "] for test context [" +
235
- testContext + "]" );
235
+ logger .debug ("Executing @AfterTransaction method [" + method + "] for test context [" + testContext
236
+ + "]" );
236
237
}
237
238
method .invoke (testContext .getTestInstance ());
238
239
}
@@ -241,15 +242,15 @@ protected void runAfterTransactionMethods(TestContext testContext) throws Except
241
242
if (afterTransactionException == null ) {
242
243
afterTransactionException = targetException ;
243
244
}
244
- logger .error ("Exception encountered while executing @AfterTransaction method [" + method +
245
- "] for test context [" + testContext + "]" , targetException );
245
+ logger .error ("Exception encountered while executing @AfterTransaction method [" + method
246
+ + "] for test context [" + testContext + "]" , targetException );
246
247
}
247
248
catch (Exception ex ) {
248
249
if (afterTransactionException == null ) {
249
250
afterTransactionException = ex ;
250
251
}
251
- logger .error ("Exception encountered while executing @AfterTransaction method [" + method +
252
- "] for test context [" + testContext + "]" , ex );
252
+ logger .error ("Exception encountered while executing @AfterTransaction method [" + method
253
+ + "] for test context [" + testContext + "]" , ex );
253
254
}
254
255
}
255
256
@@ -270,8 +271,8 @@ private void startNewTransaction(TestContext testContext, TransactionContext txC
270
271
txContext .startTransaction ();
271
272
++this .transactionsStarted ;
272
273
if (logger .isInfoEnabled ()) {
273
- logger .info ("Began transaction (" + this .transactionsStarted + "): transaction manager [" +
274
- txContext .transactionManager + "]; rollback [" + isRollback (testContext ) + "]" );
274
+ logger .info ("Began transaction (" + this .transactionsStarted + "): transaction manager ["
275
+ + txContext .transactionManager + "]; rollback [" + isRollback (testContext ) + "]" );
275
276
}
276
277
}
277
278
@@ -285,13 +286,13 @@ private void startNewTransaction(TestContext testContext, TransactionContext txC
285
286
private void endTransaction (TestContext testContext , TransactionContext txContext ) throws Exception {
286
287
boolean rollback = isRollback (testContext );
287
288
if (logger .isTraceEnabled ()) {
288
- logger .trace ("Ending transaction for test context [" + testContext + "]; transaction manager [" +
289
- txContext .transactionStatus + "]; rollback [" + rollback + "]" );
289
+ logger .trace ("Ending transaction for test context [" + testContext + "]; transaction manager ["
290
+ + txContext .transactionStatus + "]; rollback [" + rollback + "]" );
290
291
}
291
292
txContext .endTransaction (rollback );
292
293
if (logger .isInfoEnabled ()) {
293
- logger .info ((rollback ? "Rolled back" : "Committed" ) +
294
- " transaction after test execution for test context [" + testContext + "]" );
294
+ logger .info ((rollback ? "Rolled back" : "Committed" )
295
+ + " transaction after test execution for test context [" + testContext + "]" );
295
296
}
296
297
}
297
298
@@ -310,8 +311,8 @@ protected final PlatformTransactionManager getTransactionManager(TestContext tes
310
311
}
311
312
catch (BeansException ex ) {
312
313
if (logger .isWarnEnabled ()) {
313
- logger .warn ("Caught exception while retrieving transaction manager with bean name [" +
314
- tmName + "] for test context [" + testContext + "]" , ex );
314
+ logger .warn ("Caught exception while retrieving transaction manager with bean name [" + tmName
315
+ + "] for test context [" + testContext + "]" , ex );
315
316
}
316
317
throw ex ;
317
318
}
@@ -345,15 +346,15 @@ protected final boolean isRollback(TestContext testContext) throws Exception {
345
346
if (rollbackAnnotation != null ) {
346
347
boolean rollbackOverride = rollbackAnnotation .value ();
347
348
if (logger .isDebugEnabled ()) {
348
- logger .debug ("Method-level @Rollback(" + rollbackOverride + ") overrides default rollback [" +
349
- rollback + "] for test context [" + testContext + "]" );
349
+ logger .debug ("Method-level @Rollback(" + rollbackOverride + ") overrides default rollback [" + rollback
350
+ + "] for test context [" + testContext + "]" );
350
351
}
351
352
rollback = rollbackOverride ;
352
353
}
353
354
else {
354
355
if (logger .isDebugEnabled ()) {
355
- logger .debug ("No method-level @Rollback override: using default rollback [" +
356
- rollback + "] for test context [" + testContext + "]" );
356
+ logger .debug ("No method-level @Rollback override: using default rollback [" + rollback
357
+ + "] for test context [" + testContext + "]" );
357
358
}
358
359
}
359
360
return rollback ;
@@ -478,16 +479,18 @@ private TransactionConfigurationAttributes retrieveConfigurationAttributes(TestC
478
479
defaultRollback = (Boolean ) AnnotationUtils .getDefaultValue (annotationType , "defaultRollback" );
479
480
}
480
481
481
- TransactionConfigurationAttributes configAttributes =
482
- new TransactionConfigurationAttributes ( transactionManagerName , defaultRollback );
482
+ TransactionConfigurationAttributes configAttributes = new TransactionConfigurationAttributes (
483
+ transactionManagerName , defaultRollback );
483
484
if (logger .isDebugEnabled ()) {
484
- logger .debug ("Retrieved TransactionConfigurationAttributes [" + configAttributes + "] for class [" + clazz + "]" );
485
+ logger .debug ("Retrieved TransactionConfigurationAttributes [" + configAttributes + "] for class ["
486
+ + clazz + "]" );
485
487
}
486
488
this .configurationAttributes = configAttributes ;
487
489
}
488
490
return this .configurationAttributes ;
489
491
}
490
492
493
+
491
494
/**
492
495
* Internal context holder for a specific test method.
493
496
*/
@@ -499,7 +502,9 @@ private static class TransactionContext {
499
502
500
503
private TransactionStatus transactionStatus ;
501
504
502
- public TransactionContext (PlatformTransactionManager transactionManager , TransactionDefinition transactionDefinition ) {
505
+
506
+ public TransactionContext (PlatformTransactionManager transactionManager ,
507
+ TransactionDefinition transactionDefinition ) {
503
508
this .transactionManager = transactionManager ;
504
509
this .transactionDefinition = transactionDefinition ;
505
510
}
0 commit comments