Error setting database config property for IDatabaseConnection (HSQLDB)

3.2k views Asked by At

I've included fully testable code below, which generates the following error when supplied with a dataset xml containing empty fields. A sample dataset.xml is also below.

java.lang.IllegalArgumentException: table.column=places.CITY value is empty but must contain a value (to disable this feature check, set DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS to true)

The thread here is similar but is different since it uses multiple dbTester.getConnection() whereas my code only uses one, yet has the same error. The main problem relates to this line databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE); .
It seems to be ignored entirely. I've tried putting the init code inside the @Test method but the error remains.

dataset.xml

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <places address="123 Up Street" city="Chicago" id="001"/>
  <places address="456 Down Street" city="" id="002"/>
  <places address="789 Right Street" city="Boston" id="003"/>
</dataset>

Code:

import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBConnectionIT {
    IDatabaseTester databaseTester = null;
    IDatabaseConnection iConn = null;
    Connection connection = null;

    @Before
    public void init() throws Exception {
        databaseTester = new JdbcDatabaseTester(org.hsqldb.jdbcDriver.class.getName(), "jdbc:hsqldb:mem:testdb;sql.syntax_pgs=true", "sa", "");
        iConn = databaseTester.getConnection();
        DatabaseConfig databaseConfig = iConn.getConfig();
        databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE);
        connection = iConn.getConnection();
        createTable(connection);
        IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File("dataset.xml"));
        databaseTester.setDataSet(dataSet);
        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
        databaseTester.onSetup();
    }

    @Test
    public void testDBUnit() {
        try {
            PreparedStatement pst = connection.prepareStatement("select * from places");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void createTable(Connection conn) throws Exception {
        PreparedStatement pp = conn.prepareStatement(
                "CREATE TABLE PLACES" +
                        "(address VARCHAR(255), " +
                        "city TEXT, " +
                        "id VARCHAR(255) NOT NULL primary key)");
        pp.executeUpdate();
        pp.close();
    }
}

EDIT (based on César Rodríguez's answer):

I've now refactored out this method in the parent class:

 protected void setUpDatabaseConfig(DatabaseConfig databaseConfig) {
        databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE);
    }

and created a sub-class which @Overrides this method, but it's saying this sub-class is not being used. How do I address this class (DBConnectionOverride) in the parent class, to solve my problem?

class DBConnectionOverride extends DBConnectionIT {
    @Override
    protected void setUpDatabaseConfig(DatabaseConfig databaseConfig) {
        databaseConfig.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, true);
    }
}
3

There are 3 answers

2
Nova On BEST ANSWER

I've stumbled upon the correct answer, at least the one which solves my problem. It related to this line all along databaseTester.onSetup() which could simply be replaced with DatabaseOperation.CLEAN_INSERT.execute(iConn, dataSet);. Feel free comment on why this seemed to have fixed the error.

4
César Rodríguez On

You must override method setUpDatabaseConfig(DatabaseConfig config) as follows:

@Override
protected void setUpDatabaseConfig(DatabaseConfig config) {
    config.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, true); 
}

Hope it helps

2
JavaDev1987 On

for me it's work:

        IDatabaseConnection dbConn = new DatabaseDataSourceConnection(getDataSource());
        dbConn.getConfig().setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, true);
        DatabaseOperation.CLEAN_INSERT.execute(dbConn, getiDataSet(loadDBData.source()));