View Javadoc

1   /*
2    * $Id: TokenTagTest.java 471756 2006-11-06 15:01:43Z husted $
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  package org.apache.struts2.views.jsp.ui;
22  
23  import javax.servlet.jsp.JspException;
24  
25  import org.apache.struts2.util.TokenHelper;
26  import org.apache.struts2.views.jsp.AbstractUITagTest;
27  
28  
29  /***
30   * TokenTagTest
31   *
32   */
33  public class TokenTagTest extends AbstractUITagTest {
34  
35      public void testDefaultName() {
36          String tokenName = TokenHelper.DEFAULT_TOKEN_NAME;
37          TokenTag tag = new TokenTag();
38          doTokenTest(tokenName, tag);
39      }
40  
41      public void testMultipleTagsWithSameName() {
42          String tokenName = "sameName";
43          TokenTag tag = new TokenTag();
44          tag.setName(tokenName);
45  
46          String token = doTokenTest(tokenName, tag);
47  
48          TokenTag anotherTag = new TokenTag();
49          anotherTag.setName(tokenName);
50  
51          String anotherToken = doTokenTest(tokenName, anotherTag);
52          assertEquals(token, anotherToken);
53      }
54  
55      /***
56       * WW-480
57       */
58      public void testNotFindableName() {
59          String tokenName = "foo";
60          TokenTag tag = new TokenTag();
61          tag.setName(tokenName);
62          doTokenTest(tokenName, tag);
63  
64          String s = writer.toString();
65          assertTrue(s.indexOf("name=\"" + TokenHelper.DEFAULT_TOKEN_NAME) > -1);
66          assertTrue(s.indexOf("value=\"" + tokenName + "\"") > -1);
67          assertTrue(s.indexOf("name=\"" + tokenName + "\"") > -1);
68  
69          //System.out.println(s);
70      }
71  
72      public void testSuppliedName() {
73          String tokenName = "my.very.long.token.name";
74          TokenTag tag = new TokenTag();
75          tag.setName(tokenName);
76          doTokenTest(tokenName, tag);
77      }
78  
79      private String doTokenTest(String tokenName, TokenTag tag) {
80          tag.setPageContext(pageContext);
81  
82          String token = null;
83  
84          try {
85              tag.doStartTag();
86              tag.doEndTag();
87  
88              token = (String) context.get(tokenName);
89              assertNotNull(token);
90              assertEquals(token, pageContext.getSession().getAttribute(tokenName));
91          } catch (JspException e) {
92              e.printStackTrace();
93              fail();
94          }
95  
96          return token;
97      }
98  }