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
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,7 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
@@ -37,7 +38,6 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.SimpleTransactionStatus;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.springframework.transaction.annotation.Propagation.*;
|
||||
@@ -82,10 +82,10 @@ public class TransactionalTestExecutionListenerTests {
|
||||
given(testContext.getTestInstance()).willReturn(instance);
|
||||
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
|
||||
|
||||
assertFalse(instance.invoked);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
TransactionContextHolder.removeCurrentTransactionContext();
|
||||
listener.beforeTestMethod(testContext);
|
||||
assertEquals(invokedInTx, instance.invoked);
|
||||
assertEquals(invokedInTx, instance.invoked());
|
||||
}
|
||||
|
||||
private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz)
|
||||
@@ -95,10 +95,10 @@ public class TransactionalTestExecutionListenerTests {
|
||||
given(testContext.getTestInstance()).willReturn(instance);
|
||||
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest"));
|
||||
|
||||
assertFalse(instance.invoked);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
TransactionContextHolder.removeCurrentTransactionContext();
|
||||
listener.beforeTestMethod(testContext);
|
||||
assertFalse(instance.invoked);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
}
|
||||
|
||||
private void assertAfterTestMethod(Class<? extends Invocable> clazz) throws Exception {
|
||||
@@ -114,11 +114,12 @@ public class TransactionalTestExecutionListenerTests {
|
||||
|
||||
given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus());
|
||||
|
||||
assertFalse(instance.invoked);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
TransactionContextHolder.removeCurrentTransactionContext();
|
||||
listener.beforeTestMethod(testContext);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
listener.afterTestMethod(testContext);
|
||||
assertTrue(instance.invoked);
|
||||
assertTrue("callback should have been invoked", instance.invoked());
|
||||
}
|
||||
|
||||
private void assertAfterTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
|
||||
@@ -127,11 +128,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
given(testContext.getTestInstance()).willReturn(instance);
|
||||
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest"));
|
||||
|
||||
assertFalse(instance.invoked);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
TransactionContextHolder.removeCurrentTransactionContext();
|
||||
listener.beforeTestMethod(testContext);
|
||||
listener.afterTestMethod(testContext);
|
||||
assertFalse(instance.invoked);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
}
|
||||
|
||||
private void assertTransactionConfigurationAttributes(Class<?> clazz, String transactionManagerName,
|
||||
@@ -174,7 +175,7 @@ public class TransactionalTestExecutionListenerTests {
|
||||
given(testContext.getTestInstance()).willReturn(instance);
|
||||
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
|
||||
|
||||
assertFalse(instance.invoked);
|
||||
assertFalse("callback not have been invoked", instance.invoked());
|
||||
TransactionContextHolder.removeCurrentTransactionContext();
|
||||
|
||||
try {
|
||||
@@ -244,6 +245,18 @@ public class TransactionalTestExecutionListenerTests {
|
||||
assertAfterTestMethod(AfterTransactionDeclaredViaMetaAnnotationTestCase.class);
|
||||
}
|
||||
|
||||
@Ignore("Disabled until @BeforeTransaction is supported on interface default methods")
|
||||
@Test
|
||||
public void beforeTestMethodWithBeforeTransactionDeclaredAsInterfaceDefaultMethod() throws Exception {
|
||||
assertBeforeTestMethod(BeforeTransactionDeclaredAsInterfaceDefaultMethodTestCase.class);
|
||||
}
|
||||
|
||||
@Ignore("Disabled until @AfterTransaction is supported on interface default methods")
|
||||
@Test
|
||||
public void afterTestMethodWithAfterTransactionDeclaredAsInterfaceDefaultMethod() throws Exception {
|
||||
assertAfterTestMethod(AfterTransactionDeclaredAsInterfaceDefaultMethodTestCase.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveConfigurationAttributesWithMissingTransactionConfiguration() throws Exception {
|
||||
assertTransactionConfigurationAttributes(MissingTransactionConfigurationTestCase.class, "", true);
|
||||
@@ -388,17 +401,35 @@ public class TransactionalTestExecutionListenerTests {
|
||||
String transactionManager() default "metaTxMgr";
|
||||
}
|
||||
|
||||
private static abstract class Invocable {
|
||||
private interface Invocable {
|
||||
|
||||
void invoked(boolean invoked);
|
||||
|
||||
boolean invoked();
|
||||
}
|
||||
|
||||
private static class AbstractInvocable implements Invocable {
|
||||
|
||||
boolean invoked = false;
|
||||
|
||||
|
||||
@Override
|
||||
public void invoked(boolean invoked) {
|
||||
this.invoked = invoked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean invoked() {
|
||||
return this.invoked;
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
static class TransactionalDeclaredOnClassLocallyTestCase extends Invocable {
|
||||
static class TransactionalDeclaredOnClassLocallyTestCase extends AbstractInvocable {
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
public void transactionalTest() {
|
||||
@@ -406,11 +437,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class TransactionalDeclaredOnMethodLocallyTestCase extends Invocable {
|
||||
static class TransactionalDeclaredOnMethodLocallyTestCase extends AbstractInvocable {
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -424,11 +455,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
|
||||
@MetaTransactional
|
||||
static class TransactionalDeclaredOnClassViaMetaAnnotationTestCase extends Invocable {
|
||||
static class TransactionalDeclaredOnClassViaMetaAnnotationTestCase extends AbstractInvocable {
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
public void transactionalTest() {
|
||||
@@ -436,11 +467,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class TransactionalDeclaredOnMethodViaMetaAnnotationTestCase extends Invocable {
|
||||
static class TransactionalDeclaredOnMethodViaMetaAnnotationTestCase extends AbstractInvocable {
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
@MetaTransactional
|
||||
@@ -454,11 +485,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
|
||||
@MetaTxWithOverride(propagation = NOT_SUPPORTED)
|
||||
static class TransactionalDeclaredOnClassViaMetaAnnotationWithOverrideTestCase extends Invocable {
|
||||
static class TransactionalDeclaredOnClassViaMetaAnnotationWithOverrideTestCase extends AbstractInvocable {
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
public void transactionalTest() {
|
||||
@@ -466,11 +497,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class TransactionalDeclaredOnMethodViaMetaAnnotationWithOverrideTestCase extends Invocable {
|
||||
static class TransactionalDeclaredOnMethodViaMetaAnnotationWithOverrideTestCase extends AbstractInvocable {
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
@MetaTxWithOverride(propagation = NOT_SUPPORTED)
|
||||
@@ -483,11 +514,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class BeforeTransactionDeclaredLocallyTestCase extends Invocable {
|
||||
static class BeforeTransactionDeclaredLocallyTestCase extends AbstractInvocable {
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -500,11 +531,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class BeforeTransactionDeclaredViaMetaAnnotationTestCase extends Invocable {
|
||||
static class BeforeTransactionDeclaredViaMetaAnnotationTestCase extends AbstractInvocable {
|
||||
|
||||
@MetaBeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -517,11 +548,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class AfterTransactionDeclaredLocallyTestCase extends Invocable {
|
||||
static class AfterTransactionDeclaredLocallyTestCase extends AbstractInvocable {
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -534,11 +565,11 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class AfterTransactionDeclaredViaMetaAnnotationTestCase extends Invocable {
|
||||
static class AfterTransactionDeclaredViaMetaAnnotationTestCase extends AbstractInvocable {
|
||||
|
||||
@MetaAfterTransaction
|
||||
public void afterTransaction() {
|
||||
invoked = true;
|
||||
invoked(true);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -551,6 +582,48 @@ public class TransactionalTestExecutionListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
interface BeforeTransactionInterface extends Invocable {
|
||||
|
||||
@BeforeTransaction
|
||||
default void beforeTransaction() {
|
||||
invoked(true);
|
||||
}
|
||||
}
|
||||
|
||||
interface AfterTransactionInterface extends Invocable {
|
||||
|
||||
@AfterTransaction
|
||||
default void afterTransaction() {
|
||||
invoked(true);
|
||||
}
|
||||
}
|
||||
|
||||
static class BeforeTransactionDeclaredAsInterfaceDefaultMethodTestCase extends AbstractInvocable
|
||||
implements BeforeTransactionInterface {
|
||||
|
||||
@Transactional
|
||||
public void transactionalTest() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
public void nonTransactionalTest() {
|
||||
/* no-op */
|
||||
}
|
||||
}
|
||||
|
||||
static class AfterTransactionDeclaredAsInterfaceDefaultMethodTestCase extends AbstractInvocable
|
||||
implements AfterTransactionInterface {
|
||||
|
||||
@Transactional
|
||||
public void transactionalTest() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
public void nonTransactionalTest() {
|
||||
/* no-op */
|
||||
}
|
||||
}
|
||||
|
||||
static class MissingTransactionConfigurationTestCase {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user