The Spring Framework

org.springframework.test
Class AbstractTransactionalDataSourceSpringContextTests

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.springframework.test.ConditionalTestCase
              extended by org.springframework.test.AbstractSpringContextTests
                  extended by org.springframework.test.AbstractSingleSpringContextTests
                      extended by org.springframework.test.AbstractDependencyInjectionSpringContextTests
                          extended by org.springframework.test.AbstractTransactionalSpringContextTests
                              extended by org.springframework.test.AbstractTransactionalDataSourceSpringContextTests
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
AbstractAnnotationAwareTransactionalTests

public abstract class AbstractTransactionalDataSourceSpringContextTests
extends AbstractTransactionalSpringContextTests

Subclass of AbstractTransactionalSpringContextTests that adds some convenience functionality for JDBC access. Expects a DataSource bean to be defined in the Spring application context.

This class exposes a JdbcTemplate and provides an easy way to delete from the database in a new transaction.

Since:
1.1.1
Author:
Rod Johnson, Juergen Hoeller
See Also:
setDataSource(javax.sql.DataSource), getJdbcTemplate()

Field Summary
protected  JdbcTemplate jdbcTemplate
           
 
Fields inherited from class org.springframework.test.AbstractTransactionalSpringContextTests
transactionDefinition, transactionManager, transactionStatus
 
Fields inherited from class org.springframework.test.AbstractDependencyInjectionSpringContextTests
AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_NO
 
Fields inherited from class org.springframework.test.AbstractSingleSpringContextTests
applicationContext
 
Fields inherited from class org.springframework.test.ConditionalTestCase
logger
 
Constructor Summary
AbstractTransactionalDataSourceSpringContextTests()
          Default constructor for AbstractTransactionalDataSourceSpringContextTests.
AbstractTransactionalDataSourceSpringContextTests(String name)
          Constructor for AbstractTransactionalDataSourceSpringContextTests with a JUnit name.
 
Method Summary
protected  int countRowsInTable(String tableName)
          Count the rows in the given table
protected  void deleteFromTables(String[] names)
          Convenient method to delete all rows from these tables.
protected  void executeSqlScript(String sqlResourcePath, boolean continueOnError)
          Execute the given SQL script.
 JdbcTemplate getJdbcTemplate()
          Return the JdbcTemplate that this base class manages.
protected  void setComplete()
          Overridden to prevent the transaction committing if a number of tables have been cleared, as a defensive measure against accidental permanent wiping of a database.
 void setDataSource(DataSource dataSource)
          Setter: DataSource is provided by Dependency Injection.
 
Methods inherited from class org.springframework.test.AbstractTransactionalSpringContextTests
endTransaction, isDefaultRollback, isRollback, onSetUp, onSetUpBeforeTransaction, onSetUpInTransaction, onTearDown, onTearDownAfterTransaction, onTearDownInTransaction, preventTransaction, setDefaultRollback, setTransactionDefinition, setTransactionManager, startNewTransaction
 
Methods inherited from class org.springframework.test.AbstractDependencyInjectionSpringContextTests
getAutowireMode, injectDependencies, isDependencyCheck, isPopulateProtectedVariables, prepareTestInstance, setAutowireMode, setDependencyCheck, setPopulateProtectedVariables
 
Methods inherited from class org.springframework.test.AbstractSingleSpringContextTests
contextKey, createApplicationContext, createBeanDefinitionReader, customizeBeanFactory, getApplicationContext, getConfigLocations, getConfigPath, getConfigPaths, getLoadCount, loadContext, loadContextLocations, setDirty, setUp, tearDown
 
Methods inherited from class org.springframework.test.AbstractSpringContextTests
addContext, contextKeyString, getContext, hasCachedContext, isContextKeyEmpty, setDirty
 
Methods inherited from class org.springframework.test.ConditionalTestCase
getDisabledTestCount, isDisabledInThisEnvironment, recordDisabled, runBare
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

jdbcTemplate

protected JdbcTemplate jdbcTemplate
Constructor Detail

AbstractTransactionalDataSourceSpringContextTests

public AbstractTransactionalDataSourceSpringContextTests()
Default constructor for AbstractTransactionalDataSourceSpringContextTests.


AbstractTransactionalDataSourceSpringContextTests

public AbstractTransactionalDataSourceSpringContextTests(String name)
Constructor for AbstractTransactionalDataSourceSpringContextTests with a JUnit name.

Method Detail

setDataSource

public void setDataSource(DataSource dataSource)
Setter: DataSource is provided by Dependency Injection.


getJdbcTemplate

public final JdbcTemplate getJdbcTemplate()
Return the JdbcTemplate that this base class manages.


deleteFromTables

protected void deleteFromTables(String[] names)
Convenient method to delete all rows from these tables. Calling this method will make avoidance of rollback by calling setComplete() impossible.

See Also:
setComplete()

setComplete

protected final void setComplete()
Overridden to prevent the transaction committing if a number of tables have been cleared, as a defensive measure against accidental permanent wiping of a database.

Overrides:
setComplete in class AbstractTransactionalSpringContextTests
See Also:
AbstractTransactionalSpringContextTests.setComplete()

countRowsInTable

protected int countRowsInTable(String tableName)
Count the rows in the given table

Parameters:
tableName - table name to count rows in
Returns:
the number of rows in the table

executeSqlScript

protected void executeSqlScript(String sqlResourcePath,
                                boolean continueOnError)
                         throws DataAccessException
Execute the given SQL script. Will be rolled back by default, according to the fate of the current transaction.

Parameters:
sqlResourcePath - Spring resource path for the SQL script. Should normally be loaded by classpath. There should be one statement per line. Any semicolons will be removed. Do not use this method to execute DDL if you expect rollback.
continueOnError - whether or not to continue without throwing an exception in the event of an error
Throws:
DataAccessException - if there is an error executing a statement and continueOnError was false

The Spring Framework

Copyright © 2002-2007 The Spring Framework.