Search in sources :

Example 16 with IDataSet

use of org.dbunit.dataset.IDataSet in project SSM by Intel-bigdata.

the class TestAccessCountTableManager method testAddAccessCountInfo.

@Test
public void testAddAccessCountInfo() throws Exception {
    createTables(databaseTester.getConnection());
    IDataSet dataSet = new XmlDataSet(getClass().getClassLoader().getResourceAsStream("files.xml"));
    databaseTester.setDataSet(dataSet);
    databaseTester.onSetup();
    DBAdapter adapter = new DBAdapter(databaseTester.getConnection().getConnection());
    AccessCountTableManager manager = new AccessCountTableManager(adapter);
    List<FileAccessEvent> accessEvents = new ArrayList<>();
    accessEvents.add(new FileAccessEvent("file1", 0));
    accessEvents.add(new FileAccessEvent("file2", 1));
    accessEvents.add(new FileAccessEvent("file2", 2));
    accessEvents.add(new FileAccessEvent("file3", 2));
    accessEvents.add(new FileAccessEvent("file3", 3));
    accessEvents.add(new FileAccessEvent("file3", 4));
    accessEvents.add(new FileAccessEvent("file3", 5000));
    manager.onAccessEventsArrived(accessEvents);
    AccessCountTable accessCountTable = new AccessCountTable(0L, 5000L);
    ITable actual = databaseTester.getConnection().createTable(accessCountTable.getTableName());
    ITable expect = databaseTester.getDataSet().getTable("expect1");
    SortedTable sortedActual = new SortedTable(actual, new String[] { "fid" });
    sortedActual.setUseComparable(true);
    Assertion.assertEquals(expect, sortedActual);
}
Also used : DBAdapter(org.smartdata.server.metastore.DBAdapter) SortedTable(org.dbunit.dataset.SortedTable) ArrayList(java.util.ArrayList) ITable(org.dbunit.dataset.ITable) IDataSet(org.dbunit.dataset.IDataSet) FileAccessEvent(org.smartdata.metrics.FileAccessEvent) XmlDataSet(org.dbunit.dataset.xml.XmlDataSet) Test(org.junit.Test) DBTest(org.smartdata.server.metastore.DBTest)

Example 17 with IDataSet

use of org.dbunit.dataset.IDataSet in project SSM by Intel-bigdata.

the class TestTableAggregator method testAggregate.

@Test
public void testAggregate() throws Exception {
    AccessCountTableAggregator aggregator = new AccessCountTableAggregator(null);
    createTables(databaseTester.getConnection());
    IDataSet dataSet = new XmlDataSet(getClass().getClassLoader().getResourceAsStream("accessCountTable.xml"));
    databaseTester.setDataSet(dataSet);
    databaseTester.onSetup();
    AccessCountTable result = new AccessCountTable("actual", 0L, 0L, TimeGranularity.MINUTE);
    AccessCountTable table1 = new AccessCountTable("table1", 0L, 0L, TimeGranularity.SECOND);
    AccessCountTable table2 = new AccessCountTable("table2", 0L, 0L, TimeGranularity.SECOND);
    AccessCountTable table3 = new AccessCountTable("table3", 0L, 0L, TimeGranularity.SECOND);
    String aggregateStatement = aggregator.aggregateSQLStatement(result, Lists.newArrayList(table1, table2, table3));
    Statement statement = databaseTester.getConnection().getConnection().createStatement();
    statement.execute(aggregateStatement);
    statement.close();
    ITable actual = databaseTester.getConnection().createTable(result.getTableName());
    ITable expect = databaseTester.getDataSet().getTable("expect");
    Assertion.assertEquals(expect, actual);
}
Also used : Statement(java.sql.Statement) ITable(org.dbunit.dataset.ITable) IDataSet(org.dbunit.dataset.IDataSet) XmlDataSet(org.dbunit.dataset.xml.XmlDataSet) Test(org.junit.Test) DBTest(org.smartdata.server.metastore.DBTest)

Example 18 with IDataSet

use of org.dbunit.dataset.IDataSet in project openmrs-core by openmrs.

the class BaseContextSensitiveTest method executeDataSet.

/**
 * Runs the flat xml data file at the classpath location specified by
 * <code>datasetFilename</code> This is a convenience method. It simply creates an
 * {@link IDataSet} and calls {@link #executeDataSet(IDataSet)}
 *
 * @param datasetFilename String path/filename on the classpath of the xml data set to clean
 *            insert into the current database
 * @see #getConnection()
 * @see #executeDataSet(IDataSet)
 */
