001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import com.opensymphony.webwork.ServletActionContext;
008: import com.opensymphony.webwork.TestConfigurationProvider;
009: import com.opensymphony.webwork.util.TokenHelper;
010: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest;
011: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpSession;
012: import com.opensymphony.xwork.Action;
013: import com.opensymphony.xwork.ActionContext;
014: import com.opensymphony.xwork.ActionProxy;
015: import com.opensymphony.xwork.ActionProxyFactory;
016: import com.opensymphony.xwork.config.ConfigurationManager;
017: import com.opensymphony.xwork.util.OgnlValueStack;
018: import junit.framework.TestCase;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpSession;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: /**
026: * TokenInterceptorTest
027: *
028: * @author Jason Carreira
029: * Created Apr 9, 2003 11:42:01 PM
030: */
031: public class TokenInterceptorTest extends TestCase {
032:
033: ActionContext oldContext;
034: HttpSession httpSession;
035: Map extraContext;
036: Map params;
037: Map session;
038: WebWorkMockHttpServletRequest request;
039:
040: public void testNoTokenInParams() throws Exception {
041: ActionProxy proxy = buildProxy(getActionName());
042: assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy
043: .execute());
044: }
045:
046: public void testNoTokenInSession() throws Exception {
047: assertEquals(oldContext, ActionContext.getContext());
048:
049: ActionProxy proxy = buildProxy(getActionName());
050: setToken(request);
051: ActionContext.getContext().getSession().clear();
052: assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy
053: .execute());
054: }
055:
056: public void testTokenInterceptorSuccess() throws Exception {
057: ActionProxy proxy = buildProxy(getActionName());
058: setToken(request);
059: assertEquals(Action.SUCCESS, proxy.execute());
060: }
061:
062: public void testCAllExecute2Times() throws Exception {
063: ActionProxy proxy = buildProxy(getActionName());
064: setToken(request);
065: assertEquals(Action.SUCCESS, proxy.execute());
066:
067: ActionProxy proxy2 = buildProxy(getActionName());
068: // must not call setToken
069: // double post will result in a invalid.token return code
070: assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy2
071: .execute());
072: }
073:
074: protected String getActionName() {
075: return TestConfigurationProvider.TOKEN_ACTION_NAME;
076: }
077:
078: protected String setToken(HttpServletRequest request) {
079: String token = TokenHelper.setToken();
080: setToken(token);
081:
082: return token;
083: }
084:
085: protected void setToken(String token) {
086: request.getParameterMap().put(TokenHelper.TOKEN_NAME_FIELD,
087: new String[] { TokenHelper.DEFAULT_TOKEN_NAME });
088: request.getParameterMap().put(TokenHelper.DEFAULT_TOKEN_NAME,
089: new String[] { token });
090: }
091:
092: protected void setUp() throws Exception {
093: ConfigurationManager.clearConfigurationProviders();
094: ConfigurationManager
095: .addConfigurationProvider(new TestConfigurationProvider());
096: ConfigurationManager.getConfiguration().reload();
097:
098: session = new HashMap();
099: params = new HashMap();
100: extraContext = new HashMap();
101: extraContext.put(ActionContext.SESSION, session);
102: extraContext.put(ActionContext.PARAMETERS, params);
103:
104: request = new WebWorkMockHttpServletRequest();
105: httpSession = new WebWorkMockHttpSession();
106: request.setSession(httpSession);
107: request.setParameterMap(params);
108: extraContext.put(ServletActionContext.HTTP_REQUEST, request);
109:
110: OgnlValueStack stack = new OgnlValueStack();
111: stack.getContext().putAll(extraContext);
112: oldContext = new ActionContext(stack.getContext());
113: ActionContext.setContext(oldContext);
114: }
115:
116: protected ActionProxy buildProxy(String actionName)
117: throws Exception {
118: return ActionProxyFactory.getFactory().createActionProxy("",
119: actionName, extraContext, true, true);
120: }
121:
122: protected void tearDown() throws Exception {
123: ConfigurationManager.destroyConfiguration();
124: ActionContext.setContext(null);
125: }
126: }
|