23
Feb
11

H2 and DbUnit

Recently I have been working with H2 and wanted to populate the database with DbUnit. This old blog post was somewhat helpful, but uses some now deprecated methods in DbUnit so here is an updated example:

Connection jdbcConnection = DriverManager.getConnection("jdbc:h2:tcp://localhost/db;MVCC=TRUE"
    , "*", "*");
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
connection.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory());
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
IDataSet loadedDataSet =  builder.build(EventListGeneratorTest.class.getClassLoader().getResourceAsStream("TestData.xml"));
DatabaseOperation.CLEAN_INSERT.execute(connection, loadedDataSet);

Also note the addition of the H2DataTypeFactory instance. For those who are wondering, this factory only adds compatibility with boolean values. The FEATURE_QUALIFIED_TABLE_NAMES parameter allows for the formatting in the data XML.

About these ads


Jason’s Tweets


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: