Remove deprecated @TransactionConfiguration & TxCfgAttributes

Issue: SPR-14430
This commit is contained in:
Sam Brannen
2016-07-08 18:16:44 +02:00
parent 57c43f4273
commit 966d951329
9 changed files with 15 additions and 551 deletions

View File

@@ -1,85 +0,0 @@
/*
* Copyright 2002-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* JUnit 4 based integration test which verifies proper transactional behavior when the
* {@link TransactionConfiguration#defaultRollback() defaultRollback} attribute
* of the {@link TransactionConfiguration} annotation is set to <strong>{@code false}</strong>.
*
* <p>Also tests configuration of the
* {@link TransactionConfiguration#transactionManager() transaction manager name}.
*
* @author Sam Brannen
* @since 2.5
* @see TransactionConfiguration
* @see DefaultRollbackFalseRollbackAnnotationTransactionalTests
*/
@TransactionConfiguration(transactionManager = "transactionManager2", defaultRollback = false)
@Transactional
@SuppressWarnings("deprecation")
public class DefaultRollbackFalseTransactionalTests extends AbstractTransactionalSpringRunnerTests {
private static JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("dataSource2")
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
public void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(jdbcTemplate));
}
@Test
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Deleting bob", 1, deletePerson(jdbcTemplate, BOB));
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(jdbcTemplate, SUE));
assertEquals("Verifying the number of rows in the person table within a transaction.", 2,
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 2,
countRowsInPersonTable(jdbcTemplate));
}
}

View File

@@ -1,80 +0,0 @@
/*
* Copyright 2002-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* JUnit 4 based integration test which verifies proper transactional behavior when the
* {@link TransactionConfiguration#defaultRollback() defaultRollback} attribute
* of the {@link TransactionConfiguration} annotation is set to <strong>{@code true}</strong>.
*
* @author Sam Brannen
* @since 2.5
* @see TransactionConfiguration
*/
@Transactional
@TransactionConfiguration(defaultRollback = true)
@SuppressWarnings("deprecation")
public class DefaultRollbackTrueTransactionalTests extends AbstractTransactionalSpringRunnerTests {
private static int originalNumRows;
private static JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
public void verifyInitialTestData() {
originalNumRows = clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(jdbcTemplate));
}
@Test(timeout = 1000)
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(jdbcTemplate, SUE));
assertEquals("Verifying the number of rows in the person table within a transaction.", 3,
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(jdbcTemplate));
}
}

View File

