|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.unitils.database.DatabaseModule
public class DatabaseModule
Module that provides support for database testing: Creation of a datasource that connects to the test database, support for executing tests in a transaction and automatic maintenance of the test database.
A datasource will be created the first time one is requested. Which type of datasource will be created depends on the configuredDataSourceFactory. By default this will be a pooled datasource that gets its connection-url,
username and password from the unitils configuration.
The created datasource can be injected into a field of the test by annotating the field with TestDataSource.
It can then be used to install it in your DAO or other class under test.
If the DBMaintainer is enabled (by setting PROPERTY_UPDATEDATABASESCHEMA_ENABLED to true), the test database
schema will automatically be updated if needed. This check will be performed once during your test-suite run, namely
when the data source is created.
If the test class or method is annotated with Transactional with transaction mode TransactionMode.COMMIT or
TransactionMode.ROLLBACK, or if the property 'DatabaseModule.Transactional.value.default' was set to 'commit' or
'rollback', every test is executed in a transaction.
TestDataSource,
DBMaintainer,
Transactional| Nested Class Summary | |
|---|---|
protected class |
DatabaseModule.DatabaseTestListener
The TestListener for this module |
| Field Summary | |
|---|---|
protected Properties |
configuration
The configuration of Unitils |
protected DataSource |
dataSource
The datasources with the name as key |
protected Map<Class<? extends Annotation>,Map<String,String>> |
defaultAnnotationPropertyValues
Map holding the default configuration of the database module annotations |
protected String |
dialect
|
static String |
PROPERTY_UPDATEDATABASESCHEMA_ENABLED
Property indicating if the database schema should be updated before performing the tests |
static String |
PROPERTY_WRAP_DATASOURCE_IN_TRANSACTIONAL_PROXY
Property indicating whether the datasource injected onto test fields annotated with @TestDataSource or retrieved using #getTransactionalDataSourceAndActivateTransactionIfNeeded(Object) must be wrapped in a transactional proxy |
protected Set<UnitilsTransactionManagementConfiguration> |
transactionManagementConfigurations
Set of possible providers of a spring PlatformTransactionManager |
protected UnitilsTransactionManager |
transactionManager
The transaction manager |
protected boolean |
updateDatabaseSchemaEnabled
Indicates if the DBMaintainer should be invoked to update the database |
protected boolean |
wrapDataSourceInTransactionalProxy
Indicates whether the datasource injected onto test fields annotated with @TestDataSource or retrieved using #getTransactionalDataSourceAndActivateTransactionIfNeeded must be wrapped in a transactional proxy |
| Constructor Summary | |
|---|---|
DatabaseModule()
|
|
| Method Summary | |
|---|---|
void |
activateTransactionIfNeeded()
|
void |
afterInit()
Initializes the spring support object |
void |
commitTransaction(Object testObject)
Commits the current transaction. |
protected void |
endTransactionForTestMethod(Object testObject,
Method testMethod)
Commits or rollbacks the current transaction, if transactions are enabled and a transactionManager is active for the given testObject |
void |
flushDatabaseUpdates(Object testObject)
Flushes all pending updates to the database. |
DatabaseConfigurations |
getDatabaseConfigurations()
|
List<String> |
getDatabaseName(Object testObject,
Method testMethod)
Checks if the DataSet or the ExpectedDataSet contains the name of a database. |
protected DataSource |
getDataSource(String databaseName,
Map<String,DataSource> mapDatasources,
Object testObject)
|
String |
getDialect()
|
TestListener |
getTestListener()
|
protected Object |
getTestObject()
|
UnitilsTransactionManager |
getTransactionManager()
Returns the transaction manager or creates one if it does not exist yet. |
protected TransactionMode |
getTransactionMode(Object testObject,
Method testMethod)
|
DataSourceWrapper |
getWrapper(String databaseName)
|
void |
init(Properties configuration)
Initializes this module using the given Configuration |
void |
injectDataSource(Object testObject)
Assigns the TestDataSource to every field annotated with TestDataSource and calls all methods
annotated with TestDataSource |
boolean |
isTransactionsEnabled(Object testObject,
Method testMethod)
|
void |
registerTransactionManagementConfiguration()
|
void |
registerTransactionManagementConfiguration(UnitilsTransactionManagementConfiguration transactionManagementConfiguration)
|
void |
resetDatabaseState(SQLHandler sqlHandler)
Updates the database version to the current version, without issuing any other updates to the database. |
void |
rollbackTransaction(Object testObject)
Performs a rollback of the current transaction |
void |
setWrapper(DataSourceWrapper wrapper)
|
void |
startTransaction(Object testObject)
Starts a new transaction on the transaction manager configured in unitils |
protected void |
startTransactionForTestMethod(Object testObject,
Method testMethod)
Starts a transaction. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String PROPERTY_UPDATEDATABASESCHEMA_ENABLED
public static final String PROPERTY_WRAP_DATASOURCE_IN_TRANSACTIONAL_PROXY
#getTransactionalDataSourceAndActivateTransactionIfNeeded(Object) must be wrapped in a transactional proxy
protected Map<Class<? extends Annotation>,Map<String,String>> defaultAnnotationPropertyValues
protected DataSource dataSource
protected Properties configuration
protected boolean updateDatabaseSchemaEnabled
protected boolean wrapDataSourceInTransactionalProxy
#getTransactionalDataSourceAndActivateTransactionIfNeeded must be wrapped in a transactional proxy
protected UnitilsTransactionManager transactionManager
protected Set<UnitilsTransactionManagementConfiguration> transactionManagementConfigurations
PlatformTransactionManager
protected String dialect
| Constructor Detail |
|---|
public DatabaseModule()
| Method Detail |
|---|
public void init(Properties configuration)
Configuration
init in interface Moduleconfiguration - The config, not nullpublic void afterInit()
afterInit in interface Modulepublic void registerTransactionManagementConfiguration()
public void activateTransactionIfNeeded()
public UnitilsTransactionManager getTransactionManager()
public void flushDatabaseUpdates(Object testObject)
Flushable and call Flushable.flushDatabaseUpdates(Object)
on these modules.
testObject - The test object, not nullpublic void resetDatabaseState(SQLHandler sqlHandler)
sqlHandler - The DefaultSQLHandler to which all commands are issued
public void injectDataSource(Object testObject)
throws IllegalArgumentException,
IllegalAccessException,
InvocationTargetException
TestDataSource to every field annotated with TestDataSource and calls all methods
annotated with TestDataSource
testObject - The test instance, not null
InvocationTargetException
IllegalAccessException
IllegalArgumentException
protected DataSource getDataSource(String databaseName,
Map<String,DataSource> mapDatasources,
Object testObject)
protected TransactionMode getTransactionMode(Object testObject,
Method testMethod)
testObject - The test object, not nulltestMethod - The test method, not null
TransactionMode for the given object
protected void startTransactionForTestMethod(Object testObject,
Method testMethod)
testObject - The test object, not nulltestMethod - The test method, not null
protected void endTransactionForTestMethod(Object testObject,
Method testMethod)
testObject - The test object, not nulltestMethod - The test method, not nullpublic void startTransaction(Object testObject)
testObject - The test object, not nullpublic void commitTransaction(Object testObject)
testObject - The test object, not nullpublic void rollbackTransaction(Object testObject)
testObject - The test object, not null
public boolean isTransactionsEnabled(Object testObject,
Method testMethod)
testObject - The test object, not nulltestMethod - The test method, not null
public void registerTransactionManagementConfiguration(UnitilsTransactionManagementConfiguration transactionManagementConfiguration)
protected Object getTestObject()
public TestListener getTestListener()
getTestListener in interface ModuleDatabaseModule.DatabaseTestListener associated with this module
public List<String> getDatabaseName(Object testObject,
Method testMethod)
DataSet or the ExpectedDataSet contains the name of a database.
If this isn't the case, than the default databaseName of the unitils.properties is used.
If a DataSet contains a different
testObject - testMethod -
Stringpublic String getDialect()
public DataSourceWrapper getWrapper(String databaseName)
public void setWrapper(DataSourceWrapper wrapper)
wrapper - the wrapper to setpublic DatabaseConfigurations getDatabaseConfigurations()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||