1 16 17 package org.springframework.orm.hibernate.support; 18 19 import java.io.IOException ; 20 import java.sql.Connection ; 21 22 import javax.servlet.FilterChain ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import javax.transaction.Status ; 27 import javax.transaction.TransactionManager ; 28 29 import junit.framework.TestCase; 30 import net.sf.hibernate.FlushMode; 31 import net.sf.hibernate.HibernateException; 32 import net.sf.hibernate.Session; 33 import net.sf.hibernate.SessionFactory; 34 import net.sf.hibernate.Transaction; 35 import net.sf.hibernate.engine.SessionFactoryImplementor; 36 import org.easymock.MockControl; 37 38 import org.springframework.mock.web.MockFilterConfig; 39 import org.springframework.mock.web.MockHttpServletRequest; 40 import org.springframework.mock.web.MockHttpServletResponse; 41 import org.springframework.mock.web.MockServletContext; 42 import org.springframework.orm.hibernate.HibernateAccessor; 43 import org.springframework.orm.hibernate.HibernateTransactionManager; 44 import org.springframework.orm.hibernate.SessionFactoryUtils; 45 import org.springframework.transaction.TransactionDefinition; 46 import org.springframework.transaction.TransactionStatus; 47 import org.springframework.transaction.support.DefaultTransactionDefinition; 48 import org.springframework.transaction.support.TransactionSynchronizationManager; 49 import org.springframework.web.context.WebApplicationContext; 50 import org.springframework.web.context.support.StaticWebApplicationContext; 51 52 56 public class OpenSessionInViewTests extends TestCase { 57 58 public void testOpenSessionInViewInterceptorWithSingleSession() throws HibernateException { 59 MockControl sfControl = MockControl.createControl(SessionFactory.class); 60 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 61 MockControl sessionControl = MockControl.createControl(Session.class); 62 Session session = (Session) sessionControl.getMock(); 63 64 OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); 65 interceptor.setSessionFactory(sf); 66 MockServletContext sc = new MockServletContext(); 67 MockHttpServletRequest request = new MockHttpServletRequest(sc); 68 MockHttpServletResponse response = new MockHttpServletResponse(); 69 70 sf.openSession(); 71 sfControl.setReturnValue(session, 1); 72 session.getSessionFactory(); 73 sessionControl.setReturnValue(sf, 2); 74 session.setFlushMode(FlushMode.NEVER); 75 sessionControl.setVoidCallable(1); 76 sfControl.replay(); 77 sessionControl.replay(); 78 interceptor.preHandle(request, response, "handler"); 79 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 80 81 interceptor.preHandle(request, response, "handler"); 83 84 assertEquals(session, SessionFactoryUtils.getSession(sf, false)); 85 86 interceptor.preHandle(request, response, "handler"); 87 interceptor.postHandle(request, response, "handler", null); 88 interceptor.afterCompletion(request, response, "handler", null); 89 90 interceptor.postHandle(request, response, "handler", null); 91 interceptor.afterCompletion(request, response, "handler", null); 92 93 interceptor.preHandle(request, response, "handler"); 94 interceptor.postHandle(request, response, "handler", null); 95 interceptor.afterCompletion(request, response, "handler", null); 96 97 sfControl.verify(); 98 sessionControl.verify(); 99 100 sfControl.reset(); 101 sessionControl.reset(); 102 sfControl.replay(); 103 sessionControl.replay(); 104 interceptor.postHandle(request, response, "handler", null); 105 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 106 sfControl.verify(); 107 sessionControl.verify(); 108 109 sfControl.reset(); 110 sessionControl.reset(); 111 session.close(); 112 sessionControl.setReturnValue(null, 1); 113 sfControl.replay(); 114 sessionControl.replay(); 115 interceptor.afterCompletion(request, response, "handler", null); 116 assertFalse(TransactionSynchronizationManager.hasResource(sf)); 117 sfControl.verify(); 118 sessionControl.verify(); 119 } 120 121 public void testOpenSessionInViewInterceptorWithSingleSessionAndJtaTm() throws Exception { 122 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 123 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 124 MockControl sessionControl = MockControl.createControl(Session.class); 125 Session session = (Session) sessionControl.getMock(); 126 127 MockControl tmControl = MockControl.createControl(TransactionManager .class); 128 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 129 tm.getStatus(); 130 tmControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 2); 131 tm.getTransaction(); 132 tmControl.setReturnValue(null, 1); 133 134 OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); 135 interceptor.setSessionFactory(sf); 136 MockServletContext sc = new MockServletContext(); 137 MockHttpServletRequest request = new MockHttpServletRequest(sc); 138 MockHttpServletResponse response = new MockHttpServletResponse(); 139 140 sf.getConnectionProvider(); 141 sfControl.setReturnValue(null, 1); 142 sf.getTransactionManager(); 143 sfControl.setReturnValue(tm, 2); 144 sf.openSession(); 145 sfControl.setReturnValue(session, 1); 146 session.setFlushMode(FlushMode.NEVER); 147 sessionControl.setVoidCallable(1); 148 149 tmControl.replay(); 150 sfControl.replay(); 151 sessionControl.replay(); 152 153 interceptor.preHandle(request, response, "handler"); 154 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 155 156 interceptor.preHandle(request, response, "handler"); 158 159 assertEquals(session, SessionFactoryUtils.getSession(sf, false)); 160 161 interceptor.preHandle(request, response, "handler"); 162 interceptor.postHandle(request, response, "handler", null); 163 interceptor.afterCompletion(request, response, "handler", null); 164 165 interceptor.postHandle(request, response, "handler", null); 166 interceptor.afterCompletion(request, response, "handler", null); 167 168 interceptor.preHandle(request, response, "handler"); 169 interceptor.postHandle(request, response, "handler", null); 170 interceptor.afterCompletion(request, response, "handler", null); 171 172 sfControl.verify(); 173 sessionControl.verify(); 174 175 sfControl.reset(); 176 sessionControl.reset(); 177 sfControl.replay(); 178 sessionControl.replay(); 179 interceptor.postHandle(request, response, "handler", null); 180 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 181 sfControl.verify(); 182 sessionControl.verify(); 183 184 sfControl.reset(); 185 sessionControl.reset(); 186 session.close(); 187 sessionControl.setReturnValue(null, 1); 188 sfControl.replay(); 189 sessionControl.replay(); 190 interceptor.afterCompletion(request, response, "handler", null); 191 assertFalse(TransactionSynchronizationManager.hasResource(sf)); 192 sfControl.verify(); 193 sessionControl.verify(); 194 } 195 196 public void testOpenSessionInViewInterceptorWithSingleSessionAndFlush() throws HibernateException { 197 MockControl sfControl = MockControl.createControl(SessionFactory.class); 198 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 199 MockControl sessionControl = MockControl.createControl(Session.class); 200 Session session = (Session) sessionControl.getMock(); 201 202 OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); 203 interceptor.setSessionFactory(sf); 204 interceptor.setFlushMode(HibernateAccessor.FLUSH_AUTO); 205 MockServletContext sc = new MockServletContext(); 206 MockHttpServletRequest request = new MockHttpServletRequest(sc); 207 MockHttpServletResponse response = new MockHttpServletResponse(); 208 209 sf.openSession(); 210 sfControl.setReturnValue(session, 1); 211 session.getSessionFactory(); 212 sessionControl.setReturnValue(sf); 213 sfControl.replay(); 214 sessionControl.replay(); 215 interceptor.preHandle(request, response, "handler"); 216 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 217 sfControl.verify(); 218 sessionControl.verify(); 219 220 sfControl.reset(); 221 sessionControl.reset(); 222 session.flush(); 223 sessionControl.setVoidCallable(1); 224 sfControl.replay(); 225 sessionControl.replay(); 226 interceptor.postHandle(request, response, "handler", null); 227 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 228 sfControl.verify(); 229 sessionControl.verify(); 230 231 sfControl.reset(); 232 sessionControl.reset(); 233 session.close(); 234 sessionControl.setReturnValue(null, 1); 235 sfControl.replay(); 236 sessionControl.replay(); 237 interceptor.afterCompletion(request, response, "handler", null); 238 assertFalse(TransactionSynchronizationManager.hasResource(sf)); 239 sfControl.verify(); 240 sessionControl.verify(); 241 } 242 243 public void testOpenSessionInViewInterceptorAndDeferredClose() throws HibernateException { 244 MockControl sfControl = MockControl.createControl(SessionFactory.class); 245 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 246 MockControl sessionControl = MockControl.createControl(Session.class); 247 Session session = (Session) sessionControl.getMock(); 248 249 OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); 250 interceptor.setSessionFactory(sf); 251 interceptor.setSingleSession(false); 252 MockServletContext sc = new MockServletContext(); 253 MockHttpServletRequest request = new MockHttpServletRequest(sc); 254 MockHttpServletResponse response = new MockHttpServletResponse(); 255 256 sf.openSession(); 257 sfControl.setReturnValue(session, 1); 258 session.getSessionFactory(); 259 sessionControl.setReturnValue(sf); 260 sfControl.replay(); 261 sessionControl.replay(); 262 interceptor.preHandle(request, response, "handler"); 263 Session sess = SessionFactoryUtils.getSession(sf, true); 264 SessionFactoryUtils.releaseSession(sess, sf); 265 266 interceptor.preHandle(request, response, "handler"); 268 269 interceptor.preHandle(request, response, "handler"); 270 interceptor.postHandle(request, response, "handler", null); 271 interceptor.afterCompletion(request, response, "handler", null); 272 273 interceptor.postHandle(request, response, "handler", null); 274 interceptor.afterCompletion(request, response, "handler", null); 275 276 interceptor.preHandle(request, response, "handler"); 277 interceptor.postHandle(request, response, "handler", null); 278 interceptor.afterCompletion(request, response, "handler", null); 279 280 sfControl.verify(); 281 sessionControl.verify(); 282 283 sfControl.reset(); 284 sessionControl.reset(); 285 session.close(); 286 sessionControl.setReturnValue(null, 1); 287 sfControl.replay(); 288 sessionControl.replay(); 289 interceptor.postHandle(request, response, "handler", null); 290 interceptor.afterCompletion(request, response, "handler", null); 291 sfControl.verify(); 292 sessionControl.verify(); 293 } 294 295 public void testOpenSessionInViewFilterWithSingleSession() throws Exception { 296 MockControl sfControl = MockControl.createControl(SessionFactory.class); 297 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 298 MockControl sessionControl = MockControl.createControl(Session.class); 299 Session session = (Session) sessionControl.getMock(); 300 301 sf.openSession(); 302 sfControl.setReturnValue(session, 1); 303 session.getSessionFactory(); 304 sessionControl.setReturnValue(sf); 305 session.setFlushMode(FlushMode.NEVER); 306 sessionControl.setVoidCallable(1); 307 session.close(); 308 sessionControl.setReturnValue(null, 1); 309 sfControl.replay(); 310 sessionControl.replay(); 311 312 MockControl sf2Control = MockControl.createControl(SessionFactory.class); 313 final SessionFactory sf2 = (SessionFactory) sf2Control.getMock(); 314 MockControl session2Control = MockControl.createControl(Session.class); 315 Session session2 = (Session) session2Control.getMock(); 316 317 sf2.openSession(); 318 sf2Control.setReturnValue(session2, 1); 319 session2.getSessionFactory(); 320 session2Control.setReturnValue(sf); 321 session2.setFlushMode(FlushMode.NEVER); 322 session2Control.setVoidCallable(1); 323 session2.close(); 324 session2Control.setReturnValue(null, 1); 325 sf2Control.replay(); 326 session2Control.replay(); 327 328 MockServletContext sc = new MockServletContext(); 329 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 330 wac.setServletContext(sc); 331 wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); 332 wac.getDefaultListableBeanFactory().registerSingleton("mySessionFactory", sf2); 333 wac.refresh(); 334 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 335 MockHttpServletRequest request = new MockHttpServletRequest(sc); 336 MockHttpServletResponse response = new MockHttpServletResponse(); 337 338 MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); 339 MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); 340 filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory"); 341 342 final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); 343 filter.init(filterConfig); 344 final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter(); 345 filter2.init(filterConfig2); 346 347 final FilterChain filterChain = new FilterChain () { 348 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 349 throws IOException , ServletException { 350 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 351 servletRequest.setAttribute("invoked", Boolean.TRUE); 352 } 353 }; 354 355 final FilterChain filterChain2 = new FilterChain () { 356 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 357 throws IOException , ServletException { 358 assertTrue(TransactionSynchronizationManager.hasResource(sf2)); 359 filter.doFilter(servletRequest, servletResponse, filterChain); 360 } 361 }; 362 363 FilterChain filterChain3 = new FilterChain () { 364 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 365 throws IOException , ServletException { 366 filter2.doFilter(servletRequest, servletResponse, filterChain2); 367 } 368 }; 369 370 assertFalse(TransactionSynchronizationManager.hasResource(sf)); 371 assertFalse(TransactionSynchronizationManager.hasResource(sf2)); 372 filter2.doFilter(request, response, filterChain3); 373 assertFalse(TransactionSynchronizationManager.hasResource(sf)); 374 assertFalse(TransactionSynchronizationManager.hasResource(sf2)); 375 assertNotNull(request.getAttribute("invoked")); 376 377 sfControl.verify(); 378 sessionControl.verify(); 379 sf2Control.verify(); 380 session2Control.verify(); 381 382 wac.close(); 383 } 384 385 public void testOpenSessionInViewFilterWithSingleSessionAndPreBoundSession() throws Exception { 386 MockControl sfControl = MockControl.createControl(SessionFactory.class); 387 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 388 MockControl sessionControl = MockControl.createControl(Session.class); 389 Session session = (Session) sessionControl.getMock(); 390 391 sf.openSession(); 392 sfControl.setReturnValue(session, 1); 393 session.getSessionFactory(); 394 sessionControl.setReturnValue(sf); 395 session.setFlushMode(FlushMode.NEVER); 396 sessionControl.setVoidCallable(1); 397 session.close(); 398 sessionControl.setReturnValue(null, 1); 399 sfControl.replay(); 400 sessionControl.replay(); 401 402 MockServletContext sc = new MockServletContext(); 403 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 404 wac.setServletContext(sc); 405 wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); 406 wac.refresh(); 407 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 408 MockHttpServletRequest request = new MockHttpServletRequest(sc); 409 MockHttpServletResponse response = new MockHttpServletResponse(); 410 411 MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); 412 MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); 413 filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory"); 414 415 OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); 416 interceptor.setSessionFactory(sf); 417 interceptor.preHandle(request, response, "handler"); 418 419 final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); 420 filter.init(filterConfig); 421 422 final FilterChain filterChain = new FilterChain () { 423 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 424 throws IOException , ServletException { 425 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 426 servletRequest.setAttribute("invoked", Boolean.TRUE); 427 } 428 }; 429 430 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 431 filter.doFilter(request, response, filterChain); 432 assertTrue(TransactionSynchronizationManager.hasResource(sf)); 433 assertNotNull(request.getAttribute("invoked")); 434 435 interceptor.postHandle(request, response, "handler", null); 436 interceptor.afterCompletion(request, response, "handler", null); 437 438 sfControl.verify(); 439 sessionControl.verify(); 440 441 wac.close(); 442 } 443 444 public void testOpenSessionInViewFilterWithDeferredClose() throws Exception { 445 MockControl sfControl = MockControl.createControl(SessionFactory.class); 446 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 447 final MockControl sessionControl = MockControl.createControl(Session.class); 448 final Session session = (Session) sessionControl.getMock(); 449 450 sf.openSession(); 451 sfControl.setReturnValue(session, 1); 452 session.getSessionFactory(); 453 sessionControl.setReturnValue(sf); 454 session.getFlushMode(); 455 sessionControl.setReturnValue(FlushMode.NEVER, 1); 456 sfControl.replay(); 457 sessionControl.replay(); 458 459 MockControl sf2Control = MockControl.createControl(SessionFactory.class); 460 final SessionFactory sf2 = (SessionFactory) sf2Control.getMock(); 461 final MockControl session2Control = MockControl.createControl(Session.class); 462 final Session session2 = (Session) session2Control.getMock(); 463 MockControl txControl = MockControl.createControl(Transaction.class); 464 Transaction tx = (Transaction) txControl.getMock(); 465 MockControl conControl = MockControl.createControl(Connection .class); 466 Connection con = (Connection ) conControl.getMock(); 467 468 sf2.openSession(); 469 sf2Control.setReturnValue(session2, 1); 470 session2.beginTransaction(); 471 session2Control.setReturnValue(tx, 1); 472 session2.connection(); 473 session2Control.setReturnValue(con, 2); 474 tx.commit(); 475 txControl.setVoidCallable(1); 476 con.isReadOnly(); 477 conControl.setReturnValue(false, 1); 478 sf2Control.replay(); 479 session2Control.replay(); 480 txControl.replay(); 481 conControl.replay(); 482 483 MockServletContext sc = new MockServletContext(); 484 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 485 wac.setServletContext(sc); 486 wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); 487 wac.getDefaultListableBeanFactory().registerSingleton("mySessionFactory", sf2); 488 wac.refresh(); 489 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 490 MockHttpServletRequest request = new MockHttpServletRequest(sc); 491 MockHttpServletResponse response = new MockHttpServletResponse(); 492 493 MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); 494 MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); 495 filterConfig.addInitParameter("singleSession", "false"); 496 filterConfig2.addInitParameter("singleSession", "false"); 497 filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory"); 498 499 final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); 500 filter.init(filterConfig); 501 final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter(); 502 filter2.init(filterConfig2); 503 504 final FilterChain filterChain = new FilterChain () { 505 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 506 throws IOException , ServletException { 507 HibernateTransactionManager tm = new HibernateTransactionManager(sf); 508 TransactionStatus ts = tm.getTransaction( 509 new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); 510 Session sess = SessionFactoryUtils.getSession(sf, true); 511 SessionFactoryUtils.releaseSession(sess, sf); 512 tm.commit(ts); 513 514 sessionControl.verify(); 515 sessionControl.reset(); 516 try { 517 session.close(); 518 } 519 catch (HibernateException ex) { 520 } 521 sessionControl.setReturnValue(null, 1); 522 sessionControl.replay(); 523 524 servletRequest.setAttribute("invoked", Boolean.TRUE); 525 } 526 }; 527 528 final FilterChain filterChain2 = new FilterChain () { 529 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 530 throws IOException , ServletException { 531 HibernateTransactionManager tm = new HibernateTransactionManager(sf2); 532 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 533 tm.commit(ts); 534 535 session2Control.verify(); 536 session2Control.reset(); 537 try { 538 session2.close(); 539 } 540 catch (HibernateException ex) { 541 } 542 session2Control.setReturnValue(null, 1); 543 session2Control.replay(); 544 545 filter.doFilter(servletRequest, servletResponse, filterChain); 546 } 547 }; 548 549 FilterChain filterChain3 = new FilterChain () { 550 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 551 throws IOException , ServletException { 552 filter2.doFilter(servletRequest, servletResponse, filterChain2); 553 } 554 }; 555 556 filter2.doFilter(request, response, filterChain3); 557 assertNotNull(request.getAttribute("invoked")); 558 559 sfControl.verify(); 560 sessionControl.verify(); 561 sf2Control.verify(); 562 session2Control.verify(); 563 txControl.verify(); 564 conControl.verify(); 565 566 wac.close(); 567 } 568 569 public void testOpenSessionInViewFilterWithDeferredCloseAndActiveDeferredClose() throws Exception { 570 MockControl sfControl = MockControl.createControl(SessionFactory.class); 571 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 572 final MockControl sessionControl = MockControl.createControl(Session.class); 573 final Session session = (Session) sessionControl.getMock(); 574 575 sf.openSession(); 576 sfControl.setReturnValue(session, 1); 577 session.getSessionFactory(); 578 sessionControl.setReturnValue(sf); 579 session.getFlushMode(); 580 sessionControl.setReturnValue(FlushMode.NEVER, 1); 581 sfControl.replay(); 582 sessionControl.replay(); 583 584 MockServletContext sc = new MockServletContext(); 585 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 586 wac.setServletContext(sc); 587 wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); 588 wac.refresh(); 589 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 590 MockHttpServletRequest request = new MockHttpServletRequest(sc); 591 MockHttpServletResponse response = new MockHttpServletResponse(); 592 593 MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); 594 MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); 595 filterConfig.addInitParameter("singleSession", "false"); 596 filterConfig2.addInitParameter("singleSession", "false"); 597 filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory"); 598 599 OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); 600 interceptor.setSessionFactory(sf); 601 interceptor.setSingleSession(false); 602 interceptor.preHandle(request, response, "handler"); 603 604 final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); 605 filter.init(filterConfig); 606 final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter(); 607 filter2.init(filterConfig2); 608 609 final FilterChain filterChain = new FilterChain () { 610 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 611 throws IOException , ServletException { 612 HibernateTransactionManager tm = new HibernateTransactionManager(sf); 613 TransactionStatus ts = tm.getTransaction( 614 new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); 615 Session sess = SessionFactoryUtils.getSession(sf, true); 616 SessionFactoryUtils.releaseSession(sess, sf); 617 tm.commit(ts); 618 619 sessionControl.verify(); 620 sessionControl.reset(); 621 try { 622 session.close(); 623 } 624 catch (HibernateException ex) { 625 } 626 sessionControl.setReturnValue(null, 1); 627 sessionControl.replay(); 628 629 servletRequest.setAttribute("invoked", Boolean.TRUE); 630 } 631 }; 632 633 FilterChain filterChain2 = new FilterChain () { 634 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 635 throws IOException , ServletException { 636 filter.doFilter(servletRequest, servletResponse, filterChain); 637 } 638 }; 639 640 filter.doFilter(request, response, filterChain2); 641 assertNotNull(request.getAttribute("invoked")); 642 643 interceptor.postHandle(request, response, "handler", null); 644 interceptor.afterCompletion(request, response, "handler", null); 645 646 sfControl.verify(); 647 sessionControl.verify(); 648 649 wac.close(); 650 } 651 652 } 653 | Popular Tags |