diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/AbstractTransactionalSpringRunnerTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/AbstractTransactionalSpringRunnerTests.java index 532ef517d0..493be806d2 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/AbstractTransactionalSpringRunnerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/AbstractTransactionalSpringRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * 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. @@ -16,7 +16,8 @@ package org.springframework.test.context.junit4; -import org.springframework.dao.DataAccessException; +import org.junit.runner.RunWith; + import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.annotation.Transactional; @@ -31,6 +32,7 @@ import org.springframework.transaction.annotation.Transactional; * @see MethodLevelTransactionalSpringRunnerTests * @see Transactional */ +@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("transactionalTests-context.xml") public abstract class AbstractTransactionalSpringRunnerTests { @@ -46,15 +48,6 @@ public abstract class AbstractTransactionalSpringRunnerTests { return jdbcTemplate.update("DELETE FROM person"); } - protected static void createPersonTable(JdbcTemplate jdbcTemplate) { - try { - jdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))"); - } - catch (DataAccessException dae) { - // ignore - } - } - protected static int countRowsInPersonTable(JdbcTemplate jdbcTemplate) { return jdbcTemplate.queryForObject("SELECT COUNT(0) FROM person", Integer.class); } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java index 9309092107..23440f7df1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java @@ -16,7 +16,6 @@ package org.springframework.test.context.junit4; -import javax.annotation.Resource; import javax.sql.DataSource; import org.junit.After; @@ -26,14 +25,11 @@ import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; -import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.BeforeTransaction; -import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; import static org.junit.Assert.*; @@ -47,9 +43,6 @@ import static org.springframework.test.transaction.TransactionTestUtils.*; * @author Sam Brannen * @since 2.5 */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -@TestExecutionListeners(TransactionalTestExecutionListener.class) public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactionalSpringRunnerTests { protected static JdbcTemplate jdbcTemplate; @@ -63,6 +56,12 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio public final TestName testName = new TestName(); + @Autowired + public void setDataSource(DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + } + + @BeforeClass public static void beforeClass() { BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls = 0; @@ -144,14 +143,4 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio countRowsInPersonTable(jdbcTemplate)); } - - public static class DatabaseSetup { - - @Resource - void setDataSource(DataSource dataSource) { - jdbcTemplate = new JdbcTemplate(dataSource); - createPersonTable(jdbcTemplate); - } - } - } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/ClassLevelTransactionalSpringRunnerTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/ClassLevelTransactionalSpringRunnerTests.java index 3e8d2f50e3..8f29a2d014 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/ClassLevelTransactionalSpringRunnerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/ClassLevelTransactionalSpringRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * 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. @@ -16,14 +16,13 @@ package org.springframework.test.context.junit4; -import javax.annotation.Resource; import javax.sql.DataSource; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListener; @@ -38,36 +37,37 @@ import static org.junit.Assert.*; import static org.springframework.test.transaction.TransactionTestUtils.*; /** - *
* JUnit 4 based integration test which verifies support of Spring's * {@link Transactional @Transactional}, {@link TestExecutionListeners * @TestExecutionListeners}, and {@link ContextConfiguration * @ContextConfiguration} annotations in conjunction with the * {@link SpringJUnit4ClassRunner} and the following * {@link TestExecutionListener TestExecutionListeners}: - *
+ * *- * This class specifically tests usage of {@code @Transactional} defined at the - * class level. - *
+ * + *This class specifically tests usage of {@code @Transactional} defined + * at the class level. * * @author Sam Brannen * @since 2.5 * @see MethodLevelTransactionalSpringRunnerTests */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration @Transactional public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests { protected static JdbcTemplate jdbcTemplate; + @Autowired + public void setDataSource(DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + } + @AfterClass public static void verifyFinalTestData() { assertEquals("Verifying the final number of rows in the person table after all tests.", 4, @@ -103,14 +103,4 @@ public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactio countRowsInPersonTable(jdbcTemplate)); } - - public static class DatabaseSetup { - - @Resource - public void setDataSource(DataSource dataSource) { - jdbcTemplate = new JdbcTemplate(dataSource); - createPersonTable(jdbcTemplate); - } - } - } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/ConcreteTransactionalJUnit4SpringContextTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/ConcreteTransactionalJUnit4SpringContextTests.java index 851868f752..ecf14e33b8 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/ConcreteTransactionalJUnit4SpringContextTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/ConcreteTransactionalJUnit4SpringContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * 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. @@ -17,7 +17,6 @@ package org.springframework.test.context.junit4; import javax.annotation.Resource; -import javax.sql.DataSource; import org.junit.After; import org.junit.Before; @@ -26,12 +25,9 @@ import org.junit.Test; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.BeforeTransaction; -import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.tests.sample.beans.Employee; import org.springframework.tests.sample.beans.Pet; import org.springframework.transaction.annotation.Propagation; @@ -51,12 +47,9 @@ import static org.springframework.test.transaction.TransactionTestUtils.*; public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTransactionalJUnit4SpringContextTests implements BeanNameAware, InitializingBean { - protected static final String BOB = "bob"; - protected static final String JANE = "jane"; - protected static final String SUE = "sue"; - protected static final String LUKE = "luke"; - protected static final String LEIA = "leia"; - protected static final String YODA = "yoda"; + private static final String JANE = "jane"; + private static final String SUE = "sue"; + private static final String YODA = "yoda"; private boolean beanInitialized = false; @@ -68,52 +61,21 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans private Pet pet; @Autowired(required = false) - protected Long nonrequiredLong; + private Long nonrequiredLong; @Resource - protected String foo; + private String foo; - protected String bar; + private String bar; - protected static int clearPersonTable(final JdbcTemplate jdbcTemplate) { - return JdbcTestUtils.deleteFromTables(jdbcTemplate, "person"); - } - - protected static void createPersonTable(final JdbcTemplate jdbcTemplate) { - try { - jdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))"); - } - catch (DataAccessException dae) { - /* ignore */ - } - } - - protected static int countRowsInPersonTable(final JdbcTemplate jdbcTemplate) { - return JdbcTestUtils.countRowsInTable(jdbcTemplate, "person"); - } - - protected static int addPerson(final JdbcTemplate jdbcTemplate, final String name) { - return jdbcTemplate.update("INSERT INTO person VALUES(?)", name); - } - - protected static int deletePerson(final JdbcTemplate jdbcTemplate, final String name) { - return jdbcTemplate.update("DELETE FROM person WHERE name=?", name); - } - - @Override - @Resource - public void setDataSource(DataSource dataSource) { - super.setDataSource(dataSource); - } - @Autowired - protected final void setEmployee(final Employee employee) { + private final void setEmployee(final Employee employee) { this.employee = employee; } @Resource - protected final void setBar(final String bar) { + private final void setBar(final String bar) { this.bar = bar; } @@ -185,48 +147,48 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans @BeforeTransaction public void beforeTransaction() { assertEquals("Verifying the number of rows in the person table before a transactional test method.", 1, - countRowsInPersonTable(super.jdbcTemplate)); - assertEquals("Adding yoda", 1, addPerson(super.jdbcTemplate, YODA)); + countRowsInPersonTable()); + assertEquals("Adding yoda", 1, addPerson(YODA)); } @Before public void setUp() throws Exception { assertEquals("Verifying the number of rows in the person table before a test method.", - (inTransaction() ? 2 : 1), countRowsInPersonTable(super.jdbcTemplate)); + (inTransaction() ? 2 : 1), countRowsInPersonTable()); } @Test public void modifyTestDataWithinTransaction() { assertInTransaction(true); - assertEquals("Adding jane", 1, addPerson(super.jdbcTemplate, JANE)); - assertEquals("Adding sue", 1, addPerson(super.jdbcTemplate, SUE)); + assertEquals("Adding jane", 1, addPerson(JANE)); + assertEquals("Adding sue", 1, addPerson(SUE)); assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().", 4, - countRowsInPersonTable(super.jdbcTemplate)); + countRowsInPersonTable()); } @After public void tearDown() throws Exception { assertEquals("Verifying the number of rows in the person table after a test method.", - (inTransaction() ? 4 : 1), countRowsInPersonTable(super.jdbcTemplate)); + (inTransaction() ? 4 : 1), countRowsInPersonTable()); } @AfterTransaction public void afterTransaction() { - assertEquals("Deleting yoda", 1, deletePerson(super.jdbcTemplate, YODA)); + assertEquals("Deleting yoda", 1, deletePerson(YODA)); assertEquals("Verifying the number of rows in the person table after a transactional test method.", 1, - countRowsInPersonTable(super.jdbcTemplate)); + countRowsInPersonTable()); } + private int addPerson(final String name) { + return super.jdbcTemplate.update("INSERT INTO person VALUES(?)", name); + } - public static class DatabaseSetup { + private int deletePerson(final String name) { + return super.jdbcTemplate.update("DELETE FROM person WHERE name=?", name); + } - @Resource - public void setDataSource(DataSource dataSource) { - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - createPersonTable(jdbcTemplate); - clearPersonTable(jdbcTemplate); - addPerson(jdbcTemplate, BOB); - } + private int countRowsInPersonTable() { + return countRowsInTable("person"); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseRollbackAnnotationTransactionalTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseRollbackAnnotationTransactionalTests.java index 04b639cc52..0c1f043aef 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseRollbackAnnotationTransactionalTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseRollbackAnnotationTransactionalTests.java @@ -24,14 +24,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; -import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; import static org.junit.Assert.*; @@ -51,7 +46,7 @@ import static org.springframework.test.transaction.TransactionTestUtils.*; * @see DefaultRollbackFalseTransactionalTests */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(inheritLocations = false) +@ContextConfiguration(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false) @Transactional("txMgr") @Rollback(false) public class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests { @@ -89,22 +84,4 @@ public class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends Ab countRowsInPersonTable(jdbcTemplate)); } - - @Configuration - static class Config { - - @Bean - public PlatformTransactionManager txMgr() { - return new DataSourceTransactionManager(dataSource()); - } - - @Bean - public DataSource dataSource() { - return new EmbeddedDatabaseBuilder()// - .generateUniqueName(true)// - .addScript("classpath:/org/springframework/test/context/junit4/person-schema.sql") // - .build(); - } - } - } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseTransactionalTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseTransactionalTests.java index df1c17cc94..49726735f1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseTransactionalTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackFalseTransactionalTests.java @@ -16,15 +16,15 @@ package org.springframework.test.context.junit4; -import javax.annotation.Resource; import javax.sql.DataSource; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; + +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.ContextConfiguration; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @@ -35,6 +35,7 @@ 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 {@code false}. + * *
Also tests configuration of the * {@link TransactionConfiguration#transactionManager() transaction manager name}. * @@ -43,9 +44,7 @@ import static org.springframework.test.transaction.TransactionTestUtils.*; * @see TransactionConfiguration * @see DefaultRollbackFalseRollbackAnnotationTransactionalTests */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -@TransactionConfiguration(transactionManager = "txMgr", defaultRollback = false) +@TransactionConfiguration(transactionManager = "transactionManager2", defaultRollback = false) @Transactional @SuppressWarnings("deprecation") public class DefaultRollbackFalseTransactionalTests extends AbstractTransactionalSpringRunnerTests { @@ -53,6 +52,12 @@ public class DefaultRollbackFalseTransactionalTests extends AbstractTransactiona private static JdbcTemplate jdbcTemplate; + @Autowired + @Qualifier("dataSource2") + public void setDataSource(DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + } + @Before public void verifyInitialTestData() { clearPersonTable(jdbcTemplate); @@ -77,14 +82,4 @@ public class DefaultRollbackFalseTransactionalTests extends AbstractTransactiona countRowsInPersonTable(jdbcTemplate)); } - - public static class DatabaseSetup { - - @Resource - public void setDataSource(DataSource dataSource) { - jdbcTemplate = new JdbcTemplate(dataSource); - createPersonTable(jdbcTemplate); - } - } - } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueRollbackAnnotationTransactionalTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueRollbackAnnotationTransactionalTests.java index 16cee7b6f4..984fc62fd3 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueRollbackAnnotationTransactionalTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueRollbackAnnotationTransactionalTests.java @@ -24,14 +24,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; -import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; import static org.junit.Assert.*; @@ -51,7 +46,7 @@ import static org.springframework.test.transaction.TransactionTestUtils.*; * @see DefaultRollbackTrueTransactionalTests */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(inheritLocations = false) +@ContextConfiguration(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false) @Transactional("txMgr") @Rollback(true) public class DefaultRollbackTrueRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests { @@ -90,22 +85,4 @@ public class DefaultRollbackTrueRollbackAnnotationTransactionalTests extends Abs countRowsInPersonTable(jdbcTemplate)); } - - @Configuration - static class Config { - - @Bean - public PlatformTransactionManager txMgr() { - return new DataSourceTransactionManager(dataSource()); - } - - @Bean - public DataSource dataSource() { - return new EmbeddedDatabaseBuilder()// - .generateUniqueName(true)// - .addScript("classpath:/org/springframework/test/context/junit4/person-schema.sql") // - .build(); - } - } - } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueTransactionalTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueTransactionalTests.java index 873a6f64b8..aa2e736e5b 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueTransactionalTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/DefaultRollbackTrueTransactionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * 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. @@ -16,15 +16,14 @@ package org.springframework.test.context.junit4; -import javax.annotation.Resource; import javax.sql.DataSource; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @@ -40,8 +39,6 @@ import static org.springframework.test.transaction.TransactionTestUtils.*; * @since 2.5 * @see TransactionConfiguration */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration @Transactional @TransactionConfiguration(defaultRollback = true) @SuppressWarnings("deprecation") @@ -52,6 +49,11 @@ public class DefaultRollbackTrueTransactionalTests extends AbstractTransactional private static JdbcTemplate jdbcTemplate; + @Autowired + public void setDataSource(DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + } + @Before public void verifyInitialTestData() { originalNumRows = clearPersonTable(jdbcTemplate); @@ -75,14 +77,4 @@ public class DefaultRollbackTrueTransactionalTests extends AbstractTransactional countRowsInPersonTable(jdbcTemplate)); } - - public static class DatabaseSetup { - - @Resource - public void setDataSource(DataSource dataSource) { - jdbcTemplate = new JdbcTemplate(dataSource); - createPersonTable(jdbcTemplate); - } - } - } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/EmbeddedPersonDatabaseTestsConfig.java b/spring-test/src/test/java/org/springframework/test/context/junit4/EmbeddedPersonDatabaseTestsConfig.java new file mode 100644 index 0000000000..1cb550b48c --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/EmbeddedPersonDatabaseTestsConfig.java @@ -0,0 +1,50 @@ +/* + * 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.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.transaction.PlatformTransactionManager; + +/** + * Shared configuration for tests that need an embedded database pre-loaded + * with the schema for the 'person' table. + * + * @author Sam Brannen + * @since 4.2 + */ +@Configuration +public class EmbeddedPersonDatabaseTestsConfig { + + @Bean + public PlatformTransactionManager txMgr() { + return new DataSourceTransactionManager(dataSource()); + } + + @Bean + public DataSource dataSource() { + return new EmbeddedDatabaseBuilder()// + .generateUniqueName(true)// + .addScript("classpath:/org/springframework/test/jdbc/schema.sql") // + .build(); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/MethodLevelTransactionalSpringRunnerTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/MethodLevelTransactionalSpringRunnerTests.java index 36b77baecc..4270059579 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/MethodLevelTransactionalSpringRunnerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/MethodLevelTransactionalSpringRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * 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. @@ -16,14 +16,13 @@ package org.springframework.test.context.junit4; -import javax.annotation.Resource; import javax.sql.DataSource; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; - +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.ContextConfiguration; import org.springframework.test.context.TestExecutionListener; @@ -37,32 +36,26 @@ import static org.junit.Assert.*; import static org.springframework.test.transaction.TransactionTestUtils.*; /** - *
* JUnit 4 based integration test which verifies support of Spring's * {@link Transactional @Transactional}, {@link TestExecutionListeners * @TestExecutionListeners}, and {@link ContextConfiguration * @ContextConfiguration} annotations in conjunction with the * {@link SpringJUnit4ClassRunner} and the following * {@link TestExecutionListener TestExecutionListeners}: - *
+ * *- * This class specifically tests usage of {@code @Transactional} defined at the - * method level. In contrast to - * {@link ClassLevelTransactionalSpringRunnerTests}, this class omits usage of - * {@code @NotTransactional}. - *
+ * + *This class specifically tests usage of {@code @Transactional} defined
+ * at the method level.
*
* @author Sam Brannen
* @since 2.5
* @see ClassLevelTransactionalSpringRunnerTests
*/
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
@@ -70,6 +63,12 @@ public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransacti
protected static JdbcTemplate jdbcTemplate;
+ @Autowired
+ @Qualifier("dataSource2")
+ public void setDataSource(DataSource dataSource) {
+ jdbcTemplate = new JdbcTemplate(dataSource);
+ }
+
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 4,
@@ -105,14 +104,4 @@ public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransacti
countRowsInPersonTable(jdbcTemplate));
}
-
- public static class DatabaseSetup {
-
- @Resource
- public void setDataSource2(DataSource dataSource) {
- jdbcTemplate = new JdbcTemplate(dataSource);
- createPersonTable(jdbcTemplate);
- }
- }
-
}
diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackFalseTransactionalTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackFalseTransactionalTests.java
index 1543008baa..3f47fafa15 100644
--- a/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackFalseTransactionalTests.java
+++ b/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackFalseTransactionalTests.java
@@ -16,16 +16,16 @@
package org.springframework.test.context.junit4;
-import javax.annotation.Resource;
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.annotation.Rollback;
-import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
@@ -39,15 +39,19 @@ import static org.springframework.test.transaction.TransactionTestUtils.*;
* @since 2.5
* @see Rollback
*/
-@ContextConfiguration
-public class RollbackOverrideDefaultRollbackFalseTransactionalTests extends
- DefaultRollbackFalseTransactionalTests {
+public class RollbackOverrideDefaultRollbackFalseTransactionalTests extends DefaultRollbackFalseTransactionalTests {
private static int originalNumRows;
private static JdbcTemplate jdbcTemplate;
+ @Autowired
+ @Qualifier("dataSource2")
+ public void setDataSource(DataSource dataSource) {
+ jdbcTemplate = new JdbcTemplate(dataSource);
+ }
+
@Before
@Override
public void verifyInitialTestData() {
@@ -75,14 +79,4 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalTests extends
countRowsInPersonTable(jdbcTemplate));
}
-
- public static class DatabaseSetup {
-
- @Resource
- public void setDataSource(DataSource dataSource) {
- jdbcTemplate = new JdbcTemplate(dataSource);
- createPersonTable(jdbcTemplate);
- }
- }
-
}
diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackTrueTransactionalTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackTrueTransactionalTests.java
index f4edf97742..6a568eaa3f 100644
--- a/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackTrueTransactionalTests.java
+++ b/spring-test/src/test/java/org/springframework/test/context/junit4/RollbackOverrideDefaultRollbackTrueTransactionalTests.java
@@ -16,16 +16,15 @@
package org.springframework.test.context.junit4;
-import javax.annotation.Resource;
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.annotation.Rollback;
-import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
@@ -39,13 +38,16 @@ import static org.springframework.test.transaction.TransactionTestUtils.*;
* @since 2.5
* @see Rollback
*/
-@ContextConfiguration
-public class RollbackOverrideDefaultRollbackTrueTransactionalTests extends
- DefaultRollbackTrueTransactionalTests {
+public class RollbackOverrideDefaultRollbackTrueTransactionalTests extends DefaultRollbackTrueTransactionalTests {
private static JdbcTemplate jdbcTemplate;
+ @Autowired
+ public void setDataSource(DataSource dataSource) {
+ jdbcTemplate = new JdbcTemplate(dataSource);
+ }
+
@Before
@Override
public void verifyInitialTestData() {
@@ -72,14 +74,4 @@ public class RollbackOverrideDefaultRollbackTrueTransactionalTests extends
countRowsInPersonTable(jdbcTemplate));
}
-
- public static class DatabaseSetup {
-
- @Resource
- public void setDataSource(DataSource dataSource) {
- jdbcTemplate = new JdbcTemplate(dataSource);
- createPersonTable(jdbcTemplate);
- }
- }
-
}
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 4fab1e5922..1ecf018910 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
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 the original author or authors.
+ * 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.
@@ -68,7 +68,7 @@ public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
- .addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")//
+ .addScript("classpath:/org/springframework/test/jdbc/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/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.java
index b000e0c734..a285947f5f 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
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 the original author or authors.
+ * 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.
@@ -94,7 +94,7 @@ public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests exte
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
- .addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")//
+ .addScript("classpath:/org/springframework/test/jdbc/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/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java b/spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java
index cf260d9085..a7c9f9745d 100644
--- a/spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java
+++ b/spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java
@@ -183,8 +183,8 @@ public class AnnotationConfigTransactionalTestNGSpringContextTests extends
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
- .addScript("classpath:/org/springframework/test/context/testng/schema.sql")//
- .addScript("classpath:/org/springframework/test/context/testng/data.sql")//
+ .addScript("classpath:/org/springframework/test/jdbc/schema.sql")//
+ .addScript("classpath:/org/springframework/test/jdbc/data.sql")//
.build();
}
diff --git a/spring-test/src/test/resources/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests-context.xml b/spring-test/src/test/resources/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests-context.xml
deleted file mode 100644
index 666f357b7b..0000000000
--- a/spring-test/src/test/resources/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests-context.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-