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