DBUnit, HSQL and the BOOLEAN data type

Michael Wynholds ·

We have been using HSQL in-memory along with DBUnit for unit testing lately, and I found an issue using the most recent version of each. Basically, HSQL has added a new data type, BOOLEAN, which replaces BIT. But DBUnit is not updated to support this, and an error is throw when you attempt to insert some BOOLEAN data using DBUnit.

The error looks like this:

WARNING - TABLE.COLUMN data type (16, 'BOOLEAN') not recognized
and will be ignored. See FAQ for more information.

The solution outlined here is straightforward. You need to create a new data type factory that extends the DBUnit class DefaultDataTypeFactory. This new class just handles the SQL type Types.BOOLEAN as a special case. The code follows:

HsqlDataTypeFactory.java

package company.project.test;

import org.apache.commons.logging.*;
import org.dbunit.dataset.datatype.*;

import java.sql.*;

public class HsqlDataTypeFactory
  extends DefaultDataTypeFactory
{
  private static final Log log = LogFactory.getLog(HsqlDataTypeFactory.class);

  public DataType createDataType(int sqlType, String sqlTypeName)
    throws DataTypeException
  {
    if (sqlType == Types.BOOLEAN)
    {
      return DataType.BOOLEAN;
    }

    return super.createDataType(sqlType, sqlTypeName);
  }
}

Then, in order to use this data type factory, just set a property on the IDatabaseConnection DBUnit object in your code (here is an example method):

  protected IDatabaseConnection getConnection() throws Exception
  {
    IDatabaseConnection connection =
      new DatabaseConnection(dataSource.getConnection());
    DatabaseConfig config = connection.getConfig();
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                       new HsqlDataTypeFactory());

    return connection;
  }
Michael Wynholds
Michael Wynholds

Mike is the President and CEO of Carbon Five.

What can we help you with?

Tell us a bit about your project, or just shoot us an email.

Interested in a Career at Carbon Five? Check out our job openings.