    1   /*
    2    * Copyright 2004-2009 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.tags;
    17
    18  import static org.junit.Assert.assertEquals;
    19  import static org.junit.Assert.assertTrue;
    20
    21  import javax.servlet.http.HttpServletRequest;
    22
    23  import org.jdom.Element;
    24  import org.junit.Before;
    25  import org.junit.Test;
    26  import org.seasar.cubby.internal.util.TokenHelper;
    27
    28  public class TokenTagTest extends SimpleTagTestCase {
    29
    30  	private TokenTag tag;
    31
    32  	@Before
    33  	public void setup() throws Exception {
    34  		tag = new TokenTag();
    35  		setupSimpleTag(tag);
    36  	}
    37
    38  	@Test
    39  	public void doTag1() throws Exception {
    40  		HttpServletRequest request = (HttpServletRequest) context.getRequest();
    41
    42  		tag.doTag();
    43  		Element element = getResultAsElementFromContext();
    44  		String message = "nameが指定されている場合";
    45  		assertEquals(message, 3, element.getAttributes().size());
    46  		assertEquals(message, "input", element.getName());
    47  		assertEquals(message, "hidden", element.getAttributeValue("type"));
    48  		assertTrue(message, element.getAttributeValue("value").length() != 0);
    49  		assertEquals(message, TokenHelper.DEFAULT_TOKEN_NAME, element
    50  				.getAttributeValue("name"));
    51  		assertTrue(message, TokenHelper.validateToken(request.getSession(),
    52  				element.getAttributeValue("value")));
    53  	}
    54
    55  	@Test
    56  	public void doTag2() throws Exception {
    57  		HttpServletRequest request = (HttpServletRequest) context.getRequest();
    58
    59  		tag.setName("cubby.token2");
    60  		tag.doTag();
    61  		Element element = getResultAsElementFromContext();
    62  		String message = "nameが指定されている場合";
    63  		assertEquals(message, 3, element.getAttributes().size());
    64  		assertEquals(message, "input", element.getName());
    65  		assertEquals(message, "hidden", element.getAttributeValue("type"));
    66  		assertTrue(message, element.getAttributeValue("value").length() != 0);
    67  		assertEquals(message, "cubby.token2", element.getAttributeValue("name"));
    68  		assertTrue(message, TokenHelper.validateToken(request.getSession(),
    69  				element.getAttributeValue("value")));
    70  	}
    71
    72  	@Test
    73  	public void testDoTag3() throws Exception {
    74  		HttpServletRequest request = (HttpServletRequest) context.getRequest();
    75
    76  		tag.setDynamicAttribute(null, "id", "token");
    77  		tag.doTag();
    78  		Element element = getResultAsElementFromContext();
    79  		String message = "idが指定されている場合";
    80  		assertEquals(message, 4, element.getAttributes().size());
    81  		assertEquals(message, "input", element.getName());
    82  		assertEquals(message, "token", element.getAttributeValue("id"));
    83  		assertEquals(message, "hidden", element.getAttributeValue("type"));
    84  		assertTrue(message, element.getAttributeValue("value").length() != 0);
    85  		assertEquals(message, TokenHelper.DEFAULT_TOKEN_NAME, element
    86  				.getAttributeValue("name"));
    87  		assertTrue(message, TokenHelper.validateToken(request.getSession(),
    88  				element.getAttributeValue("value")));
    89  	}
    90
    91  }