Java源码示例:org.dbunit.dataset.filter.DefaultColumnFilter

示例1
@Test
public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced()
    throws Exception {
    String[] excludedColumns = { "id", "produced" };
    try (InputStream is = getClass().getClassLoader()
        .getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) {
        // given
        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
        ITable expectedTable = DefaultColumnFilter
            .excludedColumnsTable(expectedDataSet.getTable("ITEMS"), excludedColumns);

        // when
        connection.createStatement()
            .executeUpdate("INSERT INTO ITEMS (title, price, produced)  VALUES('Necklace', 199.99, now())");

        // then
        IDataSet databaseDataSet = tester.getConnection().createDataSet();
        ITable actualTable = DefaultColumnFilter
            .excludedColumnsTable(databaseDataSet.getTable("ITEMS"), excludedColumns);
        Assertion.assertEquals(expectedTable, actualTable);
    }
}
 
示例2
@Test
public void givenDataSet_whenProductIgnoredAndDelete_thenItemIsRemoved() throws Exception {
    try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) {
        // given
        ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");

        // when
        connection.createStatement().executeUpdate("delete from ITEMS where id = 2");

        // then
        IDataSet databaseDataSet = tester.getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");
        actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
        assertEquals(expectedTable, actualTable);
    }
}
 
示例3
@Test
public void givenDataSet_whenUpdateWithNoProduced_thenItemHasNewName() throws Exception {
    try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) {
        // given
        ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
        expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[] { "produced" });

        // when
        connection.createStatement().executeUpdate("update ITEMS set title="new name" where id = 1");

        // then
        IDataSet databaseDataSet = tester.getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");
        actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
        assertEquals(expectedTable, actualTable);
    }
}
 
示例4
/**
 * @param expectedDataSet
 * @param databaseDataSet
 * @param tablesNamesToCheck
 * @throws DataSetException
 * @throws DatabaseUnitException
 */
private void checkDatabase(final IDataSet expectedDataSet, final IDataSet databaseDataSet,
                           final List<String> tablesNamesToCheck)
        throws DataSetException, DatabaseUnitException {
    final DefaultColumnFilter columnFilter = createColumnFilter();
    for (final String tableName : tablesNamesToCheck) {
        final ITable actualTable = databaseDataSet.getTable(tableName);
        FilteredTableMetaData metaData = new FilteredTableMetaData(actualTable.getTableMetaData(), columnFilter);
        ITable actualTableFiltered = new CompositeTable(metaData, actualTable);
        final ITable expectedTable = expectedDataSet.getTable(tableName);
        Assertion.assertEquals(expectedTable, actualTableFiltered);
    }
}
 
示例5
/**
 * @return a column filter
 */
private DefaultColumnFilter createColumnFilter() {
    String[] excludedColumnNameArray = new String[] { "Datetime" };
    final DefaultColumnFilter columnFilter = new DefaultColumnFilter();
    for (String columnName : excludedColumnNameArray) {
        columnFilter.excludeColumn(columnName);
    }
    return columnFilter;
}
 
示例6
protected void assertTableEquals(Class<?> peristentClass, String[] includedFields, IDataSet dataset) throws Exception {
    // Fetch database data after executing your code
    IDataSet databaseDataSet = getConnection().createDataSet();
    ITable actualTable = databaseDataSet.getTable(getTableName(peristentClass));
    ITable actualData =
        DefaultColumnFilter.includedColumnsTable(actualTable, getColumnNames(peristentClass, includedFields));

    // Load expected data from an XML dataset
    ITable expectedTable = dataset.getTable(getTableName(peristentClass));
    ITable expectedData =
        DefaultColumnFilter.includedColumnsTable(expectedTable, getColumnNames(peristentClass, includedFields));

    // Assert actual database table match expected table
    Assertion.assertEquals(expectedData, actualData);
}
 
示例7
private ITable filter(final ITable table, final String[] columnsToFilter) throws DataSetException {
    final ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(table, columnsToFilter);
    return applyCustomFilters(filteredTable);
}