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);
}
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);
}
}
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);
}
}
Aggregations