Index: lams_common/test/java/org/lamsfoundation/lams/BaseTestCase.java =================================================================== diff -u --- lams_common/test/java/org/lamsfoundation/lams/BaseTestCase.java (revision 0) +++ lams_common/test/java/org/lamsfoundation/lams/BaseTestCase.java (revision 91187b0a16d5bdf638a37a1b6aabc63bd749c10a) @@ -0,0 +1,58 @@ +package org.lamsfoundation.lams; + +import net.sf.hibernate.Session; +import net.sf.hibernate.SessionFactory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; +import org.springframework.orm.hibernate.SessionFactoryUtils; +import org.springframework.orm.hibernate.SessionHolder; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import junit.framework.TestCase; +/* + * Created on Dec 4, 2004 + */ + +/** + * @author manpreet + * + * TODO To change the template for this generated type comment go to + * Window - Preferences - Java - Code Style - Code Templates + */ +public class BaseTestCase extends TestCase { + + private static String CONFIG_PATH = "/"; + + protected ApplicationContext context; + + public BaseTestCase(){ + String path = CONFIG_PATH+"applicationContext.xml"; + context = new ClassPathXmlApplicationContext(path); + } + + protected void setUp() throws Exception{ + SessionFactory sessionFactory = (SessionFactory)getBean("coreSessionFactory"); + Session s = sessionFactory.openSession(); + TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s)); + } + + protected Object getBean(String beanName) { + if (context == null) context = new FileSystemXmlApplicationContext(CONFIG_PATH+"applicationContext.xml"); + Object o = context.getBean(beanName); + return o; + } + + protected void tearDown() throws Exception{ + super.tearDown(); + SessionFactory sessionFactory = (SessionFactory)getBean("coreSessionFactory"); + SessionHolder holder = (SessionHolder)TransactionSynchronizationManager.getResource(sessionFactory); + if (holder != null) { + Session s = holder.getSession(); + s.flush(); + TransactionSynchronizationManager.unbindResource(sessionFactory); + SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory); + } + } +}