1 16 17 package org.springframework.orm.hibernate; 18 19 import java.lang.reflect.AccessibleObject ; 20 import java.lang.reflect.Method ; 21 import java.sql.SQLException ; 22 23 import junit.framework.TestCase; 24 import net.sf.hibernate.FlushMode; 25 import net.sf.hibernate.HibernateException; 26 import net.sf.hibernate.JDBCException; 27 import net.sf.hibernate.Session; 28 import net.sf.hibernate.SessionFactory; 29 import org.aopalliance.intercept.Interceptor; 30 import org.aopalliance.intercept.Invocation; 31 import org.aopalliance.intercept.MethodInvocation; 32 import org.easymock.MockControl; 33 34 import org.springframework.beans.factory.BeanFactory; 35 import org.springframework.dao.DataIntegrityViolationException; 36 import org.springframework.transaction.support.TransactionSynchronizationManager; 37 38 41 public class HibernateInterceptorTests extends TestCase { 42 43 public void testInterceptorWithNewSession() throws HibernateException { 44 MockControl sfControl = MockControl.createControl(SessionFactory.class); 45 SessionFactory sf = (SessionFactory) sfControl.getMock(); 46 MockControl sessionControl = MockControl.createControl(Session.class); 47 Session session = (Session) sessionControl.getMock(); 48 sf.openSession(); 49 sfControl.setReturnValue(session, 1); 50 session.getSessionFactory(); 51 sessionControl.setReturnValue(sf); 52 session.flush(); 53 sessionControl.setVoidCallable(1); 54 session.close(); 55 sessionControl.setReturnValue(null, 1); 56 sfControl.replay(); 57 sessionControl.replay(); 58 59 HibernateInterceptor interceptor = new HibernateInterceptor(); 60 interceptor.setSessionFactory(sf); 61 try { 62 interceptor.invoke(new TestInvocation(sf)); 63 } 64 catch (Throwable t) { 65 fail("Should not have thrown Throwable: " + t.getMessage()); 66 } 67 68 sfControl.verify(); 69 sessionControl.verify(); 70 } 71 72 public void testInterceptorWithNewSessionAndFlushNever() throws HibernateException { 73 MockControl sfControl = MockControl.createControl(SessionFactory.class); 74 SessionFactory sf = (SessionFactory) sfControl.getMock(); 75 MockControl sessionControl = MockControl.createControl(Session.class); 76 Session session = (Session) sessionControl.getMock(); 77 sf.openSession(); 78 sfControl.setReturnValue(session, 1); 79 session.getSessionFactory(); 80 sessionControl.setReturnValue(sf); 81 session.setFlushMode(FlushMode.NEVER); 82 sessionControl.setVoidCallable(1); 83 session.close(); 84 sessionControl.setReturnValue(null, 1); 85 sfControl.replay(); 86 sessionControl.replay(); 87 88 HibernateInterceptor interceptor = new HibernateInterceptor(); 89 interceptor.setFlushModeName("FLUSH_NEVER"); 90 interceptor.setSessionFactory(sf); 91 try { 92 interceptor.invoke(new TestInvocation(sf)); 93 } 94 catch (Throwable t) { 95 fail("Should not have thrown Throwable: " + t.getMessage()); 96 } 97 98 sfControl.verify(); 99 sessionControl.verify(); 100 } 101 102 public void testInterceptorWithThreadBound() { 103 MockControl sfControl = MockControl.createControl(SessionFactory.class); 104 SessionFactory sf = (SessionFactory) sfControl.getMock(); 105 MockControl sessionControl = MockControl.createControl(Session.class); 106 Session session = (Session) sessionControl.getMock(); 107 session.getSessionFactory(); 108 sessionControl.setReturnValue(sf, 1); 109 sfControl.replay(); 110 sessionControl.replay(); 111 112 TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); 113 HibernateInterceptor interceptor = new HibernateInterceptor(); 114 interceptor.setSessionFactory(sf); 115 try { 116 interceptor.invoke(new TestInvocation(sf)); 117 } 118 catch (Throwable t) { 119 fail("Should not have thrown Throwable: " + t.getMessage()); 120 } 121 finally { 122 TransactionSynchronizationManager.unbindResource(sf); 123 } 124 125 sfControl.verify(); 126 sessionControl.verify(); 127 } 128 129 public void testInterceptorWithThreadBoundAndFlushEager() throws HibernateException { 130 MockControl sfControl = MockControl.createControl(SessionFactory.class); 131 SessionFactory sf = (SessionFactory) sfControl.getMock(); 132 MockControl sessionControl = MockControl.createControl(Session.class); 133 Session session = (Session) sessionControl.getMock(); 134 session.getSessionFactory(); 135 sessionControl.setReturnValue(sf, 1); 136 session.flush(); 137 sessionControl.setVoidCallable(1); 138 sfControl.replay(); 139 sessionControl.replay(); 140 141 TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); 142 HibernateInterceptor interceptor = new HibernateInterceptor(); 143 interceptor.setFlushMode(HibernateInterceptor.FLUSH_EAGER); 144 interceptor.setSessionFactory(sf); 145 try { 146 interceptor.invoke(new TestInvocation(sf)); 147 } 148 catch (Throwable t) { 149 fail("Should not have thrown Throwable: " + t.getMessage()); 150 } 151 finally { 152 TransactionSynchronizationManager.unbindResource(sf); 153 } 154 155 sfControl.verify(); 156 sessionControl.verify(); 157 } 158 159 public void testInterceptorWithFlushFailure() throws Throwable { 160 MockControl sfControl = MockControl.createControl(SessionFactory.class); 161 SessionFactory sf = (SessionFactory) sfControl.getMock(); 162 MockControl sessionControl = MockControl.createControl(Session.class); 163 Session session = (Session) sessionControl.getMock(); 164 sf.openSession(); 165 sfControl.setReturnValue(session, 1); 166 session.getSessionFactory(); 167 sessionControl.setReturnValue(sf, 1); 168 SQLException sqlex = new SQLException ("argh", "27"); 169 session.flush(); 170 sessionControl.setThrowable(new JDBCException(sqlex), 1); 171 session.close(); 172 sessionControl.setReturnValue(null, 1); 173 sfControl.replay(); 174 sessionControl.replay(); 175 176 HibernateInterceptor interceptor = new HibernateInterceptor(); 177 interceptor.setSessionFactory(sf); 178 try { 179 interceptor.invoke(new TestInvocation(sf)); 180 fail("Should have thrown DataIntegrityViolationException"); 181 } 182 catch (DataIntegrityViolationException ex) { 183 assertEquals(sqlex, ex.getCause()); 185 } 186 187 sfControl.verify(); 188 sessionControl.verify(); 189 } 190 191 public void testInterceptorWithEntityInterceptor() throws HibernateException { 192 MockControl interceptorControl = MockControl.createControl(net.sf.hibernate.Interceptor.class); 193 net.sf.hibernate.Interceptor entityInterceptor = (net.sf.hibernate.Interceptor) interceptorControl.getMock(); 194 interceptorControl.replay(); 195 MockControl sfControl = MockControl.createControl(SessionFactory.class); 196 SessionFactory sf = (SessionFactory) sfControl.getMock(); 197 MockControl sessionControl = MockControl.createControl(Session.class); 198 Session session = (Session) sessionControl.getMock(); 199 sf.openSession(entityInterceptor); 200 sfControl.setReturnValue(session, 1); 201 session.getSessionFactory(); 202 sessionControl.setReturnValue(sf, 1); 203 session.flush(); 204 sessionControl.setVoidCallable(1); 205 session.close(); 206 sessionControl.setReturnValue(null, 1); 207 sfControl.replay(); 208 sessionControl.replay(); 209 210 HibernateInterceptor interceptor = new HibernateInterceptor(); 211 interceptor.setSessionFactory(sf); 212 interceptor.setEntityInterceptor(entityInterceptor); 213 try { 214 interceptor.invoke(new TestInvocation(sf)); 215 } 216 catch (Throwable t) { 217 fail("Should not have thrown Throwable: " + t.getMessage()); 218 } 219 220 interceptorControl.verify(); 221 sfControl.verify(); 222 sessionControl.verify(); 223 } 224 225 public void testInterceptorWithEntityInterceptorBeanName() throws HibernateException { 226 MockControl interceptorControl = MockControl.createControl(net.sf.hibernate.Interceptor.class); 227 net.sf.hibernate.Interceptor entityInterceptor = (net.sf.hibernate.Interceptor) interceptorControl.getMock(); 228 interceptorControl.replay(); 229 MockControl interceptor2Control = MockControl.createControl(net.sf.hibernate.Interceptor.class); 230 net.sf.hibernate.Interceptor entityInterceptor2 = (net.sf.hibernate.Interceptor) interceptor2Control.getMock(); 231 interceptor2Control.replay(); 232 233 MockControl sfControl = MockControl.createControl(SessionFactory.class); 234 SessionFactory sf = (SessionFactory) sfControl.getMock(); 235 MockControl sessionControl = MockControl.createControl(Session.class); 236 Session session = (Session) sessionControl.getMock(); 237 sf.openSession(entityInterceptor); 238 sfControl.setReturnValue(session, 1); 239 sf.openSession(entityInterceptor2); 240 sfControl.setReturnValue(session, 1); 241 session.getSessionFactory(); 242 sessionControl.setReturnValue(sf, 2); 243 session.flush(); 244 sessionControl.setVoidCallable(2); 245 session.close(); 246 sessionControl.setReturnValue(null, 2); 247 sfControl.replay(); 248 sessionControl.replay(); 249 250 MockControl beanFactoryControl = MockControl.createControl(BeanFactory.class); 251 BeanFactory beanFactory = (BeanFactory) beanFactoryControl.getMock(); 252 beanFactory.getBean("entityInterceptor", net.sf.hibernate.Interceptor.class); 253 beanFactoryControl.setReturnValue(entityInterceptor, 1); 254 beanFactory.getBean("entityInterceptor", net.sf.hibernate.Interceptor.class); 255 beanFactoryControl.setReturnValue(entityInterceptor2, 1); 256 beanFactoryControl.replay(); 257 258 HibernateInterceptor interceptor = new HibernateInterceptor(); 259 interceptor.setSessionFactory(sf); 260 interceptor.setEntityInterceptorBeanName("entityInterceptor"); 261 interceptor.setBeanFactory(beanFactory); 262 for (int i = 0; i < 2; i++) { 263 try { 264 interceptor.invoke(new TestInvocation(sf)); 265 } 266 catch (Throwable t) { 267 fail("Should not have thrown Throwable: " + t.getMessage()); 268 } 269 } 270 271 interceptorControl.verify(); 272 interceptor2Control.verify(); 273 sfControl.verify(); 274 sessionControl.verify(); 275 } 276 277 protected void tearDown() { 278 assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); 279 assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); 280 } 281 282 283 private static class TestInvocation implements MethodInvocation { 284 285 private SessionFactory sessionFactory; 286 287 public TestInvocation(SessionFactory sessionFactory) { 288 this.sessionFactory = sessionFactory; 289 } 290 291 public Object proceed() throws Throwable { 292 if (!TransactionSynchronizationManager.hasResource(this.sessionFactory)) { 293 throw new IllegalStateException ("Session not bound"); 294 } 295 return null; 296 } 297 298 public int getCurrentInterceptorIndex() { 299 return 0; 300 } 301 302 public int getNumberOfInterceptors() { 303 return 0; 304 } 305 306 public Interceptor getInterceptor(int i) { 307 return null; 308 } 309 310 public Method getMethod() { 311 return null; 312 } 313 314 public AccessibleObject getStaticPart() { 315 return null; 316 } 317 318 public Object getArgument(int i) { 319 return null; 320 } 321 322 public Object [] getArguments() { 323 return null; 324 } 325 326 public void setArgument(int i, Object handler) { 327 } 328 329 public int getArgumentCount() { 330 return 0; 331 } 332 333 public Object getThis() { 334 return null; 335 } 336 337 public Object getProxy() { 338 return null; 339 } 340 341 public Invocation cloneInstance() { 342 return null; 343 } 344 345 public void release() { 346 } 347 } 348 349 } 350 | Popular Tags |