org.unitils.database
Class DataSourceWrapper

java.lang.Object
  extended by org.unitils.database.DataSourceWrapper

public class DataSourceWrapper
extends Object

DataSourceWrapper.

Since:
3.4
Author:
Jeroen Horemans, Thomas De Rycke, Willemijn Wouters

Field Summary
protected  DatabaseConfiguration databaseConfiguration
           
 
Constructor Summary
DataSourceWrapper(DatabaseConfiguration databaseConfiguration, Properties unitilsConfig, UnitilsTransactionManager transactionmanager)
           
DataSourceWrapper(DatabaseConfiguration databaseConfiguration, UnitilsTransactionManager transactionManager)
           
 
Method Summary
 void activateTransactionIfNeeded()
           
 void cleanSchemas()
          Cleans all configured schema's.
 void clearSchemas()
          Clears all configured schema's.
 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.
 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
<T extends DatabaseAccessing>
T
getConfiguredDatabaseTaskInstance(Class<T> databaseTaskType)
           
 Connection getConnection()
           
 DatabaseConfiguration getDatabaseConfiguration()
           
 String getDatabaseName()
           
 DataSource getDataSource()
           
 DataSource getDataSourceAndActivateTransactionIfNeeded()
          Returns the DataSource that provides connection to the unit test database.
protected  SQLHandler getDefaultSqlHandler()
           
protected  Object getTestObject()
           
 DataSource getTransactionalDataSourceAndActivateTransactionIfNeeded(Object testObject)
          Returns the DataSource that provides connection to the unit test database.
 boolean isDataSourceLoaded()
           
 void setTransactionManager(UnitilsTransactionManager transactionManager)
           
 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

databaseConfiguration

protected DatabaseConfiguration databaseConfiguration
Constructor Detail

DataSourceWrapper

public DataSourceWrapper(DatabaseConfiguration databaseConfiguration,
                         UnitilsTransactionManager transactionManager)

DataSourceWrapper

public DataSourceWrapper(DatabaseConfiguration databaseConfiguration,
                         Properties unitilsConfig,
                         UnitilsTransactionManager transactionmanager)
Method Detail

getConnection

public Connection getConnection()
Returns:
A connection from the data source, not null

getTransactionalDataSourceAndActivateTransactionIfNeeded

public DataSource getTransactionalDataSourceAndActivateTransactionIfNeeded(Object testObject)
Returns the 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.

Parameters:
testObject - The test instance, not null
wrapDataSourceInTransactionalProxy -
Returns:
The DataSource (default database).

createDataSource

public DataSource createDataSource()
Creates a datasource by using the factory that is defined by the dataSourceFactory.className property

Returns:
the datasource

updateDatabase

public void updateDatabase()
Determines whether the test database is outdated and, if this is the case, updates the database with the latest changes. See DBMaintainer for more information.


updateDatabase

public void updateDatabase(SQLHandler sqlHandler)
Determines whether the test database is outdated and, if that is the case, updates the database with the latest changes.

Parameters:
sqlHandler - SQLHandler that needs to be used for the database updates
See Also:
DBMaintainer}

getDefaultSqlHandler

protected SQLHandler getDefaultSqlHandler()
Returns:
The default SQLHandler, which simply executes the sql statements on the unitils-configured test database

getDataSourceAndActivateTransactionIfNeeded

public DataSource getDataSourceAndActivateTransactionIfNeeded()
Returns the 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)

Returns:
The DataSource

activateTransactionIfNeeded

public void activateTransactionIfNeeded()

getTestObject

protected Object getTestObject()

getDataSource

public DataSource getDataSource()

clearSchemas

public void clearSchemas()
Clears all configured schema's. I.e. drops all tables, views and other database objects.


cleanSchemas

public void cleanSchemas()
Cleans all configured schema's. I.e. removes all data from its database tables.


disableConstraints

public void disableConstraints()
Disables all foreigh key and not-null constraints on the configured schema's.


updateSequences

public void updateSequences()
Updates all sequences that have a value below a certain configurable treshold to become equal to this treshold


getConfiguredDatabaseTaskInstance

protected <T extends DatabaseAccessing> T getConfiguredDatabaseTaskInstance(Class<T> databaseTaskType)
Parameters:
databaseTaskType - The type of database task, not null
Returns:
A configured instance of DatabaseAccessing of the given type

generateDatasetDefinition

public 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.


isDataSourceLoaded

public boolean isDataSourceLoaded()

getDatabaseConfiguration

public DatabaseConfiguration getDatabaseConfiguration()

getDatabaseName

public String getDatabaseName()
Returns:
the databaseName

setTransactionManager

public void setTransactionManager(UnitilsTransactionManager transactionManager)
Parameters:
transactionManager - the transactionManager to set


Copyright © 2014. All Rights Reserved.