Skip to content

Commit ff38224

Browse files
committed
Introduce tests for @[Before|After]Transaction on default methods
This commit introduces @ignore'd tests for future support for declaring @BeforeTransaction and @AfterTransaction on interface default methods. Issue: SPR-14183
1 parent b1bc920 commit ff38224

File tree

1 file changed

+105
-32
lines changed

1 file changed

+105
-32
lines changed

spring-test/src/test/java/org/springframework/test/context/transaction/TransactionalTestExecutionListenerTests.java

Lines changed: 105 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.lang.annotation.RetentionPolicy;
2121

2222
import org.junit.After;
23+
import org.junit.Ignore;
2324
import org.junit.Rule;
2425
import org.junit.Test;
2526
import org.junit.rules.ExpectedException;
@@ -37,7 +38,6 @@
3738
import org.springframework.transaction.support.SimpleTransactionStatus;
3839

3940
import static org.hamcrest.CoreMatchers.*;
40-
4141
import static org.junit.Assert.*;
4242
import static org.mockito.BDDMockito.*;
4343
import static org.springframework.transaction.annotation.Propagation.*;
@@ -82,10 +82,10 @@ private void assertBeforeTestMethodWithTransactionalTestMethod(Class<? extends I
8282
given(testContext.getTestInstance()).willReturn(instance);
8383
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
8484

85-
assertFalse(instance.invoked);
85+
assertFalse("callback not have been invoked", instance.invoked());
8686
TransactionContextHolder.removeCurrentTransactionContext();
8787
listener.beforeTestMethod(testContext);
88-
assertEquals(invokedInTx, instance.invoked);
88+
assertEquals(invokedInTx, instance.invoked());
8989
}
9090

9191
private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz)
@@ -95,10 +95,10 @@ private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class<? extend
9595
given(testContext.getTestInstance()).willReturn(instance);
9696
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest"));
9797

98-
assertFalse(instance.invoked);
98+
assertFalse("callback not have been invoked", instance.invoked());
9999
TransactionContextHolder.removeCurrentTransactionContext();
100100
listener.beforeTestMethod(testContext);
101-
assertFalse(instance.invoked);
101+
assertFalse("callback not have been invoked", instance.invoked());
102102
}
103103

104104
private void assertAfterTestMethod(Class<? extends Invocable> clazz) throws Exception {
@@ -114,11 +114,12 @@ private void assertAfterTestMethodWithTransactionalTestMethod(Class<? extends In
114114

115115
given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus());
116116

117-
assertFalse(instance.invoked);
117+
assertFalse("callback not have been invoked", instance.invoked());
118118
TransactionContextHolder.removeCurrentTransactionContext();
119119
listener.beforeTestMethod(testContext);
120+
assertFalse("callback not have been invoked", instance.invoked());
120121
listener.afterTestMethod(testContext);
121-
assertTrue(instance.invoked);
122+
assertTrue("callback should have been invoked", instance.invoked());
122123
}
123124

124125
private void assertAfterTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
@@ -127,11 +128,11 @@ private void assertAfterTestMethodWithNonTransactionalTestMethod(Class<? extends
127128
given(testContext.getTestInstance()).willReturn(instance);
128129
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest"));
129130

130-
assertFalse(instance.invoked);
131+
assertFalse("callback not have been invoked", instance.invoked());
131132
TransactionContextHolder.removeCurrentTransactionContext();
132133
listener.beforeTestMethod(testContext);
133134
listener.afterTestMethod(testContext);
134-
assertFalse(instance.invoked);
135+
assertFalse("callback not have been invoked", instance.invoked());
135136
}
136137

