package oss.ngocminh.lego.persistence;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.SQLException;
import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.XmlDataSet;
import oss.ngocminh.lego.db.DataSource;
import oss.ngocminh.lego.db.SingleConnectionDataSource;
public abstract class AbstractDAOTest extends DBTestCase {
public AbstractDAOTest() {
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "org.hsqldb.jdbcDriver" );
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:hsqldb:file:test/hsqldb/hsqldb" );
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "sa" );
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "" );
// System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "" )
}
@Override
protected IDataSet getDataSet() throws Exception {
return new XmlDataSet(new FileReader("test/data/" + getDataPath()));
}
protected DataSource getDataSource() {
try {
Connection connection = getConnection().getConnection();
return new SingleConnectionDataSource(connection);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Relative to <code>test/data</code>
* @return
*/
protected abstract String getDataPath();
protected Connection getSqlConnection() throws SQLException {
return getDataSource().getConnection();
}
}