View Javadoc

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