FileDocCategorySizeDatePackage
FilterTest.javaAPI DocExample13195Mon Jul 23 13:26:12 BST 2007org.apache.struts2.dispatcher

FilterTest

public class FilterTest extends TestCase
version
$Date: 2006-12-08 14:57:59 -0500 (Fri, 08 Dec 2006) $ $Id: FilterTest.java 484717 2006-12-08 19:57:59Z mrdon $

Fields Summary
protected org.springframework.mock.web.MockFilterConfig
filterConfig
protected org.springframework.mock.web.MockHttpServletRequest
request
protected org.springframework.mock.web.MockHttpServletResponse
response
protected com.mockobjects.servlet.MockFilterChain
filterChain
protected com.mockobjects.servlet.MockFilterChain
filterChain2
protected org.springframework.mock.web.MockServletContext
servletContext
protected InnerDispatcher
_dispatcher1
protected InnerDispatcher
_dispatcher2
protected ActionContextCleanUp
cleanUp
protected FilterDispatcher
filterDispatcher
protected int
cleanUpFilterCreateDispatcherCount
protected int
filterDispatcherCreateDispatcherCount
Constructors Summary
Methods Summary
protected voidsetUp()

        Dispatcher.setInstance(null);

        filterConfig = new MockFilterConfig();
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        servletContext = new MockServletContext();

        _dispatcher1 = new InnerDispatcher(servletContext){
            @Override
            public String toString() {
                return "dispatcher1";
            }
        };
        _dispatcher2 = new InnerDispatcher(servletContext){
            @Override
            public String toString() {
                return "dispatcher2";
            }
        };
        filterChain = new MockFilterChain() {
            @Override
            public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
                filterDispatcher.doFilter(req, res, filterChain2);
            }
        };
        filterChain2 = new MockFilterChain() {
            @Override
            public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
            }
        };


        cleanUp = new ActionContextCleanUp();

        filterDispatcher = new FilterDispatcher() {
            @Override
            protected Dispatcher createDispatcher(FilterConfig filterConfig) {
                filterDispatcherCreateDispatcherCount++;
                return _dispatcher2;
            }
            
            @Override
            public String toString() {
                return "filterDispatcher";
            }
        };
    
protected voidtearDown()

 // number of times FilterDispatcher create a dispatcher


    
         
        filterConfig = null;
        request = null;
        response = null;
        filterChain = null;
        filterChain2 = null;
        servletContext = null;
        _dispatcher1 = null;
        _dispatcher2 = null;
        cleanUp = null;
        filterDispatcher = null;
    
public voidtestUsingFilterDispatcherOnly()

        assertEquals(cleanUpFilterCreateDispatcherCount, 0);
        assertEquals(filterDispatcherCreateDispatcherCount, 0);
        assertFalse(_dispatcher1.init);
        assertFalse(_dispatcher1.prepare);
        assertFalse(_dispatcher1.wrapRequest);
        assertFalse(_dispatcher1.service);
        assertFalse(_dispatcher2.init);
        assertFalse(_dispatcher2.prepare);
        assertFalse(_dispatcher2.wrapRequest);
        assertFalse(_dispatcher2.service);

        filterDispatcher.init(filterConfig);
        FilterDispatcher.setActionMapper(new FilterTest.InnerMapper());
        filterDispatcher.doFilter(request, response, filterChain2);
        filterDispatcher.destroy();

        // we are using FilterDispatcher only, so cleanUp filter's Dispatcher should not be created.
        assertEquals(cleanUpFilterCreateDispatcherCount, 0);
        assertEquals(filterDispatcherCreateDispatcherCount, 1);
        assertFalse(_dispatcher1.init);
        assertFalse(_dispatcher1.prepare);
        assertFalse(_dispatcher1.wrapRequest);
        assertFalse(_dispatcher1.service);
        assertTrue(_dispatcher2.init);
        assertTrue(_dispatcher2.prepare);
        assertTrue(_dispatcher2.wrapRequest);
        assertTrue(_dispatcher2.service);
        assertTrue(Dispatcher.getInstance() == null);
    
public voidtestUsingFilterDispatcherOnly_Multiple()

        
        filterDispatcher.setActionMapper(new FilterTest.InnerMapper());

        assertEquals(cleanUpFilterCreateDispatcherCount, 0);
        assertEquals(filterDispatcherCreateDispatcherCount, 0);
        assertFalse(_dispatcher1.prepare);
        assertFalse(_dispatcher1.wrapRequest);
        assertFalse(_dispatcher1.service);
        assertFalse(_dispatcher1.cleanUp);
        assertFalse(_dispatcher2.prepare);
        assertFalse(_dispatcher2.wrapRequest);
        assertFalse(_dispatcher2.service);
        assertFalse(_dispatcher2.cleanUp);

        filterDispatcher.init(filterConfig);
        FilterDispatcher.setActionMapper(new FilterTest.InnerMapper());
        filterDispatcher.doFilter(request, response, filterChain2);
        filterDispatcher.doFilter(request, response, filterChain2);
        filterDispatcher.destroy();

        assertEquals(cleanUpFilterCreateDispatcherCount, 0);
        // We should create dispatcher once, although filter.doFilter(...) is called  many times.
        assertEquals(filterDispatcherCreateDispatcherCount, 1);
        assertFalse(_dispatcher1.prepare);
        assertFalse(_dispatcher1.wrapRequest);
        assertFalse(_dispatcher1.service);
        assertFalse(_dispatcher1.cleanUp);
        assertTrue(_dispatcher2.prepare);
        assertTrue(_dispatcher2.wrapRequest);
        assertTrue(_dispatcher2.service);
        assertTrue(_dispatcher2.cleanUp);
        assertTrue(Dispatcher.getInstance() == null);