Guest User

Untitled

a guest
Oct 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package fi.almamedia.dtf.core;
  2.  
  3. import org.dbunit.database.DatabaseConnection;
  4. import org.dbunit.database.IDatabaseConnection;
  5. import org.dbunit.dataset.IDataSet;
  6. import org.dbunit.dataset.ReplacementDataSet;
  7. import org.dbunit.dataset.xml.XmlDataSet;
  8. import org.dbunit.operation.DatabaseOperation;
  9. import org.junit.Before;
  10. import org.junit.Ignore;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.jdbc.datasource.DataSourceUtils;
  14. import org.springframework.test.context.transaction.TransactionConfiguration;
  15. import org.springframework.transaction.annotation.Transactional;
  16.  
  17. import javax.annotation.Resource;
  18. import javax.persistence.EntityManager;
  19. import javax.persistence.PersistenceContext;
  20. import javax.sql.DataSource;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.net.URL;
  24. import java.sql.Timestamp;
  25.  
  26. /**
  27. * Base class for db unit based tests
  28. *
  29. * @author 616286
  30. */
  31. @Ignore
  32. @TransactionConfiguration(transactionManager = "dtfCoreTransactionManager", defaultRollback = true)
  33. public class BaseDtfCoreDbUnitTest extends BaseDtfCoreTest {
  34.  
  35. private static final Logger log = LoggerFactory.getLogger(BaseDtfCoreDbUnitTest.class);
  36.  
  37. @PersistenceContext(unitName = "dtfCoreEntityManagerFactory")
  38. private EntityManager entityManager;
  39.  
  40. @Resource
  41. protected DataSource dtfDataSource;
  42.  
  43. protected String dbContentFile = null;
  44.  
  45. public BaseDtfCoreDbUnitTest() {
  46. super();
  47. }
  48.  
  49. @Before
  50. @Transactional
  51. public void setUp() throws Exception {
  52. initializeDB(dbContentFile);
  53. afterSetUp();
  54. }
  55.  
  56. protected void initializeDB(String dbFile, DatabaseOperation databaseOperation) {
  57. InputStreamReader isr = null;
  58. try {
  59. if (dbFile != null) {
  60. URL resource = getClass().getClassLoader().getResource(dbFile);
  61. isr = new InputStreamReader(resource.openStream(), "UTF8");
  62. IDatabaseConnection conn = new DatabaseConnection(DataSourceUtils.getConnection(dtfDataSource));
  63. IDataSet dataSet = new XmlDataSet(isr);
  64. ReplacementDataSet replacementDataSet = new ReplacementDataSet(dataSet);
  65. replacementDataSet.addReplacementObject("CURRENT_SQL_TIMESTAMP", new Timestamp(System.currentTimeMillis()).toString());
  66. databaseOperation.execute(conn, replacementDataSet);
  67. }
  68. } catch (Exception e) {
  69. throw new IllegalStateException(e);
  70. } finally {
  71. if (isr != null) {
  72. try {
  73. isr.close();
  74. } catch (IOException e) {
  75. log.error("Closing stream", e);
  76. }
  77. }
  78. }
  79. }
  80.  
  81. protected void initializeDB(String dbFile) {
  82. initializeDB(dbFile, DatabaseOperation.INSERT);
  83. }
  84.  
  85. protected void afterSetUp() throws Exception {
  86. }
  87.  
  88. protected void flush() {
  89. entityManager.flush();
  90. }
  91.  
  92. protected void clear() {
  93. entityManager.clear();
  94. entityManager.getEntityManagerFactory().getCache().evictAll();
  95. }
  96. }
Add Comment
Please, Sign In to add comment