0

I'm having a problem doing some testing using JUnit 4, HSQLDB_2.2.9 and DBUnit_2.4.8. I'm consuming the following input file (input.xml):

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
    <record_type record_type="Logical" />
    <record_type record_type="Conceptual" />
</dataset>

I'm consuming this file using the getDataSet method as follows:

@Override
protected IDataSet getDataSet() throws Exception
{
    System.out.println("Getting data set...");
    loadedDataSet = new  FlatXmlDataSetBuilder();
    FlatXmlDataSet dataSet = loadedDataSet.build(new File("test\\test\\files\\input.xml"));
    System.out.println("Dataset is "+dataSet.getTable("record_type").getValue(0, "record_type"));
    return dataSet;
}

Now my SetUp method does the following:

protected void setUp() throws Exception
{    
    System.out.println("Setting up environment...");
    getSetUpOperation();
    tearDown();
    createTables();


}

The createTables method is as follows:

private void createTables() throws SQLException, Exception 
{
    System.out.println("Creating database tables");
    String createRecordTypeTableSQL = "CREATE TABLE IF NOT EXISTS record_type "+
    "(record_type VARCHAR(30) NOT NULL,"+
    "PRIMARY KEY (record_type))";

    PreparedStatement createErrorTypeTablePS = getConnection().getConnection().prepareStatement(createRecordTypeTableSQL);              
    createRecordTypeTablePS.executeUpdate();

}

My get connection is as follows:

@Override
protected IDatabaseConnection getConnection() throws Exception 
{
    Connection jdbcConnection = null;

    Class.forName("org.hsqldb.jdbc.JDBCDriver");

    String url = "jdbc:hsqldb:sample";
    Properties props = new Properties();
    props.setProperty("user","sa");
    props.setProperty("password","");
    jdbcConnection = DriverManager.getConnection(url, props);

    return new DatabaseConnection(jdbcConnection);
}

As for my test case, it's as follows:

@Test
public void testSelect() throws Exception

{

String selectSQL = "SELECT * from record_type";
PreparedStatement selectPS = 

getConnection().getConnection().prepareStatement(selectSQL);

ResultSet resultRS = selectPS.executeQuery();

String recordType=null;

if(resultRS.next())
{
    recordType = resultRS.getString("record_type");
    System.out.println("Found record type: "+recordType);
}

 assertEquals("Result   ","Logical",recordType);


}

So what I'm expecting is the value "Logical", but the result I'm getting is "null". To validate if my getDataSet method is consuming the input.xml file correctly, I added the line:

    System.out.println("Dataset is "+dataSet.getTable("record_type").getValue(0, "record_type"));

Which is returning the record_type "Logical" just as expected. So I think that my problem is that the returned IDataSet that's returned by the getDataSet method is not binding the retrieved data to my created database table.

Note: I tried to insert a record_type manually to the database, and it was successfully inserted and retrieved by my test method, so there's no problem with the database connection as well.

Any ideas?

UPDATE: The only thing that fixed my problem was adding the following to my getDataSet() method:

DatabaseOperation.CLEAN_INSERT.execute(getConnection(), dataSet);

I'm sure that I was able to do this without this added line of code. Is there anything wrong that I'm doing?

Mouhammed Soueidane
  • 1,056
  • 2
  • 24
  • 42
  • There may be an issue of case-sensitivity of the table name and column names. `CREATE TABLE IF NOT EXISTS record_type` results in `RECORD_TYPE` as table name. You can use `CREATE TABLE IF NOT EXISTS "record_type"` etc. – fredt Dec 27 '12 at 11:39
  • I already checked this but it doesn't seem to be the problem since when I try to retrieve data from my dataset after consuming my input file, I'm able to do that without any problems. I appreciate the help though. – Mouhammed Soueidane Dec 27 '12 at 14:18

0 Answers0