View Javadoc

1   /*
2    * $Id: AnchorTagTest.java 520249 2007-03-20 03:18:23Z 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;
22  
23  import java.io.StringWriter;
24  
25  import javax.servlet.jsp.JspWriter;
26  
27  import org.apache.struts2.views.jsp.ui.AnchorTag;
28  import org.apache.struts2.views.jsp.ui.StrutsBodyContent;
29  
30  
31  /***
32   * Unit test for {@ link AnchorTag}.
33   */
34  public class AnchorTagTest extends AbstractUITagTest {
35      private StringWriter writer = new StringWriter();
36      private AnchorTag tag;
37  
38      protected void setUp() throws Exception {
39          super.setUp();
40  
41          request.setScheme("http");
42          request.setServerName("localhost");
43          request.setServerPort(80);
44  
45          tag = new AnchorTag();
46          tag.setPageContext(pageContext);
47          JspWriter jspWriter = new StrutsMockJspWriter(writer);
48          pageContext.setJspWriter(jspWriter);
49      }
50  
51      public void testActionURL() throws Exception {
52          tag.setHref("TestAction.action");
53          tag.doStartTag();
54          tag.doEndTag();
55          assertTrue(writer.toString().indexOf("href=\"TestAction.action\"") > -1);
56          assertEquals("<a href=\"TestAction.action\"></a>", writer.toString());
57      }
58  
59      public void testNoNewLineAtEnd() throws Exception {
60          tag.setHref("TestAction.action");
61          tag.doStartTag();
62          tag.doEndTag();
63          assertFalse(writer.toString().endsWith("\n"));
64      }
65  
66      public void testAccessKey() throws Exception {
67          tag.setHref("TestAction.action");
68          tag.setAccesskey("T");
69          tag.doStartTag();
70          tag.doEndTag();
71          assertTrue(writer.toString().indexOf("accesskey=\"T\"") > -1);
72          assertFalse(writer.toString().endsWith("\n"));
73      }
74  
75      public void testId() throws Exception {
76          tag.setId("home&improvements");
77          tag.doStartTag();
78          tag.doEndTag();
79          assertEquals("<a id=\"home&amp;improvements\"></a>", writer.toString());
80          assertFalse(writer.toString().endsWith("\n"));
81      }
82  
83      public void testTitle() throws Exception {
84      	tag.setHref("home.ftl");
85          tag.setTitle("home & improvements");
86          tag.doStartTag();
87          tag.doEndTag();
88          assertEquals("<a href=\"home.ftl\" title=\"home &amp; improvements\"></a>", writer.toString());
89          assertFalse(writer.toString().endsWith("\n"));
90      }
91  
92      public void testOnMouseOver() throws Exception {
93          tag.setHref("TestAction.action");
94          tag.setOnmouseover("over");
95          tag.doStartTag();
96          tag.doEndTag();
97          assertTrue(writer.toString().indexOf("onmouseover=\"over\"") > -1);
98          assertFalse(writer.toString().endsWith("\n"));
99      }
100 
101     public void testOnMouseOverAndFocus() throws Exception {
102         tag.setHref("TestAction.action");
103         tag.setOnmouseover("overme");
104         tag.setOnfocus("focusme");
105         tag.doStartTag();
106         tag.doEndTag();
107         assertTrue(writer.toString().indexOf("onmouseover=\"overme\"") > -1);
108         assertTrue(writer.toString().indexOf("onfocus=\"focusme\"") > -1);
109         assertFalse(writer.toString().endsWith("\n"));
110     }
111 
112     public void testWithContent() throws Exception {
113         tag.setHref("TestAction.action");
114         tag.doStartTag();
115         writer.write("Home");       
116         tag.doEndTag();
117         assertEquals("<a href=\"TestAction.action\">Home</a>", writer.toString());
118         assertFalse(writer.toString().endsWith("\n"));
119     }
120 
121     public void testAddParameters() throws Exception {
122         tag.setHref("/TestAction.action");
123         String bodyText = "<img src=\"#\"/>";
124         StrutsBodyContent bodyContent = new StrutsBodyContent(null);
125         bodyContent.print(bodyText);
126         tag.setBodyContent(bodyContent);
127 
128         tag.doStartTag();
129         tag.doEndTag();
130     }
131 
132 }