Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fi.almamedia.dtf.core;
- import org.dbunit.database.DatabaseConnection;
- import org.dbunit.database.IDatabaseConnection;
- import org.dbunit.dataset.IDataSet;
- import org.dbunit.dataset.ReplacementDataSet;
- import org.dbunit.dataset.xml.XmlDataSet;
- import org.dbunit.operation.DatabaseOperation;
- import org.junit.Before;
- import org.junit.Ignore;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.jdbc.datasource.DataSourceUtils;
- import org.springframework.test.context.transaction.TransactionConfiguration;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import javax.persistence.EntityManager;
- import javax.persistence.PersistenceContext;
- import javax.sql.DataSource;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.sql.Timestamp;
- /**
- * Base class for db unit based tests
- *
- * @author 616286
- */
- @Ignore
- @TransactionConfiguration(transactionManager = "dtfCoreTransactionManager", defaultRollback = true)
- public class BaseDtfCoreDbUnitTest extends BaseDtfCoreTest {
- private static final Logger log = LoggerFactory.getLogger(BaseDtfCoreDbUnitTest.class);
- @PersistenceContext(unitName = "dtfCoreEntityManagerFactory")
- private EntityManager entityManager;
- @Resource
- protected DataSource dtfDataSource;
- protected String dbContentFile = null;
- public BaseDtfCoreDbUnitTest() {
- super();
- }
- @Before
- @Transactional
- public void setUp() throws Exception {
- initializeDB(dbContentFile);
- afterSetUp();
- }
- protected void initializeDB(String dbFile, DatabaseOperation databaseOperation) {
- InputStreamReader isr = null;
- try {
- if (dbFile != null) {
- URL resource = getClass().getClassLoader().getResource(dbFile);
- isr = new InputStreamReader(resource.openStream(), "UTF8");
- IDatabaseConnection conn = new DatabaseConnection(DataSourceUtils.getConnection(dtfDataSource));
- IDataSet dataSet = new XmlDataSet(isr);
- ReplacementDataSet replacementDataSet = new ReplacementDataSet(dataSet);
- replacementDataSet.addReplacementObject("CURRENT_SQL_TIMESTAMP", new Timestamp(System.currentTimeMillis()).toString());
- databaseOperation.execute(conn, replacementDataSet);
- }
- } catch (Exception e) {
- throw new IllegalStateException(e);
- } finally {
- if (isr != null) {
- try {
- isr.close();
- } catch (IOException e) {
- log.error("Closing stream", e);
- }
- }
- }
- }
- protected void initializeDB(String dbFile) {
- initializeDB(dbFile, DatabaseOperation.INSERT);
- }
- protected void afterSetUp() throws Exception {
- }
- protected void flush() {
- entityManager.flush();
- }
- protected void clear() {
- entityManager.clear();
- entityManager.getEntityManagerFactory().getCache().evictAll();
- }
- }
Add Comment
Please, Sign In to add comment