From: <fg...@us...> - 2007-02-05 17:40:46
|
Revision: 201 http://svn.sourceforge.net/openutils/?rev=201&view=rev Author: fgiust Date: 2007-02-05 09:40:35 -0800 (Mon, 05 Feb 2007) Log Message: ----------- configurable deferred close Modified Paths: -------------- trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java Property Changed: ---------------- trunk/openutils-testing-testng/ Modified: trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java =================================================================== --- trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java 2007-02-05 17:23:33 UTC (rev 200) +++ trunk/openutils-testing-junit/src/main/java/it/openutils/testing/junit/DbUnitTestCase.java 2007-02-05 17:40:35 UTC (rev 201) @@ -16,6 +16,7 @@ package it.openutils.testing.junit; import java.io.InputStream; +import java.util.Map; import javax.sql.DataSource; @@ -53,8 +54,18 @@ protected static IDataSet truncateDataSet; /** + * Should use deferred close emulating the spring OpenSessionInView filter? Default is <code>true</code> + * @return <code>true</code> if deferred close should be used + */ + protected boolean mimicSessionFilter() + { + return true; + } + + /** * {@inheritDoc} */ + @SuppressWarnings("unchecked") @Override protected void setUp() throws Exception { @@ -139,13 +150,18 @@ } // mimic the Spring OpenSessionInViewFilter - this.sessionFactory = (SessionFactory) ctx.getBean("sessionFactory"); + if (mimicSessionFilter()) + { + Map<String, SessionFactory> sfbeans = ctx.getBeansOfType(SessionFactory.class); + if (sfbeans.isEmpty()) + { + fail("No bean of type org.hibernate.SessionFactory found in spring context"); + } + this.sessionFactory = sfbeans.get(sfbeans.keySet().iterator().next()); - // SessionFactoryUtils.getSession(sessionFactory, true).setFlushMode(FlushMode.NEVER); - SessionFactoryUtils.initDeferredClose(this.sessionFactory); + SessionFactoryUtils.initDeferredClose(this.sessionFactory); + } - // @todo find a way to disable cache - // this.sessionFactory.openSession().setCacheMode(CacheMode.IGNORE); } /** @@ -154,8 +170,11 @@ @Override protected void tearDown() throws Exception { - // close open hibernate sessions, mimic the OpenSessionInViewFilter - SessionFactoryUtils.processDeferredClose(this.sessionFactory); + if (mimicSessionFilter()) + { + // close open hibernate sessions, mimic the OpenSessionInViewFilter + SessionFactoryUtils.processDeferredClose(this.sessionFactory); + } // regenerate db initial state String datesetFileName = "/initial-load.xml"; @@ -197,7 +216,7 @@ * return the current Hibernate SessionFactory * @return SessionFactory object */ - public SessionFactory getSessionFactory() + protected SessionFactory getSessionFactory() { return sessionFactory; } Property changes on: trunk/openutils-testing-testng ___________________________________________________________________ Name: svn:ignore - .checkstyle + .checkstyle target Modified: trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java =================================================================== --- trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java 2007-02-05 17:23:33 UTC (rev 200) +++ trunk/openutils-testing-testng/src/main/java/it/openutils/testing/testng/DbUnitTestCase.java 2007-02-05 17:40:35 UTC (rev 201) @@ -22,6 +22,7 @@ import java.io.OutputStream; import java.sql.SQLException; import java.text.MessageFormat; +import java.util.Map; import javax.sql.DataSource; @@ -41,6 +42,9 @@ import org.dbunit.dataset.filter.SequenceTableFilter; import org.dbunit.dataset.xml.XmlDataSet; import org.dbunit.operation.DatabaseOperation; +import org.hibernate.SessionFactory; +import org.springframework.orm.hibernate3.SessionFactoryUtils; +import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -68,6 +72,11 @@ protected static IDataSet truncateDataSet; /** + * Hibernate session factory. + */ + private SessionFactory sessionFactory; + + /** * Optional schema (needed for ORACLE). * @return <code>null</code> */ @@ -77,12 +86,35 @@ } /** + * Should use deferred close emulating the spring OpenSessionInView filter? Default is <code>true</code> + * @return <code>true</code> if deferred close should be used + */ + protected boolean mimicSessionFilter() + { + return true; + } + + /** * Setup the Database before running the test method. * @throws Exception Any exception. */ + @SuppressWarnings("unchecked") @BeforeMethod protected void setUpDbUnit() throws Exception { + // mimic the Spring OpenSessionInViewFilter + if (mimicSessionFilter()) + { + Map<String, SessionFactory> sfbeans = ctx.getBeansOfType(SessionFactory.class); + if (sfbeans.isEmpty()) + { + Assert.fail("No bean of type org.hibernate.SessionFactory found in spring context"); + } + this.sessionFactory = sfbeans.get(sfbeans.keySet().iterator().next()); + + SessionFactoryUtils.initDeferredClose(this.sessionFactory); + } + // check for xml IDataSet dataSet = loadDataSet(getDataFileName("xml")); @@ -128,6 +160,11 @@ @AfterMethod protected void tearDownDbUnit() throws Exception { + if (mimicSessionFilter()) + { + // close open hibernate sessions, mimic the OpenSessionInViewFilter + SessionFactoryUtils.processDeferredClose(this.sessionFactory); + } // regenerate db initial state String datesetFileName = "/initial-load.xml"; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |