1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.util;
17  
18  import junit.framework.TestCase;
19  
20  import org.seasar.cubby.CubbyConstants;
21  import org.seasar.framework.mock.servlet.MockHttpSession;
22  import org.seasar.framework.mock.servlet.MockHttpSessionImpl;
23  import org.seasar.framework.mock.servlet.MockServletContextImpl;
24  import org.seasar.framework.util.LruHashMap;
25  
26  public class TokenHelperTest extends TestCase {
27  
28  	public void testGenerateGUID() throws Exception {
29  		String guid = TokenHelper.generateGUID();
30  		assertEquals("GUIDは32文字", 32, guid.length());
31  	}
32  
33  	public void testSetToken() throws Exception {
34  		String token = "tokenstring";
35  		MockHttpSession session = new MockHttpSessionImpl(new MockServletContextImpl("/cubby"));
36  		LruHashMap tokenMap = new LruHashMap(32);
37  		session.setAttribute(CubbyConstants.ATTR_TOKEN, tokenMap);
38  		TokenHelper.setToken(session, token);
39  
40  		assertTrue("トークン文字列がトークン用のMapのキーとして追加", tokenMap
41  				.containsKey("tokenstring"));
42  	}
43  
44  	public void testValidateToken() throws Exception {
45  		String token = "tokenstring";
46  		MockHttpSession session = new MockHttpSessionImpl(new MockServletContextImpl("/cubby"));
47  		LruHashMap tokenMap = new LruHashMap(32);
48  		session.setAttribute(CubbyConstants.ATTR_TOKEN, tokenMap);
49  		TokenHelper.setToken(session, token);
50  
51  		assertFalse("セッション中に存在しないトークン文字列", TokenHelper.validateToken(
52  				session, "dummytoken"));
53  		assertTrue("セッション中に存在するトークン文字列", TokenHelper.validateToken(
54  				session, "tokenstring"));
55  		assertFalse("1度validateTokenした後は指定したトークン文字列は除去されている", TokenHelper.validateToken(
56  				session, "tokenstring"));
57  	}
58  }