Search in sources :

Example 1 with Difference

use of org.dbunit.assertion.Difference in project sonarqube by SonarSource.

the class AbstractDbTester method assertDbUnit.

public void assertDbUnit(Class testClass, String filename, String[] excludedColumnNames, String... tables) {
    IDatabaseConnection connection = null;
    try {
        connection = dbUnitConnection();
        IDataSet dataSet = connection.createDataSet();
        String path = "/" + testClass.getName().replace('.', '/') + "/" + filename;
        InputStream inputStream = testClass.getResourceAsStream(path);
        if (inputStream == null) {
            throw new IllegalStateException(String.format("File '%s' does not exist", path));
        }
        IDataSet expectedDataSet = dbUnitDataSet(inputStream);
        for (String table : tables) {
            DiffCollectingFailureHandler diffHandler = new DiffCollectingFailureHandler();
            ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(dataSet.getTable(table), excludedColumnNames);
            ITable filteredExpectedTable = DefaultColumnFilter.excludedColumnsTable(expectedDataSet.getTable(table), excludedColumnNames);
            Assertion.assertEquals(filteredExpectedTable, filteredTable, diffHandler);
            // Evaluate the differences and ignore some column values
            List diffList = diffHandler.getDiffList();
            for (Object o : diffList) {
                Difference diff = (Difference) o;
                if (!"[ignore]".equals(diff.getExpectedValue())) {
                    throw new DatabaseUnitException(diff.toString());
                }
            }
        }
    } catch (DatabaseUnitException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (Exception e) {
        throw translateException("Error while checking results", e);
    } finally {
        closeQuietly(connection);
    }
}
Also used : DiffCollectingFailureHandler(org.dbunit.assertion.DiffCollectingFailureHandler) InputStream(java.io.InputStream) DatabaseUnitException(org.dbunit.DatabaseUnitException) ITable(org.dbunit.dataset.ITable) Lists.asList(com.google.common.collect.Lists.asList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Difference(org.dbunit.assertion.Difference) IDataSet(org.dbunit.dataset.IDataSet) SQLException(java.sql.SQLException) DatabaseUnitException(org.dbunit.DatabaseUnitException)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Lists.asList (com.google.common.collect.Lists.asList)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 InputStream (java.io.InputStream)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DatabaseUnitException (org.dbunit.DatabaseUnitException)1 DiffCollectingFailureHandler (org.dbunit.assertion.DiffCollectingFailureHandler)1 Difference (org.dbunit.assertion.Difference)1 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)1 IDataSet (org.dbunit.dataset.IDataSet)1 ITable (org.dbunit.dataset.ITable)1