Inspect in memory hsqldb while debugging

喜夏-厌秋 提交于 2019-11-29 19:06:06

HSQL is in memory, so when you say that you're connecting with SQLDB Database Manager, you're not - you are instead connecting to another database in the memory space of the SQLDB Database Manager, not the one in the memory space of the unit test. This is why the database in the SQLDB Database Manager is empty.

You can run HSQL as a server using org.hsqldb.Server as described here.

Although the org.hsqldb.Server class is typically used to start-up a seperate process, you could instantiate and configure it in your unit test, which should allow a remote process to connect and query the database.

Alternatively, you'll have to write some sort of dump functionality that is called from within your unit test as need be.

As an aside, using HSQL in unit tests is just proving your code works against HSQL, which is different to the actual database. This means you can get false positives and vice versa. The same thing can be achieved with a mocking API or better, save the database testing for some decent integration tests that works with the real database.

In your unit test or in the @Before / setUp() method, you can add the following line to launch the HSQL Database Manager:


org.hsqldb.util.DatabaseManager.main(new String[] {
  "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
});

or for the improved Swing version


org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
  "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
});

The DB manager lets you inspect your schema and run SQL queries on the live in-memory database while the application is running.

Make sure to set a beakpoint or pause the execution one way or another if you want to check the state of your database at a specific line.

Gwaptiva

Run your unit test to a breakpoint, then in the Debug perspective of Eclipse, open the Display view (Window, Show View, Display) and enter

    org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
  "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
});

(as per dimdm's post), highlight it, right-click and choose Execute.

You could also use the DatabaseManagerSwing class included in [HSQLDB][1] passing to it an open connection, that allows you to see the state of the database in the transaction the connection is in.

DatabaseManagerSwing manager = new DatabaseManagerSwing();
manager.main();
manager.connect(connection);
manager.start();

The above accepted answer works perfectly only if the database name, username and password is untouched (testdb, SA, blank password).

For custom database name, username and password you will get the following exception

java.sql.SQLInvalidAuthroizationSpecException: invalid authorization specification - not found: SA

Then, you have to connect manually.

To connect directly use the following snippet

org.hsqldb.util.DatabaseManager.main(new String[] {
  "--url",  "jdbc:hsqldb:mem:yourdbname", "--noexit",
  "--user", "dbusername", "--password", "dbpassword"
});

or for the improved Swing version

org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
  "--url",  "jdbc:hsqldb:mem:yourdbname", "--noexit",
  "--user", "dbusername", "--password", "dbpassword"
});

Before executing the above snippet update the following

  • yourdbname - Update yourdbname with real database name
  • dbusername - Update dbusername with your database username
  • dbpassword - Update dbpassword with your database password

The problem with the database-manager freezing can be resolved by starting the database-manager in a new thread, and sleeping the thread the test runs on.

Add this code snippet to your test and you will be able to inspect the database while debugging. Remember to change the database-url to your database.

Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
                    "--url",  "jdbc:hsqldb:mem://localhost:9001", "--noexit"
            });
        }
    });

t.start();

try {
    Thread.sleep(10000000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!