public class DatabaseModule extends Object implements Module
DataSourceFactory. 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| Modifier and Type | Class and Description |
|---|---|
protected class |
DatabaseModule.DatabaseTestListener
The
TestListener for this module |
| Modifier and Type | Field and Description |
|---|---|
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
|
static String |
PROPERTY_RESET_SEQUENCES
Sets all the sequences to the lowest acceptable value.
|
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
|
protected Map<String,DataSourceWrapper> |
wrappers |
| Constructor and Description |
|---|
DatabaseModule() |
| Modifier and Type | Method and Description |
|---|---|
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() |
protected DataSource |
getDataSource(String databaseName,
Map<String,DataSource> mapDatasources,
Object testObject) |
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(DataSourceWrapper wrapper) |
void |
registerTransactionManagementConfiguration(UnitilsTransactionManagementConfiguration transactionManagementConfiguration) |
void |
resetDatabaseState(SQLHandler sqlHandler,
DataSourceWrapper wrapper)
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
|
protected void |
setFieldDataSource(String databaseName,
DataSource dataSource,
Object testObject,
Set<Field> fields,
Set<Method> methods) |
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.
|
public static final String PROPERTY_UPDATEDATABASESCHEMA_ENABLED
public static final String PROPERTY_RESET_SEQUENCES
public static final String PROPERTY_WRAP_DATASOURCE_IN_TRANSACTIONAL_PROXY
#getTransactionalDataSourceAndActivateTransactionIfNeeded(Object)
must be wrapped in a transactional proxyprotected Map<Class<? extends Annotation>,Map<String,String>> defaultAnnotationPropertyValues
protected DataSource dataSource
protected Properties configuration
protected boolean updateDatabaseSchemaEnabled
protected boolean wrapDataSourceInTransactionalProxy
protected UnitilsTransactionManager transactionManager
protected Set<UnitilsTransactionManagementConfiguration> transactionManagementConfigurations
PlatformTransactionManagerprotected Map<String,DataSourceWrapper> wrappers
public void init(Properties configuration)
Configurationpublic void afterInit()
public void registerTransactionManagementConfiguration()
public void registerTransactionManagementConfiguration(DataSourceWrapper wrapper)
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, DataSourceWrapper wrapper)
sqlHandler - The DefaultSQLHandler to which all commands are
issuedpublic void injectDataSource(Object testObject)
TestDataSource to every field annotated with
TestDataSource and calls all methods annotated with
TestDataSourcetestObject - The test instance, not nullInvocationTargetExceptionIllegalAccessExceptionIllegalArgumentExceptionprotected void setFieldDataSource(String databaseName, DataSource dataSource, Object testObject, Set<Field> fields, Set<Method> methods)
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 nullTransactionMode for the given objectprotected void startTransactionForTestMethod(Object testObject, Method testMethod)
testObject - The test object, not nulltestMethod - The test method, not nullprotected 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 nullpublic boolean isTransactionsEnabled(Object testObject, Method testMethod)
testObject - The test object, not nulltestMethod - The test method, not nullpublic void registerTransactionManagementConfiguration(UnitilsTransactionManagementConfiguration transactionManagementConfiguration)
protected Object getTestObject()
public TestListener getTestListener()
getTestListener in interface ModuleTestListener associated with this modulepublic DataSourceWrapper getWrapper(String databaseName)
databaseName - public void setWrapper(DataSourceWrapper wrapper)
wrapper - the wrapper to setpublic DatabaseConfigurations getDatabaseConfigurations()
Copyright © 2017. All rights reserved.