public void testFilterDefinitionWithInit() throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.xml");
AbstractShiroFilter shiroFilter = (AbstractShiroFilter) context.getBean("shiroFilter");
FilterConfig mockFilterConfig = createNiceMock(FilterConfig.class);
ServletContext mockServletContext = createNiceMock(ServletContext.class);
expect(mockFilterConfig.getServletContext()).andReturn(mockServletContext).anyTimes();
HttpServletRequest mockRequest = createNiceMock(HttpServletRequest.class);
expect(mockRequest.getContextPath()).andReturn("/").anyTimes();
expect(mockRequest.getRequestURI()).andReturn("/").anyTimes();
HttpServletResponse mockResponse = createNiceMock(HttpServletResponse.class);
replay(mockFilterConfig);
replay(mockServletContext);
shiroFilter.init(mockFilterConfig);
verify(mockServletContext);
verify(mockFilterConfig);
FilterChain filterChain = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
assertNotNull(request.getSession());
//this line asserts the fix for the user-reported issue:
assertNotNull(request.getSession().getServletContext());
}
};
replay(mockRequest);
replay(mockResponse);
shiroFilter.doFilter(mockRequest, mockResponse, filterChain);
verify(mockResponse);
verify(mockRequest);
}