From 500a4dd99579b00027918abda7d4d74f2a65b3a9 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 16 May 2012 03:08:15 +0200 Subject: [PATCH] Fix tx annotated tests so that they pass in the build AbstractTransactionalAnnotatedConfigClassTests is now annotated with @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) so that side-effects between tests are avoided. Re-enabled TransactionalAnnotatedConfigClassWithAtConfigurationTests and TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests. Also introduced a log4j FileAppender for tests that writes to "build/spring-test.log". Issue: SPR-9051 --- ...ransactionalAnnotatedConfigClassTests.java | 43 +++---------------- ...edConfigClassWithAtConfigurationTests.java | 6 +-- ...figClassesWithoutAtConfigurationTests.java | 4 +- spring-test/src/test/resources/log4j.xml | 19 ++++++-- 4 files changed, 27 insertions(+), 45 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AbstractTransactionalAnnotatedConfigClassTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AbstractTransactionalAnnotatedConfigClassTests.java index 0c6580ee574..cb00d7af816 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AbstractTransactionalAnnotatedConfigClassTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AbstractTransactionalAnnotatedConfigClassTests.java @@ -24,22 +24,22 @@ import static org.springframework.test.transaction.TransactionTestUtils.inTransa import javax.sql.DataSource; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.transaction.annotation.Transactional; /** - * This set of tests investigates the claims made in + * This set of tests (i.e., all concrete subclasses) investigates the claims made in * SPR-9051 * with regard to transactional tests. * @@ -48,20 +48,13 @@ import org.springframework.transaction.annotation.Transactional; * @see org.springframework.test.context.testng.AnnotationConfigTransactionalTestNGSpringContextTests */ @RunWith(SpringJUnit4ClassRunner.class) +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public abstract class AbstractTransactionalAnnotatedConfigClassTests { protected static final String JANE = "jane"; protected static final String SUE = "sue"; protected static final String YODA = "yoda"; - protected static final int NUM_TESTS = 2; - protected static final int NUM_TX_TESTS = 1; - - private static int numSetUpCalls = 0; - private static int numSetUpCallsInTransaction = 0; - private static int numTearDownCalls = 0; - private static int numTearDownCallsInTransaction = 0; - protected DataSource dataSourceFromTxManager; protected DataSource dataSourceViaInjection; @@ -82,11 +75,11 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests { this.jdbcTemplate = new JdbcTemplate(dataSource); } - protected int countRowsInTable(String tableName) { + private int countRowsInTable(String tableName) { return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName); } - protected int createPerson(String name) { + private int createPerson(String name) { return jdbcTemplate.update("INSERT INTO person VALUES(?)", name); } @@ -103,22 +96,6 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests { assertEquals("Adding '" + name + "'", 1, createPerson(name)); } - @BeforeClass - public static void beforeClass() { - numSetUpCalls = 0; - numSetUpCallsInTransaction = 0; - numTearDownCalls = 0; - numTearDownCallsInTransaction = 0; - } - - @AfterClass - public static void afterClass() { - assertEquals("number of calls to setUp().", NUM_TESTS, numSetUpCalls); - assertEquals("number of calls to setUp() within a transaction.", NUM_TX_TESTS, numSetUpCallsInTransaction); - assertEquals("number of calls to tearDown().", NUM_TESTS, numTearDownCalls); - assertEquals("number of calls to tearDown() within a transaction.", NUM_TX_TESTS, numTearDownCallsInTransaction); - } - @Test public void autowiringFromConfigClass() { assertNotNull("The employee should have been autowired.", employee); @@ -133,10 +110,6 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests { @Before public void setUp() throws Exception { - numSetUpCalls++; - if (inTransaction()) { - numSetUpCallsInTransaction++; - } assertNumRowsInPersonTable((inTransaction() ? 1 : 0), "before a test method"); } @@ -151,10 +124,6 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests { @After public void tearDown() throws Exception { - numTearDownCalls++; - if (inTransaction()) { - numTearDownCallsInTransaction++; - } assertNumRowsInPersonTable((inTransaction() ? 3 : 0), "after a test method"); } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassWithAtConfigurationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassWithAtConfigurationTests.java index 845dff1cd33..3b5a579d8a2 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassWithAtConfigurationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassWithAtConfigurationTests.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertSame; import javax.sql.DataSource; import org.junit.Before; -import org.junit.Ignore; import org.springframework.beans.Employee; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -38,7 +37,6 @@ import org.springframework.transaction.PlatformTransactionManager; * @since 3.2 * @see TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests */ -@Ignore("Disabled until working within the build") @ContextConfiguration public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends AbstractTransactionalAnnotatedConfigClassTests { @@ -70,6 +68,8 @@ public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends public DataSource dataSource() { return new EmbeddedDatabaseBuilder()// .addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")// + // Ensure that this in-memory database is only used by this class: + .setName(getClass().getName())// .build(); } @@ -78,7 +78,7 @@ public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends @Before public void compareDataSources() throws Exception { - // NOTE: the two DataSource instances are the same! + // NOTE: the two DataSource instances ARE the same! assertSame(dataSourceFromTxManager, dataSourceViaInjection); } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.java index 975f00f6665..44063fe18b9 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertNotSame; import javax.sql.DataSource; import org.junit.Before; -import org.junit.Ignore; import org.springframework.beans.Employee; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -45,7 +44,6 @@ import org.springframework.transaction.PlatformTransactionManager; * @see Bean * @see TransactionalAnnotatedConfigClassWithAtConfigurationTests */ -@Ignore("Disabled until working within the build") @ContextConfiguration(classes = TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class) public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests extends AbstractTransactionalAnnotatedConfigClassTests { @@ -95,6 +93,8 @@ public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests exte public DataSource dataSource() { return new EmbeddedDatabaseBuilder()// .addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")// + // Ensure that this in-memory database is only used by this class: + .setName(getClass().getName())// .build(); } diff --git a/spring-test/src/test/resources/log4j.xml b/spring-test/src/test/resources/log4j.xml index 326b5d0d2ea..aec45e4ea9f 100644 --- a/spring-test/src/test/resources/log4j.xml +++ b/spring-test/src/test/resources/log4j.xml @@ -11,6 +11,13 @@ + + + + + + + @@ -21,7 +28,11 @@ - - - + + + + \ No newline at end of file