Search in sources :

Example 1 with IDatabaseConnection

use of org.dbunit.database.IDatabaseConnection in project sonarqube by SonarSource.

the class AbstractDbTester method dbUnitConnection.

private IDatabaseConnection dbUnitConnection() {
    try {
        IDatabaseConnection connection = db.getDbUnitTester().getConnection();
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, db.getDbUnitFactory());
        return connection;
    } catch (Exception e) {
        throw translateException("Error while getting connection", e);
    }
}
Also used : IDatabaseConnection(org.dbunit.database.IDatabaseConnection) SQLException(java.sql.SQLException) DatabaseUnitException(org.dbunit.DatabaseUnitException)

Example 2 with IDatabaseConnection

use of org.dbunit.database.IDatabaseConnection 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 3 with IDatabaseConnection

use of org.dbunit.database.IDatabaseConnection in project sonarqube by SonarSource.

the class AbstractDbTester method prepareDbUnit.

private void prepareDbUnit(InputStream... dataSetStream) {
    IDatabaseConnection connection = null;
    try {
        IDataSet[] dataSets = new IDataSet[dataSetStream.length];
        for (int i = 0; i < dataSetStream.length; i++) {
            dataSets[i] = dbUnitDataSet(dataSetStream[i]);
        }
        db.getDbUnitTester().setDataSet(new CompositeDataSet(dataSets));
        connection = dbUnitConnection();
        new InsertIdentityOperation(DatabaseOperation.INSERT).execute(connection, db.getDbUnitTester().getDataSet());
    } catch (Exception e) {
        throw translateException("Could not setup DBUnit data", e);
    } finally {
        closeQuietly(connection);
    }
}
Also used : CompositeDataSet(org.dbunit.dataset.CompositeDataSet) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet) InsertIdentityOperation(org.dbunit.ext.mssql.InsertIdentityOperation) SQLException(java.sql.SQLException) DatabaseUnitException(org.dbunit.DatabaseUnitException)

Example 4 with IDatabaseConnection

use of org.dbunit.database.IDatabaseConnection in project head by mifos.

the class DbUnitUtilities method dumpDatabase.

public void dumpDatabase(String fileName, Connection jdbcConnection) throws SQLException, DatabaseUnitException, FileNotFoundException, IOException {
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
    IDataSet fullDataSet = connection.createDataSet();
    FlatXmlDataSet.write(fullDataSet, new FileOutputStream(fileName));
}
Also used : FileOutputStream(java.io.FileOutputStream) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet)

Example 5 with IDatabaseConnection

use of org.dbunit.database.IDatabaseConnection in project head by mifos.

the class DbUnitUtilities method loadDataFromFile.

public void loadDataFromFile(String filename, DriverManagerDataSource dataSource) throws DatabaseUnitException, SQLException, IOException {
    Connection jdbcConnection = null;
    IDataSet dataSet = getDataSetFromDataSetDirectoryFile(filename);
    try {
        jdbcConnection = DataSourceUtils.getConnection(dataSource);
        IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
        DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataSet);
    } finally {
        if (jdbcConnection != null) {
            jdbcConnection.close();
        }
        DataSourceUtils.releaseConnection(jdbcConnection, dataSource);
    }
}
Also used : 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)

Aggregations

IDatabaseConnection (org.dbunit.database.IDatabaseConnection)17 IDataSet (org.dbunit.dataset.IDataSet)11 DatabaseConnection (org.dbunit.database.DatabaseConnection)9 Connection (java.sql.Connection)8 SQLException (java.sql.SQLException)4 DatabaseUnitException (org.dbunit.DatabaseUnitException)4 ITable (org.dbunit.dataset.ITable)3 FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)3 FileOutputStream (java.io.FileOutputStream)2 StringReader (java.io.StringReader)2 DataSourceDatabaseTester (org.dbunit.DataSourceDatabaseTester)2 IDatabaseTester (org.dbunit.IDatabaseTester)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 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1