View Javadoc

1   /*
2    * $Id: TokenInterceptorTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.interceptor;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpSession;
29  
30  import org.apache.struts2.ServletActionContext;
31  import org.apache.struts2.StrutsTestCase;
32  import org.apache.struts2.TestConfigurationProvider;
33  import org.apache.struts2.util.TokenHelper;
34  import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
35  import org.apache.struts2.views.jsp.StrutsMockHttpSession;
36  
37  import com.opensymphony.xwork2.Action;
38  import com.opensymphony.xwork2.ActionContext;
39  import com.opensymphony.xwork2.ActionProxy;
40  import com.opensymphony.xwork2.ActionProxyFactory;
41  import com.opensymphony.xwork2.config.ConfigurationManager;
42  import com.opensymphony.xwork2.util.ValueStack;
43  import com.opensymphony.xwork2.util.ValueStackFactory;
44  
45  
46  /***
47   * TokenInterceptorTest
48   */
49  public class TokenInterceptorTest extends StrutsTestCase {
50  
51      ActionContext oldContext;
52      HttpSession httpSession;
53      Map extraContext;
54      Map params;
55      Map session;
56      StrutsMockHttpServletRequest request;
57  
58  
59      public void testNoTokenInParams() throws Exception {
60          ActionProxy proxy = buildProxy(getActionName());
61          assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
62      }
63  
64      public void testNoTokenInSession() throws Exception {
65          assertEquals(oldContext, ActionContext.getContext());
66  
67          ActionProxy proxy = buildProxy(getActionName());
68          setToken(request);
69          ActionContext.getContext().getSession().clear();
70          assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
71      }
72  
73      public void testTokenInterceptorSuccess() throws Exception {
74          ActionProxy proxy = buildProxy(getActionName());
75          setToken(request);
76          assertEquals(Action.SUCCESS, proxy.execute());
77      }
78  
79      public void testCAllExecute2Times() throws Exception {
80          ActionProxy proxy = buildProxy(getActionName());
81          setToken(request);
82          assertEquals(Action.SUCCESS, proxy.execute());
83  
84          ActionProxy proxy2 = buildProxy(getActionName());
85          // must not call setToken
86          // double post will result in a invalid.token return code
87          assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy2.execute());
88      }
89  
90      protected String getActionName() {
91          return TestConfigurationProvider.TOKEN_ACTION_NAME;
92      }
93  
94      protected String setToken(HttpServletRequest request) {
95          String token = TokenHelper.setToken();
96          setToken(token);
97  
98          return token;
99      }
100 
101     protected void setToken(String token) {
102         request.getParameterMap().put(TokenHelper.TOKEN_NAME_FIELD, new String[]{
103                 TokenHelper.DEFAULT_TOKEN_NAME
104         });
105         request.getParameterMap().put(TokenHelper.DEFAULT_TOKEN_NAME, new String[]{
106                 token
107         });
108     }
109 
110     protected void setUp() throws Exception {
111         loadConfigurationProviders(new TestConfigurationProvider());
112 
113         session = new HashMap();
114         params = new HashMap();
115         extraContext = new HashMap();
116         extraContext.put(ActionContext.SESSION, session);
117         extraContext.put(ActionContext.PARAMETERS, params);
118 
119         request = new StrutsMockHttpServletRequest();
120         httpSession = new StrutsMockHttpSession();
121         request.setSession(httpSession);
122         request.setParameterMap(params);
123         extraContext.put(ServletActionContext.HTTP_REQUEST, request);
124 
125         ValueStack stack = ActionContext.getContext().getValueStack();
126         stack.getContext().putAll(extraContext);
127         oldContext = new ActionContext(stack.getContext());
128         ActionContext.setContext(oldContext);
129     }
130 
131     protected ActionProxy buildProxy(String actionName) throws Exception {
132         return actionProxyFactory.createActionProxy("", actionName, extraContext, true, true);
133     }
134 }