View Javadoc

1   /*
2    * $Id: TokenHelperTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.util;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  import com.opensymphony.xwork2.ActionContext;
26  
27  
28  /***
29   * TokenHelperTest
30   *
31   */
32  public class TokenHelperTest extends TestCase {
33  
34      private Map session;
35  
36  
37      public void testSetToken() {
38          String token = TokenHelper.setToken();
39          assertEquals(token, session.get(TokenHelper.DEFAULT_TOKEN_NAME));
40      }
41  
42      public void testSetTokenWithName() {
43          String tokenName = "myTestToken";
44          String token = TokenHelper.setToken(tokenName);
45          assertEquals(token, session.get(tokenName));
46      }
47  
48      public void testValidToken() {
49          String tokenName = "validTokenTest";
50          String token = TokenHelper.setToken(tokenName);
51          assertEquals(token, session.get(tokenName));
52          ActionContext.getContext().getParameters().put(TokenHelper.TOKEN_NAME_FIELD, new String[]{tokenName});
53          ActionContext.getContext().getParameters().put(tokenName, new String[]{token});
54          assertTrue(TokenHelper.validToken());
55      }
56  
57      protected void setUp() throws Exception {
58          session = new HashMap();
59          Map params = new HashMap();
60          Map ctxMap = new HashMap();
61          ctxMap.put(ActionContext.SESSION, session);
62          ctxMap.put(ActionContext.PARAMETERS, params);
63          ActionContext ctx = new ActionContext(ctxMap);
64          ActionContext.setContext(ctx);
65      }
66  
67      protected void tearDown() {
68          ActionContext.setContext(null);
69      }
70  }
71