Question

First let me explain what I am doing:

Creating a Java project in Eclipse (JUNO) to unit test SQL for a Postgres database.

Using Maven2 as the build tool. I have DBUnit, and SQL maven plugins.

The goal is to drop the schema and rebuild the tables and load some data in the tables.

I've tested the SQL so I know it works. I have tested the connections so I know the URL is right.

Now onto my problem. I am new to Maven for unit testing SQL. I have tried to follow most of the online documentation. I have created my pom from the examples. While the Java code compiles, the build blows by the SQL work in the pom.xml file. Here is my pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.premierinc.esd</groupId>
<artifactId>sqlunittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sqlunittest</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>3.8.1</junit.version>
    <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
    <sql.maven.plugin.version>1.5</sql.maven.plugin.version>
    <postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
</properties>

<repositories>
    <repository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <updatePolicy>never</updatePolicy>
        </releases>
    </pluginRepository>
</pluginRepositories>


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.jdbc.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>${sql.maven.plugin.version}</version>

                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>


                <executions>
                    <execution>
                        <id>drop-schema-before-test-if-any</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <!-- need another database to drop the targeted one -->
                            <url>jdbc:postgresql://localhost:5432:postgres</url>
                            <autocommit>true</autocommit>
                            <sqlCommand>DROP SCHEMA chipen CASCADE</sqlCommand>
                            <!-- ignore error when database is not available -->
                            <onError>continue</onError>
                        </configuration>
                    </execution>


                    <execution>
                        <id>create-schema</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>src/main/sql/CHI-PEN-schema.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>dbunit-maven-plugin</artifactId>
                <version>1.0-beta-3</version>


                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>

                    <execution>

                        <phase>test-compile</phase>
                        <goals>
                            <goal>operation</goal>
                        </goals>
                        <!-- specific configurations -->
                        <configuration>
                            <type>CLEAN_INSERT</type>
                            <src>src/test/data/testdb_chipen_data.xml</src>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
        </plugins>
    </pluginManagement>
</build>

Any suggestions or hints would be greatly appreciated.

Thanks in advance.

Was it helpful?

Solution

I add the pom of your post here in corrected form. I have removed in particular the repository definition, cause they are the defaults in Maven so convention over configurations mean to define only what is really needed to define. Furthermore i have removed the pluginManagement tags, cause this means not to really execute the things it means define things. To be more accurate pluginManagement is intended to define the versions of plugins but usually not the configuration. This is usually used in parent pom's:

<project ...>
  ..
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
           Plugin groupId, artifactId, version
        </plugin>
        .
      </plugin>
    </pluginManagement>
    ..
</project>

Let us get back to your pom. The follow should run:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.premierinc.esd</groupId>
<artifactId>sqlunittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sqlunittest</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>3.8.1</junit.version>
    <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
    <sql.maven.plugin.version>1.5</sql.maven.plugin.version>
    <postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.jdbc.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>${sql.maven.plugin.version}</version>

                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>
                    <execution>
                        <id>drop-schema-before-test-if-any</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <!-- need another database to drop the targeted one -->
                            <url>jdbc:postgresql://localhost:5432:postgres</url>
                            <autocommit>true</autocommit>
                            <sqlCommand>DROP SCHEMA chipen CASCADE</sqlCommand>
                            <!-- ignore error when database is not available -->
                            <onError>continue</onError>
                        </configuration>
                    </execution>


                    <execution>
                        <id>create-schema</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>src/main/sql/CHI-PEN-schema.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>dbunit-maven-plugin</artifactId>
                <version>1.0-beta-3</version>


                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>

                    <execution>

                        <phase>test-compile</phase>
                        <goals>
                            <goal>operation</goal>
                        </goals>
                        <!-- specific configurations -->
                        <configuration>
                            <type>CLEAN_INSERT</type>
                            <src>src/test/data/testdb_chipen_data.xml</src>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
        </plugins>
</build>

Apart from the above a thing like this are integration tests and not unit tests but this is a different question/discussion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top