1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
20
20
import java .lang .annotation .RetentionPolicy ;
21
21
22
22
import org .junit .After ;
23
+ import org .junit .Ignore ;
23
24
import org .junit .Rule ;
24
25
import org .junit .Test ;
25
26
import org .junit .rules .ExpectedException ;
37
38
import org .springframework .transaction .support .SimpleTransactionStatus ;
38
39
39
40
import static org .hamcrest .CoreMatchers .*;
40
-
41
41
import static org .junit .Assert .*;
42
42
import static org .mockito .BDDMockito .*;
43
43
import static org .springframework .transaction .annotation .Propagation .*;
@@ -82,10 +82,10 @@ private void assertBeforeTestMethodWithTransactionalTestMethod(Class<? extends I
82
82
given (testContext .getTestInstance ()).willReturn (instance );
83
83
given (testContext .getTestMethod ()).willReturn (clazz .getDeclaredMethod ("transactionalTest" ));
84
84
85
- assertFalse (instance .invoked );
85
+ assertFalse ("callback not have been invoked" , instance .invoked () );
86
86
TransactionContextHolder .removeCurrentTransactionContext ();
87
87
listener .beforeTestMethod (testContext );
88
- assertEquals (invokedInTx , instance .invoked );
88
+ assertEquals (invokedInTx , instance .invoked () );
89
89
}
90
90
91
91
private void assertBeforeTestMethodWithNonTransactionalTestMethod (Class <? extends Invocable > clazz )
@@ -95,10 +95,10 @@ private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class<? extend
95
95
given (testContext .getTestInstance ()).willReturn (instance );
96
96
given (testContext .getTestMethod ()).willReturn (clazz .getDeclaredMethod ("nonTransactionalTest" ));
97
97
98
- assertFalse (instance .invoked );
98
+ assertFalse ("callback not have been invoked" , instance .invoked () );
99
99
TransactionContextHolder .removeCurrentTransactionContext ();
100
100
listener .beforeTestMethod (testContext );
101
- assertFalse (instance .invoked );
101
+ assertFalse ("callback not have been invoked" , instance .invoked () );
102
102
}
103
103
104
104
private void assertAfterTestMethod (Class <? extends Invocable > clazz ) throws Exception {
@@ -114,11 +114,12 @@ private void assertAfterTestMethodWithTransactionalTestMethod(Class<? extends In
114
114
115
115
given (tm .getTransaction (BDDMockito .any (TransactionDefinition .class ))).willReturn (new SimpleTransactionStatus ());
116
116
117
- assertFalse (instance .invoked );
117
+ assertFalse ("callback not have been invoked" , instance .invoked () );
118
118
TransactionContextHolder .removeCurrentTransactionContext ();
119
119
listener .beforeTestMethod (testContext );
120
+ assertFalse ("callback not have been invoked" , instance .invoked ());
120
121
listener .afterTestMethod (testContext );
121
- assertTrue (instance .invoked );
122
+ assertTrue ("callback should have been invoked" , instance .invoked () );
122
123
}
123
124
124
125
private void assertAfterTestMethodWithNonTransactionalTestMethod (Class <? extends Invocable > clazz ) throws Exception {
@@ -127,11 +128,11 @@ private void assertAfterTestMethodWithNonTransactionalTestMethod(Class<? extends
127
128
given (testContext .getTestInstance ()).willReturn (instance );
128
129
given (testContext .getTestMethod ()).willReturn (clazz .getDeclaredMethod ("nonTransactionalTest" ));
129
130
130
- assertFalse (instance .invoked );
131
+ assertFalse ("callback not have been invoked" , instance .invoked () );
131
132
TransactionContextHolder .removeCurrentTransactionContext ();
132
133
listener .beforeTestMethod (testContext );
133
134
listener .afterTestMethod (testContext );
134
- assertFalse (instance .invoked );
135
+ assertFalse ("callback not have been invoked" , instance .invoked () );
135
136
}
136
137
137
138
private void assertTransactionConfigurationAttributes (Class <?> clazz , String transactionManagerName ,
@@ -174,7 +175,7 @@ protected PlatformTransactionManager getTransactionManager(TestContext testConte
174
175
given (testContext .getTestInstance ()).willReturn (instance );
175
176
given (testContext .getTestMethod ()).willReturn (clazz .getDeclaredMethod ("transactionalTest" ));
176
177
177
- assertFalse (instance .invoked );
178
+ assertFalse ("callback not have been invoked" , instance .invoked () );
178
179
TransactionContextHolder .removeCurrentTransactionContext ();
179
180
180
181
try {
@@ -244,6 +245,18 @@ public void afterTestMethodWithAfterTransactionDeclaredViaMetaAnnotation() throw
244
245
assertAfterTestMethod (AfterTransactionDeclaredViaMetaAnnotationTestCase .class );
245
246
}
246
247
248
+ @ Ignore ("Disabled until @BeforeTransaction is supported on interface default methods" )
249
+ @ Test
250
+ public void beforeTestMethodWithBeforeTransactionDeclaredAsInterfaceDefaultMethod () throws Exception {
251
+ assertBeforeTestMethod (BeforeTransactionDeclaredAsInterfaceDefaultMethodTestCase .class );
252
+ }
253
+
254
+ @ Ignore ("Disabled until @AfterTransaction is supported on interface default methods" )
255
+ @ Test
256
+ public void afterTestMethodWithAfterTransactionDeclaredAsInterfaceDefaultMethod () throws Exception {
257
+ assertAfterTestMethod (AfterTransactionDeclaredAsInterfaceDefaultMethodTestCase .class );
258
+ }
259
+
247
260
@ Test
248
261
public void retrieveConfigurationAttributesWithMissingTransactionConfiguration () throws Exception {
249
262
assertTransactionConfigurationAttributes (MissingTransactionConfigurationTestCase .class , "" , true );
@@ -388,29 +401,47 @@ public void isRollbackWithRollbackAndTransactionConfigurationDeclaredAtClassLeve
388
401
String transactionManager () default "metaTxMgr" ;
389
402
}
390
403
391
- private static abstract class Invocable {
404
+ private interface Invocable {
405
+
406
+ void invoked (boolean invoked );
407
+
408
+ boolean invoked ();
409
+ }
410
+
411
+ private static class AbstractInvocable implements Invocable {
392
412
393
413
boolean invoked = false ;
414
+
415
+
416
+ @ Override
417
+ public void invoked (boolean invoked ) {
418
+ this .invoked = invoked ;
419
+ }
420
+
421
+ @ Override
422
+ public boolean invoked () {
423
+ return this .invoked ;
424
+ }
394
425
}
395
426
396
427
@ Transactional
397
- static class TransactionalDeclaredOnClassLocallyTestCase extends Invocable {
428
+ static class TransactionalDeclaredOnClassLocallyTestCase extends AbstractInvocable {
398
429
399
430
@ BeforeTransaction
400
431
public void beforeTransaction () {
401
- invoked = true ;
432
+ invoked ( true ) ;
402
433
}
403
434
404
435
public void transactionalTest () {
405
436
/* no-op */
406
437
}
407
438
}
408
439
409
- static class TransactionalDeclaredOnMethodLocallyTestCase extends Invocable {
440
+ static class TransactionalDeclaredOnMethodLocallyTestCase extends AbstractInvocable {
410
441
411
442
@ BeforeTransaction
412
443
public void beforeTransaction () {
413
- invoked = true ;
444
+ invoked ( true ) ;
414
445
}
415
446
416
447
@ Transactional
@@ -424,23 +455,23 @@ public void nonTransactionalTest() {
424
455
}
425
456
426
457
@ MetaTransactional
427
- static class TransactionalDeclaredOnClassViaMetaAnnotationTestCase extends Invocable {
458
+ static class TransactionalDeclaredOnClassViaMetaAnnotationTestCase extends AbstractInvocable {
428
459
429
460
@ BeforeTransaction
430
461
public void beforeTransaction () {
431
- invoked = true ;
462
+ invoked ( true ) ;
432
463
}
433
464
434
465
public void transactionalTest () {
435
466
/* no-op */
436
467
}
437
468
}
438
469
439
- static class TransactionalDeclaredOnMethodViaMetaAnnotationTestCase extends Invocable {
470
+ static class TransactionalDeclaredOnMethodViaMetaAnnotationTestCase extends AbstractInvocable {
440
471
441
472
@ BeforeTransaction
442
473
public void beforeTransaction () {
443
- invoked = true ;
474
+ invoked ( true ) ;
444
475
}
445
476
446
477
@ MetaTransactional
@@ -454,23 +485,23 @@ public void nonTransactionalTest() {
454
485
}
455
486
456
487
@ MetaTxWithOverride (propagation = NOT_SUPPORTED )
457
- static class TransactionalDeclaredOnClassViaMetaAnnotationWithOverrideTestCase extends Invocable {
488
+ static class TransactionalDeclaredOnClassViaMetaAnnotationWithOverrideTestCase extends AbstractInvocable {
458
489
459
490
@ BeforeTransaction
460
491
public void beforeTransaction () {
461
- invoked = true ;
492
+ invoked ( true ) ;
462
493
}
463
494
464
495
public void transactionalTest () {
465
496
/* no-op */
466
497
}
467
498
}
468
499
469
- static class TransactionalDeclaredOnMethodViaMetaAnnotationWithOverrideTestCase extends Invocable {
500
+ static class TransactionalDeclaredOnMethodViaMetaAnnotationWithOverrideTestCase extends AbstractInvocable {
470
501
471
502
@ BeforeTransaction
472
503
public void beforeTransaction () {
473
- invoked = true ;
504
+ invoked ( true ) ;
474
505
}
475
506
476
507
@ MetaTxWithOverride (propagation = NOT_SUPPORTED )
@@ -483,11 +514,11 @@ public void nonTransactionalTest() {
483
514
}
484
515
}
485
516
486
- static class BeforeTransactionDeclaredLocallyTestCase extends Invocable {
517
+ static class BeforeTransactionDeclaredLocallyTestCase extends AbstractInvocable {
487
518
488
519
@ BeforeTransaction
489
520
public void beforeTransaction () {
490
- invoked = true ;
521
+ invoked ( true ) ;
491
522
}
492
523
493
524
@ Transactional
@@ -500,11 +531,11 @@ public void nonTransactionalTest() {
500
531
}
501
532
}
502
533
503
- static class BeforeTransactionDeclaredViaMetaAnnotationTestCase extends Invocable {
534
+ static class BeforeTransactionDeclaredViaMetaAnnotationTestCase extends AbstractInvocable {
504
535
505
536
@ MetaBeforeTransaction
506
537
public void beforeTransaction () {
507
- invoked = true ;
538
+ invoked ( true ) ;
508
539
}
509
540
510
541
@ Transactional
@@ -517,11 +548,11 @@ public void nonTransactionalTest() {
517
548
}
518
549
}
519
550
520
- static class AfterTransactionDeclaredLocallyTestCase extends Invocable {
551
+ static class AfterTransactionDeclaredLocallyTestCase extends AbstractInvocable {
521
552
522
553
@ AfterTransaction
523
554
public void afterTransaction () {
524
- invoked = true ;
555
+ invoked ( true ) ;
525
556
}
526
557
527
558
@ Transactional
@@ -534,13 +565,55 @@ public void nonTransactionalTest() {
534
565
}
535
566
}
536
567
537
- static class AfterTransactionDeclaredViaMetaAnnotationTestCase extends Invocable {
568
+ static class AfterTransactionDeclaredViaMetaAnnotationTestCase extends AbstractInvocable {
538
569
539
570
@ MetaAfterTransaction
540
571
public void afterTransaction () {
541
- invoked = true ;
572
+ invoked (true );
573
+ }
574
+
575
+ @ Transactional
576
+ public void transactionalTest () {
577
+ /* no-op */
578
+ }
579
+
580
+ public void nonTransactionalTest () {
581
+ /* no-op */
582
+ }
583
+ }
584
+
585
+ interface BeforeTransactionInterface extends Invocable {
586
+
587
+ @ BeforeTransaction
588
+ default void beforeTransaction () {
589
+ invoked (true );
590
+ }
591
+ }
592
+
593
+ interface AfterTransactionInterface extends Invocable {
594
+
595
+ @ AfterTransaction
596
+ default void afterTransaction () {
597
+ invoked (true );
598
+ }
599
+ }
600
+
601
+ static class BeforeTransactionDeclaredAsInterfaceDefaultMethodTestCase extends AbstractInvocable
602
+ implements BeforeTransactionInterface {
603
+
604
+ @ Transactional
605
+ public void transactionalTest () {
606
+ /* no-op */
542
607
}
543
608
609
+ public void nonTransactionalTest () {
610
+ /* no-op */
611
+ }
612
+ }
613
+
614
+ static class AfterTransactionDeclaredAsInterfaceDefaultMethodTestCase extends AbstractInvocable
615
+ implements AfterTransactionInterface {
616
+
544
617
@ Transactional
545
618
public void transactionalTest () {
546
619
/* no-op */
0 commit comments