FileDocCategorySizeDatePackage
TokenInterceptorTest.javaAPI DocExample5037Mon Jul 23 13:26:18 BST 2007org.apache.struts2.interceptor

TokenInterceptorTest

public class TokenInterceptorTest extends org.apache.struts2.StrutsTestCase
TokenInterceptorTest

Fields Summary
com.opensymphony.xwork2.ActionContext
oldContext
HttpSession
httpSession
Map
extraContext
Map
params
Map
session
org.apache.struts2.views.jsp.StrutsMockHttpServletRequest
request
Constructors Summary
Methods Summary
protected com.opensymphony.xwork2.ActionProxybuildProxy(java.lang.String actionName)

        return actionProxyFactory.createActionProxy("", actionName, extraContext, true, true);
    
protected java.lang.StringgetActionName()

        return TestConfigurationProvider.TOKEN_ACTION_NAME;
    
protected java.lang.StringsetToken(javax.servlet.http.HttpServletRequest request)

        String token = TokenHelper.setToken();
        setToken(token);

        return token;
    
protected voidsetToken(java.lang.String token)

        request.getParameterMap().put(TokenHelper.TOKEN_NAME_FIELD, new String[]{
                TokenHelper.DEFAULT_TOKEN_NAME
        });
        request.getParameterMap().put(TokenHelper.DEFAULT_TOKEN_NAME, new String[]{
                token
        });
    
protected voidsetUp()

        loadConfigurationProviders(new TestConfigurationProvider());

        session = new HashMap();
        params = new HashMap();
        extraContext = new HashMap();
        extraContext.put(ActionContext.SESSION, session);
        extraContext.put(ActionContext.PARAMETERS, params);

        request = new StrutsMockHttpServletRequest();
        httpSession = new StrutsMockHttpSession();
        request.setSession(httpSession);
        request.setParameterMap(params);
        extraContext.put(ServletActionContext.HTTP_REQUEST, request);

        ValueStack stack = ValueStackFactory.getFactory().createValueStack();
        stack.getContext().putAll(extraContext);
        oldContext = new ActionContext(stack.getContext());
        ActionContext.setContext(oldContext);
    
protected voidtearDown()

        configurationManager.destroyConfiguration();
        ActionContext.setContext(null);
    
public voidtestCAllExecute2Times()

        ActionProxy proxy = buildProxy(getActionName());
        setToken(request);
        assertEquals(Action.SUCCESS, proxy.execute());

        ActionProxy proxy2 = buildProxy(getActionName());
        // must not call setToken
        // double post will result in a invalid.token return code
        assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy2.execute());
    
public voidtestNoTokenInParams()

        ActionProxy proxy = buildProxy(getActionName());
        assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
    
public voidtestNoTokenInSession()

        assertEquals(oldContext, ActionContext.getContext());

        ActionProxy proxy = buildProxy(getActionName());
        setToken(request);
        ActionContext.getContext().getSession().clear();
        assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
    
public voidtestTokenInterceptorSuccess()

        ActionProxy proxy = buildProxy(getActionName());
        setToken(request);
        assertEquals(Action.SUCCESS, proxy.execute());