KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > interceptor > TokenInterceptorTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25
26 /**
27  * TokenInterceptorTest
28  *
29  * @author Jason Carreira
30  * Created Apr 9, 2003 11:42:01 PM
31  */

32 public class TokenInterceptorTest extends TestCase {
33     //~ Instance fields ////////////////////////////////////////////////////////
34

35     ActionContext oldContext;
36     HttpSession httpSession;
37     Map JavaDoc extraContext;
38     Map JavaDoc params;
39     Map JavaDoc session;
40     WebWorkMockHttpServletRequest request;
41
42     //~ Methods ////////////////////////////////////////////////////////////////
43

44     public void testNoTokenInParams() {
45         try {
46             ActionProxy proxy = buildProxy(getActionName());
47             assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
48         } catch (Exception JavaDoc e) {
49             e.printStackTrace();
50             fail();
51         }
52     }
53
54     public void testNoTokenInSession() throws Exception JavaDoc {
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 JavaDoc {
64         ActionProxy proxy = buildProxy(getActionName());
65         setToken(request);
66         assertEquals(Action.SUCCESS, proxy.execute());
67     }
68
69     protected String JavaDoc getActionName() {
70         return TestConfigurationProvider.TOKEN_ACTION_NAME;
71     }
72
73     protected String JavaDoc setToken(HttpServletRequest request) {
74         String JavaDoc token = TokenHelper.setToken(request);
75         setToken(token);
76
77         return token;
78     }
79
80     protected void setToken(String JavaDoc token) {
81         request.getParameterMap().put(TokenHelper.TOKEN_NAME_FIELD, new String JavaDoc[]{
82             TokenHelper.DEFAULT_TOKEN_NAME
83         });
84         request.getParameterMap().put(TokenHelper.DEFAULT_TOKEN_NAME, new String JavaDoc[]{
85             token
86         });
87     }
88
89     protected void setUp() throws Exception JavaDoc {
90         ConfigurationManager.clearConfigurationProviders();
91         ConfigurationManager.addConfigurationProvider(new TestConfigurationProvider());
92         ConfigurationManager.getConfiguration().reload();
93
94         session = new HashMap JavaDoc();
95         params = new HashMap JavaDoc();
96         extraContext = new HashMap JavaDoc();
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 JavaDoc actionName) throws Exception JavaDoc {
113         return ActionProxyFactory.getFactory().createActionProxy("", actionName, extraContext, true);
114     }
115
116     protected void tearDown() throws Exception JavaDoc {
117         ConfigurationManager.destroyConfiguration();
118         ActionContext.setContext(null);
119     }
120 }
121
Popular Tags