Search in sources :

Example 6 with FlatXmlDataSetBuilder

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

the class DTDIgnoredFlatXMLDataSet method createDataSet.

@Override
protected IDataSet createDataSet(Resource resource) throws Exception {
    FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
    builder.setDtdMetadata(false);
    InputStream is = resource.getInputStream();
    try {
        return builder.build(is);
    } finally {
        is.close();
    }
}
Also used : InputStream(java.io.InputStream) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Example 7 with FlatXmlDataSetBuilder

use of org.dbunit.dataset.xml.FlatXmlDataSetBuilder in project sharding-jdbc by shardingjdbc.

the class SQLAssertHelper method executeQueryWithPreparedStatement.

/**
 * Execute query with prepared statement.
 *
 * @param isExecute is execute method
 * @param abstractDataSourceAdapter data source adapter
 * @param parameters parameters
 * @param file file
 *
 * @throws MalformedURLException malformed URL exception
 * @throws SQLException SQL exception
 * @throws DatabaseUnitException database unit exception
 */
public void executeQueryWithPreparedStatement(final boolean isExecute, final AbstractDataSourceAdapter abstractDataSourceAdapter, final List<String> parameters, final File file) throws MalformedURLException, SQLException, DatabaseUnitException {
    try (Connection conn = abstractDataSourceAdapter.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(sql))) {
        setParameters(preparedStatement, parameters);
        ReplacementDataSet expectedDataSet = new ReplacementDataSet(new FlatXmlDataSetBuilder().build(file));
        expectedDataSet.addReplacementObject("[null]", null);
        for (ITable each : expectedDataSet.getTables()) {
            String tableName = each.getTableMetaData().getTableName();
            IDatabaseConnection connection = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn);
            ITable actualTable;
            if (isExecute) {
                actualTable = createTable(tableName, preparedStatement, connection);
            } else {
                actualTable = connection.createTable(tableName, preparedStatement);
            }
            Assertion.assertEquals(expectedDataSet.getTable(tableName), actualTable);
        }
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) ReplacementDataSet(org.dbunit.dataset.ReplacementDataSet) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) PreparedStatement(java.sql.PreparedStatement) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Example 8 with FlatXmlDataSetBuilder

use of org.dbunit.dataset.xml.FlatXmlDataSetBuilder in project sharding-jdbc by shardingjdbc.

the class SQLAssertHelper method assertResult.

public void assertResult(final Connection connection, final File file) throws MalformedURLException, SQLException, DatabaseUnitException {
    if (sql.contains("TEMP")) {
        return;
    }
    ITableIterator expectedTableIterator = new FlatXmlDataSetBuilder().build(file).iterator();
    try (Connection conn = connection) {
        while (expectedTableIterator.next()) {
            ITable expectedTable = expectedTableIterator.getTable();
            String actualTableName = expectedTable.getTableMetaData().getTableName();
            String verifySql = "SELECT * FROM " + actualTableName + " WHERE status = '" + getStatus(file) + "'" + getOrderByCondition(actualTableName);
            ITable actualTable = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn).createQueryTable(actualTableName, verifySql);
            Assertion.assertEquals(expectedTable, actualTable);
        }
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) ITableIterator(org.dbunit.dataset.ITableIterator) ITable(org.dbunit.dataset.ITable) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Example 9 with FlatXmlDataSetBuilder

use of org.dbunit.dataset.xml.FlatXmlDataSetBuilder in project sharding-jdbc by shardingjdbc.

the class SQLAssertHelper method executeQueryWithStatement.

/**
 * Execute query with statement.
 *
 * @param abstractDataSourceAdapter data source adapter
 * @param parameters parameters
 * @param file file
 *
 * @throws MalformedURLException malformed URL exception
 * @throws SQLException SQL exception
 * @throws DatabaseUnitException database unit exception
 */
public void executeQueryWithStatement(final AbstractDataSourceAdapter abstractDataSourceAdapter, final List<String> parameters, final File file) throws MalformedURLException, SQLException, DatabaseUnitException {
    try (Connection conn = abstractDataSourceAdapter.getConnection()) {
        String querySql = SQLPlaceholderUtil.replaceStatement(sql, parameters.toArray());
        ReplacementDataSet expectedDataSet = new ReplacementDataSet(new FlatXmlDataSetBuilder().build(file));
        expectedDataSet.addReplacementObject("[null]", null);
        for (ITable each : expectedDataSet.getTables()) {
            String tableName = each.getTableMetaData().getTableName();
            IDatabaseConnection connection = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn);
            ITable actualTable = connection.createQueryTable(tableName, querySql);
            // TODO customized CachedResultSetTable with statement
            Assertion.assertEquals(expectedDataSet.getTable(tableName), actualTable);
        }
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) ReplacementDataSet(org.dbunit.dataset.ReplacementDataSet) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Example 10 with FlatXmlDataSetBuilder

use of org.dbunit.dataset.xml.FlatXmlDataSetBuilder in project sharding-jdbc by shardingjdbc.

the class AbstractHintTest method assertDataSetEquals.

private void assertDataSetEquals(final String expectedDataSetFile, final Connection connection, final String sql, final DatabaseType type, final Object[] params) throws SQLException, DatabaseUnitException {
    try (Connection conn = connection;
        PreparedStatement ps = conn.prepareStatement(sql)) {
        int i = 1;
        for (Object param : params) {
            ps.setObject(i++, param);
        }
        ITable actualTable = DBUnitUtil.getConnection(new DatabaseEnvironment(type), connection).createTable("t_order", ps);
        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new InputStreamReader(AbstractHintTest.class.getClassLoader().getResourceAsStream(expectedDataSetFile)));
        Assertion.assertEquals(expectedDataSet.getTable("t_order"), actualTable);
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) InputStreamReader(java.io.InputStreamReader) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ITable(org.dbunit.dataset.ITable) IDataSet(org.dbunit.dataset.IDataSet) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Aggregations

FlatXmlDataSetBuilder (org.dbunit.dataset.xml.FlatXmlDataSetBuilder)22 Connection (java.sql.Connection)9 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)8 InputStream (java.io.InputStream)7 InputStreamReader (java.io.InputStreamReader)7 IDataSet (org.dbunit.dataset.IDataSet)7 DatabaseEnvironment (io.shardingjdbc.core.common.env.DatabaseEnvironment)6 ITable (org.dbunit.dataset.ITable)6 File (java.io.File)4 PreparedStatement (java.sql.PreparedStatement)3 IDatabaseTester (org.dbunit.IDatabaseTester)3 DatabaseConnection (org.dbunit.database.DatabaseConnection)3 ReplacementDataSet (org.dbunit.dataset.ReplacementDataSet)3 FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)3 MySqlConnection (org.dbunit.ext.mysql.MySqlConnection)3 ShardingJdbcDatabaseTester (io.shardingjdbc.core.common.env.ShardingJdbcDatabaseTester)2 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 FilteredDataSet (org.dbunit.dataset.FilteredDataSet)2