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 class
ARuntimeException
for use in database access methods and tests. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final String
static final String
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"
.static final String
If the value ofSYSPROP_MATS_TEST_H2
is this value, thecreateStandard()
will use the URLFILE_BASED_TEST_H2_DATABASE_URL
, which is"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE"
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Cleans the test database: Runs SQL"DROP ALL OBJECTS DELETE FILES"
.void
cleanDatabase
(boolean createDataTable) Cleans the test database: Runs SQL"DROP ALL OBJECTS DELETE FILES"
, and optionally invokescreateDataTable()
.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.static TestH2DataSource
Creates aTestH2DataSource
using the supplied URL.void
Creates a test "datatable", runs SQL"CREATE TABLE datatable ( data VARCHAR )"
, using a SQL Connection fromthis
DataSource.static void
createDataTable
(Connection connection) Creates a test "datatable", runs SQL"CREATE TABLE datatable ( data VARCHAR )"
, using the provided SQL Connection.static TestH2DataSource
Creates aTestH2DataSource
using the URLFILE_BASED_TEST_H2_DATABASE_URL
, which is"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE"
.static TestH2DataSource
Creates a unique (random)TestH2DataSource
using the URLIN_MEMORY_TEST_H2_DATABASE_URL
, which is"jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1"
.static TestH2DataSource
Creates an in-memoryTestH2DataSource
as 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.int
Get 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) void
Inserts the provided 'data' into the SQL Table 'datatable', using a SQL Connection fromthis
DataSource.static void
insertDataIntoDataTable
(Connection sqlConnection, String data) Inserts the provided 'data' into the SQL Table 'datatable', using the provided SQL Connection.boolean
isWrapperFor
(Class<?> iface) void
setDescription
(String description) Set the description.void
setLoginTimeout
(int seconds) void
setLogWriter
(PrintWriter out) void
setPassword
(String password) Set the current password.void
setPasswordChars
(char[] password) Set the current password in the form of a char array.void
Set the current URL.void
Set the current URL.void
Set the current user name.<T> T
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilder
Methods inherited from interface javax.sql.ConnectionPoolDataSource
createPooledConnectionBuilder
Methods inherited from interface javax.sql.DataSource
createConnectionBuilder
Methods 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_H2
is 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-memoryTestH2DataSource
as 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 aTestH2DataSource
using the URLFILE_BASED_TEST_H2_DATABASE_URL
, which is"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE"
.- Returns:
- the created
TestH2DataSource
.
-
createInMemoryRandom
Creates a unique (random)TestH2DataSource
using the URLIN_MEMORY_TEST_H2_DATABASE_URL
, which is"jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1"
.- Returns:
- the created
TestH2DataSource
.
-
create
Creates aTestH2DataSource
using 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 fromthis
DataSource. -
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 fromthis
DataSource.- 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 fromthis
DataSource.
-
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:
getConnection
in interfaceDataSource
- Throws:
SQLException
-
getConnection
- Specified by:
getConnection
in interfaceDataSource
- Throws:
SQLException
-
unwrap
- Specified by:
unwrap
in interfaceWrapper
- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperFor
in interfaceWrapper
- Throws:
SQLException
-
getPooledConnection
- Specified by:
getPooledConnection
in interfaceConnectionPoolDataSource
- Throws:
SQLException
-
getPooledConnection
- Specified by:
getPooledConnection
in interfaceConnectionPoolDataSource
- Throws:
SQLException
-
getXAConnection
- Specified by:
getXAConnection
in interfaceXADataSource
- Throws:
SQLException
-
getXAConnection
- Specified by:
getXAConnection
in interfaceXADataSource
- Throws:
SQLException
-
getLogWriter
- Specified by:
getLogWriter
in interfaceCommonDataSource
- Specified by:
getLogWriter
in interfaceConnectionPoolDataSource
- Specified by:
getLogWriter
in interfaceDataSource
- Specified by:
getLogWriter
in interfaceXADataSource
- Throws:
SQLException
-
setLogWriter
- Specified by:
setLogWriter
in interfaceCommonDataSource
- Specified by:
setLogWriter
in interfaceConnectionPoolDataSource
- Specified by:
setLogWriter
in interfaceDataSource
- Specified by:
setLogWriter
in interfaceXADataSource
- Throws:
SQLException
-
setLoginTimeout
- Specified by:
setLoginTimeout
in interfaceCommonDataSource
- Specified by:
setLoginTimeout
in interfaceConnectionPoolDataSource
- Specified by:
setLoginTimeout
in interfaceDataSource
- Specified by:
setLoginTimeout
in interfaceXADataSource
- Throws:
SQLException
-
getLoginTimeout
- Specified by:
getLoginTimeout
in interfaceCommonDataSource
- Specified by:
getLoginTimeout
in interfaceConnectionPoolDataSource
- Specified by:
getLoginTimeout
in interfaceDataSource
- Specified by:
getLoginTimeout
in interfaceXADataSource
- Throws:
SQLException
-
getParentLogger
- Specified by:
getParentLogger
in interfaceCommonDataSource
- Throws:
SQLFeatureNotSupportedException
-