FileDocCategorySizeDatePackage
ServletConfigInterceptorTest.javaAPI DocExample8122Mon Jul 23 13:26:18 BST 2007org.apache.struts2.interceptor

ServletConfigInterceptorTest

public class ServletConfigInterceptorTest extends org.apache.struts2.StrutsTestCase
Unit test for {@link ServletConfigInterceptor}.

Fields Summary
private ServletConfigInterceptor
interceptor
Constructors Summary
Methods Summary
private com.opensymphony.xwork2.mock.MockActionInvocationcreateActionInvocation(java.lang.Object mock)

        MockActionInvocation mai = new MockActionInvocation();
        mai.setResultCode("success");
        mai.setInvocationContext(ActionContext.getContext());
        mai.setAction(mock);
        return mai;
    
protected voidsetUp()

        super.setUp();
        interceptor = new ServletConfigInterceptor();
        interceptor.init();
    
protected voidtearDown()

        super.tearDown();
        interceptor.destroy();
        interceptor = null;
    
public voidtestApplicationAware()

        MockControl control = MockControl.createControl(ApplicationAware.class);
        ApplicationAware mock = (ApplicationAware) control.getMock();

        MockActionInvocation mai = createActionInvocation(mock);

        Map app = new HashMap();
        mai.getInvocationContext().setApplication(app);

        mock.setApplication(app);
        control.setVoidCallable();

        control.replay();
        interceptor.intercept(mai);
        control.verify();
    
public voidtestParameterAware()

        MockControl control = MockControl.createControl(ParameterAware.class);
        ParameterAware mock = (ParameterAware) control.getMock();

        MockActionInvocation mai = createActionInvocation(mock);

        Map param = new HashMap();
        mai.getInvocationContext().setParameters(param);

        mock.setParameters(param);
        control.setVoidCallable();

        control.replay();
        interceptor.intercept(mai);
        control.verify();
    
public voidtestPrincipalAware()

        MockControl control = MockControl.createControl(PrincipalAware.class);
        control.setDefaultMatcher(MockControl.ALWAYS_MATCHER); // less strick match is needed for this unit test to be conducted using mocks
        PrincipalAware mock = (PrincipalAware) control.getMock();

        MockActionInvocation mai = createActionInvocation(mock);

        MockServletContext ctx = new MockServletContext();
        mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);

        mock.setPrincipalProxy(null); // we can do this because of ALWAYS_MATCHER
        control.setVoidCallable();

        control.replay();
        interceptor.intercept(mai);
        control.verify();
    
public voidtestPrincipalProxy()

        // uni test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class
        MockHttpServletRequest req = new MockHttpServletRequest();
        req.setUserPrincipal(null);
        req.setRemoteUser("Santa");

        MyPrincipalAction action = new MyPrincipalAction();
        MockActionInvocation mai = createActionInvocation(action);
        mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);

        assertNull(action.getProxy());
        interceptor.intercept(mai);
        assertNotNull(action.getProxy());

        PrincipalProxy proxy = action.getProxy();
        assertEquals(proxy.getRequest(), req);
        assertNull(proxy.getUserPrincipal());
        assertTrue(! proxy.isRequestSecure());
        assertTrue(! proxy.isUserInRole("no.role"));
        assertEquals("Santa", proxy.getRemoteUser());

    
public voidtestServletContextAware()

        MockControl control = MockControl.createControl(ServletContextAware.class);
        ServletContextAware mock = (ServletContextAware) control.getMock();

        MockActionInvocation mai = createActionInvocation(mock);

        MockServletContext ctx = new MockServletContext();
        mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);

        mock.setServletContext((ServletContext) ctx);
        control.setVoidCallable();

        control.replay();
        interceptor.intercept(mai);
        control.verify();
    
public voidtestServletRequestAware()

        MockControl control = MockControl.createControl(ServletRequestAware.class);
        ServletRequestAware mock = (ServletRequestAware) control.getMock();

        MockHttpServletRequest req = new MockHttpServletRequest();

        MockActionInvocation mai = createActionInvocation(mock);
        mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);

        mock.setServletRequest((HttpServletRequest) req);
        control.setVoidCallable();

        control.replay();
        interceptor.intercept(mai);
        control.verify();
    
public voidtestServletResponseAware()

        MockControl control = MockControl.createControl(ServletResponseAware.class);
        ServletResponseAware mock = (ServletResponseAware) control.getMock();

        MockHttpServletResponse res = new MockHttpServletResponse();

        MockActionInvocation mai = createActionInvocation(mock);
        mai.getInvocationContext().put(StrutsStatics.HTTP_RESPONSE, res);

        mock.setServletResponse((HttpServletResponse) res);
        control.setVoidCallable();

        control.replay();
        interceptor.intercept(mai);
        control.verify();
    
public voidtestSessionAware()

        MockControl control = MockControl.createControl(SessionAware.class);
        SessionAware mock = (SessionAware) control.getMock();

        MockActionInvocation mai = createActionInvocation(mock);

        Map session = new HashMap();
        mai.getInvocationContext().setSession(session);

        mock.setSession(session);
        control.setVoidCallable();

        control.replay();
        interceptor.intercept(mai);
        control.verify();