View Javadoc

1   /*
2    * $Id: TokenTagTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp.ui;
19  
20  import javax.servlet.jsp.JspException;
21  
22  import org.apache.struts2.util.TokenHelper;
23  import org.apache.struts2.views.jsp.AbstractUITagTest;
24  
25  
26  /***
27   * TokenTagTest
28   *
29   */
30  public class TokenTagTest extends AbstractUITagTest {
31  
32      public void testDefaultName() {
33          String tokenName = TokenHelper.DEFAULT_TOKEN_NAME;
34          TokenTag tag = new TokenTag();
35          doTokenTest(tokenName, tag);
36      }
37  
38      public void testMultipleTagsWithSameName() {
39          String tokenName = "sameName";
40          TokenTag tag = new TokenTag();
41          tag.setName(tokenName);
42  
43          String token = doTokenTest(tokenName, tag);
44  
45          TokenTag anotherTag = new TokenTag();
46          anotherTag.setName(tokenName);
47  
48          String anotherToken = doTokenTest(tokenName, anotherTag);
49          assertEquals(token, anotherToken);
50      }
51  
52      /***
53       * WW-480
54       */
55      public void testNotFindableName() {
56          String tokenName = "foo";
57          TokenTag tag = new TokenTag();
58          tag.setName(tokenName);
59          doTokenTest(tokenName, tag);
60  
61          String s = writer.toString();
62          assertTrue(s.indexOf("name=\"" + TokenHelper.DEFAULT_TOKEN_NAME) > -1);
63          assertTrue(s.indexOf("value=\"" + tokenName + "\"") > -1);
64          assertTrue(s.indexOf("name=\"" + tokenName + "\"") > -1);
65  
66          //System.out.println(s);
67      }
68  
69      public void testSuppliedName() {
70          String tokenName = "my.very.long.token.name";
71          TokenTag tag = new TokenTag();
72          tag.setName(tokenName);
73          doTokenTest(tokenName, tag);
74      }
75  
76      private String doTokenTest(String tokenName, TokenTag tag) {
77          tag.setPageContext(pageContext);
78  
79          String token = null;
80  
81          try {
82              tag.doStartTag();
83              tag.doEndTag();
84  
85              token = (String) context.get(tokenName);
86              assertNotNull(token);
87              assertEquals(token, pageContext.getSession().getAttribute(tokenName));
88          } catch (JspException e) {
89              e.printStackTrace();
90              fail();
91          }
92  
93          return token;
94      }
95  }