Package io.mats3.test
Class TestH2DataSource
java.lang.Object
io.mats3.test.TestH2DataSource
- All Implemented Interfaces:
Wrapper,CommonDataSource,ConnectionPoolDataSource,DataSource,XADataSource
public class TestH2DataSource
extends Object
implements XADataSource, DataSource, ConnectionPoolDataSource
A wrapped H2 DataBase DataSource which has a couple of extra methods which simplifies testing, in particular the
cleanDatabase(), and createDataTable() method with associated convenience methods for storing and
getting simple values.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classARuntimeExceptionfor use in database access methods and tests. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final StringSystem property ("-D" jvm argument) that if set will change the methodcreateStandard()from returning an in-memory H2 DataSource, to instead return a DataSource using the URL from the value, with the special case that if the value is "file", it will be"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE".static final StringIf the value ofSYSPROP_MATS_TEST_H2is this value, thecreateStandard()will use the URLFILE_BASED_TEST_H2_DATABASE_URL, which is"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE". -
Method Summary
Modifier and TypeMethodDescriptionvoidCleans the test database: Runs SQL"DROP ALL OBJECTS DELETE FILES".voidcleanDatabase(boolean createDataTable) Cleans the test database: Runs SQL"DROP ALL OBJECTS DELETE FILES", and optionally invokescreateDataTable().voidclose()Closes the database IF it is a random in-memory URL (as created bycreateInMemoryRandom(), note: this method will be picked up by Spring as a destroy-method if the instance is made available as a Bean.static TestH2DataSourceCreates aTestH2DataSourceusing the supplied URL.voidCreates a test "datatable", runs SQL"CREATE TABLE datatable ( data VARCHAR )", using a SQL Connection fromthisDataSource.static voidcreateDataTable(Connection connection) Creates a test "datatable", runs SQL"CREATE TABLE datatable ( data VARCHAR )", using the provided SQL Connection.static TestH2DataSourceCreates aTestH2DataSourceusing the URLFILE_BASED_TEST_H2_DATABASE_URL, which is"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE".static TestH2DataSourceCreates a unique (random)TestH2DataSourceusing the URLIN_MEMORY_TEST_H2_DATABASE_URL, which is"jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1".static TestH2DataSourceCreates an in-memoryTestH2DataSourceas specified by the URLIN_MEMORY_TEST_H2_DATABASE_URL, which is"jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1", unless the System PropertySYSPROP_MATS_TEST_H2("mats.test.h2") is directly set to a different URL to use, with the special case that if it isSYSPROP_VALUE_FILE_BASED("file"), in which caseFILE_BASED_TEST_H2_DATABASE_URL("jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE) is used as URL.getConnection(String username, String password) getDataFromDataTable(Connection sqlConnection) Get the current description.intGet the current password.getPooledConnection(String user, String password) getUrl()Get the current URL.getURL()Get the current URL.getUser()Get the current user name.getXAConnection(String user, String password) voidInserts the provided 'data' into the SQL Table 'datatable', using a SQL Connection fromthisDataSource.static voidinsertDataIntoDataTable(Connection sqlConnection, String data) Inserts the provided 'data' into the SQL Table 'datatable', using the provided SQL Connection.booleanisWrapperFor(Class<?> iface) voidsetDescription(String description) Set the description.voidsetLoginTimeout(int seconds) voidsetLogWriter(PrintWriter out) voidsetPassword(String password) Set the current password.voidsetPasswordChars(char[] password) Set the current password in the form of a char array.voidSet the current URL.voidSet the current URL.voidSet the current user name.<T> TMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilderMethods inherited from interface javax.sql.ConnectionPoolDataSource
createPooledConnectionBuilderMethods inherited from interface javax.sql.DataSource
createConnectionBuilderMethods inherited from interface javax.sql.XADataSource
createXAConnectionBuilder
-
Field Details
-
SYSPROP_MATS_TEST_H2
System property ("-D" jvm argument) that if set will change the methodcreateStandard()from returning an in-memory H2 DataSource, to instead return a DataSource using the URL from the value, with the special case that if the value is "file", it will be"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE".Value is
"mats.test.activemq"- See Also:
-
SYSPROP_VALUE_FILE_BASED
If the value ofSYSPROP_MATS_TEST_H2is this value, thecreateStandard()will use the URLFILE_BASED_TEST_H2_DATABASE_URL, which is"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE".Value is
"file"- See Also:
-
FILE_BASED_TEST_H2_DATABASE_URL
- See Also:
-
IN_MEMORY_TEST_H2_DATABASE_URL
- See Also:
-
-
Method Details
-
createStandard
Creates an in-memoryTestH2DataSourceas specified by the URLIN_MEMORY_TEST_H2_DATABASE_URL, which is"jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1", unless the System PropertySYSPROP_MATS_TEST_H2("mats.test.h2") is directly set to a different URL to use, with the special case that if it isSYSPROP_VALUE_FILE_BASED("file"), in which caseFILE_BASED_TEST_H2_DATABASE_URL("jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE) is used as URL. Notice that the methodcleanDatabase()is invoked when creating the DataSource, which is relevant when using the file-based variant.- Returns:
- the created
TestH2DataSource.
-
createFileBased
Creates aTestH2DataSourceusing the URLFILE_BASED_TEST_H2_DATABASE_URL, which is"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE".- Returns:
- the created
TestH2DataSource.
-
createInMemoryRandom
Creates a unique (random)TestH2DataSourceusing the URLIN_MEMORY_TEST_H2_DATABASE_URL, which is"jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1".- Returns:
- the created
TestH2DataSource.
-
create
Creates aTestH2DataSourceusing the supplied URL.- Returns:
- the created
TestH2DataSource.
-
cleanDatabase
public void cleanDatabase()Cleans the test database: Runs SQL"DROP ALL OBJECTS DELETE FILES". -
cleanDatabase
public void cleanDatabase(boolean createDataTable) Cleans the test database: Runs SQL"DROP ALL OBJECTS DELETE FILES", and optionally invokescreateDataTable().- Parameters:
createDataTable- whether to invokecreateDataTable()afterwards.
-
createDataTable
public void createDataTable()Creates a test "datatable", runs SQL"CREATE TABLE datatable ( data VARCHAR )", using a SQL Connection fromthisDataSource. -
createDataTable
Creates a test "datatable", runs SQL"CREATE TABLE datatable ( data VARCHAR )", using the provided SQL Connection. -
insertDataIntoDataTable
Inserts the provided 'data' into the SQL Table 'datatable', using a SQL Connection fromthisDataSource.- Parameters:
data- the data to insert.
-
insertDataIntoDataTable
Inserts the provided 'data' into the SQL Table 'datatable', using the provided SQL Connection.- Parameters:
sqlConnection- the SQL Connection to use to insert data.data- the data to insert.
-
getDataFromDataTable
- Returns:
- all rows in the 'datatable' (probably created by
createDataTable(), using a SQL Connection fromthisDataSource.
-
getDataFromDataTable
- Parameters:
sqlConnection- the SQL Connection to use to fetch data.- Returns:
- all rows in the 'datatable' (probably created by
createDataTable(), using the provided SQL Connection - the SQL is"SELECT data FROM datatable ORDER BY data".
-
close
public void close()Closes the database IF it is a random in-memory URL (as created bycreateInMemoryRandom(), note: this method will be picked up by Spring as a destroy-method if the instance is made available as a Bean. -
getURL
Get the current URL.- Returns:
- the URL
-
getUrl
Get the current URL. This method does the same as getURL, but this methods signature conforms the JavaBean naming convention.- Returns:
- the URL
-
setURL
Set the current URL.- Parameters:
url- the new URL
-
setUrl
Set the current URL. This method does the same as setURL, but this methods signature conforms the JavaBean naming convention.- Parameters:
url- the new URL
-
setPassword
Set the current password.- Parameters:
password- the new password.
-
setPasswordChars
public void setPasswordChars(char[] password) Set the current password in the form of a char array.- Parameters:
password- the new password in the form of a char array.
-
getPassword
Get the current password.- Returns:
- the password
-
getUser
Get the current user name.- Returns:
- the user name
-
setUser
Set the current user name.- Parameters:
user- the new user name
-
getDescription
Get the current description.- Returns:
- the description
-
setDescription
Set the description.- Parameters:
description- the new description
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
unwrap
- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperForin interfaceWrapper- Throws:
SQLException
-
getPooledConnection
- Specified by:
getPooledConnectionin interfaceConnectionPoolDataSource- Throws:
SQLException
-
getPooledConnection
- Specified by:
getPooledConnectionin interfaceConnectionPoolDataSource- Throws:
SQLException
-
getXAConnection
- Specified by:
getXAConnectionin interfaceXADataSource- Throws:
SQLException
-
getXAConnection
- Specified by:
getXAConnectionin interfaceXADataSource- Throws:
SQLException
-
getLogWriter
- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceConnectionPoolDataSource- Specified by:
getLogWriterin interfaceDataSource- Specified by:
getLogWriterin interfaceXADataSource- Throws:
SQLException
-
setLogWriter
- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceConnectionPoolDataSource- Specified by:
setLogWriterin interfaceDataSource- Specified by:
setLogWriterin interfaceXADataSource- Throws:
SQLException
-
setLoginTimeout
- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceConnectionPoolDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Specified by:
setLoginTimeoutin interfaceXADataSource- Throws:
SQLException
-
getLoginTimeout
- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceConnectionPoolDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Specified by:
getLoginTimeoutin interfaceXADataSource- Throws:
SQLException
-
getParentLogger
- Specified by:
getParentLoggerin interfaceCommonDataSource- Throws:
SQLFeatureNotSupportedException
-