Search in sources :

Example 1 with DataSetException

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

the class FlatXmlDataSetExtractor method toString.

public String toString() {
    StringBuilder sb = new StringBuilder();
    try {
        for (String tableName : ds.getTableNames()) {
            sb.append("Table:").append(tableName).append("\n");
            ITable table = ds.getTable(tableName);
            for (int i = 0; i < table.getRowCount(); i++) {
                ITableMetaData md = table.getTableMetaData();
                sb.append("    ");
                for (Column col : md.getColumns()) {
                    sb.append(col.getColumnName()).append(": ").append(table.getValue(i, col.getColumnName()));
                    sb.append(", ");
                }
                sb.delete(sb.length() - 2, sb.length());
                sb.append("\n");
            }
        }
    } catch (DataSetException e) {
        sb.append("Error: ").append(e.getMessage());
    }
    return sb.toString();
}
Also used : DataSetException(org.dbunit.dataset.DataSetException) Column(org.dbunit.dataset.Column) ITableMetaData(org.dbunit.dataset.ITableMetaData) ITable(org.dbunit.dataset.ITable)

Example 2 with DataSetException

use of org.dbunit.dataset.DataSetException 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 3 with DataSetException

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

the class DatabaseUpgradeTestUtil method executeDataset.

public void executeDataset(String path) throws IOException, SQLException {
    InputStream inputStream = getClass().getResourceAsStream(path);
    ReplacementDataSet replacementDataSet;
    try {
        replacementDataSet = new ReplacementDataSet(new FlatXmlDataSet(new InputStreamReader(inputStream), false, true, false));
        inputStream.close();
    } catch (DataSetException e) {
        throw new IOException(e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    replacementDataSet.addReplacementObject("[NULL]", null);
    try {
        DatabaseOperation.REFRESH.execute(dbUnitConnection, replacementDataSet);
        connection.commit();
    } catch (DatabaseUnitException e) {
        throw new IOException(e);
    }
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) InputStreamReader(java.io.InputStreamReader) DataSetException(org.dbunit.dataset.DataSetException) ReplacementDataSet(org.dbunit.dataset.ReplacementDataSet) InputStream(java.io.InputStream) DatabaseUnitException(org.dbunit.DatabaseUnitException) IOException(java.io.IOException)

Aggregations

DataSetException (org.dbunit.dataset.DataSetException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 ReplacementDataSet (org.dbunit.dataset.ReplacementDataSet)2 FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Reader (java.io.Reader)1 DatabaseUnitException (org.dbunit.DatabaseUnitException)1 DatabaseUnitRuntimeException (org.dbunit.DatabaseUnitRuntimeException)1 Column (org.dbunit.dataset.Column)1 IDataSet (org.dbunit.dataset.IDataSet)1 ITable (org.dbunit.dataset.ITable)1 ITableMetaData (org.dbunit.dataset.ITableMetaData)1