English 中文(简体)
hudson build a project using junit maven and HSQLDB in server mode
原标题:

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 ?

问题回答

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.

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>

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>




相关问题
Derby gets stuck closing?

I ve got unit tests (in maven) that use derby. At the end of the test run, there is a very long pause, with these log messages before the pause. INFO: Closing Hibernate SessionFactory Nov 16, 2009 8:...

Execute goal on parent after children complete

I have a multi-module maven project (several levels of nesting). Normally, when I execute a maven build (like mvn install or whatever), maven will run all the goals for the parent project before ...

Including dependencies in a jar with Maven

Is there a way to force maven(2.0.9) to include all the dependencies in a single jar file? I have a project the builds into a single jar file. I want the classes from dependencies to be copied into ...

Java Equivalent of distcc

Distcc makes it easy to distribute a C or C++ compile job across a number of machines, and is a godsend for working with large, frequently-built codebases. An increasing number of our large projects ...

Maven Assembly Problem

I have a maven multiple-module project, which is giving me headaches in the assembly:assembly phase. I have a module which has an assembly defined in it which works fine when I invoke mvn assembly:...

热门标签