public void executeDataSet(String datasetFilename) {
    // try to get the given filename from the cache
    IDataSet xmlDataSetToRun = cachedDatasets.get(datasetFilename);
    // if we didn't find it in the cache, load it
    if (xmlDataSetToRun == null) {
        File file = new File(datasetFilename);
        InputStream fileInInputStreamFormat = null;
        Reader reader = null;
        try {
            try {
                // if its a classpath path to the file
                if (file.exists()) {
                    fileInInputStreamFormat = new FileInputStream(datasetFilename);
                } else {
                    fileInInputStreamFormat = getClass().getClassLoader().getResourceAsStream(datasetFilename);
                    if (fileInInputStreamFormat == null)
                        throw new FileNotFoundException("Unable to find '" + datasetFilename + "' in the classpath");
                }
                reader = new InputStreamReader(fileInInputStreamFormat);
                ReplacementDataSet replacementDataSet = new ReplacementDataSet(new FlatXmlDataSet(reader, false, true, false));
                replacementDataSet.addReplacementObject("[NULL]", null);
                xmlDataSetToRun = replacementDataSet;
                reader.close();
            } catch (DataSetException | IOException e) {
                throw new DatabaseUnitRuntimeException(e);
            }
        } finally {
            IOUtils.closeQuietly(fileInInputStreamFormat);
            IOUtils.closeQuietly(reader);
        }
        // cache the xmldataset for future runs of this file
        cachedDatasets.put(datasetFilename, xmlDataSetToRun);
    }
    executeDataSet(xmlDataSetToRun);
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DatabaseUnitRuntimeException(org.dbunit.DatabaseUnitRuntimeException) DataSetException(org.dbunit.dataset.DataSetException) ReplacementDataSet(org.dbunit.dataset.ReplacementDataSet) IDataSet(org.dbunit.dataset.IDataSet) File(java.io.File)

Example 19 with IDataSet

use of org.dbunit.dataset.IDataSet in project openmrs-core by openmrs.

the class MigrateDataSet method doMigration.

/**
 * Recurse over the given file and convert all xml files there in
 *
 * @param fileOrDirectory
 * @throws Exception
 */
private static void doMigration(File fileOrDirectory) throws Exception {
    String filename = fileOrDirectory.getName();
    if (filename.startsWith(".svn") || filename.endsWith("TestingApplicationContext.xml")) {
    // skip .svn files
    } else if (fileOrDirectory.isDirectory()) {
        for (File innerFile : fileOrDirectory.listFiles()) {
            doMigration(innerFile);
        }
    } else if (filename.endsWith(".xml")) {
        System.out.println("Migrating " + fileOrDirectory.getAbsolutePath());
        System.out.println(execMysqlCmd("DROP DATABASE IF EXISTS " + tempDatabaseName, null, false));
        System.out.println(execMysqlCmd("CREATE DATABASE " + tempDatabaseName + " DEFAULT CHARACTER SET utf8", null, false));
        System.out.println(execMysqlCmd(null, OLD_SCHEMA_FILE, true));
        System.out.println(execMysqlCmd(null, OLD_UPDATE_FILE, true));
        // the straight-up database connection
        String url = "jdbc:mysql://localhost/" + tempDatabaseName;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, credentials[0], credentials[1]);
        // database connection for dbunit
        IDatabaseConnection dbunitConnection = new DatabaseConnection(con);
        try (InputStream fileOrDirectoryStream = new FileInputStream(fileOrDirectory)) {
            PreparedStatement ps = con.prepareStatement("SET FOREIGN_KEY_CHECKS=0;");
            ps.execute();
            ps.close();
            IDataSet dataset = new FlatXmlDataSet(fileOrDirectoryStream);
            DatabaseOperation.REFRESH.execute(dbunitConnection, dataset);
            // turn off foreign key checks here too.
            System.out.println(execMysqlCmd("SET FOREIGN_KEY_CHECKS=0", NEW_UPDATE_FILE, true));
            System.out.println("Dumping new xml file");
            // get a new connection so dbunit knows the right column headers
            dbunitConnection = new DatabaseConnection(con);
            // full database export that will ignore empty tables
            FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream(fileOrDirectory));
            datasetWriter.write(dbunitConnection.createDataSet());
        } catch (Exception e) {
            System.err.println("Unable to convert: " + filename + " Error: " + e.getMessage());
        } finally {
            dbunitConnection = null;
        }
        System.out.println("Finished!");
    }
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) PreparedStatement(java.sql.PreparedStatement) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) FlatXmlWriter(org.dbunit.dataset.xml.FlatXmlWriter) 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 20 with IDataSet

use of org.dbunit.dataset.IDataSet 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

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