View Javadoc

1   /*
2    * $Id: TokenHelperTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
27  import junit.framework.TestCase;
28  
29  import com.opensymphony.xwork2.ActionContext;
30  
31  
32  /***
33   * TokenHelperTest
34   *
35   */
36  public class TokenHelperTest extends TestCase {
37  
38      private Map session;
39  
40  
41      public void testSetToken() {
42          String token = TokenHelper.setToken();
43          assertEquals(token, session.get(TokenHelper.DEFAULT_TOKEN_NAME));
44      }
45  
46      public void testSetTokenWithName() {
47          String tokenName = "myTestToken";
48          String token = TokenHelper.setToken(tokenName);
49          assertEquals(token, session.get(tokenName));
50      }
51  
52      public void testValidToken() {
53          String tokenName = "validTokenTest";
54          String token = TokenHelper.setToken(tokenName);
55          assertEquals(token, session.get(tokenName));
56          ActionContext.getContext().getParameters().put(TokenHelper.TOKEN_NAME_FIELD, new String[]{tokenName});
57          ActionContext.getContext().getParameters().put(tokenName, new String[]{token});
58          assertTrue(TokenHelper.validToken());
59      }
60  
61      protected void setUp() throws Exception {
62          session = new HashMap();
63          Map params = new HashMap();
64          Map ctxMap = new HashMap();
65          ctxMap.put(ActionContext.SESSION, session);
66          ctxMap.put(ActionContext.PARAMETERS, params);
67          ActionContext ctx = new ActionContext(ctxMap);
68          ActionContext.setContext(ctx);
69      }
70  
71      protected void tearDown() {
72          ActionContext.setContext(null);
73      }
74  }
75