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.validator.validators;
17  
18  import javax.servlet.http.HttpSession;
19  
20  import org.seasar.cubby.controller.ThreadContext;
21  import org.seasar.cubby.util.TokenHelper;
22  import org.seasar.cubby.validator.ValidationContext;
23  import org.seasar.extension.unit.S2TestCase;
24  import org.seasar.framework.mock.servlet.MockHttpServletRequestImpl;
25  import org.seasar.framework.mock.servlet.MockServletContextImpl;
26  
27  public class TokenValidatorTest extends S2TestCase {
28  
29  	@SuppressWarnings("unchecked")
30  	public void testValidate() throws Exception {
31  		TokenValidator validator = new TokenValidator();
32  		MockServletContextImpl servletContext = new MockServletContextImpl(
33  				"/cubby");
34  		ThreadContext.setRequest(new MockHttpServletRequestImpl(servletContext,
35  				"/servlet"));
36  		HttpSession session = ThreadContext.getRequest().getSession();
37  
38  		ValidationContext context = new ValidationContext();
39  		validator.validate(context, new Object[] { "tokenstring" });
40  		assertFalse("セッション中にトークン文字列が存在しないためエラー", context.getMessageInfos()
41  				.isEmpty());
42  
43  		TokenHelper.setToken(session, "tokenstring");
44  		context = new ValidationContext();
45  		validator.validate(context, new Object[] { "tokenstring" });
46  		assertTrue("セッション中にトークン文字列が存在するためエラーではない", context.getMessageInfos()
47  				.isEmpty());
48  
49  		context = new ValidationContext();
50  		validator.validate(context, new Object[] { "tokenstring" });
51  		assertFalse("セッション中のトークン文字列が除去された(2重サブミットの状態)ためエラー", context
52  				.getMessageInfos().isEmpty());
53  	}
54  
55  	public void testRequestIsNull() throws Exception {
56  		ThreadContext.setRequest(null);
57  		TokenValidator validator = new TokenValidator();
58  		ValidationContext context = new ValidationContext();
59  		try {
60  			validator.validate(context, new Object[] { "tokenstring" });
61  			fail("ThreadContext.getRequest()がnullの場合、ここは通らない");
62  		} catch (IllegalStateException ex) {
63  		}
64  	}
65  
66  }