View Javadoc

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