Error setting database config property for IDatabaseConnection (HSQLDB)

假如想象 提交于 2019-12-04 04:08:00

问题


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);
    }
}

回答1:


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.




回答2:


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




回答3:


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()));


来源:https://stackoverflow.com/questions/41471468/error-setting-database-config-property-for-idatabaseconnection-hsqldb

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