Search in sources :

Example 36 with IDataSet

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

the class DatabaseTestUtils method getXmlDataSet.

/**
     * given an XML string, returns an data set with [null] replaced by actual null objects.
     * @param xmlString
     * @return IDataSet
     * @throws IOException
     * @throws DataSetException
     */
public IDataSet getXmlDataSet(String xmlString) throws IOException, DataSetException {
    boolean dtdMetadata = false;
    boolean enableColumnSensing = true;
    IDataSet xmlDataSet = new FlatXmlDataSet(createTempFile(xmlString), dtdMetadata, enableColumnSensing);
    return this.getDataSetWithNullsReplaced(xmlDataSet);
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) IDataSet(org.dbunit.dataset.IDataSet)

Example 37 with IDataSet

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

the class SimpleDataSetTest method testGetDataSet.

public void testGetDataSet() throws DataSetException, IOException {
    this.simpleDataSet.row("firstTable", "column1=value1", "column2=[null]");
    IDataSet actualDataSet = this.simpleDataSet.getDataSet();
    Assert.assertNotNull(actualDataSet);
    Assert.assertEquals("value1", actualDataSet.getTable("firstTable").getValue(0, "column1"));
    Assert.assertEquals(null, actualDataSet.getTable("firstTable").getValue(0, "column2"));
}
Also used : IDataSet(org.dbunit.dataset.IDataSet)

Example 38 with IDataSet

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

the class DataSetUpgradeUtil method upgrade.

private void upgrade(String fileName, ApplicationContext applicationContext) throws Exception {
    System.out.println("Upgrading: " + fileName);
    dbUnitUtilities = new DbUnitUtilities();
    IDataSet dataSet = null;
    try {
        dataSet = dbUnitUtilities.getDataSetFromFile(fileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Class.forName("com.mysql.jdbc.Driver");
    Connection jdbcConnection = null;
    try {
        // TODO use same db cxn info that Mifos uses! (local.properties)
        jdbcConnection = DriverManager.getConnection(getUrl(databaseName, port, params), user, password);
        jdbcConnection.setAutoCommit(false);
        resetDatabase(databaseName, jdbcConnection);
        SqlExecutor.execute(new FileInputStream(schemaFileName), jdbcConnection);
        jdbcConnection.commit();
        jdbcConnection.setAutoCommit(true);
        IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
        databaseConnection.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.FALSE);
        DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataSet);
    } finally {
        if (jdbcConnection != null) {
            jdbcConnection.close();
        }
    }
    System.setProperty(TestingService.TEST_MODE_SYSTEM_PROPERTY, "acceptance");
    ApplicationInitializer applicationInitializer = new ApplicationInitializer();
    applicationInitializer.dbUpgrade(applicationContext);
    applicationContext.getBean(DatabaseUpgradeSupport.class).contraction();
    applicationInitializer.setAttributesOnContext(null);
    StaticHibernateUtil.closeSession();
    System.out.println(" upgrade done!");
}
Also used : Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) ApplicationInitializer(org.mifos.framework.ApplicationInitializer) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) DatabaseUpgradeSupport(org.mifos.db.upgrade.DatabaseUpgradeSupport) IOException(java.io.IOException) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet) FileInputStream(java.io.FileInputStream)

Example 39 with IDataSet

use of org.dbunit.dataset.IDataSet in project sonarqube by SonarSource.

the class AbstractDbTester method assertDbUnit.

public void assertDbUnit(Class testClass, String filename, String[] excludedColumnNames, String... tables) {
    IDatabaseConnection connection = null;
    try {
        connection = dbUnitConnection();
        IDataSet dataSet = connection.createDataSet();
        String path = "/" + testClass.getName().replace('.', '/') + "/" + filename;
        InputStream inputStream = testClass.getResourceAsStream(path);
        if (inputStream == null) {
            throw new IllegalStateException(String.format("File '%s' does not exist", path));
        }
        IDataSet expectedDataSet = dbUnitDataSet(inputStream);
        for (String table : tables) {
            DiffCollectingFailureHandler diffHandler = new DiffCollectingFailureHandler();
            ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(dataSet.getTable(table), excludedColumnNames);
            ITable filteredExpectedTable = DefaultColumnFilter.excludedColumnsTable(expectedDataSet.getTable(table), excludedColumnNames);
            Assertion.assertEquals(filteredExpectedTable, filteredTable, diffHandler);
            // Evaluate the differences and ignore some column values
            List diffList = diffHandler.getDiffList();
            for (Object o : diffList) {
                Difference diff = (Difference) o;
                if (!"[ignore]".equals(diff.getExpectedValue())) {
                    throw new DatabaseUnitException(diff.toString());
                }
            }
        }
    } catch (DatabaseUnitException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (Exception e) {
        throw translateException("Error while checking results", e);
    } finally {
        closeQuietly(connection);
    }
}
Also used : DiffCollectingFailureHandler(org.dbunit.assertion.DiffCollectingFailureHandler) InputStream(java.io.InputStream) DatabaseUnitException(org.dbunit.DatabaseUnitException) ITable(org.dbunit.dataset.ITable) Lists.asList(com.google.common.collect.Lists.asList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Difference(org.dbunit.assertion.Difference) IDataSet(org.dbunit.dataset.IDataSet) SQLException(java.sql.SQLException) DatabaseUnitException(org.dbunit.DatabaseUnitException)

Example 40 with IDataSet

use of org.dbunit.dataset.IDataSet in project nextprot-api by calipho-sib.

the class GenerateDTD method generateDTD.

@Test
public void generateDTD() throws Exception {
    IDatabaseConnection connection = new DatabaseConnection(dsLocator.getDataSource().getConnection());
    // write DTD file
    IDataSet dataSet = connection.createDataSet();
    Writer out = new OutputStreamWriter(new FileOutputStream(dtdFile));
    FlatDtdWriter datasetWriter = new FlatDtdWriter(out);
    datasetWriter.setContentModel(FlatDtdWriter.CHOICE);
    // You could also use the sequence model which is the default
    // datasetWriter.setContentModel(FlatDtdWriter.SEQUENCE);
    datasetWriter.write(dataSet);
    // delete file after the test
    new File(dtdFile).delete();
}
Also used : FileOutputStream(java.io.FileOutputStream) FlatDtdWriter(org.dbunit.dataset.xml.FlatDtdWriter) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) OutputStreamWriter(java.io.OutputStreamWriter) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet) File(java.io.File) FlatDtdWriter(org.dbunit.dataset.xml.FlatDtdWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Test(org.junit.Test)

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