Creating Temporary Database That Works Across Maven Test Phases

HSQL Unit Test -- How to Create Multiple In-Memory Schemas?

HSQLDB supports multiple schemas in the same database. Foreign keys can reference tables from different schemas. The following apply to the very latest HSQLDB 2.2.6 snapshot available from http://hsqldb.org

  1. Before running your tests, execute CREATE SCHEMA schemaname for each schema.
  2. Doesn't matter where, you can specify the absolute path on the command line arguments when running. See the HSQLD Guide and JavaDoc on server.
  3. Yes.
  4. No. You use the SQL statement to create the schemas.

Note you have two options for running HSQLDB, one is as a server, the other is as an embedded database. In the case of server, it must be started before the test run. In both cases, you need to connect to the database and create the schemas before your tests.

Maven: How to create temporary folders?

You can use ant plugin, you can create a folder during a given phase and then you can remove it during another phase:

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
....
<configuration>
<task>
<mkdir>./your_directory_to create</mkdir>
</task>
</configuration>
</execution>
</executions>
</plugin>

Exporting In-Memory HSQL Database Content as SQL During Maven Build

With HSQLDB use:

SCRIPT <filepath>

Example:

SCRIPT '/opt/dump/mydb.script'

Keep Derby Network Server running after build

As described in my answer here, you can use Derby as your database via the derby-maven-plugin which I wrote and is available on GitHub and via Maven Central.

See here for details on how to use it. This will basically remove the need for you to start Derby through your tests and it will keep it up and running while the tests are executing. In combination with the sql-maven-plugin, you could have a reasonably decent testing environment.

To further clarify, the server does not run after the build has finished. However, under target/derby you can find your database which you can run, if you need to investigate the produced data.

Unit Testing database layer DAO with derby in memory

Okay, further to my comment I have tried this and it works as expected. When running tests the testing specific persistence.xml in src/test/resources/META-INF is picked up as expected.

You can then create a BaseDao class long the lines of the following:

public class BaseDao {

private static EntityManagerFactory emf;

public static EntityManager getEntityManager() {

if (emf == null) {

emf = Persistence.createEntityManagerFactory("test");
}

return emf.createEntityManager();
}
}

}

Which is all a lot nicer!

Getting rid of derby.log

You can get rid of derby.log file by creating the following class

public class DerbyUtil {
public static final OutputStream DEV_NULL = new OutputStream() {
public void write(int b) {}
};
}

and setting the JVM system property derby.stream.error.field, for example, using the following JVM command-line argument:

-Dderby.stream.error.field=DerbyUtil.DEV_NULL

Credit to whom it is due.



Related Topics



Leave a reply



Submit