Search in sources :

Example 6 with IDataSet

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

the class DatabaseTestUtils method cleanAndInsertDataSet.

/**
     * Execute a DbUnit CLEAN_INSERT. Parameter xmlString must be formatted as a DBUnit
     * xml dataset. This method can be safely invoked inside a Spring-managed transaction.
     * @param xmlString
     * @param dataSource
     * @throws IOException
     * @throws DataSetException
     * @throws SQLException
     * @throws DatabaseUnitException
     */
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public //Rationale: You cannot define new local variables in the try block because the finally block must reference it.
void cleanAndInsertDataSet(String xmlString, DriverManagerDataSource dataSource) throws IOException, DataSetException, SQLException, DatabaseUnitException {
    StringReader dataSetXmlStream = new StringReader(xmlString);
    IDataSet dataSet = new FlatXmlDataSet(dataSetXmlStream);
    cleanAndInsertDataSet(dataSource, dataSet);
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) StringReader(java.io.StringReader) IDataSet(org.dbunit.dataset.IDataSet)

Example 7 with IDataSet

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

the class DbUnitDataImportExport method loadDataFromFile.

private void loadDataFromFile(String fileName) throws DatabaseUnitException, SQLException, IOException, ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
    Connection jdbcConnection = null;
    boolean enableColumnSensing = true;
    IDataSet dataSet = new FlatXmlDataSet(new File(fileName), false, enableColumnSensing);
    try {
        jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/" + databaseName + "?sessionVariables=FOREIGN_KEY_CHECKS=0", user, password);
        IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
        databaseConnection.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE);
        DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataSet);
    } finally {
        if (jdbcConnection != null) {
            jdbcConnection.close();
        }
    }
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet) File(java.io.File)

Example 8 with IDataSet

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

the class DbUnitDataImportExport method dumpData.

public void dumpData(String fileName) throws ClassNotFoundException, SQLException, DatabaseUnitException, FileNotFoundException, IOException {
    // database connection
    Class.forName("com.mysql.jdbc.Driver");
    Connection jdbcConnection = null;
    IDataSet fullDataSet;
    try {
        jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/" + databaseName, user, password);
        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
        connection.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE);
        // sequenced data should not be necessary when foreign key constraints
        // are turned off on the connection...
        fullDataSet = connection.createDataSet();
        FlatXmlDataSet.write(fullDataSet, new FileOutputStream(fileName));
    } finally {
        if (jdbcConnection != null) {
            jdbcConnection.close();
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet)

Example 9 with IDataSet

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

the class CollectionSheetEntryCustomerAccountTest method verifyCollectionSheetData.

@SuppressWarnings("PMD.SignatureDeclareThrowsException")
private // one of the dependent methods throws Exception
void verifyCollectionSheetData(String filename) throws Exception {
    IDataSet expectedDataSet = dbUnitUtilities.getDataSetFromDataSetDirectoryFile(filename);
    IDataSet databaseDataSet = dbUnitUtilities.getDataSetForTables(dataSource, new String[] { FEE_TRXN_DETAIL, ACCOUNT_TRXN, LOAN_TRXN_DETAIL, ACCOUNT_PAYMENT, LOAN_SUMMARY, LOAN_SCHEDULE, LOAN_ACTIVITY_DETAILS, ACCOUNT_STATUS_CHANGE_HISTORY, FINANCIAL_TRXN, CUSTOMER_ACCOUNT_ACTIVITY, CUSTOMER_TRXN_DETAIL });
    verifyTransactionsAfterSortingTables(expectedDataSet, databaseDataSet);
}
Also used : IDataSet(org.dbunit.dataset.IDataSet)

Example 10 with IDataSet

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

Aggregations

IDataSet (org.dbunit.dataset.IDataSet)54 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)21 DatabaseConnection (org.dbunit.database.DatabaseConnection)16 Test (org.junit.Test)15 ITable (org.dbunit.dataset.ITable)13 Connection (java.sql.Connection)12 FileOutputStream (java.io.FileOutputStream)11 File (java.io.File)10 FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)8 XmlDataSet (org.dbunit.dataset.xml.XmlDataSet)8 InputStream (java.io.InputStream)7 InputStreamReader (java.io.InputStreamReader)7 FlatXmlDataSetBuilder (org.dbunit.dataset.xml.FlatXmlDataSetBuilder)7 OutputStreamWriter (java.io.OutputStreamWriter)5 Writer (java.io.Writer)5 IDatabaseTester (org.dbunit.IDatabaseTester)5 FileInputStream (java.io.FileInputStream)4 FlatDtdWriter (org.dbunit.dataset.xml.FlatDtdWriter)4 BaseCwsCmsInMemoryPersistenceTest (gov.ca.cwds.data.legacy.cms.persistence.BaseCwsCmsInMemoryPersistenceTest)3 DatabaseEnvironment (io.shardingjdbc.core.common.env.DatabaseEnvironment)3