Clean up database tests in spring-test

This commit is contained in:
Sam Brannen 2015-07-26 20:54:27 +02:00
parent aae0bd2fb4
commit 5b8b1dd378
31 changed files with 175 additions and 347 deletions

View File

@ -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);
}

View File

@ -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);
}
}
}

View File

@ -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.*;
/**
* <p>
* JUnit 4 based integration test which verifies support of Spring's
* {@link Transactional &#64;Transactional}, {@link TestExecutionListeners
* &#64;TestExecutionListeners}, and {@link ContextConfiguration
* &#64;ContextConfiguration} annotations in conjunction with the
* {@link SpringJUnit4ClassRunner} and the following
* {@link TestExecutionListener TestExecutionListeners}:
* </p>
*
* <ul>
* <li>{@link DependencyInjectionTestExecutionListener}</li>
* <li>{@link DirtiesContextTestExecutionListener}</li>
* <li>{@link TransactionalTestExecutionListener}</li>
* </ul>
* <p>
* This class specifically tests usage of {@code @Transactional} defined at the
* <strong>class level</strong>.
* </p>
*
* <p>This class specifically tests usage of {@code @Transactional} defined
* at the <strong>class level</strong>.
*
* @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);
}
}
}

View File

@ -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");
}
}

View File

@ -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();
}
}
}

View File

@ -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 <strong>{@code false}</strong>.
*
* <p>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);
}
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}

View File

@ -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.*;
/**
* <p>
* JUnit 4 based integration test which verifies support of Spring's
* {@link Transactional &#64;Transactional}, {@link TestExecutionListeners
* &#64;TestExecutionListeners}, and {@link ContextConfiguration
* &#64;ContextConfiguration} annotations in conjunction with the
* {@link SpringJUnit4ClassRunner} and the following
* {@link TestExecutionListener TestExecutionListeners}:
* </p>
*
* <ul>
* <li>{@link DependencyInjectionTestExecutionListener}</li>
* <li>{@link DirtiesContextTestExecutionListener}</li>
* <li>{@link TransactionalTestExecutionListener}</li>
* </ul>
* <p>
* This class specifically tests usage of {@code @Transactional} defined at the
* <strong>method level</strong>. In contrast to
* {@link ClassLevelTransactionalSpringRunnerTests}, this class omits usage of
* {@code @NotTransactional}.
* </p>
*
* <p>This class specifically tests usage of {@code @Transactional} defined
* at the <strong>method level</strong>.
*
* @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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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();
}

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.BeforeAndAfterTransactionAnnotationTests$DatabaseSetup" />
</beans>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.ClassLevelTransactionalSpringRunnerTests$DatabaseSetup" />
</beans>

View File

@ -1,11 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="transactionalTests-context.xml" />
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.ConcreteTransactionalJUnit4SpringContextTests$DatabaseSetup" />
<jdbc:initialize-database data-source="dataSource" >
<jdbc:script location="classpath:/org/springframework/test/jdbc/data.sql"/>
</jdbc:initialize-database>
<bean id="employee" class="org.springframework.tests.sample.beans.Employee">
<property name="name" value="John Smith" />

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
<bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:data-source-ref="dataSource" />
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.DefaultRollbackFalseTransactionalTests$DatabaseSetup" />
</beans>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.DefaultRollbackTrueTransactionalTests$DatabaseSetup" />
</beans>

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
<jdbc:embedded-database id="dataSource" generate-name="true" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:data-source-ref="dataSource" />

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.MethodLevelTransactionalSpringRunnerTests$DatabaseSetup" />
</beans>

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
<bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:data-source-ref="dataSource" />
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.RollbackOverrideDefaultRollbackFalseTransactionalTests$DatabaseSetup" />
</beans>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="databaseSetup"
class="org.springframework.test.context.junit4.RollbackOverrideDefaultRollbackTrueTransactionalTests$DatabaseSetup" />
</beans>

View File

@ -1 +0,0 @@
CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))

View File

@ -1,6 +0,0 @@
DROP TABLE person IF EXISTS;
CREATE TABLE person (
name VARCHAR(20) NOT NULL,
PRIMARY KEY(name)
);

View File

@ -4,8 +4,13 @@
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<jdbc:embedded-database id="dataSource" generate-name="true" type="HSQL" />
<jdbc:embedded-database id="dataSource2" generate-name="true" type="HSQL" />
<jdbc:embedded-database id="dataSource" generate-name="true">
<jdbc:script location="classpath:/org/springframework/test/jdbc/schema.sql"/>
</jdbc:embedded-database>
<jdbc:embedded-database id="dataSource2" generate-name="true">
<jdbc:script location="classpath:/org/springframework/test/jdbc/schema.sql"/>
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />

View File

@ -18,8 +18,8 @@
p:data-source-ref="dataSource" />
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:/org/springframework/test/context/testng/schema.sql" />
<jdbc:script location="classpath:/org/springframework/test/context/testng/data.sql" />
<jdbc:script location="classpath:/org/springframework/test/jdbc/schema.sql" />
<jdbc:script location="classpath:/org/springframework/test/jdbc/data.sql" />
</jdbc:embedded-database>
</beans>

View File

@ -1 +0,0 @@
INSERT INTO person VALUES('bob');

View File

@ -1,4 +0,0 @@
CREATE TABLE person (
name VARCHAR(20) NOT NULL,
PRIMARY KEY(name)
);

View File

@ -1,3 +1,5 @@
DROP TABLE person IF EXISTS;
CREATE TABLE person (
name VARCHAR(20) NOT NULL,
PRIMARY KEY(name)