Class ConnectionPreservingOperationListener
- All Implemented Interfaces:
IOperationListener
IOperationListener that forwards every callback to a delegate except a
close of a connection whose lifecycle is owned elsewhere: it drops
operationSetUpFinished/operationTearDownFinished for the connection (or
connections) it protects, so the delegate's DefaultOperationListener-style
close cannot run on one still in use for a later step - a row count check verify, a
connection injected into the test method, or the shared connection
DefaultPrepAndExpectedTestCase drives its whole lifecycle on.
Two shapes: a blanket listener protects every connection (the caller owns them all), and a
scoped one
protects the single connection its Supplier names, re-evaluated on each callback
since that connection may not be resolved yet when the listener is installed; any other
connection has its callbacks forwarded unchanged, so a tester handing out a fresh connection
per operation still has each of those closed right after its own operation.
The shield suppresses the whole operationSetUpFinished/
operationTearDownFinished callback for a protected connection, not only its
connection.close(): IOperationListener has no way to signal "do your other
work but skip the close". This is exactly right for the delegates dbUnit ships -
DefaultOperationListener (closes, nothing else) and
IOperationListener.NO_OP_OPERATION_LISTENER (does nothing). A custom delegate that
also does non-close work in those callbacks (commit a transaction, release a lock,
record a metric) does not see them for a protected connection; do that work from
connectionRetrieved instead, which is always forwarded, or manage the connection
entirely in the delegate and do not protect it here.
- Since:
- 3.6.0
- Author:
- Jeff Jensen
-
Field Summary
Fields inherited from interface org.dbunit.IOperationListener
NO_OP_OPERATION_LISTENER -
Constructor Summary
ConstructorsConstructorDescriptionCreates a blanket listener that protects every connection from a close by the delegate, forwarding onlyconnectionRetrieved.ConnectionPreservingOperationListener(IOperationListener delegate, Supplier<IDatabaseConnection> protectedConnection) Creates a listener that protects the one connectionprotectedConnectionsupplies, forwarding every callback for any other connection. -
Method Summary
Modifier and TypeMethodDescriptionvoidconnectionRetrieved(IDatabaseConnection connection) Is invoked immediately after a connection was newly created or an existing connection is retrieved to do some work on it.final IOperationListenerReturns the listener this one forwards to.voidoperationSetUpFinished(IDatabaseConnection connection) Notification of the completion of theIDatabaseTester.onSetup()method.voidoperationTearDownFinished(IDatabaseConnection connection) Notification of the completion of theIDatabaseTester.onTearDown()method Should close the given connection if desired.static IOperationListenerunwrap(IOperationListener existingListener) Returns the listener a newly-installed wrapper should delegate to, so re-wrapping a tester shared across tests does not nest one wrapper layer per test: an existingConnectionPreservingOperationListener's own delegate, the tester's existing listener as-is, or a freshDefaultOperationListenerwhen it had none.static booleanunwrapsToNoOp(IOperationListener listener) Returns whetherlistener, after unwrapping anyConnectionPreservingOperationListenerlayers, isIOperationListener.NO_OP_OPERATION_LISTENER- the established, pre-existing signal that a tester's connection is managed elsewhere and must not be closed by whatever is driving the tester's lifecycle.
-
Constructor Details
-
ConnectionPreservingOperationListener
Creates a blanket listener that protects every connection from a close by the delegate, forwarding onlyconnectionRetrieved.- Parameters:
delegate- The listener to forward to.
-
ConnectionPreservingOperationListener
public ConnectionPreservingOperationListener(IOperationListener delegate, Supplier<IDatabaseConnection> protectedConnection) Creates a listener that protects the one connectionprotectedConnectionsupplies, forwarding every callback for any other connection.- Parameters:
delegate- The listener to forward to.protectedConnection- Supplies the connection to protect, re-read on each callback; may returnnullbefore that connection is resolved, protecting nothing until it is.
-
-
Method Details
-
getDelegate
Returns the listener this one forwards to.- Returns:
- The delegate listener.
-
connectionRetrieved
Description copied from interface:IOperationListenerIs invoked immediately after a connection was newly created or an existing connection is retrieved to do some work on it. It should be used to initialize theDatabaseConfigof the connection with user defined parameters.- Specified by:
connectionRetrievedin interfaceIOperationListener- Parameters:
connection- The database connection
-
operationSetUpFinished
Description copied from interface:IOperationListenerNotification of the completion of theIDatabaseTester.onSetup()method. Should close the given connection if desired.- Specified by:
operationSetUpFinishedin interfaceIOperationListener- Parameters:
connection- The database connection
-
operationTearDownFinished
Description copied from interface:IOperationListenerNotification of the completion of theIDatabaseTester.onTearDown()method Should close the given connection if desired.- Specified by:
operationTearDownFinishedin interfaceIOperationListener- Parameters:
connection- The database connection
-
unwrap
Returns the listener a newly-installed wrapper should delegate to, so re-wrapping a tester shared across tests does not nest one wrapper layer per test: an existingConnectionPreservingOperationListener's own delegate, the tester's existing listener as-is, or a freshDefaultOperationListenerwhen it had none."Had none" is read from a
nullIDatabaseTester.getOperationListener(), the interface default.AbstractDatabaseTesterlazily creates aDefaultOperationListeneron first use, so anullthere genuinely means "none yet" and the fresh one substituted here matches. A customIDatabaseTesterthat manages anIOperationListenerinternally without exposing it throughgetOperationListener()will have that internal listener bypassed - such a tester must overridegetOperationListener()for it to be preserved.- Parameters:
existingListener- The tester's current listener, possiblynull.- Returns:
- The listener a new wrapper should delegate to.
-
unwrapsToNoOp
Returns whetherlistener, after unwrapping anyConnectionPreservingOperationListenerlayers, isIOperationListener.NO_OP_OPERATION_LISTENER- the established, pre-existing signal that a tester's connection is managed elsewhere and must not be closed by whatever is driving the tester's lifecycle.- Parameters:
listener- The listener to inspect, possibly a wrapper ornull.- Returns:
truewhen it unwraps to the no-op listener.
-