001: /*
002: * $Id: TokenInterceptorTest.java 476642 2006-11-18 22:40:18Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.interceptor;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028:
029: import org.apache.struts2.ServletActionContext;
030: import org.apache.struts2.StrutsTestCase;
031: import org.apache.struts2.TestConfigurationProvider;
032: import org.apache.struts2.util.TokenHelper;
033: import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
034: import org.apache.struts2.views.jsp.StrutsMockHttpSession;
035:
036: import com.opensymphony.xwork2.Action;
037: import com.opensymphony.xwork2.ActionContext;
038: import com.opensymphony.xwork2.ActionProxy;
039: import com.opensymphony.xwork2.ActionProxyFactory;
040: import com.opensymphony.xwork2.config.ConfigurationManager;
041: import com.opensymphony.xwork2.util.ValueStack;
042: import com.opensymphony.xwork2.util.ValueStackFactory;
043:
044: /**
045: * TokenInterceptorTest
046: */
047: public class TokenInterceptorTest extends StrutsTestCase {
048:
049: ActionContext oldContext;
050: HttpSession httpSession;
051: Map extraContext;
052: Map params;
053: Map session;
054: StrutsMockHttpServletRequest request;
055:
056: public void testNoTokenInParams() throws Exception {
057: ActionProxy proxy = buildProxy(getActionName());
058: assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy
059: .execute());
060: }
061:
062: public void testNoTokenInSession() throws Exception {
063: assertEquals(oldContext, ActionContext.getContext());
064:
065: ActionProxy proxy = buildProxy(getActionName());
066: setToken(request);
067: ActionContext.getContext().getSession().clear();
068: assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy
069: .execute());
070: }
071:
072: public void testTokenInterceptorSuccess() throws Exception {
073: ActionProxy proxy = buildProxy(getActionName());
074: setToken(request);
075: assertEquals(Action.SUCCESS, proxy.execute());
076: }
077:
078: public void testCAllExecute2Times() throws Exception {
079: ActionProxy proxy = buildProxy(getActionName());
080: setToken(request);
081: assertEquals(Action.SUCCESS, proxy.execute());
082:
083: ActionProxy proxy2 = buildProxy(getActionName());
084: // must not call setToken
085: // double post will result in a invalid.token return code
086: assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy2
087: .execute());
088: }
089:
090: protected String getActionName() {
091: return TestConfigurationProvider.TOKEN_ACTION_NAME;
092: }
093:
094: protected String setToken(HttpServletRequest request) {
095: String token = TokenHelper.setToken();
096: setToken(token);
097:
098: return token;
099: }
100:
101: protected void setToken(String token) {
102: request.getParameterMap().put(TokenHelper.TOKEN_NAME_FIELD,
103: new String[] { TokenHelper.DEFAULT_TOKEN_NAME });
104: request.getParameterMap().put(TokenHelper.DEFAULT_TOKEN_NAME,
105: new String[] { token });
106: }
107:
108: protected void setUp() throws Exception {
109: loadConfigurationProviders(new TestConfigurationProvider());
110:
111: session = new HashMap();
112: params = new HashMap();
113: extraContext = new HashMap();
114: extraContext.put(ActionContext.SESSION, session);
115: extraContext.put(ActionContext.PARAMETERS, params);
116:
117: request = new StrutsMockHttpServletRequest();
118: httpSession = new StrutsMockHttpSession();
119: request.setSession(httpSession);
120: request.setParameterMap(params);
121: extraContext.put(ServletActionContext.HTTP_REQUEST, request);
122:
123: ValueStack stack = ValueStackFactory.getFactory()
124: .createValueStack();
125: stack.getContext().putAll(extraContext);
126: oldContext = new ActionContext(stack.getContext());
127: ActionContext.setContext(oldContext);
128: }
129:
130: protected ActionProxy buildProxy(String actionName)
131: throws Exception {
132: return actionProxyFactory.createActionProxy("", actionName,
133: extraContext, true, true);
134: }
135:
136: protected void tearDown() throws Exception {
137: configurationManager.destroyConfiguration();
138: ActionContext.setContext(null);
139: }
140: }
|