1 5 package com.opensymphony.webwork.interceptor; 6 7 import com.opensymphony.webwork.ServletActionContext; 8 import com.opensymphony.webwork.TestConfigurationProvider; 9 import com.opensymphony.webwork.util.TokenHelper; 10 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest; 11 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpSession; 12 import com.opensymphony.xwork.Action; 13 import com.opensymphony.xwork.ActionContext; 14 import com.opensymphony.xwork.ActionProxy; 15 import com.opensymphony.xwork.ActionProxyFactory; 16 import com.opensymphony.xwork.config.ConfigurationManager; 17 import com.opensymphony.xwork.util.OgnlValueStack; 18 import junit.framework.TestCase; 19 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpSession ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 26 32 public class TokenInterceptorTest extends TestCase { 33 35 ActionContext oldContext; 36 HttpSession httpSession; 37 Map extraContext; 38 Map params; 39 Map session; 40 WebWorkMockHttpServletRequest request; 41 42 44 public void testNoTokenInParams() { 45 try { 46 ActionProxy proxy = buildProxy(getActionName()); 47 assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute()); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 fail(); 51 } 52 } 53 54 public void testNoTokenInSession() throws Exception { 55 assertEquals(oldContext, ActionContext.getContext()); 56 57 ActionProxy proxy = buildProxy(getActionName()); 58 setToken(request); 59 request.setSession(null); 60 assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute()); 61 } 62 63 public void testTokenInterceptorSuccess() throws Exception { 64 ActionProxy proxy = buildProxy(getActionName()); 65 setToken(request); 66 assertEquals(Action.SUCCESS, proxy.execute()); 67 } 68 69 protected String getActionName() { 70 return TestConfigurationProvider.TOKEN_ACTION_NAME; 71 } 72 73 protected String setToken(HttpServletRequest request) { 74 String token = TokenHelper.setToken(request); 75 setToken(token); 76 77 return token; 78 } 79 80 protected void setToken(String token) { 81 request.getParameterMap().put(TokenHelper.TOKEN_NAME_FIELD, new String []{ 82 TokenHelper.DEFAULT_TOKEN_NAME 83 }); 84 request.getParameterMap().put(TokenHelper.DEFAULT_TOKEN_NAME, new String []{ 85 token 86 }); 87 } 88 89 protected void setUp() throws Exception { 90 ConfigurationManager.clearConfigurationProviders(); 91 ConfigurationManager.addConfigurationProvider(new TestConfigurationProvider()); 92 ConfigurationManager.getConfiguration().reload(); 93 94 session = new HashMap (); 95 params = new HashMap (); 96 extraContext = new HashMap (); 97 extraContext.put(ActionContext.SESSION, session); 98 extraContext.put(ActionContext.PARAMETERS, params); 99 100 request = new WebWorkMockHttpServletRequest(); 101 httpSession = new WebWorkMockHttpSession(); 102 request.setSession(httpSession); 103 request.setParameterMap(params); 104 extraContext.put(ServletActionContext.HTTP_REQUEST, request); 105 106 OgnlValueStack stack = new OgnlValueStack(); 107 stack.getContext().putAll(extraContext); 108 oldContext = new ActionContext(stack.getContext()); 109 ActionContext.setContext(oldContext); 110 } 111 112 protected ActionProxy buildProxy(String actionName) throws Exception { 113 return ActionProxyFactory.getFactory().createActionProxy("", actionName, extraContext, true); 114 } 115 116 protected void tearDown() throws Exception { 117 ConfigurationManager.destroyConfiguration(); 118 ActionContext.setContext(null); 119 } 120 } 121 | Popular Tags |