Perform some JDBC as part of test listener hooks

I am trying to hook into Test task’s lifecycle hooks (#beforeSuite, #beforeTest, #afterTest, #afterSuite) to perform some various JDBC work. But I am having a tough time figuring this one out. They all say that they “[add] a closure to be notified …”. How exactly is the closure notified? In my closure I need to open a JDBC Connection using the classpath and systemProperties of the test task. I initially tried to do with with Ant’s SQL task:

	beforeSuite {
		Test matrixTask = tasks.getByPath( 'matrix_derby' ) as Test

		// create the database
		project.ant {
					driver: matrixTask.systemProperties.get( 'hibernate.connection.driver_class' ),
					url: 'jdbc:derby:hibernate_orm_test;create=true',
					userid: matrixTask.systemProperties.get( 'hibernate.connection.username' ),
					password: matrixTask.systemProperties.get( 'hibernate.connection.password' ),
					classpath: matrixTask.classpath.asFileTree.asPath
			) {

but for the life of me, I could not figure out how to tell AntBuilder the SQL. So I thought I would implement a similar class myself.

This is a custom test task that I configure from a mini-DSL. I won’t bore you with all the details :slight_smile: But I do have a custom Convention object that acts as the delegate for applying the mini-DSL script. I just do not know what to do.

I think ideally, I’d like to see:

beforeSuite {

connect( url: ‘jdbc:derby:hibernate_orm_test;create=true’ )

beforeSuite {

connect( url: ‘jdbc:derby:hibernate_orm_test;create=true’, user: ‘blah’ )

beforeSuite { ... connect( url: 'jdbc:derby:hibernate_orm_test;create=true' ) { statement( "DROP schema ..." ) } }


Any ideas how I could achieve this?

Not that I have a solution, but this problem applies to any Ant task that normally takes some of its input from the element body, as opposed to attributes. I don’t how this translates to Gradle.

I can’t extract from that what the particular problem is.

He can’t figure out how to specify the SQL string, hopefully with minimal escaping, and also specify the additional connection parameters. The interface to specify the parameters is straightforward, but there’s no obvious way to specify the SQL string. When used from Ant, the SQL string is the body of the element.

I guess what he’s failed to do in this description is describe what happens when he’s tried various things. They didn’t work, obviously, but I don’t know exactly what error messages he’s been getting.

When using AntBuilder with a task that takes a literal parameter you just pass it as a method argument. Since the ‘sql’ task has configuration attributes you just combine that with named arguments.

ant.sql(driver: '', userid: 'user', password: 'pass', 'SELECT 1 FROM DUAL')

Mark, that was the answer for Ant tasks. Thanks! All that being said… the Ant SQL task is a poor solution for the problem I am ultimately trying to address.

Luke, etal. The background is that I am reworking a plugin I use in the Hibernate build[1] which allows us to run our tests against different databases. We need that easily extendable and without changes to the build itself. The solution I came up with borrows from the idea of Maven profiles, but adds some Gradle power to it. In the initial iteration that “power” was to simply define a specific DSL to allow the “profile” to add extra driver/test dependencies.

But I still had an itch to scratch… We often need (or would be highly beneficial) to allow each profile to hook in to the various lifecycle phases of the test executions. E.g., here for Derby and H2 and other embedded databases it would be great to create the database at the start of all tests and to destroy it after all tests are done (it makes the tests faster). So I started work on expanding the specific DSL to allow access to the test specifically to allow defining these before/after hooks.

The downfall of the Ant SQL task is that it requires a sql statement. Derby for example just needs me to open a connection with the create=true or shutdown=true parameters for creating/destroying the db. The shutdown is especially difficult since I really cannot connect with shutdown=true and perform some SQL.

Sorry my initial post was unclear. I combined 2 things discussions, which made it even more confusing. Mark answered the Ant SQL task question. However, knowing that long term that solution will not work, I am just looking for some help in designing a proper solution, because at the moment I am completely lost as to where to start.


So are you interested in defining new lifecycle events to TestListener, or are you unsure about how to get contextual information to use inside the existing events?

It’s not exactly clear to me what didn’t work in your first example, forcing you to use the Ant SQL task.