KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > TokenHelperTest


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

5 package com.opensymphony.webwork.util;
6
7 import com.mockobjects.dynamic.C;
8 import com.mockobjects.dynamic.Mock;
9 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpSession;
10 import junit.framework.TestCase;
11
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpSession JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * TokenHelperTest
20  *
21  * @author Jason Carreira
22  * Created Apr 3, 2003 10:13:08 AM
23  */

24 public class TokenHelperTest extends TestCase {
25     //~ Instance fields ////////////////////////////////////////////////////////
26

27     private HttpServletRequest JavaDoc request;
28     private HttpSession session;
29     private Mock mockRequest;
30
31     //~ Methods ////////////////////////////////////////////////////////////////
32

33     public void testSetToken() {
34         String JavaDoc token = TokenHelper.setToken(request);
35         assertEquals(token, session.getAttribute(TokenHelper.DEFAULT_TOKEN_NAME));
36     }
37
38     public void testSetTokenWithName() {
39         String JavaDoc tokenName = "myTestToken";
40         String JavaDoc token = TokenHelper.setToken(tokenName, request);
41         assertEquals(token, session.getAttribute(tokenName));
42     }
43
44     public void testValidToken() {
45         String JavaDoc tokenName = "validTokenTest";
46         Map JavaDoc params = new HashMap JavaDoc();
47
48         String JavaDoc token = TokenHelper.setToken(tokenName, request);
49         assertEquals(token, session.getAttribute(tokenName));
50         params.put(TokenHelper.TOKEN_NAME_FIELD, new String JavaDoc[]{tokenName});
51         params.put(tokenName, new String JavaDoc[]{token});
52         mockRequest.matchAndReturn("getParameterMap", params);
53         assertTrue(TokenHelper.validToken(request));
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         session = new WebWorkMockHttpSession();
58         mockRequest = new Mock(HttpServletRequest JavaDoc.class);
59         mockRequest.matchAndReturn("getSession", C.ANY_ARGS, session);
60         request = (HttpServletRequest JavaDoc) mockRequest.proxy();
61     }
62
63     protected void tearDown() {
64         mockRequest.verify();
65     }
66 }
67
Popular Tags