@@ -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.
@@ -23,7 +23,6 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
@@ -31,15 +30,16 @@ import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* Extension of {@link DefaultRollbackFalseTransactionalTests} which
* tests method-level <em>rollback override</em> behavior via the
* Extension of {@link DefaultRollbackFalseRollbackAnnotationTransactionalTests}
* which tests method-level <em>rollback override</em> behavior via the
* {@link Rollback @Rollback} annotation.
*
* @author Sam Brannen
* @since 2.5
* @see Rollback
*/
public class RollbackOverrideDefaultRollbackFalseTransactionalTests extends DefaultRollbackFalseTransactionalTests {
public class RollbackOverrideDefaultRollbackFalseTransactionalTests
extends DefaultRollbackFalseRollbackAnnotationTransactionalTests {
private static int originalNumRows;
@@ -47,7 +47,6 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalTests extends Defa
@Autowired
@Qualifier("dataSource2")
@Override
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);

View File

@@ -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.
@@ -30,15 +30,16 @@ import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* Extension of {@link DefaultRollbackTrueTransactionalTests} which
* tests method-level <em>rollback override</em> behavior via the
* Extension of {@link DefaultRollbackTrueRollbackAnnotationTransactionalTests}
* which tests method-level <em>rollback override</em> behavior via the
* {@link Rollback @Rollback} annotation.
*
* @author Sam Brannen
* @since 2.5
* @see Rollback
*/
public class RollbackOverrideDefaultRollbackTrueTransactionalTests extends DefaultRollbackTrueTransactionalTests {
public class RollbackOverrideDefaultRollbackTrueTransactionalTests
extends DefaultRollbackTrueRollbackAnnotationTransactionalTests {
private static JdbcTemplate jdbcTemplate;

View File

@@ -104,8 +104,8 @@ StandardJUnit4FeaturesTests.class,//
ConcreteTransactionalJUnit4SpringContextTests.class,//
ClassLevelTransactionalSpringRunnerTests.class,//
MethodLevelTransactionalSpringRunnerTests.class,//
DefaultRollbackTrueTransactionalTests.class,//
DefaultRollbackFalseTransactionalTests.class,//
DefaultRollbackTrueRollbackAnnotationTransactionalTests.class,//
DefaultRollbackFalseRollbackAnnotationTransactionalTests.class,//
RollbackOverrideDefaultRollbackTrueTransactionalTests.class,//
RollbackOverrideDefaultRollbackFalseTransactionalTests.class,//
BeforeAndAfterTransactionAnnotationTests.class,//

View File

@@ -36,7 +36,6 @@ import org.springframework.transaction.annotation.Propagation;
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.*;
@@ -47,7 +46,6 @@ import static org.springframework.transaction.annotation.Propagation.*;
* @author Sam Brannen
* @since 4.0
*/
@SuppressWarnings("deprecation")
public class TransactionalTestExecutionListenerTests {
private final PlatformTransactionManager tm = mock(PlatformTransactionManager.class);
@@ -135,16 +133,6 @@ public class TransactionalTestExecutionListenerTests {
assertFalse("callback should not have been invoked", instance.invoked());
}
private void assertTransactionConfigurationAttributes(Class<?> clazz, String transactionManagerName,
boolean defaultRollback) {
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
TransactionConfigurationAttributes attributes = listener.retrieveConfigurationAttributes(testContext);
assertNotNull(attributes);
assertEquals(transactionManagerName, attributes.getTransactionManagerName());
assertEquals(defaultRollback, attributes.isDefaultRollback());
}
private void assertIsRollback(Class<?> clazz, boolean rollback) throws NoSuchMethodException, Exception {
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("test"));
@@ -255,70 +243,6 @@ public class TransactionalTestExecutionListenerTests {
assertAfterTestMethod(AfterTransactionDeclaredAsInterfaceDefaultMethodTestCase.class);
}
@Test
public void retrieveConfigurationAttributesWithMissingTransactionConfiguration() throws Exception {
assertTransactionConfigurationAttributes(MissingTransactionConfigurationTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesWithEmptyTransactionConfiguration() throws Exception {
assertTransactionConfigurationAttributes(EmptyTransactionConfigurationTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesWithExplicitValues() throws Exception {
assertTransactionConfigurationAttributes(TransactionConfigurationWithExplicitValuesTestCase.class, "tm", false);
}
@Test
public void retrieveConfigurationAttributesViaMetaAnnotation() throws Exception {
assertTransactionConfigurationAttributes(TransactionConfigurationViaMetaAnnotationTestCase.class, "metaTxMgr",
true);
}
@Test
public void retrieveConfigurationAttributesViaMetaAnnotationWithOverride() throws Exception {
assertTransactionConfigurationAttributes(TransactionConfigurationViaMetaAnnotationWithOverrideTestCase.class,
"overriddenTxMgr", true);
}
@Test
public void retrieveConfigurationAttributesWithEmptyTransactionalAnnotation() throws Exception {
assertTransactionConfigurationAttributes(EmptyTransactionalTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesFromTransactionalAnnotationWithExplicitQualifier() throws Exception {
// The test class configures "tm" as the qualifier via @Transactional;
// however, retrieveConfigurationAttributes() only supports
// @TransactionConfiguration. So we actually expect "" as the qualifier here,
// relying on beforeTestMethod() to properly obtain the actual qualifier via the
// TransactionAttribute.
assertTransactionConfigurationAttributes(TransactionalWithExplicitQualifierTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesFromTransactionalAnnotationViaMetaAnnotation() throws Exception {
// The test class configures "metaTxMgr" as the qualifier via @Transactional;
// however, retrieveConfigurationAttributes() only supports
// @TransactionConfiguration. So we actually expect "" as the qualifier here,
// relying on beforeTestMethod() to properly obtain the actual qualifier via the
// TransactionAttribute.
assertTransactionConfigurationAttributes(TransactionalViaMetaAnnotationTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesFromTransactionalAnnotationViaMetaAnnotationWithExplicitQualifier()
throws Exception {
// The test class configures "overriddenTxMgr" as the qualifier via
// @Transactional; however, retrieveConfigurationAttributes() only supports
// @TransactionConfiguration. So we actually expect "" as the qualifier here,
// relying on beforeTestMethod() to properly obtain the actual qualifier via the
// TransactionAttribute.
assertTransactionConfigurationAttributes(TransactionalViaMetaAnnotationWithExplicitQualifierTestCase.class, "",
true);
}
@Test
public void isRollbackWithMissingRollback() throws Exception {
assertIsRollback(MissingRollbackTestCase.class, true);
@@ -364,16 +288,6 @@ public class TransactionalTestExecutionListenerTests {
assertIsRollback(ClassLevelRollbackViaMetaAnnotationOnTestInterfaceTestCase.class, false);
}
@Test
public void isRollbackWithRollbackAndTransactionConfigurationDeclaredAtClassLevel() throws Exception {
Class<?> clazz = ClassLevelRollbackAndTransactionConfigurationTestCase.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
exception.expect(IllegalStateException.class);
exception.expectMessage(containsString("annotated with both @Rollback and @TransactionConfiguration, but only one is permitted"));
listener.isRollback(testContext);
}
// -------------------------------------------------------------------------
@@ -402,13 +316,6 @@ public class TransactionalTestExecutionListenerTests {
private static @interface MetaAfterTransaction {
}
@TransactionConfiguration
@Retention(RetentionPolicy.RUNTIME)
private static @interface MetaTxConfig {
String transactionManager() default "metaTxMgr";
}
private interface Invocable {
void invoked(boolean invoked);
@@ -632,41 +539,6 @@ public class TransactionalTestExecutionListenerTests {
}
}
static class MissingTransactionConfigurationTestCase {
}
@TransactionConfiguration
static class EmptyTransactionConfigurationTestCase {
}
@TransactionConfiguration(transactionManager = "tm", defaultRollback = false)
static class TransactionConfigurationWithExplicitValuesTestCase {
}
@MetaTxConfig
static class TransactionConfigurationViaMetaAnnotationTestCase {
}
@MetaTxConfig(transactionManager = "overriddenTxMgr")
static class TransactionConfigurationViaMetaAnnotationWithOverrideTestCase {
}
@Transactional
static class EmptyTransactionalTestCase {
}
@Transactional(transactionManager = "tm")
static class TransactionalWithExplicitQualifierTestCase {
}
@MetaTransactional
static class TransactionalViaMetaAnnotationTestCase {
}
@MetaTxWithOverride(transactionManager = "tm")
static class TransactionalViaMetaAnnotationWithExplicitQualifierTestCase {
}
static class MissingRollbackTestCase {
public void test() {
@@ -694,14 +566,6 @@ public class TransactionalTestExecutionListenerTests {
}
}
@Rollback
@TransactionConfiguration
static class ClassLevelRollbackAndTransactionConfigurationTestCase {
public void test() {
}
}
@Rollback
static class EmptyClassLevelRollbackTestCase {