Driving Test Data with groovy + hibernate
The appfuse guys have a pretty cool setup where they have the hibernate plugin and the dbunit plugin working together to reload your dev database with each build. While this rocks I would rather populate data using my existing hibernate model. Using gmaven I tied together a script to initialize everything.
In my pom I have the following two plugsin configured:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>annotationconfiguration</implementation>
<!--
Use 'jpaconfiguration' if you're using JPA.
<implementation>jpaconfiguration</implementation>
-->
</component>
</components>
<componentProperties>
<drop>true</drop>
<jdk5>true</jdk5>
<propertyfile>target/classes/jdbc.properties</propertyfile>
<skip>${maven.test.skip}</skip>
</componentProperties>
</configuration>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>${jdbc.groupId}</groupId>
<artifactId>${jdbc.artifactId}</artifactId>
<version>${jdbc.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0-rc-5</version>
<configuration>
<source>${pom.basedir}/src/test/resources/sampledata.groovy</source>
</configuration>
<executions>
<execution>
<id>populate-db</id>
<phase>test-compile</phase>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>${jdbc.groupId}</groupId>
<artifactId>${jdbc.artifactId}</artifactId>
<version>${jdbc.version}</version>
</dependency>
</dependencies>
</plugin>
Then I wrote the following script (that is referenced in the plugin above):
// initializes hibernate
this.sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory()
this.call = 0
class SaveDelegate {
Session session
DateTime now
def save(o) {
session.saveOrUpdate(o)
session.flush()
return o;
}
// Used to save the objects.
def save(o) {
def con = DriverManager.getConnection(project.properties['jdbc.url'], project.properties['jdbc.username'], project.properties['jdbc.password'])
def session = this.sessionFactory.openSession(con)
try {
println call++
if (o instanceof Closure) {
return o.call(new SaveDelegate(session: session, now: this.now))
}
else {
return new SaveDelegate(session: session, now: this.now).save(o)
}
} catch (e) {
System.err.println e.message
return null;
} finally {
session.close()
con.close()
}
}
try {
// The save function above lets me handle objects in different ways:
// just save an instance
save new Person(name: 'Issac Brock')
// save with a reference
def f = save(new Person(name:'Jimmy Paige'))
// and finally with a closure
def f = save { s ->
def f = new Person(name: "Scott Weiland")
... do other stuff
return s.save f
}
} finally {
//cleanup
this.sessionFactory.close()
}
}
Advertisement