Search in sources :

Example 1 with DatabaseUnitRuntimeException

use of org.dbunit.DatabaseUnitRuntimeException 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 2 with DatabaseUnitRuntimeException

use of org.dbunit.DatabaseUnitRuntimeException in project openmrs-core by openmrs.

the class BaseContextSensitiveTest method executeDataSet.

/**
 * Run the given dataset specified by the <code>dataset</code> argument
 *
 * @param dataset IDataSet to run on the current database used by Spring
 * @see #getConnection()
 */
public void executeDataSet(IDataSet dataset) {
    try {
        Connection connection = getConnection();
        IDatabaseConnection dbUnitConn = setupDatabaseConnection(connection);
        // Do the actual update/insert:
        // insert new rows, update existing rows, and leave others alone
        DatabaseOperation.REFRESH.execute(dbUnitConn, dataset);
    } catch (DatabaseUnitException | SQLException e) {
        throw new DatabaseUnitRuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) DatabaseUnitException(org.dbunit.DatabaseUnitException) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseUnitRuntimeException(org.dbunit.DatabaseUnitRuntimeException)

Example 3 with DatabaseUnitRuntimeException

use of org.dbunit.DatabaseUnitRuntimeException in project openmrs-core by openmrs.

the class BaseContextSensitiveTest method deleteAllData.

/**
 * This is a convenience method to clear out all rows in all tables in the current connection.
 * <p>
 * This operation always results in a commit.
 *
 * @throws Exception
 */
public void deleteAllData() {
    try {
        Context.clearSession();
        Connection connection = getConnection();
        turnOffDBConstraints(connection);
        IDatabaseConnection dbUnitConn = setupDatabaseConnection(connection);
        // find all the tables for this connection
        ResultSet resultSet = connection.getMetaData().getTables(null, "PUBLIC", "%", null);
        DefaultDataSet dataset = new DefaultDataSet();
        while (resultSet.next()) {
            String tableName = resultSet.getString(3);
            dataset.addTable(new DefaultTable(tableName));
        }
        // do the actual deleting/truncating
        DatabaseOperation.DELETE_ALL.execute(dbUnitConn, dataset);
        turnOnDBConstraints(connection);
        connection.commit();
        updateSearchIndex();
        isBaseSetup = false;
    } catch (SQLException | DatabaseUnitException e) {
        throw new DatabaseUnitRuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) DefaultDataSet(org.dbunit.dataset.DefaultDataSet) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) ResultSet(java.sql.ResultSet) DefaultTable(org.dbunit.dataset.DefaultTable) DatabaseUnitException(org.dbunit.DatabaseUnitException) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseUnitRuntimeException(org.dbunit.DatabaseUnitRuntimeException)

Aggregations

DatabaseUnitRuntimeException (org.dbunit.DatabaseUnitRuntimeException)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 DatabaseUnitException (org.dbunit.DatabaseUnitException)2 DatabaseConnection (org.dbunit.database.DatabaseConnection)2 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ResultSet (java.sql.ResultSet)1 DataSetException (org.dbunit.dataset.DataSetException)1 DefaultDataSet (org.dbunit.dataset.DefaultDataSet)1 DefaultTable (org.dbunit.dataset.DefaultTable)1 IDataSet (org.dbunit.dataset.IDataSet)1 ReplacementDataSet (org.dbunit.dataset.ReplacementDataSet)1 FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)1