Class SimpleSingleConnectionDataSource
- java.lang.Object
-
- org.kiwiproject.test.jdbc.SimpleSingleConnectionDataSource
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Wrapper
,CommonDataSource
,DataSource
public class SimpleSingleConnectionDataSource extends Object implements DataSource, Closeable
A very simple implementation ofDataSource
intended to be used only during tests. As its name suggests, this implementation stores a singleConnection
which is always returned by thegetConnection
methods.Note specifically that the single connection ignores calls to
close()
, since we do not expect the code under test to close connections. To close the single connection, close this instance, which implementsCloseable
in addition toDataSource
.The single Connection is eagerly initialized during construction since the expected usage pattern is to create an object before all tests run (e.g. using a JUnit
@BeforeAll
). Therefore, we keep things simple by eagerly initializing instead of waiting for the firstgetConnection
call to occur.To be very clear, this is intended to be used only during tests, and specifically in tests that execute within a transaction that is rolled back once each test has executed. This ensures no data is actually stored in the database making for faster tests (due to no commit overhead) and also permits testing simultaneously against a shared database, for example a database setup for multiple developers or continuous integration servers to run tests against.
This simple implementation does not support all of the
DataSource
methods. See the docs for each method.- Implementation Note:
- This is heavily influenced from and some code copied from Spring's
SingleConnectionDataSource
but is much simpler since we do not need it to be as generic as Spring's version. We mostly used the code relating to theInvocationHandler
that ignores calls to close aConnection
, but are only handling one methodclose
that we want to intercept.
-
-
Constructor Summary
Constructors Constructor Description SimpleSingleConnectionDataSource(String url, String username)
Create a new SingleConnectionDataSource with the given database URL and username.SimpleSingleConnectionDataSource(String url, String username, String password)
Create a new SingleConnectionDataSource with the given database URL, username, and password to be supplied toDriverManager
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the underlying singleConnection
.Connection
getConnection()
Always returns the single Connection stored in this DataSource.Connection
getConnection(String username, String password)
Returns the single Connection if and only if the given username and password match the ones provided when this instance was constructed.int
getLoginTimeout()
Returns 0, indicating the default system timeout is to be used.PrintWriter
getLogWriter()
Getting a log writer is not supported.Logger
getParentLogger()
Return a parent logger with the JUL global logger name.boolean
isWrapperFor(Class<?> iface)
Return true when the specified class is assignment-compatible with this instance.void
setLoginTimeout(int seconds)
Setting a login timeout is not supported.void
setLogWriter(PrintWriter out)
Setting a log writer is not supported.<T> T
unwrap(Class<T> iface)
Returnsthis
when the specified class is assignment-compatible with this instance.-
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.DataSource
createConnectionBuilder
-
-
-
-
Constructor Detail
-
SimpleSingleConnectionDataSource
public SimpleSingleConnectionDataSource(String url, String username)
Create a new SingleConnectionDataSource with the given database URL and username. An empty password is supplied toDriverManager
.The single Connection is eagerly initialized in this constructor.
- Parameters:
url
- the database URLusername
- the database username
-
SimpleSingleConnectionDataSource
public SimpleSingleConnectionDataSource(String url, String username, String password)
Create a new SingleConnectionDataSource with the given database URL, username, and password to be supplied toDriverManager
.The single Connection is eagerly initialized in this constructor.
- Parameters:
url
- the database URLusername
- the database usernamepassword
- the database password
-
-
Method Detail
-
close
public void close()
Closes the underlying singleConnection
. If any errors occur they are logged at WARN level but no exceptions are thrown.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
getConnection
public Connection getConnection() throws SQLException
Always returns the single Connection stored in this DataSource.Attempts to close it will be ignored.
- Specified by:
getConnection
in interfaceDataSource
- Returns:
- the single Connection of this DataSource
- Throws:
SQLException
- Implementation Note:
- Ignoring Sonar warning about getters and setters using the "expected" fields. While named like a getter method, it isn't really a getter method and this rule doesn't apply.
-
getConnection
public Connection getConnection(String username, String password) throws SQLException
Returns the single Connection if and only if the given username and password match the ones provided when this instance was constructed. It does not make sense to allow different username and password for the same exact Connection.- Specified by:
getConnection
in interfaceDataSource
- Parameters:
username
- the database usernamepassword
- the database password- Returns:
- the single Connection of this DataSource
- Throws:
SQLException
- if the given username and password don't match the ones assigned to this instance- Implementation Note:
- Adapted from from Spring's
SingleConnectionDataSource
-
getLogWriter
public PrintWriter getLogWriter() throws SQLException
Getting a log writer is not supported.Always throws an
UnsupportedOperationException
.- Specified by:
getLogWriter
in interfaceCommonDataSource
- Specified by:
getLogWriter
in interfaceDataSource
- Throws:
SQLException
-
setLogWriter
public void setLogWriter(PrintWriter out) throws SQLException
Setting a log writer is not supported.Always throws an
UnsupportedOperationException
.- Specified by:
setLogWriter
in interfaceCommonDataSource
- Specified by:
setLogWriter
in interfaceDataSource
- Throws:
SQLException
-
setLoginTimeout
public void setLoginTimeout(int seconds) throws SQLException
Setting a login timeout is not supported.- Specified by:
setLoginTimeout
in interfaceCommonDataSource
- Specified by:
setLoginTimeout
in interfaceDataSource
- Throws:
SQLException
-
getLoginTimeout
public int getLoginTimeout() throws SQLException
Returns 0, indicating the default system timeout is to be used.- Specified by:
getLoginTimeout
in interfaceCommonDataSource
- Specified by:
getLoginTimeout
in interfaceDataSource
- Throws:
SQLException
-
getParentLogger
public Logger getParentLogger() throws SQLFeatureNotSupportedException
Return a parent logger with the JUL global logger name.- Specified by:
getParentLogger
in interfaceCommonDataSource
- Returns:
- a Logger with the global logger name
- Throws:
SQLFeatureNotSupportedException
- See Also:
Logger.GLOBAL_LOGGER_NAME
-
unwrap
public <T> T unwrap(Class<T> iface) throws SQLException
Returnsthis
when the specified class is assignment-compatible with this instance.- Specified by:
unwrap
in interfaceWrapper
- Throws:
SQLException
- if the specified class is not assignment-compatible with this instance- See Also:
Class.isInstance(Object)
- Implementation Note:
- This was copied from Spring's
SingleConnectionDataSource
.
-
isWrapperFor
public boolean isWrapperFor(Class<?> iface) throws SQLException
Return true when the specified class is assignment-compatible with this instance.- Specified by:
isWrapperFor
in interfaceWrapper
- Throws:
SQLException
-
-