Search in sources :

Example 1 with ITable

use of org.dbunit.dataset.ITable 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 ITable

use of org.dbunit.dataset.ITable 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 ITable

use of org.dbunit.dataset.ITable in project head by mifos.

the class DatabaseTestUtils method verifyTable.

/**
     * Verify that a database table matches a dataSet table. dataSetXml must be formatted as a DBUnit
     * xml dataset. This method can be safely invoked inside a Spring-managed transaction.
     * @param dataSetXml
     * @param tableName
     * @param dataSource
     * @throws Exception
     */
// one of the dependent methods throws Exception
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void verifyTable(String dataSetXml, String tableName, DriverManagerDataSource dataSource) throws Exception {
    Connection jdbcConnection = null;
    StringReader dataSetXmlStream = new StringReader(dataSetXml);
    try {
        jdbcConnection = DataSourceUtils.getConnection(dataSource);
        IDatabaseTester databaseTester = new DataSourceDatabaseTester(dataSource);
        IDatabaseConnection databaseConnection = databaseTester.getConnection();
        databaseConnection.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE);
        IDataSet databaseDataSet = databaseConnection.createDataSet();
        ITable actualTable = databaseDataSet.getTable(tableName);
        IDataSet expectedDataSet = new FlatXmlDataSet(dataSetXmlStream);
        ITable expectedTable = expectedDataSet.getTable(tableName);
        Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, new String[] { "id" });
    } finally {
        if (null != jdbcConnection) {
            jdbcConnection.close();
        }
        DataSourceUtils.releaseConnection(jdbcConnection, dataSource);
    }
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) StringReader(java.io.StringReader) IDatabaseTester(org.dbunit.IDatabaseTester) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet) DataSourceDatabaseTester(org.dbunit.DataSourceDatabaseTester)

Example 4 with ITable

use of org.dbunit.dataset.ITable in project SSM by Intel-bigdata.

the class TestDBAdapter method testCreateProportionView.

@Test
public void testCreateProportionView() throws Exception {
    Statement statement = databaseTester.getConnection().getConnection().createStatement();
    statement.execute(AccessCountTable.createTableSQL("table1"));
    statement.execute(AccessCountTable.createTableSQL("table2"));
    statement.execute(AccessCountTable.createTableSQL("table3"));
    statement.execute(AccessCountTable.createTableSQL("expect"));
    IDataSet dataSet = new XmlDataSet(getClass().getClassLoader().getResourceAsStream("accessCountTable.xml"));
    databaseTester.setDataSet(dataSet);
    databaseTester.onSetup();
    AccessCountTable table3 = new AccessCountTable("table3", 0L, 10L, TimeGranularity.SECOND);
    DBAdapter dbAdapter = new DBAdapter(databaseTester.getConnection().getConnection());
    // 50%
    AccessCountTable viewTable = new AccessCountTable(0L, 5L);
    dbAdapter.createProportionView(viewTable, table3);
    ITable actual = databaseTester.getConnection().createTable(viewTable.getTableName());
    ITable expect = databaseTester.getConnection().createTable(table3.getTableName());
    SortedTable sortedActual = new SortedTable(actual, new String[] { "fid" });
    sortedActual.setUseComparable(true);
    Assert.assertTrue(sortedActual.getRowCount() == expect.getRowCount());
    for (int i = 0; i < expect.getRowCount(); i++) {
        Integer actualAC = (Integer) sortedActual.getValue(i, AccessCountTable.ACCESSCOUNT_FIELD);
        Integer expectAC = (Integer) expect.getValue(i, AccessCountTable.ACCESSCOUNT_FIELD);
        Assert.assertTrue(actualAC == expectAC / 2);
    }
}
Also used : DBAdapter(org.smartdata.server.metastore.DBAdapter) Statement(java.sql.Statement) SortedTable(org.dbunit.dataset.SortedTable) ITable(org.dbunit.dataset.ITable) IDataSet(org.dbunit.dataset.IDataSet) AccessCountTable(org.smartdata.server.metastore.tables.AccessCountTable) XmlDataSet(org.dbunit.dataset.xml.XmlDataSet) Test(org.junit.Test)

Example 5 with ITable

use of org.dbunit.dataset.ITable in project SSM by Intel-bigdata.

the class TestAccessCountTableManager method testAddAccessCountInfo.

@Test
public void testAddAccessCountInfo() throws Exception {
    createTables(databaseTester.getConnection());
    IDataSet dataSet = new XmlDataSet(getClass().getClassLoader().getResourceAsStream("files.xml"));
    databaseTester.setDataSet(dataSet);
    databaseTester.onSetup();
    DBAdapter adapter = new DBAdapter(databaseTester.getConnection().getConnection());
    AccessCountTableManager manager = new AccessCountTableManager(adapter);
    List<FileAccessEvent> accessEvents = new ArrayList<>();
    accessEvents.add(new FileAccessEvent("file1", 0));
    accessEvents.add(new FileAccessEvent("file2", 1));
    accessEvents.add(new FileAccessEvent("file2", 2));
    accessEvents.add(new FileAccessEvent("file3", 2));
    accessEvents.add(new FileAccessEvent("file3", 3));
    accessEvents.add(new FileAccessEvent("file3", 4));
    accessEvents.add(new FileAccessEvent("file3", 5000));
    manager.onAccessEventsArrived(accessEvents);
    AccessCountTable accessCountTable = new AccessCountTable(0L, 5000L);
    ITable actual = databaseTester.getConnection().createTable(accessCountTable.getTableName());
    ITable expect = databaseTester.getDataSet().getTable("expect1");
    SortedTable sortedActual = new SortedTable(actual, new String[] { "fid" });
    sortedActual.setUseComparable(true);
    Assertion.assertEquals(expect, sortedActual);
}
Also used : DBAdapter(org.smartdata.server.metastore.DBAdapter) SortedTable(org.dbunit.dataset.SortedTable) ArrayList(java.util.ArrayList) ITable(org.dbunit.dataset.ITable) IDataSet(org.dbunit.dataset.IDataSet) FileAccessEvent(org.smartdata.metrics.FileAccessEvent) XmlDataSet(org.dbunit.dataset.xml.XmlDataSet) Test(org.junit.Test) DBTest(org.smartdata.server.metastore.DBTest)

Aggregations

ITable (org.dbunit.dataset.ITable)10 IDataSet (org.dbunit.dataset.IDataSet)8 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)5 DatabaseUnitException (org.dbunit.DatabaseUnitException)4 Connection (java.sql.Connection)3 SortedTable (org.dbunit.dataset.SortedTable)3 XmlDataSet (org.dbunit.dataset.xml.XmlDataSet)3 Test (org.junit.Test)3 InputStreamReader (java.io.InputStreamReader)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 FlatXmlDataSetBuilder (org.dbunit.dataset.xml.FlatXmlDataSetBuilder)2 H2Connection (org.dbunit.ext.h2.H2Connection)2 MySqlConnection (org.dbunit.ext.mysql.MySqlConnection)2 DBAdapter (org.smartdata.server.metastore.DBAdapter)2 DBTest (org.smartdata.server.metastore.DBTest)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