View Javadoc

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