Giter Club home page Giter Club logo

inmemdb-maven-plugin's Introduction

In-Memory Database Maven Plugin

Build Status

The In-Memory Database Maven Plugin is a Maven plugin that can launch and shutdown an embedded in-memory SQL database within the Maven build life-cycle. During the launch the database can be seeded using DDL/DML scripts and/or DBUnit data sets.

The following database implementations are supported:

  • derby - An embedded/in-memory Apache Derby database.

  • h2 - An embedded/in-memory H2 database.

  • hsqldb - An embedded/in-memory HSQLDB database.

The following source file formats are supported:

  • .sql - Contains DDL/DML SQL commands to create the database structure and/or insert test data

  • .csv - A file containing a comma separated value (CSV) data set. The first row of the data set contains the column names and the file name corresponds to the table name.

  • .xml - A file containing a flat DBUnit XML data set.

  • .dbunit.xml - A file containing a DBUnit XML data set.

  • .xls - A Microsoft Excel spread sheet containing one or more work sheets. The name of the work sheet corresponds to the table name and the first row of each work sheet contains the column names.

Example

The In-Memory Database Maven Plugin can be used to automate integration tests having a dependency on an external database server.

pom.xml

The POM here is from taken from the webapp integration test for the In-Memory Database Maven Plugin.

<?xml version="1.0" encoding="UTF-8"?>
<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>webapp</groupId>
    <artifactId>webapp</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
                <artifactId>inmemdb-maven-plugin</artifactId>
                <version>1.4.3</version>
                <configuration>
                    <monitorKey>inmemdb</monitorKey>
                    <monitorPort>11527</monitorPort>
                    <skip>${maven.test.skip}</skip>
                    <attributes>
                        <territory>ga_IE</territory>
                    </attributes>
                </configuration>
                <executions>
                    <execution>
                        <id>run</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <daemon>true</daemon>
                            <type>derby</type>
                            <database>test</database>
                            <username>sa</username>
                            <password></password>
                            <sources>
                                <script>
                                    <sourceFile>src/test/resources/create_database.sql</sourceFile>
                                </script>
                                <dataSet>
                                    <sourceFile>src/test/resources/users.dbunit.xml</sourceFile>
                                </dataSet>
                            </sources>
                        </configuration>
                    </execution>

You can add special configuration on your database connection URL with attributes tag in the configuration part.

				...
				<configuration>
                    <monitorKey>inmemdb</monitorKey>
                    <monitorPort>11527</monitorPort>
					<type>h2</type>
					<database>test</database>
					<attributes>
						<PAGE_SIZE>512</PAGE_SIZE>
					</attributes>
				</configuration>
				...

The In-Memory Database Maven Plugin is configured here to launch an in-memory Apache Derby database server as a daemon process during the pre-integration-test phase of the build life cycle. The database is initialised with a database schema and some seed data.

                    <execution>
                        <id>stop</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                        <phase>post-integration-test</phase>
                    </execution>

The In-Memory Database Maven Plugin is configured here to shutdown the in-memory Apache Derby database server during the post-integration-test phase of the build life cycle.

                </executions>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.8.v20121106</version>
                <configuration>
                    <stopKey>jetty</stopKey>
                    <stopPort>19080</stopPort>
                    <daemon>true</daemon>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                    <jettyXml>src/test/jetty/jetty.xml</jettyXml>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>9080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>

The Jetty Maven Plugin is configured here to launch the Jetty servlet container as a daemon process during the pre-integration-test phase of the build life cycle.

                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>

The Jetty Maven Plugin is configured here to shutdown the Jetty servlet container during the post-integration-test phase of the build life cycle.

                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.derby</groupId>
                        <artifactId>derbyclient</artifactId>
                        <version>10.9.1.0</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-dbcp</groupId>
                        <artifactId>commons-dbcp</artifactId>
                        <version>1.3</version>
                    </dependency>
                </dependencies>
            </plugin>

The org.apache.derby:derbyclient and commons-dbcp:commons-dbcp dependencies are required in order to define the data source that connects to the in-memory Apache Derby database.

            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.12.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The Maven Failsafe Plugin is used to execute the integration tests during the integration-test phase of the build life cycle.

        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.btmatthews.selenium.junit4</groupId>
            <artifactId>selenium-junit4-runner</artifactId>
            <version>1.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

The com.btmatthews.selenium.junit4:selenium-junit-runner and junit:junit dependencies are required by the integration tests.

jetty.xml

Add the following fragment of XML to the jetty.xml to define a data source for the test database.

