Reputation: 1955
I've just been using DBUnit for the last 2 days and I got a problem comparing a table with an xml file.
I've pasted all the code and simplified some code so people can try and find out what the problem is.
Actually, the method that does the extraction (from DB2) works well. I've simply added a method that is quite similar to the extract but its function is to compare a table with an XML file. In debug mode, when I get to this line : ITable tableAComparer = dataSet.getTable(table.getSchema().concat(".").concat(table.getTableName()));
,
I get a NoSuchTableException which I don't understand why...
Does anybody have a hint?
Thanks.
public abstract class TestNonRegressionAbstract extends DBTestCase {
private IDatabaseConnection dbConn;
private Config cfg;
private ArrayList<Table> tablesToExtract = new ArrayList<Table>();
private String path = null;
/**
* Methode permettant de cleaner la bd avant le chargement
*
* @see org.dbunit.DatabaseTestCase#setUp()
*/
protected void setUp() throws Exception {
path = new java.io.File("").getAbsolutePath().concat("/junit/");
}
/**
*
* Méthode qui extrait les tables qui doivent être backupées avant les tests
*
* @throws Exception si une exception est lancée
*/
protected void extractTables() throws Exception {
try {
for (Table table : tablesToExtract) {
dbConn = initDBUnitConnection(table.getSchema());
QueryDataSet dataSet = new QueryDataSet(dbConn);
dataSet.addTable(table.getSchema().concat(".").concat(table.getTableName()));
FlatXmlDataSet.write(dataSet, new FileOutputStream(path + table.getNomFichier()));
dbConn.close();
dbConn = null;
}
} finally {
if (dbConn != null)
dbConn.close();
dbConn = null;
}
}
/**
* Méthode qui compare une table de la bd avec un fichier xml contenant les valeurs de la table
* avant l'exécution du test.
*
* @throws Exception l'exception.
*/
protected void compareTables() throws Exception {
this.getTablesToExtract().add(new Table("PRM", "TCALENDRIER", "PRM_TCALENDRIER.XML"));
try {
for (Table table : tablesToExtract) {
DiffCollectingFailureHandler myHandler = new DiffCollectingFailureHandler();
dbConn = initDBUnitConnection(table.getSchema());
dbConn.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
QueryDataSet dataSet = new QueryDataSet(dbConn);
ITable tableAComparer = dataSet.getTable(table.getSchema().concat(".").concat(table.getTableName()));
ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(tableAComparer, new String[]{"MAJPAR", "MAJTIMESTAMP"});
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new File(path + table.getNomFichier()));
Assertion.assertEquals(expectedDataSet.getTable(table.getTableName()), filteredTable, myHandler);
@SuppressWarnings("unchecked")
List<Difference> diffList = myHandler.getDiffList();
for (Difference difference : diffList) {
System.out.println("Différence trouvé :" + difference.getColumnName()
+ " Actuel:" + difference.getActualTable() + " Attendu:" + difference.getExpectedValue());
}
dbConn.close();
dbConn = null;
}
} finally {
if (dbConn != null)
dbConn.close();
dbConn = null;
}
}
/**
* Méthode qui initialise la connection DBUnit
*
* @param cfg
* la configuration avec les param BD
* @return La connection
* @throws Exception
* si erreur
*/
private IDatabaseConnection initDBUnitConnection(String schema) throws Exception {
if (dbConn == null) {
if (cfg == null) {
initConfig();
}
String driver = cfg.getValue("CRP_DB_DRIVER");
String dbName = cfg.getValue("CRP_DB_NAME");
String dbUser = cfg.getValue("CRP_DB_USER");
String dbPswd = cfg.getValue("CRP_DB_PASSWORD");
Class.forName(driver);
Connection connDBDemande = DriverManager.getConnection(dbName, dbUser, dbPswd);
// Init DBUnit connection
//dbConn = new DatabaseConnection(connDBDemande);
dbConn = new DatabaseConnection(connDBDemande, schema);
}
return dbConn;
}
private void initConfig() throws Exception {
if (cfg == null) {
cfg = new Config("com/foo/bar/junit/nonregression/CRPTest.properties");
}
}
/**
* @see org.dbunit.DatabaseTestCase#getDataSet()
*/
@Override
protected IDataSet getDataSet() throws Exception {
IDataSet databaseDataSet = getDbConn().createDataSet();
return databaseDataSet;
}
/**
*
* Méthode qui récupère la table
*
* @param table le nom de la table à récuperer
* @return la table
* @throws Exception si une exception survient
*/
protected ITable getDataFromTable(String table) throws Exception {
ITable actualTable = getDataSet().getTable(table);
return actualTable;
}
/**
*
* Méthode qui retourne la bd via DBUnit
*
* @return la connection à la BD via DBUnit
*/
public IDatabaseConnection getDbConn() {
return this.dbConn;
}
public void setDbConn(IDatabaseConnection aDbConn) {
this.dbConn = aDbConn;
}
public class Table {
public Table(String schema, String tableName, String nomFichier) {
this.schema = schema;
this.tableName = tableName;
this.nomFichier = nomFichier;
}
private String schema;
private String tableName;
private String nomFichier;
public String getSchema() {
return this.schema;
}
public void setSchema(String aSchema) {
this.schema = aSchema;
}
public String getTableName() {
return this.tableName;
}
public void setTableName(String aTableName) {
this.tableName = aTableName;
}
public String getNomFichier() {
return this.nomFichier;
}
public void setNomFichier(String aNomFichier) {
this.nomFichier = aNomFichier;
}
}
public ArrayList<Table> getTablesToExtract() {
return this.tablesToExtract;
}
public void setTablesToExtract(ArrayList<Table> aTablesToExtract) {
this.tablesToExtract = aTablesToExtract;
}
}
Upvotes: 1
Views: 1298
Reputation: 1955
Finally found the answer to my problem:
First, I needed to add the table to the QueryDataSet object (dataSet) this way.
dataSet.addTable(table.getSchema().concat(".").concat(table.getTableName()), table.getSelectStatement());
and by adding the query parameter by adding a SELECT statement.
Upvotes: 1