//session2Control.expectAndReturn(session2.getRepository(), repo);
sf2Control.replay();
session2Control.replay();
MockServletContext sc = new MockServletContext();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
wac.getDefaultListableBeanFactory().registerSingleton("mySessionFactory", sf2);
wac.refresh();
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory");
MockFilterConfig filterConfig3 = new MockFilterConfig(wac.getServletContext(), "filter3");
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
filter.init(filterConfig);
final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter();
filter2.init(filterConfig2);
final OpenSessionInViewFilter filter3 = new OpenSessionInViewFilter();
filter3.init(filterConfig3);
final FilterChain filterChain = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
assertTrue(TransactionSynchronizationManager.hasResource(sf));
// check sf-related things
assertSame(holder, TransactionSynchronizationManager.getResource(sf));
assertSame(session, holder.getSession());
servletRequest.setAttribute("invoked", Boolean.TRUE);
}
};
final FilterChain filterChain2 = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
assertTrue(TransactionSynchronizationManager.hasResource(sf));
// check sf-related things
assertSame(holder, TransactionSynchronizationManager.getResource(sf));
assertSame(session, holder.getSession());
filter3.doFilter(servletRequest, servletResponse, filterChain);
}
};
final FilterChain filterChain3 = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
assertTrue(TransactionSynchronizationManager.hasResource(sf2));
// check sf2-related things
assertSame(holder2, TransactionSynchronizationManager.getResource(sf2));
assertSame(session2, holder2.getSession());
filter.doFilter(servletRequest, servletResponse, filterChain2);
}
};
FilterChain filterChain4 = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
filter2.doFilter(servletRequest, servletResponse, filterChain3);
}
};
assertFalse(TransactionSynchronizationManager.hasResource(sf));
assertFalse(TransactionSynchronizationManager.hasResource(sf2));
filter2.doFilter(request, response, filterChain4);
assertFalse(TransactionSynchronizationManager.hasResource(sf));
assertFalse(TransactionSynchronizationManager.hasResource(sf2));
assertNotNull(request.getAttribute("invoked"));
sfControl.verify();
sessionControl.verify();
sf2Control.verify();
session2Control.verify();
wac.close();
}