30
30
import org .junit .Test ;
31
31
32
32
import org .springframework .beans .factory .annotation .Autowired ;
33
+ import org .springframework .context .ApplicationEventPublisher ;
33
34
import org .springframework .context .ConfigurableApplicationContext ;
34
35
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
35
36
import org .springframework .context .annotation .Bean ;
39
40
import org .springframework .stereotype .Component ;
40
41
import org .springframework .tests .transaction .CallCountingTransactionManager ;
41
42
import org .springframework .transaction .annotation .EnableTransactionManagement ;
43
+ import org .springframework .transaction .annotation .Propagation ;
42
44
import org .springframework .transaction .annotation .Transactional ;
43
45
import org .springframework .transaction .support .TransactionSynchronizationAdapter ;
44
46
import org .springframework .transaction .support .TransactionSynchronizationManager ;
45
47
import org .springframework .transaction .support .TransactionTemplate ;
46
48
import org .springframework .util .LinkedMultiValueMap ;
47
49
import org .springframework .util .MultiValueMap ;
48
50
49
- import static org .assertj .core .api .Assertions .assertThat ;
50
- import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
51
- import static org .springframework .transaction .event .TransactionPhase .AFTER_COMMIT ;
52
- import static org .springframework .transaction .event .TransactionPhase .AFTER_COMPLETION ;
53
- import static org .springframework .transaction .event .TransactionPhase .AFTER_ROLLBACK ;
54
- import static org .springframework .transaction .event .TransactionPhase .BEFORE_COMMIT ;
51
+ import static org .assertj .core .api .Assertions .*;
52
+ import static org .springframework .transaction .event .TransactionPhase .*;
55
53
56
54
/**
57
55
* Integration tests for {@link TransactionalEventListener} support
@@ -66,7 +64,7 @@ public class TransactionalEventListenerTests {
66
64
67
65
private EventCollector eventCollector ;
68
66
69
- private TransactionTemplate transactionTemplate = new TransactionTemplate ( new CallCountingTransactionManager ()) ;
67
+ private TransactionTemplate transactionTemplate ;
70
68
71
69
72
70
@ After
@@ -148,7 +146,7 @@ public void afterCommit() {
148
146
149
147
@ Test
150
148
public void afterCommitWithTransactionalComponentListenerProxiedViaDynamicProxy () {
151
- load (TransactionalConfiguration . class , TransactionalComponentTestListener .class );
149
+ load (TransactionalComponentTestListener .class );
152
150
this .transactionTemplate .execute (status -> {
153
151
getContext ().publishEvent ("SKIP" );
154
152
getEventCollector ().assertNoEventReceived ();
@@ -250,6 +248,38 @@ public void noTransaction() {
250
248
getEventCollector ().assertTotalEventsCount (0 );
251
249
}
252
250
251
+ @ Test
252
+ public void transactionDemarcationWithNotSupportedPropagation () {
253
+ load (BeforeCommitTestListener .class , AfterCompletionTestListener .class );
254
+ getContext ().getBean (TestBean .class ).notSupported ();
255
+ getEventCollector ().assertTotalEventsCount (0 );
256
+ }
257
+
258
+ @ Test
259
+ public void transactionDemarcationWithSupportsPropagationAndNoTransaction () {
260
+ load (BeforeCommitTestListener .class , AfterCompletionTestListener .class );
261
+ getContext ().getBean (TestBean .class ).supports ();
262
+ getEventCollector ().assertTotalEventsCount (0 );
263
+ }
264
+
265
+ @ Test
266
+ public void transactionDemarcationWithSupportsPropagationAndExistingTransaction () {
267
+ load (BeforeCommitTestListener .class , AfterCompletionTestListener .class );
268
+ this .transactionTemplate .execute (status -> {
269
+ getContext ().getBean (TestBean .class ).supports ();
270
+ getEventCollector ().assertNoEventReceived ();
271
+ return null ;
272
+ });
273
+ getEventCollector ().assertTotalEventsCount (2 );
274
+ }
275
+
276
+ @ Test
277
+ public void transactionDemarcationWithRequiredPropagation () {
278
+ load (BeforeCommitTestListener .class , AfterCompletionTestListener .class );
279
+ getContext ().getBean (TestBean .class ).required ();
280
+ getEventCollector ().assertTotalEventsCount (2 );
281
+ }
282
+
253
283
@ Test
254
284
public void noTransactionWithFallbackExecution () {
255
285
load (FallbackExecutionTestListener .class );
@@ -299,49 +329,50 @@ public void conditionFoundOnMetaAnnotation() {
299
329
300
330
301
331
protected EventCollector getEventCollector () {
302
- return eventCollector ;
332
+ return this . eventCollector ;
303
333
}
304
334
305
335
protected ConfigurableApplicationContext getContext () {
306
- return context ;
336
+ return this . context ;
307
337
}
308
338
309
339
private void load (Class <?>... classes ) {
310
340
List <Class <?>> allClasses = new ArrayList <>();
311
341
allClasses .add (BasicConfiguration .class );
312
342
allClasses .addAll (Arrays .asList (classes ));
313
- doLoad (allClasses .toArray (new Class <?>[allClasses . size () ]));
343
+ doLoad (allClasses .toArray (new Class <?>[0 ]));
314
344
}
315
345
316
346
private void doLoad (Class <?>... classes ) {
317
347
this .context = new AnnotationConfigApplicationContext (classes );
318
348
this .eventCollector = this .context .getBean (EventCollector .class );
349
+ this .transactionTemplate = this .context .getBean (TransactionTemplate .class );
319
350
}
320
351
321
352
322
353
@ Configuration
354
+ @ EnableTransactionManagement
323
355
static class BasicConfiguration {
324
356
325
- @ Bean // set automatically with tx management
326
- public TransactionalEventListenerFactory transactionalEventListenerFactory () {
327
- return new TransactionalEventListenerFactory ();
328
- }
329
-
330
357
@ Bean
331
358
public EventCollector eventCollector () {
332
359
return new EventCollector ();
333
360
}
334
- }
335
-
336
361
337
- @ EnableTransactionManagement
338
- @ Configuration
339
- static class TransactionalConfiguration {
362
+ @ Bean
363
+ public TestBean testBean (ApplicationEventPublisher eventPublisher ) {
364
+ return new TestBean (eventPublisher );
365
+ }
340
366
341
367
@ Bean
342
368
public CallCountingTransactionManager transactionManager () {
343
369
return new CallCountingTransactionManager ();
344
370
}
371
+
372
+ @ Bean
373
+ public TransactionTemplate transactionTemplate () {
374
+ return new TransactionTemplate (transactionManager ());
375
+ }
345
376
}
346
377
347
378
@@ -399,6 +430,31 @@ public void assertTotalEventsCount(int number) {
399
430
}
400
431
401
432
433
+ static class TestBean {
434
+
435
+ private final ApplicationEventPublisher eventPublisher ;
436
+
437
+ TestBean (ApplicationEventPublisher eventPublisher ) {
438
+ this .eventPublisher = eventPublisher ;
439
+ }
440
+
441
+ @ Transactional (propagation = Propagation .NOT_SUPPORTED )
442
+ public void notSupported () {
443
+ this .eventPublisher .publishEvent ("test" );
444
+ }
445
+
446
+ @ Transactional (propagation = Propagation .SUPPORTS )
447
+ public void supports () {
448
+ this .eventPublisher .publishEvent ("test" );
449
+ }
450
+
451
+ @ Transactional (propagation = Propagation .REQUIRED )
452
+ public void required () {
453
+ this .eventPublisher .publishEvent ("test" );
454
+ }
455
+ }
456
+
457
+
402
458
static abstract class BaseTransactionalTestListener {
403
459
404
460
static final String FAIL_MSG = "FAIL" ;
0 commit comments