Search in sources :

Example 6 with DatabaseConnection

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

use of org.dbunit.database.DatabaseConnection 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 DatabaseConnection

use of org.dbunit.database.DatabaseConnection 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 DatabaseConnection

use of org.dbunit.database.DatabaseConnection in project ORCID-Source by ORCID.

the class DBUnitTest method getDBConnection.

public static IDatabaseConnection getDBConnection() throws Exception {
    DriverManagerDataSource dataSource = (DriverManagerDataSource) context.getBean("simpleDataSource");
    Connection jdbcConnection = dataSource.getConnection();
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
    connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new CustomDataTypeFactory());
    return connection;
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) 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)

Example 10 with DatabaseConnection

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

the class DbUnitUtilities method getDataSetForTables.

/**
     * Returns a DbUnit DataSet for several tables.
     * @param driverManagerDataSource TODO
     * @param tableNames
     * @return IDataSet
     * @throws Exception
     */
// one of the dependent methods throws Exception
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public IDataSet getDataSetForTables(DriverManagerDataSource driverManagerDataSource, String[] tableNames) throws Exception {
    Connection jdbcConnection = null;
    IDataSet databaseDataSet = null;
    try {
        jdbcConnection = DataSourceUtils.getConnection(driverManagerDataSource);
        IDatabaseTester databaseTester = new DataSourceDatabaseTester(driverManagerDataSource);
        IDatabaseConnection databaseConnection = databaseTester.getConnection();
        databaseDataSet = databaseConnection.createDataSet(tableNames);
    } finally {
        jdbcConnection.close();
        DataSourceUtils.releaseConnection(jdbcConnection, driverManagerDataSource);
    }
    return databaseDataSet;
}
Also used : Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseTester(org.dbunit.IDatabaseTester) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet) DataSourceDatabaseTester(org.dbunit.DataSourceDatabaseTester)

Aggregations

DatabaseConnection (org.dbunit.database.DatabaseConnection)12 Connection (java.sql.Connection)11 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)10 IDataSet (org.dbunit.dataset.IDataSet)8 FileOutputStream (java.io.FileOutputStream)4 File (java.io.File)3 DataSourceDatabaseTester (org.dbunit.DataSourceDatabaseTester)2 IDatabaseTester (org.dbunit.IDatabaseTester)2 FilteredDataSet (org.dbunit.dataset.FilteredDataSet)2 FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)2 FlatXmlDataSetBuilder (org.dbunit.dataset.xml.FlatXmlDataSetBuilder)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ITable (org.dbunit.dataset.ITable)1 ReplacementDataSet (org.dbunit.dataset.ReplacementDataSet)1 DatabaseUpgradeSupport (org.mifos.db.upgrade.DatabaseUpgradeSupport)1 ApplicationInitializer (org.mifos.framework.ApplicationInitializer)1 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1