samedi janv. 16, 2010

Testing with the GlassFish Maven plugin and JavaDB Embedded

This blog has moved to alexismp.wordpress.com
Follow the link for the most up-to-date version of this blog entry.

Having GlassFish v3 usable in embedded mode made it easy to create a maven plugin with multiple goals (see this previous entry). This in turn makes it easy to configure the plugin for maven-driven unit testing. Here's an example :

<plugin>
    <groupId>org.glassfish</groupId>
    <artifactId>maven-embedded-glassfish-plugin</artifactId>
    <version>3.0</version>
    <configuration>
       <goalPrefix>embedded-glassfish</goalPrefix>
       <app>${basedir}/target/myapp.ear</app>
       <autoDelete>true</autoDelete>
       <port>8080</port>

    </configuration>

    <executions>
       <execution>
          <id>start-glassfish</id>
          <phase>pre-integration-test</phase>
          <goals>
             <goal>start</goal>
          </goals>
       </execution>
       <execution>
          <id>glassfish-deploy</id>
          <phase>pre-integration-test</phase>
          <goals>
             <goal>deploy</goal>
          </goals>
       </execution>
       <execution>
          <id>glassfish-undeploy</id>
          <phase>post-integration-test</phase>
          <goals>
             <goal>undeploy</goal>
          </goals>
       </execution>
       <execution>
          <id>stop-glassfish</id>
          <phase>post-integration-test</phase>
          <goals>
             <goal>stop</goal>
          </goals>
       </execution>
    </executions>
</plugin>

Now it's certainly nice to be able to automate tests right from a single pom.xml and not have to deal with starting, deploying, stopping the app server. It's even better when the runtime starts fast and uses only the required memory (as it is the case with GlassFish v3), but what about running tests involving a database? If a database server needs to be started using external tools (or worse, manually) it's a bit back to square "one".

This is where JavaDB embedded can come in handy. The trick with an application server like GlassFish is that it's not enough to use the embedded driver (org.apache.derby.jdbc.EmbeddedDriver), you also need to reference an embedded JTA datasource. GlassFish v3 ships with one such datasource pre-configured: jdbc/__TimerPool so lazy as I am, I simply reused this in by setup - using the following JPA persistence-unit enables my test to fire up an embedded (in-process) instance of JavaDB (no separate startup, no port used). As the names implies, this pool isn't really a general purpose one (it's used for by the Timer Service) so this is a bit of a hack. A handy one, but a hack.

<persistence-unit name="tempdb">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/__TimerPool</jta-data-source>
    <properties>
       <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
       <property name="eclipselink.target-database" value="DERBY"/>
       <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
</persistence-unit>

You see here that I use the drop-and-create-tables ddl generation strategy (create-tables would work too) and consider the database to be volatile. The next step would be to run all of this in-memory and no longer write files to disk (which shouldn't be that hard).

About

This blog has moved

Alexis Moussine-Pouchkine's Weblog

GlassFish - Stay Connected

Search

Archives
« avril 2014
lun.mar.mer.jeu.ven.sam.dim.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today
Blogroll

No bookmarks in folder

News

No bookmarks in folder