View Javadoc

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