|
||||||||||
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 |
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(java.lang.Object) must be wrapped in a transactional proxy |
Constructor Summary | |
---|---|
DatabaseModule()
|
Method Summary | ||
---|---|---|
void |
activateTransactionIfNeeded()
|
|
void |
afterInit()
Initializes the spring support object |
|
void |
cleanSchemas()
Cleans all configured schema's. |
|
void |
clearSchemas()
Clears all configured schema's. |
|
void |
commitTransaction(Object testObject)
Commits the current transaction. |
|
protected DataSource |
createDataSource()
Creates a datasource by using the factory that is defined by the dataSourceFactory.className property |
|
void |
disableConstraints()
Disables all foreigh key and not-null constraints on the configured schema's. |
|
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. |
|
void |
generateDatasetDefinition()
Generates a definition file that defines the structure of dataset's, i.e. a XSD of DTD that describes the structure of the database. |
|
protected
|
getConfiguredDatabaseTaskInstance(Class<T> databaseTaskType)
|
|
DataSource |
getDataSource()
|
|
DataSource |
getDataSourceAndActivateTransactionIfNeeded()
Returns the DataSource that provides connection to the unit test database. |
|
protected SQLHandler |
getDefaultSqlHandler()
|
|
TestListener |
getTestListener()
Creates the test listener for this module. |
|
protected Object |
getTestObject()
|
|
DataSource |
getTransactionalDataSourceAndActivateTransactionIfNeeded(Object testObject)
Returns the DataSource that provides connection to the unit test database. |
|
UnitilsTransactionManager |
getTransactionManager()
Returns the transaction manager or creates one if it does not exist yet. |
|
protected TransactionMode |
getTransactionMode(Object testObject,
Method testMethod)
|
|
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 |
isDataSourceLoaded()
|
|
boolean |
isTransactionsEnabled(Object testObject,
Method testMethod)
|
|
void |
registerTransactionManagementConfiguration(UnitilsTransactionManagementConfiguration transactionManagementConfiguration)
|
|
void |
resetDatabaseState()
Updates the database version to the current version, without issuing any other update to the database. |
|
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 |
startTransaction(Object testObject)
Starts a new transaction on the transaction manager configured in unitils |
|
protected void |
startTransactionForTestMethod(Object testObject,
Method testMethod)
Starts a transaction. |
|
void |
updateDatabase()
Determines whether the test database is outdated and, if this is the case, updates the database with the latest changes. |
|
void |
updateDatabase(SQLHandler sqlHandler)
Determines whether the test database is outdated and, if that is the case, updates the database with the latest changes. |
|
void |
updateSequences()
Updates all sequences that have a value below a certain configurable treshold to become equal to this treshold |
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(java.lang.Object)
must be wrapped in a transactional proxy
protected UnitilsTransactionManager transactionManager
protected Set<UnitilsTransactionManagementConfiguration> transactionManagementConfigurations
PlatformTransactionManager
Constructor Detail |
---|
public DatabaseModule()
Method Detail |
---|
public void init(Properties configuration)
Configuration
init
in interface Module
configuration
- The config, not nullpublic void afterInit()
afterInit
in interface Module
public DataSource getTransactionalDataSourceAndActivateTransactionIfNeeded(Object testObject)
DataSource
that provides connection to the unit test database. When invoked the first
time, the DBMaintainer is invoked to make sure the test database is up-to-date (if database updating is enabled)
If the property PROPERTY_WRAP_DATASOURCE_IN_TRANSACTIONAL_PROXY
has been set to true, the DataSource
returned will make sure that, for the duration of a transaction, the same java.sql.Connection
is returned,
and that invocations of the close() method of these connections are suppressed.
testObject
- The test instance, not null
DataSource
public DataSource getDataSourceAndActivateTransactionIfNeeded()
DataSource
that provides connection to the unit test database. When invoked the first
time, the DBMaintainer is invoked to make sure the test database is up-to-date (if database updating is enabled)
DataSource
public DataSource getDataSource()
public void activateTransactionIfNeeded()
public boolean isDataSourceLoaded()
public UnitilsTransactionManager getTransactionManager()
public void flushDatabaseUpdates(Object testObject)
Flushable
and call Flushable.flushDatabaseUpdates(Object)
on these modules.
testObject
- The test object, not nullpublic void updateDatabase()
DBMaintainer
for more information.
public void updateDatabase(SQLHandler sqlHandler)
sqlHandler
- SQLHandler that needs to be used for the database updatesDBMaintainer}
public void resetDatabaseState()
public void resetDatabaseState(SQLHandler sqlHandler)
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 TestDataSource
testObject
- The test instance, not nullprotected DataSource createDataSource()
protected TransactionMode getTransactionMode(Object testObject, Method testMethod)
testObject
- The test object, not nulltestMethod
- The test method, not null
TransactionMode
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 null
public void clearSchemas()
public void cleanSchemas()
public void disableConstraints()
public void updateSequences()
public void generateDatasetDefinition()
protected <T extends DatabaseAccessing> T getConfiguredDatabaseTaskInstance(Class<T> databaseTaskType)
databaseTaskType
- The type of database task, not null
DatabaseAccessing
of the given typeprotected SQLHandler getDefaultSqlHandler()
public void registerTransactionManagementConfiguration(UnitilsTransactionManagementConfiguration transactionManagementConfiguration)
protected Object getTestObject()
public TestListener getTestListener()
Module
getTestListener
in interface Module
DatabaseModule.DatabaseTestListener
associated with this module
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |