Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wipe data from my HSQLDB after every test?

I had some JUnit tests already written in my project which used to populate data in the setup method. Now I have added maven to my project and I want to execute all test cases form maven i.e. using mvn test. The problem now is that my data base is not cleared after every test class has run. I need to clear the HSQLDB after test cases of each class have run.

like image 280
Robin Avatar asked Feb 14 '11 09:02

Robin


People also ask

What does H in Hsqldb stand for?

HSQLDB (HyperSQL DataBase) is the leading SQL relational database system written in Java. It offers a small, fast multithreaded and transactional database engine with in-memory and disk-based tables and supports embedded and server modes. It includes a powerful command line SQL tool and simple GUI query tools.

What is the full form of Hsqldb?

HSQLDB (Hyper SQL Database) is a relational database management system written in Java. It has a JDBC driver and supports a large subset of SQL-92, SQL:2008, SQL:2011, and SQL:2016 standards.

What is Hsqldb in-memory database?

HSQLDB (HyperSQL Database) HSQLDB is an open source project, also written in Java, representing a relational database. It follows the SQL and JDBC standards and supports SQL features such as stored procedures and triggers. It can be used in the in-memory mode, or it can be configured to use disk storage.


1 Answers

  1. You can clear the data by dropping the schema. The default schema is called PUBLIC. If you execute the SQL satement below, it will clear all data and drop all tables.

    DROP SCHEMA PUBLIC CASCADE

  2. Alternatively, if you need the table and schema object definitions, you can create a file: database containing the objects but no data, and add the property below to the .properties file. Using this type of database for tests, the changes to data are not persisted

    files_read_only=true

  3. The latest alternative, available in HSQLDB 2.2.6 and later allows you to clear all the data in a schema while keeping the tables. In the example below, the PUBLIC schema is cleared.

    TRUNCATE SCHEMA public AND COMMIT

    This statement has been enhanced in the latest versions of HSQLDB. See http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_truncate_statement under Truncate Statement

like image 82
fredt Avatar answered Sep 19 '22 01:09

fredt