Delete remaining SimpleJdbcTemplate usage

This commit deletes all remaining usage of the deprecated
SimpleJdbcTemplate class within the framework itself.

Issue: SPR-11895
This commit is contained in:
Sam Brannen 2014-06-22 15:40:41 +02:00
parent 0c1249fe42
commit f1517f03ff
18 changed files with 156 additions and 167 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@ -18,6 +18,7 @@ package org.springframework.orm.jpa;
import java.lang.reflect.Proxy;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.FlushModeType;
@ -144,7 +145,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
protected final void insertPerson(String firstName) {
String INSERT_PERSON = "INSERT INTO PERSON (ID, FIRST_NAME, LAST_NAME) VALUES (?, ?, ?)";
simpleJdbcTemplate.update(INSERT_PERSON, 1, firstName, "Blair");
jdbcTemplate.update(INSERT_PERSON, 1, firstName, "Blair");
}
public void testEntityManagerProxyRejectsProgrammaticTxManagement() {

View File

@ -26,6 +26,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
* @author Rod Johnson
* @author Juergen Hoeller
*/
@SuppressWarnings("deprecation")
public abstract class AbstractEntityManagerFactoryIntegrationTests extends AbstractJpaTests {
public static final String[] ECLIPSELINK_CONFIG_LOCATIONS = new String[] {

View File

@ -35,6 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Rod Johnson
* @since 2.0
*/
@SuppressWarnings("deprecation")
public class ApplicationManagedEntityManagerIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests {
@NotTransactional

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -36,6 +36,7 @@ import org.springframework.test.annotation.NotTransactional;
* @author Rod Johnson
* @since 2.0
*/
@SuppressWarnings("deprecation")
public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests {
@NotTransactional

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@ -26,6 +26,7 @@ import org.springframework.orm.jpa.EntityManagerFactoryInfo;
*
* @author Juergen Hoeller
*/
@SuppressWarnings("deprecation")
public class EclipseLinkEntityManagerFactoryIntegrationTests extends AbstractContainerEntityManagerFactoryIntegrationTests {
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -35,6 +35,7 @@ import org.springframework.test.annotation.IfProfileValue;
*/
// Essentially @Ignore-d since AnnotationBeanConfigurerAspect cannot be found
@IfProfileValue(name="test-group", value="broken")
@SuppressWarnings("deprecation")
public class HibernateEntityManagerFactoryIntegrationTests extends
AbstractContainerEntityManagerFactoryIntegrationTests {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -26,6 +26,7 @@ import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrat
*
* @author Juergen Hoeller
*/
@SuppressWarnings("deprecation")
public class HibernateMultiEntityManagerFactoryIntegrationTests extends
AbstractContainerEntityManagerFactoryIntegrationTests {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -37,6 +37,7 @@ import org.springframework.transaction.support.TransactionTemplate;
*
* @author Costin Leau
*/
@SuppressWarnings("deprecation")
public class OpenJpaEntityManagerFactoryIntegrationTests extends AbstractContainerEntityManagerFactoryIntegrationTests {
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2014 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.
@ -27,6 +27,7 @@ import org.springframework.orm.jpa.support.PersistenceInjectionTests.DefaultPubl
* @author Rod Johnson
* @author Juergen Hoeller
*/
@SuppressWarnings("deprecation")
public class PersistenceInjectionIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests {
@Autowired

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,12 +20,9 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import javax.sql.DataSource;
import junit.framework.AssertionFailedError;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
@ -35,8 +32,8 @@ import org.springframework.util.Assert;
/**
* <p>
* Java 5 specific subclass of
* {@link AbstractTransactionalDataSourceSpringContextTests}, exposing a
* {@link SimpleJdbcTemplate} and obeying annotations for transaction control.
* {@link AbstractTransactionalDataSourceSpringContextTests}, obeying annotations
* for transaction control.
* </p>
* <p>
* For example, test methods can be annotated with the regular Spring
@ -74,8 +71,6 @@ import org.springframework.util.Assert;
public abstract class AbstractAnnotationAwareTransactionalTests extends
AbstractTransactionalDataSourceSpringContextTests {
protected SimpleJdbcTemplate simpleJdbcTemplate;
private final TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource();
/**
@ -109,13 +104,6 @@ public abstract class AbstractAnnotationAwareTransactionalTests extends
}
@Override
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
// JdbcTemplate will be identically configured
this.simpleJdbcTemplate = new SimpleJdbcTemplate(this.jdbcTemplate);
}
/**
* Search for a unique {@link ProfileValueSource} in the supplied
* {@link ApplicationContext}. If found, the

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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,7 @@
package org.springframework.test.context.junit4;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
@ -31,7 +31,6 @@ import org.springframework.transaction.annotation.Transactional;
* @see MethodLevelTransactionalSpringRunnerTests
* @see Transactional
*/
@SuppressWarnings("deprecation")
@ContextConfiguration("transactionalTests-context.xml")
public abstract class AbstractTransactionalSpringRunnerTests {
@ -43,29 +42,29 @@ public abstract class AbstractTransactionalSpringRunnerTests {
protected static final String YODA = "yoda";
protected static int clearPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
return simpleJdbcTemplate.update("DELETE FROM person");
protected static int clearPersonTable(JdbcTemplate jdbcTemplate) {
return jdbcTemplate.update("DELETE FROM person");
}
protected static void createPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
protected static void createPersonTable(JdbcTemplate jdbcTemplate) {
try {
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
jdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
}
catch (DataAccessException dae) {
// ignore
}
}
protected static int countRowsInPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
return simpleJdbcTemplate.queryForInt("SELECT COUNT(0) FROM person");
protected static int countRowsInPersonTable(JdbcTemplate jdbcTemplate) {
return jdbcTemplate.queryForObject("SELECT COUNT(0) FROM person", Integer.class);
}
protected static int addPerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
protected static int addPerson(JdbcTemplate jdbcTemplate, String name) {
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
protected static int deletePerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
protected static int deletePerson(JdbcTemplate jdbcTemplate, String name) {
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2014 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,9 +16,6 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import javax.annotation.Resource;
import javax.sql.DataSource;
@ -27,7 +24,7 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
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;
@ -35,6 +32,9 @@ 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.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* JUnit 4 based integration test which verifies
* {@link BeforeTransaction @BeforeTransaction} and
@ -43,13 +43,12 @@ import org.springframework.transaction.annotation.Transactional;
* @author Sam Brannen
* @since 2.5
*/
@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({ TransactionalTestExecutionListener.class })
@TestExecutionListeners(TransactionalTestExecutionListener.class)
public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactionalSpringRunnerTests {
protected static SimpleJdbcTemplate simpleJdbcTemplate;
protected static JdbcTemplate jdbcTemplate;
protected static int numBeforeTransactionCalls = 0;
protected static int numAfterTransactionCalls = 0;
@ -66,7 +65,7 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
@AfterClass
public static void afterClass() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 3,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
assertEquals("Verifying the total number of calls to beforeTransaction().", 2,
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls);
assertEquals("Verifying the total number of calls to afterTransaction().", 2,
@ -78,8 +77,8 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
assertInTransaction(false);
this.inTransaction = true;
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls++;
clearPersonTable(simpleJdbcTemplate);
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
clearPersonTable(jdbcTemplate);
assertEquals("Adding yoda", 1, addPerson(jdbcTemplate, YODA));
}
@AfterTransaction
@ -87,44 +86,44 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
assertInTransaction(false);
this.inTransaction = false;
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls++;
assertEquals("Deleting yoda", 1, deletePerson(simpleJdbcTemplate, YODA));
assertEquals("Deleting yoda", 1, deletePerson(jdbcTemplate, YODA));
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 0,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Before
public void before() {
assertEquals("Verifying the number of rows in the person table before a test method.", (this.inTransaction ? 1
: 0), countRowsInPersonTable(simpleJdbcTemplate));
: 0), countRowsInPersonTable(jdbcTemplate));
}
@Test
@Transactional
public void transactionalMethod1() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
assertEquals("Verifying the number of rows in the person table within transactionalMethod1().", 2,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
@Transactional
public void transactionalMethod2() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
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 transactionalMethod2().", 3,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
public void nonTransactionalMethod() {
assertInTransaction(false);
assertEquals("Adding luke", 1, addPerson(simpleJdbcTemplate, LUKE));
assertEquals("Adding leia", 1, addPerson(simpleJdbcTemplate, LEIA));
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
assertEquals("Adding luke", 1, addPerson(jdbcTemplate, LUKE));
assertEquals("Adding leia", 1, addPerson(jdbcTemplate, LEIA));
assertEquals("Adding yoda", 1, addPerson(jdbcTemplate, YODA));
assertEquals("Verifying the number of rows in the person table without a transaction.", 3,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@ -132,8 +131,8 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
@Resource
void setDataSource(DataSource dataSource) {
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
}
}

View File

@ -16,9 +16,6 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import javax.annotation.Resource;
import javax.sql.DataSource;
@ -26,7 +23,7 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
@ -36,6 +33,9 @@ import org.springframework.test.context.transaction.TransactionalTestExecutionLi
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* <p>
* JUnit 4 based integration test which verifies support of Spring's
@ -59,48 +59,47 @@ import org.springframework.transaction.annotation.Transactional;
* @since 2.5
* @see MethodLevelTransactionalSpringRunnerTests
*/
@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
protected static SimpleJdbcTemplate simpleJdbcTemplate;
protected static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 4,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Before
public void verifyInitialTestData() {
clearPersonTable(simpleJdbcTemplate);
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
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(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void modifyTestDataWithoutTransaction() {
assertInTransaction(false);
assertEquals("Adding luke", 1, addPerson(simpleJdbcTemplate, LUKE));
assertEquals("Adding leia", 1, addPerson(simpleJdbcTemplate, LEIA));
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
assertEquals("Adding luke", 1, addPerson(jdbcTemplate, LUKE));
assertEquals("Adding leia", 1, addPerson(jdbcTemplate, LEIA));
assertEquals("Adding yoda", 1, addPerson(jdbcTemplate, YODA));
assertEquals("Verifying the number of rows in the person table without a transaction.", 4,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@ -108,8 +107,8 @@ public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactio
@Resource
public void setDataSource(DataSource dataSource) {
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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,9 +16,6 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import javax.annotation.Resource;
import javax.sql.DataSource;
@ -26,11 +23,14 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
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;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* <p>
* JUnit 4 based integration test which verifies proper transactional behavior when the
@ -44,38 +44,37 @@ import org.springframework.transaction.annotation.Transactional;
* @since 2.5
* @see TransactionConfiguration
*/
@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(transactionManager = "txMgr", defaultRollback = false)
@Transactional
public class DefaultRollbackFalseTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
protected static SimpleJdbcTemplate simpleJdbcTemplate;
protected static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 2,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Before
public void verifyInitialTestData() {
clearPersonTable(simpleJdbcTemplate);
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
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(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@ -83,8 +82,8 @@ public class DefaultRollbackFalseTransactionalSpringRunnerTests extends Abstract
@Resource
public void setDataSource(DataSource dataSource) {
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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,9 +16,6 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import javax.annotation.Resource;
import javax.sql.DataSource;
@ -26,11 +23,14 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
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;
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
@ -40,7 +40,6 @@ import org.springframework.transaction.annotation.Transactional;
* @since 2.5
* @see TransactionConfiguration
*/
@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(defaultRollback = true)
@ -48,31 +47,31 @@ public class DefaultRollbackTrueTransactionalSpringRunnerTests extends AbstractT
protected static int originalNumRows;
protected static SimpleJdbcTemplate simpleJdbcTemplate;
protected static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Before
public void verifyInitialTestData() {
originalNumRows = clearPersonTable(simpleJdbcTemplate);
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
originalNumRows = clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test(timeout = 1000)
@Transactional
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
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(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@ -80,8 +79,8 @@ public class DefaultRollbackTrueTransactionalSpringRunnerTests extends AbstractT
@Resource
public void setDataSource(DataSource dataSource) {
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
}
}

View File

@ -16,9 +16,6 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import javax.annotation.Resource;
import javax.sql.DataSource;
@ -26,7 +23,7 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
@ -35,6 +32,9 @@ import org.springframework.test.context.support.DirtiesContextTestExecutionListe
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* <p>
* JUnit 4 based integration test which verifies support of Spring's
@ -60,49 +60,48 @@ import org.springframework.transaction.annotation.Transactional;
* @since 2.5
* @see ClassLevelTransactionalSpringRunnerTests
*/
@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
protected static SimpleJdbcTemplate simpleJdbcTemplate;
protected static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 4,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Before
public void verifyInitialTestData() {
clearPersonTable(simpleJdbcTemplate);
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
@Transactional("transactionManager2")
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
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(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
public void modifyTestDataWithoutTransaction() {
assertInTransaction(false);
assertEquals("Adding luke", 1, addPerson(simpleJdbcTemplate, LUKE));
assertEquals("Adding leia", 1, addPerson(simpleJdbcTemplate, LEIA));
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
assertEquals("Adding luke", 1, addPerson(jdbcTemplate, LUKE));
assertEquals("Adding leia", 1, addPerson(jdbcTemplate, LEIA));
assertEquals("Adding yoda", 1, addPerson(jdbcTemplate, YODA));
assertEquals("Verifying the number of rows in the person table without a transaction.", 4,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@ -110,8 +109,8 @@ public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransacti
@Resource
public void setDataSource2(DataSource dataSource) {
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2014 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,19 +16,19 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
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.*;
/**
* Extension of {@link DefaultRollbackFalseTransactionalSpringRunnerTests} which
* tests method-level <em>rollback override</em> behavior via the
@ -38,29 +38,28 @@ import org.springframework.test.context.ContextConfiguration;
* @since 2.5
* @see Rollback
*/
@SuppressWarnings("deprecation")
@ContextConfiguration
public class RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests extends
DefaultRollbackFalseTransactionalSpringRunnerTests {
protected static int originalNumRows;
protected static SimpleJdbcTemplate simpleJdbcTemplate;
protected static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Before
@Override
public void verifyInitialTestData() {
originalNumRows = clearPersonTable(simpleJdbcTemplate);
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
originalNumRows = clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Test
@ -68,11 +67,11 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests
@Override
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
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(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@ -80,8 +79,8 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests
@Resource
public void setDataSource(DataSource dataSource) {
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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,20 +16,20 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* Extension of {@link DefaultRollbackTrueTransactionalSpringRunnerTests} which
* tests method-level <em>rollback override</em> behavior via the
@ -39,27 +39,26 @@ import org.springframework.transaction.annotation.Transactional;
* @since 2.5
* @see Rollback
*/
@SuppressWarnings("deprecation")
@ContextConfiguration
public class RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests extends
DefaultRollbackTrueTransactionalSpringRunnerTests {
protected static SimpleJdbcTemplate simpleJdbcTemplate;
protected static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 3,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Override
@Before
public void verifyInitialTestData() {
clearPersonTable(simpleJdbcTemplate);
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@Override
@ -68,10 +67,10 @@ public class RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests e
@Rollback(false)
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
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(simpleJdbcTemplate));
countRowsInPersonTable(jdbcTemplate));
}
@ -79,8 +78,8 @@ public class RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests e
@Resource
public void setDataSource(DataSource dataSource) {
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
}
}