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