public class TestH2DataSource
extends java.lang.Object
implements javax.sql.XADataSource, javax.sql.DataSource, javax.sql.ConnectionPoolDataSource
cleanDatabase(), and createDataTable() method with associated convenience methods for storing and
getting simple values.| Modifier and Type | Class and Description |
|---|---|
static class |
TestH2DataSource.TestH2DataSourceException
A
RuntimeException for use in database access methods and tests. |
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
FILE_BASED_TEST_H2_DATABASE_URL |
static java.lang.String |
IN_MEMORY_TEST_H2_DATABASE_URL |
static java.lang.String |
SYSPROP_MATS_TEST_H2
System property ("-D" jvm argument) that if set will change the method
createStandard() 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 java.lang.String |
SYSPROP_VALUE_FILE_BASED
If the value of
SYSPROP_MATS_TEST_H2 is this value, the createStandard() will use the URL
FILE_BASED_TEST_H2_DATABASE_URL, which is "jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE". |
| Modifier and Type | Method and Description |
|---|---|
void |
cleanDatabase()
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 invokes
createDataTable(). |
void |
close()
Closes the database IF it is a random in-memory URL (as created by
createInMemoryRandom(), note:
this method will be picked up by Spring as a destroy-method if the instance is made available as a Bean. |
static TestH2DataSource |
create(java.lang.String url)
Creates a
TestH2DataSource using the supplied URL. |
void |
createDataTable()
Creates a test "datatable", runs SQL
"CREATE TABLE datatable ( data VARCHAR )", using a SQL
Connection from this DataSource. |
static void |
createDataTable(java.sql.Connection connection)
Creates a test "datatable", runs SQL
"CREATE TABLE datatable ( data VARCHAR )", using the provided
SQL Connection. |
static TestH2DataSource |
createFileBased()
Creates a
TestH2DataSource using the URL FILE_BASED_TEST_H2_DATABASE_URL, which is
"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE". |
static TestH2DataSource |
createInMemoryRandom()
Creates a unique (random)
TestH2DataSource using the URL IN_MEMORY_TEST_H2_DATABASE_URL, which
is "jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1". |
static TestH2DataSource |
createStandard()
Creates an in-memory
TestH2DataSource as specified by the URL IN_MEMORY_TEST_H2_DATABASE_URL,
which is "jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1", unless the System
Property SYSPROP_MATS_TEST_H2 ("mats.test.h2") is directly set to a different URL to use,
with the special case that if it is SYSPROP_VALUE_FILE_BASED ("file"), in which case
FILE_BASED_TEST_H2_DATABASE_URL ("jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE) is used as URL. |
java.sql.Connection |
getConnection() |
java.sql.Connection |
getConnection(java.lang.String username,
java.lang.String password) |
java.util.List<java.lang.String> |
getDataFromDataTable() |
static java.util.List<java.lang.String> |
getDataFromDataTable(java.sql.Connection sqlConnection) |
java.lang.String |
getDescription()
Get the current description.
|
int |
getLoginTimeout() |
java.io.PrintWriter |
getLogWriter() |
java.util.logging.Logger |
getParentLogger() |
java.lang.String |
getPassword()
Get the current password.
|
javax.sql.PooledConnection |
getPooledConnection() |
javax.sql.PooledConnection |
getPooledConnection(java.lang.String user,
java.lang.String password) |
java.lang.String |
getUrl()
Get the current URL.
|
java.lang.String |
getURL()
Get the current URL.
|
java.lang.String |
getUser()
Get the current user name.
|
javax.sql.XAConnection |
getXAConnection() |
javax.sql.XAConnection |
getXAConnection(java.lang.String user,
java.lang.String password) |
static void |
insertDataIntoDataTable(java.sql.Connection sqlConnection,
java.lang.String data)
Inserts the provided 'data' into the SQL Table 'datatable', using the provided SQL Connection.
|
void |
insertDataIntoDataTable(java.lang.String data)
Inserts the provided 'data' into the SQL Table 'datatable', using a SQL Connection from
this
DataSource. |
boolean |
isWrapperFor(java.lang.Class<?> iface) |
void |
setDescription(java.lang.String description)
Set the description.
|
void |
setLoginTimeout(int seconds) |
void |
setLogWriter(java.io.PrintWriter out) |
void |
setPassword(java.lang.String password)
Set the current password.
|
void |
setPasswordChars(char[] password)
Set the current password in the form of a char array.
|
void |
setUrl(java.lang.String url)
Set the current URL.
|
void |
setURL(java.lang.String url)
Set the current URL.
|
void |
setUser(java.lang.String user)
Set the current user name.
|
<T> T |
unwrap(java.lang.Class<T> iface) |
public static final java.lang.String SYSPROP_MATS_TEST_H2
createStandard() 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"
public static final java.lang.String SYSPROP_VALUE_FILE_BASED
SYSPROP_MATS_TEST_H2 is this value, the createStandard() will use the URL
FILE_BASED_TEST_H2_DATABASE_URL, which is "jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE".
Value is "file"
public static final java.lang.String FILE_BASED_TEST_H2_DATABASE_URL
public static final java.lang.String IN_MEMORY_TEST_H2_DATABASE_URL
public static TestH2DataSource createStandard()
TestH2DataSource as specified by the URL IN_MEMORY_TEST_H2_DATABASE_URL,
which is "jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1", unless the System
Property SYSPROP_MATS_TEST_H2 ("mats.test.h2") is directly set to a different URL to use,
with the special case that if it is SYSPROP_VALUE_FILE_BASED ("file"), in which case
FILE_BASED_TEST_H2_DATABASE_URL ("jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE) is used as URL.
Notice that the method cleanDatabase() is invoked when creating the DataSource, which is relevant
when using the file-based variant.TestH2DataSource.public static TestH2DataSource createFileBased()
TestH2DataSource using the URL FILE_BASED_TEST_H2_DATABASE_URL, which is
"jdbc:h2:./matsTestH2DB;AUTO_SERVER=TRUE".TestH2DataSource.public static TestH2DataSource createInMemoryRandom()
TestH2DataSource using the URL IN_MEMORY_TEST_H2_DATABASE_URL, which
is "jdbc:h2:mem:matsTestH2DB_[randomness];DB_CLOSE_DELAY=-1".TestH2DataSource.public static TestH2DataSource create(java.lang.String url)
TestH2DataSource using the supplied URL.TestH2DataSource.public void cleanDatabase()
"DROP ALL OBJECTS DELETE FILES".public void cleanDatabase(boolean createDataTable)
"DROP ALL OBJECTS DELETE FILES", and optionally invokes
createDataTable().createDataTable - whether to invoke createDataTable() afterwards.public void createDataTable()
"CREATE TABLE datatable ( data VARCHAR )", using a SQL
Connection from this DataSource.public static void createDataTable(java.sql.Connection connection)
"CREATE TABLE datatable ( data VARCHAR )", using the provided
SQL Connection.public void insertDataIntoDataTable(java.lang.String data)
this
DataSource.data - the data to insert.public static void insertDataIntoDataTable(java.sql.Connection sqlConnection,
java.lang.String data)
sqlConnection - the SQL Connection to use to insert data.data - the data to insert.public java.util.List<java.lang.String> getDataFromDataTable()
createDataTable(), using a SQL Connection from
this DataSource.public static java.util.List<java.lang.String> getDataFromDataTable(java.sql.Connection sqlConnection)
sqlConnection - the SQL Connection to use to fetch data.createDataTable(), using the provided SQL
Connection - the SQL is "SELECT data FROM datatable ORDER BY data".public void close()
createInMemoryRandom(), note:
this method will be picked up by Spring as a destroy-method if the instance is made available as a Bean.public java.lang.String getURL()
public java.lang.String getUrl()
public void setURL(java.lang.String url)
url - the new URLpublic void setUrl(java.lang.String url)
url - the new URLpublic void setPassword(java.lang.String password)
password - the new password.public void setPasswordChars(char[] password)
password - the new password in the form of a char array.public java.lang.String getPassword()
public java.lang.String getUser()
public void setUser(java.lang.String user)
user - the new user namepublic java.lang.String getDescription()
public void setDescription(java.lang.String description)
description - the new descriptionpublic java.sql.Connection getConnection()
throws java.sql.SQLException
getConnection in interface javax.sql.DataSourcejava.sql.SQLExceptionpublic java.sql.Connection getConnection(java.lang.String username,
java.lang.String password)
throws java.sql.SQLException
getConnection in interface javax.sql.DataSourcejava.sql.SQLExceptionpublic <T> T unwrap(java.lang.Class<T> iface)
throws java.sql.SQLException
unwrap in interface java.sql.Wrapperjava.sql.SQLExceptionpublic boolean isWrapperFor(java.lang.Class<?> iface)
throws java.sql.SQLException
isWrapperFor in interface java.sql.Wrapperjava.sql.SQLExceptionpublic javax.sql.PooledConnection getPooledConnection()
throws java.sql.SQLException
getPooledConnection in interface javax.sql.ConnectionPoolDataSourcejava.sql.SQLExceptionpublic javax.sql.PooledConnection getPooledConnection(java.lang.String user,
java.lang.String password)
throws java.sql.SQLException
getPooledConnection in interface javax.sql.ConnectionPoolDataSourcejava.sql.SQLExceptionpublic javax.sql.XAConnection getXAConnection()
throws java.sql.SQLException
getXAConnection in interface javax.sql.XADataSourcejava.sql.SQLExceptionpublic javax.sql.XAConnection getXAConnection(java.lang.String user,
java.lang.String password)
throws java.sql.SQLException
getXAConnection in interface javax.sql.XADataSourcejava.sql.SQLExceptionpublic java.io.PrintWriter getLogWriter()
throws java.sql.SQLException
getLogWriter in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionpublic void setLogWriter(java.io.PrintWriter out)
throws java.sql.SQLException
setLogWriter in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionpublic void setLoginTimeout(int seconds)
throws java.sql.SQLException
setLoginTimeout in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionpublic int getLoginTimeout()
throws java.sql.SQLException
getLoginTimeout in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionpublic java.util.logging.Logger getParentLogger()
throws java.sql.SQLFeatureNotSupportedException
getParentLogger in interface javax.sql.CommonDataSourcejava.sql.SQLFeatureNotSupportedException