View Javadoc

1   /*
2    * $Id: TokenHelperTest.java 781805 2009-06-04 17:33:00Z wesw $
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  
22  package org.apache.struts2.util;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  import junit.framework.TestCase;
29  
30  import com.opensymphony.xwork2.ActionContext;
31  
32  
33  /***
34   * TokenHelperTest
35   *
36   */
37  public class TokenHelperTest extends TestCase {
38  
39      private Map session;
40  
41  
42      public void testSetToken() {
43          String token = TokenHelper.setToken();
44          assertEquals(token, session.get(TokenHelper.DEFAULT_TOKEN_NAME));
45      }
46  
47      public void testSetTokenWithName() {
48          String tokenName = "myTestToken";
49          String token = TokenHelper.setToken(tokenName);
50          assertEquals(token, session.get(tokenName));
51      }
52  
53      public void testValidToken() {
54          String tokenName = "validTokenTest";
55          String token = TokenHelper.setToken(tokenName);
56          assertEquals(token, session.get(tokenName));
57          ActionContext.getContext().getParameters().put(TokenHelper.TOKEN_NAME_FIELD, new String[]{tokenName});
58          ActionContext.getContext().getParameters().put(tokenName, new String[]{token});
59          assertTrue(TokenHelper.validToken());
60      }
61  
62      public void testGetTokenDoesNotNpe() {
63          String token = TokenHelper.getToken(null);
64          assertTrue(token == null);
65  
66          String token2 = TokenHelper.getToken("");
67          assertTrue(token2 == null);
68      }
69  
70      protected void setUp() throws Exception {
71          session = new HashMap();
72          Map params = new TreeMap();
73          Map ctxMap = new TreeMap();
74          ctxMap.put(ActionContext.SESSION, session);
75          ctxMap.put(ActionContext.PARAMETERS, params);
76          ActionContext ctx = new ActionContext(ctxMap);
77          ActionContext.setContext(ctx);
78      }
79  
80      protected void tearDown() {
81          ActionContext.setContext(null);
82      }
83  }
84