Search in sources :

Example 1 with DatabaseUnitException

use of org.dbunit.DatabaseUnitException in project sonarqube by SonarSource.

the class AbstractDbTester method assertDbUnitTable.

public void assertDbUnitTable(Class testClass, String filename, String table, String... columns) {
    IDatabaseConnection connection = dbUnitConnection();
    try {
        IDataSet dataSet = connection.createDataSet();
        String path = "/" + testClass.getName().replace('.', '/') + "/" + filename;
        IDataSet expectedDataSet = dbUnitDataSet(testClass.getResourceAsStream(path));
        ITable filteredTable = DefaultColumnFilter.includedColumnsTable(dataSet.getTable(table), columns);
        ITable filteredExpectedTable = DefaultColumnFilter.includedColumnsTable(expectedDataSet.getTable(table), columns);
        Assertion.assertEquals(filteredExpectedTable, filteredTable);
    } catch (DatabaseUnitException e) {
        fail(e.getMessage());
    } catch (SQLException e) {
        throw translateException("Error while checking results", e);
    } finally {
        closeQuietly(connection);
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseUnitException(org.dbunit.DatabaseUnitException) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet)

Example 2 with DatabaseUnitException

use of org.dbunit.DatabaseUnitException in project head by mifos.

the class DbUnitUtilities method verifySortedTableWithOrdering.

public void verifySortedTableWithOrdering(String tableName, IDataSet databaseDataSet, IDataSet expectedDataSet, String[] sortingColumns, Boolean actualDBComparableFlag, Boolean expectedDBComparableFlag) throws DataSetException, DatabaseUnitException {
    Assert.assertNotNull("Didn't find requested table [" + tableName + "] in columnsToIgnoreWhenVerifyingTables map.", columnsToIgnoreWhenVerifyingTables.get(tableName));
    ITable expectedTable = getCaseInsensitiveTable(tableName, expectedDataSet);
    ITable actualTable = getCaseInsensitiveTable(tableName, databaseDataSet);
    actualTable = DefaultColumnFilter.includedColumnsTable(actualTable, expectedTable.getTableMetaData().getColumns());
    SortedTable sortedExpectedTable = new SortedTable(expectedTable, sortingColumns);
    sortedExpectedTable.setUseComparable(expectedDBComparableFlag);
    expectedTable = sortedExpectedTable;
    SortedTable sortedActualTable = new SortedTable(actualTable, sortingColumns);
    sortedActualTable.setUseComparable(actualDBComparableFlag);
    actualTable = sortedActualTable;
    if (LOG.isDebugEnabled()) {
        printTable(expectedTable);
        printTable(actualTable);
    }
    try {
        Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, columnsToIgnoreWhenVerifyingTables.get(tableName));
    } catch (AssertionError e) {
        throw new DatabaseUnitException(getTableDiff(expectedTable, actualTable), e);
    }
}
Also used : SortedTable(org.dbunit.dataset.SortedTable) DatabaseUnitException(org.dbunit.DatabaseUnitException) ITable(org.dbunit.dataset.ITable)

Example 3 with DatabaseUnitException

use of org.dbunit.DatabaseUnitException 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)

Example 4 with DatabaseUnitException

use of org.dbunit.DatabaseUnitException in project head by mifos.

the class DbUnitUtilities method verifyTable.

/**
     * Compare two tables using DbUnit DataSets
     * @param tableName
     * @param databaseDataSet
     * @param expectedDataSet
     * @throws DataSetException
     * @throws DatabaseUnitException
     */
public void verifyTable(String tableName, IDataSet databaseDataSet, IDataSet expectedDataSet) throws DataSetException, DatabaseUnitException {
    Assert.assertNotNull("Didn't find requested table [" + tableName + "] in columnsToIgnoreWhenVerifyingTables map.", columnsToIgnoreWhenVerifyingTables.get(tableName));
    ITable expectedTable = getCaseInsensitiveTable(tableName, expectedDataSet);
    ITable actualTable = getCaseInsensitiveTable(tableName, databaseDataSet);
    actualTable = DefaultColumnFilter.includedColumnsTable(actualTable, expectedTable.getTableMetaData().getColumns());
    try {
        Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, columnsToIgnoreWhenVerifyingTables.get(tableName));
    } catch (AssertionError e) {
        throw new DatabaseUnitException(getTableDiff(expectedTable, actualTable), e);
    }
}
Also used : DatabaseUnitException(org.dbunit.DatabaseUnitException) ITable(org.dbunit.dataset.ITable)

Aggregations

DatabaseUnitException (org.dbunit.DatabaseUnitException)4 ITable (org.dbunit.dataset.ITable)4 SQLException (java.sql.SQLException)2 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)2 IDataSet (org.dbunit.dataset.IDataSet)2 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 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DiffCollectingFailureHandler (org.dbunit.assertion.DiffCollectingFailureHandler)1 Difference (org.dbunit.assertion.Difference)1 SortedTable (org.dbunit.dataset.SortedTable)1