1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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&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 & 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 }