views:

1432

answers:

3

I have a project of persistence with spring and hibernate built with maven, I'm running the testing using Junit and a test database HSQLDB, when I do a test first initialize the database HSQLDB in server mode, is there some way to make hudson initializes the database, or with maven ?

A: 

We do that with Maven under Hudson, with a profile that triggers the maven-antrun-plugin in the process-test-resources phase. In our case the maven-antrun-plugin runs a Java class that generates the schema, but of course there are other options. It looks like this:

    <profile>
        <id>dev</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-database-schema-new</id>
                            <phase>process-test-resources</phase>
                            <configuration>
                                <tasks>
                                    ...
                                </tasks>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
FelixM
This strategy also is valid for me
atomsfat
+2  A: 

I'd use DbUnit and the DbUnit Maven Plugin for that. You can use it to Clear database and insert a dataset before test phase and/or to setup data for each test cases (see the Getting Started for JUnit 3, see this blog post for example for JUnit 4).

Another option would be to use the SQL Maven Plugin. The examples section has a configuration that shows how to drop/create a database and schema, then populate it before the test phase, and drop the database after the test phase).

The later approach gives you less control on data setup between tests which is why I prefer DbUnit.

Pascal Thivent
I start with SQL Maven Plugin and it works thanks, But next time I will use DbUnit
atomsfat
A: 

I add the folowing to pom.

<build>
        <extensions>
            <extension>
                <groupId>hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <version>1.8.0.7</version>
            </extension>
            <extension>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.14</version>
            </extension>
        </extensions>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.0</version>

                <configuration>
                    <components>
                        <component>
                            <name>hbm2java</name>
                            <implementation>annotationconfiguration</implementation>
                            <outputDirectory>/src/main/java</outputDirectory>
                        </component>

                    </components>
                    <componentProperties>
                        <jdk5>true</jdk5>
                        <export>false</export>
                        <drop>true</drop>
                        <outputfilename>schema.sql</outputfilename>
                    </componentProperties>
                </configuration>

                <executions>
                    <execution>
                        <id>generate-ddl</id>
                        <phase>process-classes</phase>
                        <goals>
                            <!--Genera Esquema-->
                            <goal>hbm2ddl</goal>
                            <!--Genera Clases -->
                        <!-- <goal>hbm2java</goal>  -->

                        </goals>
                    </execution>
                </executions>
            </plugin>



            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>create-schema</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>target/hibernate3/sql/schema.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>


                    <execution>
                        <id>drop-db-after-test</id>
                        <phase>test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <autocommit>true</autocommit>
                                <sqlCommand>DROP SCHEMA public CASCADE</sqlCommand>
                            </configuration>
                        </execution>
                    </executions>

                    <dependencies>
                        <dependency>
                            <groupId>hsqldb</groupId>
                            <artifactId>hsqldb</artifactId>
                            <version>1.8.0.7</version>
                        </dependency>
                    </dependencies>

                    <configuration>
                        <driver>org.hsqldb.jdbcDriver</driver>
                        <username>sa</username>
                        <password></password>
                        <url>jdbc:hsqldb:file:etc/out/test.db;shutdown=true</url>
                        <autocommit>true</autocommit>
                        <skip>${maven.test.skip}</skip>
                    </configuration>
                </plugin>

            </plugins>
        </build>

and the folowing to my datasource:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true"
 destroy-method="close">
 <property name="driverClassName" value="org.hsqldb.jdbcDriver" />

 <property name="url" value="jdbc:hsqldb:file:etc/out/test.db;shutdown=true" />


    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>
atomsfat