<New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>
        <Ref id="Server" />
    </Arg>
    <Arg>jdbc/test</Arg>
    <Arg>
        <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="username">sa</Set>
            <Set name="password"></Set>
            <Set name="url">jdbc:derby://localhost/memory:test</Set>
            <Set name="driverClassName">org.apache.derby.jdbc.ClientDriver</Set>
        </New>
    </Arg>
</New>

web.xml

Add the following to the web.xml to make the data source defined in jetty.xml inside the web application:

<resource-ref>
    <res-ref-name>jdbc/test</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Maven Central Coordinates

The In-Memory Database Maven Plugin has been published in Maven Central at the following coordinates:

<plugin>
    <groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
    <artifactId>inmemdb-maven-plugin</artifactId>
    <version>1.4.3</version>
</plugin>

Credits

This project contains contributions from:

License & Source Code

The In-Memory Database Maven Plugin is made available under the Apache License and the source code is hosted on GitHub at https://github.com/bmatthews68/inmemdb-maven-plugin.

inmemdb-maven-plugin's People

Contributors

ngirot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

inmemdb-maven-plugin's Issues

No message when port is already in use

When using the plugin to launch an hsqldb instance on a port that is already in use (in this case also hsqldb by a different user) the plugin just continues as if nothing is wrong, but the db is not configured correctly.
It would be nice to show this in the output and maybe even break the maven run with an error.

Maven 3.0.5 required?

Is there a specific reason why maven 3.0.5 is required? Debian ships with 3.0.4 which renders inmemdb-maven-plugin unusable for now.

Support for Maven 3.1

inmemdb-maven-plugin 1.4.2 is not compatible with Maven 3.1:

[ERROR] Failed to execute goal com.btmatthews.maven.plugins.inmemdb:inmemdb-maven-plugin:1.4.2:run (run) on project privacy-impl: The plugin com.btmatthews.maven.plugins.inmemdb:inmemdb-maven-plugin:1.4.2 requires Maven version 3.0.5 -> [Help 1]

For Derby (other databases too?), provide ability to specify output directory for derby.log file

Derby creates a semi-annoying derby.log file that remains in one's working directory after running the plugin. Ideally, it would be nice if it we had the option of dumping it under target/ so it would get erased with each mvn clean. Right now, one can accomplish that by adding the rather cumbersome properties-maven-plugin to the pom like so:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<property>
<name>derby.system.home</name>
<value>${basedir}/target/derby-system</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>

(derby.log gets output whereever the derby.system.home is defined.) But if the inmemdb plugin had a parameter like ${basedir}/target/derby-system (or or or whatever) that could avoid the need for the above properties plugin that would be great.

Derbynet looks like it changed

Hi,

Here's the stack when I try to shut down derbydb with version 10.10.2:

J102 : [0] org/apache/derby/impl/drda/NetworkServerControlImpl$3
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.derby.iapi.jdbc.DRDAServerStarter.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/apache/derby/impl/drda/NetworkServerControlImpl$3
at org.apache.derby.impl.drda.NetworkServerControlImpl.blockingStart(Unknown Source)
... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.derby.impl.drda.NetworkServerControlImpl$3
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:259)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:235)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:227)
... 7 more

Does it work with Datasource from outside

Hello,

I have used your plugin to populate a hsql database but I experiencing some problems,
I'm using openjpa with annotation class.
I have two datasources and when i run IT my tests uses the persistence file that use the Test datasource.
when running IT I got an exception :

Unable to retrieve EntityManager

Inserting data on h2 database via script does not work.

Inserting data via script on h2 db does not work.

I have the following maven plugin configuration where i try to clean the database, create the tables, insert some data. When Inserting the data using a sql script it does not work when the db is H2. It does work when the db is hsqldb. Using a dbunit datascriptb on a h2 database does work.

com.btmatthews.maven.plugins.inmemdb inmemdb-maven-plugin 1.4.3 11527 inmemdb h2 testDB <script> ${project.basedir}/src/test/resources/sql/cleanup.sql </script> <script> ${project.basedir}/src/test/resources/sql/schema.sql </script>
                    <dataSet>
            <sourceFile>${project.basedir}/src/test/resources/sql/dataSet.xml</sourceFile>
        </dataSet>
        <!--<script>-->,
                <!--<sourceFile>${project.basedir}/src/test/resources/sql/test-data.sql</sourceFile>-->
                        <!--</script>-->
                    </sources>
                </configuration>
            </plugin>

deamon mode does not execute sql-sources

If <daemon>true</daemon> the scripts declared within <sources> will not be executed.

This is impractical for the use case when you want to start and initialize the database in pre-integration-test

centos 6.5 java.net.BindException: Cannot assign requested address

Hello,