137138
private void assertTransactionConfigurationAttributes(Class<?> clazz, String transactionManagerName,
@@ -174,7 +175,7 @@ protected PlatformTransactionManager getTransactionManager(TestContext testConte
174175
given(testContext.getTestInstance()).willReturn(instance);
175176
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
176177

177-
assertFalse(instance.invoked);
178+
assertFalse("callback not have been invoked", instance.invoked());
178179
TransactionContextHolder.removeCurrentTransactionContext();
179180

180181
try {
@@ -244,6 +245,18 @@ public void afterTestMethodWithAfterTransactionDeclaredViaMetaAnnotation() throw
244245
assertAfterTestMethod(AfterTransactionDeclaredViaMetaAnnotationTestCase.class);
245246
}
246247

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+
247260
@Test
248261
public void retrieveConfigurationAttributesWithMissingTransactionConfiguration() throws Exception {
249262
assertTransactionConfigurationAttributes(MissingTransactionConfigurationTestCase.class, "", true);
@@ -388,29 +401,47 @@ public void isRollbackWithRollbackAndTransactionConfigurationDeclaredAtClassLeve
388401
String transactionManager() default "metaTxMgr";
389402
}
390403

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 {
392412

393413
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+
}
394425
}
395426

396427
@Transactional
397-
static class TransactionalDeclaredOnClassLocallyTestCase extends Invocable {
428+
static class TransactionalDeclaredOnClassLocallyTestCase extends AbstractInvocable {
398429

399430
@BeforeTransaction
400431
public void beforeTransaction() {
401-
invoked = true;
432+
invoked(true);
402433
}
403434

404435
public void transactionalTest() {
405436
/* no-op */
406437
}
407438
}
408439

409-
static class TransactionalDeclaredOnMethodLocallyTestCase extends Invocable {
440+
static class TransactionalDeclaredOnMethodLocallyTestCase extends AbstractInvocable {
410441

411442
@BeforeTransaction
412443
public void beforeTransaction() {
413-
invoked = true;
444+
invoked(true);
414445
}
415446

416447
@Transactional
@@ -424,23 +455,23 @@ public void nonTransactionalTest() {
424455
}
425456

426457
@MetaTransactional
427-
static class TransactionalDeclaredOnClassViaMetaAnnotationTestCase extends Invocable {
458+
static class TransactionalDeclaredOnClassViaMetaAnnotationTestCase extends AbstractInvocable {
428459

429460
@BeforeTransaction
430461
public void beforeTransaction() {
431-
invoked = true;
462+
invoked(true);
432463
}
433464

434465
public void transactionalTest() {
435466
/* no-op */
436467
}
437468
}
438469

439-
static class TransactionalDeclaredOnMethodViaMetaAnnotationTestCase extends Invocable {
470+
static class TransactionalDeclaredOnMethodViaMetaAnnotationTestCase extends AbstractInvocable {
440471

441472
@BeforeTransaction
442473
public void beforeTransaction() {
443-
invoked = true;
474+
invoked(true);
444475
}
445476

446477
@MetaTransactional
@@ -454,23 +485,23 @@ public void nonTransactionalTest() {
454485
}
455486

456487
@MetaTxWithOverride(propagation = NOT_SUPPORTED)
457-
static class TransactionalDeclaredOnClassViaMetaAnnotationWithOverrideTestCase extends Invocable {
488+
static class TransactionalDeclaredOnClassViaMetaAnnotationWithOverrideTestCase extends AbstractInvocable {
458489

459490
@BeforeTransaction
460491
public void beforeTransaction() {
461-
invoked = true;
492+
invoked(true);
462493
}
463494

464495
public void transactionalTest() {
465496
/* no-op */
466497
}
467498
}
468499

469-
static class TransactionalDeclaredOnMethodViaMetaAnnotationWithOverrideTestCase extends Invocable {
500+
static class TransactionalDeclaredOnMethodViaMetaAnnotationWithOverrideTestCase extends AbstractInvocable {
470501

471502
@BeforeTransaction
472503
public void beforeTransaction() {
473-
invoked = true;
504+
invoked(true);
474505
}
475506

476507
@MetaTxWithOverride(propagation = NOT_SUPPORTED)
@@ -483,11 +514,11 @@ public void nonTransactionalTest() {
483514
}
484515
}
485516

486-
static class BeforeTransactionDeclaredLocallyTestCase extends Invocable {
517+
static class BeforeTransactionDeclaredLocallyTestCase extends AbstractInvocable {
487518

488519
@BeforeTransaction
489520
public void beforeTransaction() {
490-
invoked = true;
521+
invoked(true);
491522
}
492523

493524
@Transactional
@@ -500,11 +531,11 @@ public void nonTransactionalTest() {
500531
}
501532
}
502533

503-
static class BeforeTransactionDeclaredViaMetaAnnotationTestCase extends Invocable {
534+
static class BeforeTransactionDeclaredViaMetaAnnotationTestCase extends AbstractInvocable {
504535

505536
@MetaBeforeTransaction
506537
public void beforeTransaction() {
507-
invoked = true;
538+
invoked(true);
508539
}
509540

510541
@Transactional
@@ -517,11 +548,11 @@ public void nonTransactionalTest() {
517548
}
518549
}
519550

520-
static class AfterTransactionDeclaredLocallyTestCase extends Invocable {
551+
static class AfterTransactionDeclaredLocallyTestCase extends AbstractInvocable {
521552

522553
@AfterTransaction
523554
public void afterTransaction() {
524-
invoked = true;
555+
invoked(true);
525556
}
526557

527558
@Transactional
@@ -534,13 +565,55 @@ public void nonTransactionalTest() {
534565
}
535566
}
536567

537-
static class AfterTransactionDeclaredViaMetaAnnotationTestCase extends Invocable {
568+
static class AfterTransactionDeclaredViaMetaAnnotationTestCase extends AbstractInvocable {
538569

539570
@MetaAfterTransaction
540571
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 */
542607
}
543608

609+
public void nonTransactionalTest() {
610+
/* no-op */
611+
}
612+
}
613+
614+
static class AfterTransactionDeclaredAsInterfaceDefaultMethodTestCase extends AbstractInvocable
615+
implements AfterTransactionInterface {
616+
544617
@Transactional
545618
public void transactionalTest() {
546619
/* no-op */

0 commit comments

Comments
 (0)