View Javadoc

    1   /*
    2    * $Id: TokenInterceptorTest.java 476642 2006-11-18 22:40:18Z mrdon $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.interceptor;
    22
    23  import java.util.HashMap;
    24  import java.util.Map;
    25
    26  import javax.servlet.http.HttpServletRequest;
    27  import javax.servlet.http.HttpSession;
    28
    29  import org.apache.struts2.ServletActionContext;
    30  import org.apache.struts2.StrutsTestCase;
    31  import org.apache.struts2.TestConfigurationProvider;
    32  import org.apache.struts2.util.TokenHelper;
    33  import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
    34  import org.apache.struts2.views.jsp.StrutsMockHttpSession;
    35
    36  import com.opensymphony.xwork2.Action;
    37  import com.opensymphony.xwork2.ActionContext;
    38  import com.opensymphony.xwork2.ActionProxy;
    39  import com.opensymphony.xwork2.ActionProxyFactory;
    40  import com.opensymphony.xwork2.config.ConfigurationManager;
    41  import com.opensymphony.xwork2.util.ValueStack;
    42  import com.opensymphony.xwork2.util.ValueStackFactory;
    43
    44
    45  /***
    46   * TokenInterceptorTest
    47   */
    48  public class TokenInterceptorTest extends StrutsTestCase {
    49
    50      ActionContext oldContext;
    51      HttpSession httpSession;
    52      Map extraContext;
    53      Map params;
    54      Map session;
    55      StrutsMockHttpServletRequest request;
    56
    57
    58      public void testNoTokenInParams() throws Exception {
    59          ActionProxy proxy = buildProxy(getActionName());
    60          assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
    61      }
    62
    63      public void testNoTokenInSession() throws Exception {
    64          assertEquals(oldContext, ActionContext.getContext());
    65
    66          ActionProxy proxy = buildProxy(getActionName());
    67          setToken(request);
    68          ActionContext.getContext().getSession().clear();
    69          assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
    70      }
    71
    72      public void testTokenInterceptorSuccess() throws Exception {
    73          ActionProxy proxy = buildProxy(getActionName());
    74          setToken(request);
    75          assertEquals(Action.SUCCESS, proxy.execute());
    76      }
    77
    78      public void testCAllExecute2Times() throws Exception {
    79          ActionProxy proxy = buildProxy(getActionName());
    80          setToken(request);
    81          assertEquals(Action.SUCCESS, proxy.execute());
    82
    83          ActionProxy proxy2 = buildProxy(getActionName());
    84          // must not call setToken
    85          // double post will result in a invalid.token return code
    86          assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy2.execute());
    87      }
    88
    89      protected String getActionName() {
    90          return TestConfigurationProvider.TOKEN_ACTION_NAME;
    91      }
    92
    93      protected String setToken(HttpServletRequest request) {
    94          String token = TokenHelper.setToken();
    95          setToken(token);
    96
    97          return token;
    98      }
    99
    100     protected void setToken(String token) {
    101         request.getParameterMap().put(TokenHelper.TOKEN_NAME_FIELD, new String[]{
    102                 TokenHelper.DEFAULT_TOKEN_NAME
    103         });
    104         request.getParameterMap().put(TokenHelper.DEFAULT_TOKEN_NAME, new String[]{
    105                 token
    106         });
    107     }
    108
    109     protected void setUp() throws Exception {
    110         loadConfigurationProviders(new TestConfigurationProvider());
    111
    112         session = new HashMap();
    113         params = new HashMap();
    114         extraContext = new HashMap();
    115         extraContext.put(ActionContext.SESSION, session);
    116         extraContext.put(ActionContext.PARAMETERS, params);
    117
    118         request = new StrutsMockHttpServletRequest();
    119         httpSession = new StrutsMockHttpSession();
    120         request.setSession(httpSession);
    121         request.setParameterMap(params);
    122         extraContext.put(ServletActionContext.HTTP_REQUEST, request);
    123
    124         ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    125         stack.getContext().putAll(extraContext);
    126         oldContext = new ActionContext(stack.getContext());
    127         ActionContext.setContext(oldContext);
    128     }
    129
    130     protected ActionProxy buildProxy(String actionName) throws Exception {
    131         return actionProxyFactory.createActionProxy("", actionName, extraContext, true, true);
    132     }
    133
    134     protected void tearDown() throws Exception {
    135         configurationManager.destroyConfiguration();
    136         ActionContext.setContext(null);
    137     }
    138 }