Advertisement
Guest User

Untitled

a guest
Jul 18th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. public class DbUnitHelper {
  2.  
  3. private Connection conexao;
  4. private DatabaseConnection conexaoDBUnit;
  5. private String xmlFolder;
  6.  
  7. public DbUnitHelper(String xmlFolder) {
  8. this.xmlFolder = xmlFolder;
  9.  
  10. try {
  11. Class.forName("com.mysql.jdbc.Driver").newInstance();
  12. conexao = DriverManager.getConnection("jdbc:mysql://localhost/teste_dbunit", "root", "root");
  13. conexaoDBUnit = new DatabaseConnection(conexao);
  14. DatabaseConfig config = conexaoDBUnit.getConfig();
  15. config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
  16. } catch (Exception e) {
  17. throw new RuntimeException("Erro inicializando DBUnit", e);
  18. }
  19. }
  20.  
  21. public void execute(DatabaseOperation operation, String xml) {
  22. try {
  23. InputStream is = getClass().getResourceAsStream("/" + xmlFolder + "/" + xml);
  24. FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
  25. IDataSet dataSet = builder.build(is);
  26.  
  27. operation.execute(conexaoDBUnit, dataSet);
  28. } catch (Exception e) {
  29. throw new RuntimeException("Erro executando DbUnit", e);
  30. }
  31. }
  32.  
  33. public void close() {
  34. try {
  35. conexaoDBUnit.close();
  36. conexao.close();
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement