Search in sources :

Example 1 with FlatXmlDataSet

use of org.dbunit.dataset.xml.FlatXmlDataSet in project head by mifos.

the class AcceptanceDatabaseTestUtils method deleteDataFromTable.

public void deleteDataFromTable(String tableName, DriverManagerDataSource dataSource) throws IOException, DataSetException, SQLException, DatabaseUnitException {
    StringReader dataSetXmlStream = new StringReader("<dataset><" + tableName + "/></dataset>");
    IDataSet dataSet = new FlatXmlDataSet(dataSetXmlStream);
    IDatabaseConnection databaseConnection = new DatabaseDataSourceConnection(dataSource);
    databaseConnection.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE);
    DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataSet);
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) DatabaseDataSourceConnection(org.dbunit.database.DatabaseDataSourceConnection) StringReader(java.io.StringReader) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet)

Example 2 with FlatXmlDataSet

use of org.dbunit.dataset.xml.FlatXmlDataSet 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 3 with FlatXmlDataSet

use of org.dbunit.dataset.xml.FlatXmlDataSet 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 4 with FlatXmlDataSet

use of org.dbunit.dataset.xml.FlatXmlDataSet 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 5 with FlatXmlDataSet

use of org.dbunit.dataset.xml.FlatXmlDataSet in project sonarqube by SonarSource.

the class AbstractDbTester method dbUnitDataSet.

private IDataSet dbUnitDataSet(InputStream stream) {
    try {
        ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
        dataSet.addReplacementObject("[null]", null);
        dataSet.addReplacementObject("[false]", Boolean.FALSE);
        dataSet.addReplacementObject("[true]", Boolean.TRUE);
        return dataSet;
    } catch (Exception e) {
        throw translateException("Could not read the dataset stream", e);
    }
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) ReplacementDataSet(org.dbunit.dataset.ReplacementDataSet) SQLException(java.sql.SQLException) DatabaseUnitException(org.dbunit.DatabaseUnitException)

Aggregations

FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)7 IDataSet (org.dbunit.dataset.IDataSet)5 StringReader (java.io.StringReader)3 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)3 Connection (java.sql.Connection)2 DatabaseConnection (org.dbunit.database.DatabaseConnection)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 SQLException (java.sql.SQLException)1 DataSourceDatabaseTester (org.dbunit.DataSourceDatabaseTester)1 DatabaseUnitException (org.dbunit.DatabaseUnitException)1 IDatabaseTester (org.dbunit.IDatabaseTester)1 DatabaseDataSourceConnection (org.dbunit.database.DatabaseDataSourceConnection)1 ITable (org.dbunit.dataset.ITable)1 ReplacementDataSet (org.dbunit.dataset.ReplacementDataSet)1 FlatXmlDataSetBuilder (org.dbunit.dataset.xml.FlatXmlDataSetBuilder)1