1 16 package com.blandware.atleap.persistence.core; 17 18 import junit.framework.TestCase; 19 import org.apache.commons.beanutils.BeanUtils; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.springframework.context.ApplicationContext; 23 import org.springframework.context.support.ClassPathXmlApplicationContext; 24 import org.springframework.util.ClassUtils; 25 import org.springframework.transaction.support.TransactionSynchronizationManager; 26 import org.springframework.orm.hibernate3.SessionHolder; 27 import org.springframework.orm.hibernate3.SessionFactoryUtils; 28 import org.springframework.dao.DataAccessResourceFailureException; 29 import org.hibernate.SessionFactory; 30 import org.hibernate.Session; 31 import org.hibernate.FlushMode; 32 import org.hibernate.Hibernate; 33 34 import java.util.Enumeration ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 import java.util.MissingResourceException ; 38 import java.util.ResourceBundle ; 39 40 49 public abstract class BaseDAOTestCase extends TestCase { 50 protected final Log log = LogFactory.getLog(getClass()); 51 protected final static ApplicationContext ctx; 52 protected ResourceBundle rb; 53 54 57 protected SessionFactory sessionFactory; 58 59 62 private boolean participate; 63 64 67 private Session session; 68 69 static { 72 String pkg = ClassUtils.classPackageAsResourcePath(BaseDAOTestCase.class); 73 82 83 String [] paths = {"classpath*:/" + pkg + "/applicationContext-*.xml", 84 "classpath*:META-INF/applicationContext-*.xml"}; 85 ctx = new ClassPathXmlApplicationContext(paths); 86 } 87 88 89 public BaseDAOTestCase() { 90 String className = this.getClass().getName(); 93 94 try { 95 rb = ResourceBundle.getBundle(className); 96 } catch ( MissingResourceException mre ) { 97 } 99 } 100 101 protected void setUp() throws Exception { 102 sessionFactory = lookupSessionFactory(); 104 session = null; 105 participate = false; 106 if ( TransactionSynchronizationManager.hasResource(sessionFactory) ) { 107 participate = true; 109 } else { 110 if ( log.isDebugEnabled() ) { 111 log.debug("Opening temporary Hibernate session in test case"); 112 } 113 session = getSession(sessionFactory); 114 session.setFlushMode(FlushMode.AUTO); 115 TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); 116 } 117 } 118 119 protected void tearDown() throws Exception { 120 if ( !participate ) { 122 TransactionSynchronizationManager.unbindResource(sessionFactory); 123 if ( log.isDebugEnabled() ) { 124 log.debug("Closing temporary Hibernate session in test case"); 125 } 126 closeSession(session, sessionFactory); 127 } 128 } 129 130 138 protected SessionFactory lookupSessionFactory() { 139 if ( log.isDebugEnabled() ) { 140 log.debug("Using session factory '" + getSessionFactoryBeanName() + "' for StartupListener"); 141 } 142 return (SessionFactory) ctx.getBean(getSessionFactoryBeanName()); 143 } 144 145 151 protected String getSessionFactoryBeanName() { 152 return "sessionFactory"; 153 } 154 155 170 protected Session getSession(SessionFactory sessionFactory) 171 throws DataAccessResourceFailureException { 172 Session session = SessionFactoryUtils.getSession(sessionFactory, true); 173 session.setFlushMode(FlushMode.NEVER); 174 return session; 175 } 176 177 190 protected void closeSession(Session session, SessionFactory sessionFactory) { 191 SessionFactoryUtils.releaseSession(session, sessionFactory); 192 } 193 194 202 protected Object populate(Object obj) throws Exception { 203 Map map = new HashMap (); 206 207 for ( Enumeration keys = rb.getKeys(); keys.hasMoreElements(); ) { 208 String key = (String ) keys.nextElement(); 209 map.put(key, rb.getString(key)); 210 } 211 212 BeanUtils.copyProperties(obj, map); 213 214 return obj; 215 } 216 } 217 | Popular Tags |