I am trying to use this plugin on centos 6.5 but I get a java.net.BindException: Cannot assign requested address.

[INFO] --- inmemdb-maven-plugin:1.3.0:run (run) @ roller-webapp ---
[INFO] Configured database port: 4224
[INFO] Configured database username: APP
[INFO] Configured database password: APP
[INFO] Configured database name: rollerdb
[INFO]
[INFO] --- maven-surefire-plugin:2.16:test (default-test) @ roller-webapp ---
[ERROR] Error starting or stopping the monitor
java.net.BindException: Cannot assign requested address
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
at java.net.ServerSocket.bind(ServerSocket.java:376)
at java.net.ServerSocket.(ServerSocket.java:237)
at com.btmatthews.utils.monitor.Monitor.runMonitor(Monitor.java:82)
at com.btmatthews.utils.monitor.Monitor$1.run(Monitor.java:108)
at java.lang.Thread.run(Thread.java:722)

I have checked port 4224 and its not in use.

The exception does not give enough information on which port it cannot assign, and I have looked at the source also.

Adding the element made no difference.

Have you any ideas on what port it wants?

        <plugin>
            <groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
            <artifactId>inmemdb-maven-plugin</artifactId>
            <configuration>
                <monitorKey>inmemdb</monitorKey>
                <monitorPort>11527</monitorPort>
            </configuration>
            <executions>
                <execution>
                    <id>run</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <daemon>true</daemon>
                        <type>derby</type>
                        <database>rollerdb</database>
                        <username>APP</username>
                        <password>APP</password>
                        <port>4224</port>
                        <sources>
                            <script>
                                <sourceFile>${pom.basedir}/target/dbscripts/derby/createdb.sql</sourceFile>
                            </script>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>stop</id>
                    <phase>package</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Cheers Greg

Introduce dynamic dependency resolution

Eliminate the need to download all implementations and their transitive dependencies and instead use dynamic resolution to just download the implementation for the target database.

Add support for loading from DBUnit flat XML

Modify DBUnitXMLLoader to determine if the schema is matches the verbose XML format.
Create DBUnitFlatXMLLoader to load the flat XML format files which will have the same .dbunit.xml as the verbose XML format.
Add DBIUnitFlatXMLLoader to the loaders.

Hsqldb is mandatory even if we're using derby

Even if using derby, a noclassdeffound appears at shutdown:
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/hsqldb/DatabaseURL
at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at com.btmatthews.maven.plugins.inmemdb.db.derby.DerbyDatabase.stop(DerbyDatabase.java:207)
at com.btmatthews.utils.monitor.Monitor.executeCommand(Monitor.java:265)
at com.btmatthews.utils.monitor.Monitor.runMonitorInternal(Monitor.java:180)
at com.btmatthews.utils.monitor.Monitor.runMonitor(Monitor.java:128)
at com.btmatthews.utils.monitor.Monitor$1.run(Monitor.java:152)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.hsqldb.DatabaseURL
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:259)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:235)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:227)
... 10 more

Error: is an unsupported file type

I want to start the in memory database and then run the script to create the schema. Database gets started correctly, but when it try to run the script to create the schema, getting this error. As per the documentation, .sql is a supported file type.

[INFO] Started embedded HSQLDB database
[INFO] Server has been started
[INFO] Executing initialization scripts and loading data sets
[INFO] Loading Script[src/test/resources/TestAppHyperSql.sql]
[ERROR] 'src/test/resources/TestAppHyperSql.sql' is an unsupported file type.

Thanks,
nadeem.

Provide an option for the derby.log file to be generated

For the vast majority of test generations (including all successful ones) the derby.log file is useless and it's nice that the plugin is not cluttering work folders by adding that file in. However, while debugging failed test cases (particularly with JPA/Hibernate) it can sometimes be helpful when one activates SQL statement logging: http://www.jroller.com/gmazza/entry/apache_derby_setup#logging to see the SQL statements that the JPA classes generated to try to figure out what went wrong. Derby presently prints this in the derby.log file.

I would recommend a <showLogFiles/> option, which I would default to FALSE so you don't even need to list it in your plugin if you're not debugging JPA or JDBC or whatever. However, if you need to see the SQL statements, you can add that setting in and configure your derby.properties file as described in the link above to see the SQL output if you wish. It is not necessary for the plugin to configure the location of the derby.log file, as that can be set by defining derby.system.home within the now-required derby.properties file (and it defaults to the directory in which you ran Maven if it is not set.)

This is not a crucial matter presently, as worst-case, one can downgrade to 1.2.0 of the plugin for this debugging functionality if ever needed. I either never or almost never have a need it myself, but I think it's a good-to-have.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.