Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.util.Properties;
- import org.dbunit.Assertion;
- import org.dbunit.database.DatabaseConfig;
- import org.dbunit.database.DatabaseConnection;
- import org.dbunit.dataset.CompositeDataSet;
- import org.dbunit.dataset.IDataSet;
- import org.dbunit.dataset.ITable;
- import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
- import org.dbunit.ext.mysql.MySqlDataTypeFactory;
- public class DatasetTest {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- IDataSet expected = getFileDataset();
- IDataSet actual = getQueryDataset();
- Assertion.assertEquals(expected, actual);
- System.out.print("All fine");
- }
- private static IDataSet getFileDataset() throws Exception {
- FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
- InputStream input = DatasetTest.class.getClassLoader().getResourceAsStream("Expected.xml");
- return builder.build(input);
- }
- private static IDataSet getQueryDataset() throws Exception {
- String query = "SELECT `table1`.`label`" +
- ", `table2`.`label` AS `table2_label` " +
- "FROM `table1`, `table2` " +
- "WHERE `table1`.`id` = `table2`.`table1_id`";
- ITable table = getConnection().createQueryTable("test_dataset", query);
- return new CompositeDataSet(new ITable[] { table });
- }
- private static DatabaseConnection getConnection() throws Exception {
- Properties props = new Properties();
- props.put("user", "user");
- props.put("password", "pass");
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", props);
- DatabaseConnection dbConn = new DatabaseConnection(conn);
- dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
- return dbConn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement