org.unitils.database.transaction.impl
Class DefaultUnitilsTransactionManager

java.lang.Object
  extended by org.unitils.database.transaction.impl.DefaultUnitilsTransactionManager
All Implemented Interfaces:
UnitilsTransactionManager

public class DefaultUnitilsTransactionManager
extends Object
implements UnitilsTransactionManager

Implements transactions for unit tests, by delegating to a spring PlatformTransactionManager. The concrete implementation of PlatformTransactionManager that is used depends on the test class. If a custom PlatformTransactionManager was configured in a spring ApplicationContext, this one is used. If not, a suitable subclass of PlatformTransactionManager is created, depending on the configuration of a test. E.g. if some ORM persistence unit was configured on the test, a PlatformTransactionManager that can offer transactional behavior for such a persistence unit is used. If no such configuration is found, a DataSourceTransactionManager is used.

Author:
Filip Neven, Tim Ducheyne

Field Summary
protected  Map<Object,org.springframework.transaction.PlatformTransactionManager> testObjectPlatformTransactionManagerMap
          ThreadLocal for holding the PlatformTransactionManager that is used by the current test
protected  Map<Object,Boolean> testObjectTransactionActiveMap
           
protected  Map<Object,org.springframework.transaction.TransactionStatus> testObjectTransactionStatusMap
          ThreadLocal for holding the TransactionStatus that keeps track of the current test's transaction status
protected  List<UnitilsTransactionManagementConfiguration> transactionManagementConfigurations
          Set of possible providers of a spring PlatformTransactionManager, not null
 
Constructor Summary
DefaultUnitilsTransactionManager()
           
 
Method Summary
 void activateTransactionIfNeeded(Object testObject)
           
 void commit(Object testObject)
          Commits the transaction.
protected  org.springframework.transaction.TransactionDefinition createTransactionDefinition(Object testObject)
          Returns a TransactionDefinition object containing the necessary transaction parameters.
protected  void doStartTransaction(Object testObject, UnitilsTransactionManagementConfiguration transactionManagementConfiguration)
           
 DataSource getTransactionalDataSource(DataSource dataSource)
          Returns the given datasource, wrapped in a spring TransactionAwareDataSourceProxy
protected  UnitilsTransactionManagementConfiguration getTransactionManagementConfiguration(Object testObject)
           
 void init(Set<UnitilsTransactionManagementConfiguration> transactionManagementConfigurations)
          Initialize the transaction manager
 void rollback(Object testObject)
          Rolls back the transaction.
protected  void setTransactionManagementConfigurations(Set<UnitilsTransactionManagementConfiguration> transactionManagementConfigurationsSet)
           
 void startTransaction(Object testObject)
          Starts the transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

testObjectTransactionActiveMap

protected Map<Object,Boolean> testObjectTransactionActiveMap

testObjectTransactionStatusMap

protected Map<Object,org.springframework.transaction.TransactionStatus> testObjectTransactionStatusMap
ThreadLocal for holding the TransactionStatus that keeps track of the current test's transaction status


testObjectPlatformTransactionManagerMap

protected Map<Object,org.springframework.transaction.PlatformTransactionManager> testObjectPlatformTransactionManagerMap
ThreadLocal for holding the PlatformTransactionManager that is used by the current test


transactionManagementConfigurations

protected List<UnitilsTransactionManagementConfiguration> transactionManagementConfigurations
Set of possible providers of a spring PlatformTransactionManager, not null

Constructor Detail

DefaultUnitilsTransactionManager

public DefaultUnitilsTransactionManager()
Method Detail

init

public void init(Set<UnitilsTransactionManagementConfiguration> transactionManagementConfigurations)
Description copied from interface: UnitilsTransactionManager
Initialize the transaction manager

Specified by:
init in interface UnitilsTransactionManager
Parameters:
transactionManagementConfigurations - Set of possible providers of a spring PlatformTransactionManager, not null

getTransactionalDataSource

public DataSource getTransactionalDataSource(DataSource dataSource)
Returns the given datasource, wrapped in a spring TransactionAwareDataSourceProxy

Specified by:
getTransactionalDataSource in interface UnitilsTransactionManager
Parameters:
dataSource - The data source to wrap, not null
Returns:
A transactional data source, not null

startTransaction

public void startTransaction(Object testObject)
Starts the transaction. Starts a transaction on the PlatformTransactionManager that is configured for the given testObject.

Specified by:
startTransaction in interface UnitilsTransactionManager
Parameters:
testObject - The test object, not null

activateTransactionIfNeeded

public void activateTransactionIfNeeded(Object testObject)
Specified by:
activateTransactionIfNeeded in interface UnitilsTransactionManager

doStartTransaction

protected void doStartTransaction(Object testObject,
                                  UnitilsTransactionManagementConfiguration transactionManagementConfiguration)

commit

public void commit(Object testObject)
Commits the transaction. Uses the PlatformTransactionManager and transaction that is associated with the given test object.

Specified by:
commit in interface UnitilsTransactionManager
Parameters:
testObject - The test object, not null

rollback

public void rollback(Object testObject)
Rolls back the transaction. Uses the PlatformTransactionManager and transaction that is associated with the given test object.

Specified by:
rollback in interface UnitilsTransactionManager
Parameters:
testObject - The test object, not null

createTransactionDefinition

protected org.springframework.transaction.TransactionDefinition createTransactionDefinition(Object testObject)
Returns a TransactionDefinition object containing the necessary transaction parameters. Simply returns a default DefaultTransactionDefinition object with the 'propagation required' attribute

Parameters:
testObject - The test object, not null
Returns:
The default TransactionDefinition

getTransactionManagementConfiguration

protected UnitilsTransactionManagementConfiguration getTransactionManagementConfiguration(Object testObject)

setTransactionManagementConfigurations

protected void setTransactionManagementConfigurations(Set<UnitilsTransactionManagementConfiguration> transactionManagementConfigurationsSet)


Copyright © 2011. All Rights Reserved.