1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.jsp.ui;
23
24 import java.util.*;
25
26 import org.apache.struts2.views.jsp.AbstractUITagTest;
27 import org.apache.struts2.TestAction;
28 import org.apache.commons.lang.xwork.StringUtils;
29
30 import com.opensymphony.xwork2.Action;
31 import com.opensymphony.xwork2.ActionSupport;
32
33 /***
34 * ActionMessageTag test case.
35 *
36 */
37 public class ActionMessageTagTest extends AbstractUITagTest {
38
39 public void testNoActionMessages() throws Exception {
40
41 ActionMessageTag tag = new ActionMessageTag();
42 ((InternalActionSupport)action).setHasActionMessage(false);
43 tag.setPageContext(pageContext);
44 tag.doStartTag();
45 tag.doEndTag();
46
47 verify(ActionMessageTagTest.class.getResource("actionmessage-1.txt"));
48 }
49
50 public void testActionMessageEscape() throws Exception {
51
52 ActionMessageTag tag = new ActionMessageTag();
53 TestAction testAction = new TestAction();
54 testAction.addActionMessage("<p>hey</p>");
55 stack.pop();
56 stack.push(testAction);
57 tag.setEscape(true);
58 tag.setPageContext(pageContext);
59 tag.doStartTag();
60 tag.doEndTag();
61
62 assertEquals(normalize("<ul class=\"actionMessage\"><li><span><p>hey</p></span></li></ul>", true),
63 normalize(writer.toString(), true));
64 }
65
66 public void testActionErrorsDontEscape() throws Exception {
67
68 ActionMessageTag tag = new ActionMessageTag();
69 TestAction testAction = new TestAction();
70 testAction.addActionMessage("<p>hey</p>");
71 stack.pop();
72 stack.push(testAction);
73 tag.setEscape(false);
74 tag.setPageContext(pageContext);
75 tag.doStartTag();
76 tag.doEndTag();
77
78 assertEquals(normalize("<ul class=\"actionMessage\"><li><span><p>hey</p></span></li></ul>", true),
79 normalize(writer.toString(), true));
80 }
81
82
83 public void testYesActionMessages() throws Exception {
84
85 ActionMessageTag tag = new ActionMessageTag();
86 tag.setId("someid");
87 ((InternalActionSupport)action).setHasActionMessage(true);
88 tag.setPageContext(pageContext);
89 tag.doStartTag();
90 tag.doEndTag();
91
92 verify(ActionMessageTagTest.class.getResource("actionmessage-2.txt"));
93 }
94
95 public void testYesActionMessagesWithEmptyMessages() throws Exception {
96
97 ActionMessageTag tag = new ActionMessageTag();
98 tag.setId("someid");
99 InternalActionSupport internalActionSupport = (InternalActionSupport) action;
100 internalActionSupport.setJustNullElement(true);
101 tag.setPageContext(pageContext);
102 tag.doStartTag();
103 tag.doEndTag();
104
105 assertTrue(StringUtils.isBlank(writer.toString()));
106 }
107
108 public void testNullMessage() throws Exception {
109
110 ActionMessageTag tag = new ActionMessageTag();
111 tag.setId("someid");
112 ((InternalActionSupport)action).setHasActionMessage(true);
113 ((InternalActionSupport)action).addActionMessage(null);
114 tag.setPageContext(pageContext);
115 tag.doStartTag();
116 tag.doEndTag();
117
118 verify(ActionMessageTagTest.class.getResource("actionmessage-2.txt"));
119 }
120
121 public Action getAction() {
122 return new InternalActionSupport();
123 }
124
125 /***
126 * Internal ActionSupport class for testing, can be in state with
127 * or without action messages.
128 *
129 */
130 public class InternalActionSupport extends ActionSupport {
131
132 private static final long serialVersionUID = -3230043189352453629L;
133
134 private boolean canHaveActionMessage;
135 private boolean justNullElement;
136
137 public void setHasActionMessage(boolean canHaveActionMessage) {
138 this.canHaveActionMessage = canHaveActionMessage;
139 }
140
141 public void setJustNullElement(boolean justNullElement) {
142 this.justNullElement = justNullElement;
143 }
144
145 public Collection getActionMessages() {
146 if (justNullElement) {
147 return Arrays.asList(null);
148 } else if (canHaveActionMessage) {
149 List messages = new ArrayList();
150 messages.add("action message number 1");
151 messages.add("action message number 2");
152 messages.add("action message number 3");
153 return messages;
154 }
155 else {
156 return Collections.EMPTY_LIST;
157 }
158 }
159
160 public boolean hasActionMessages() {
161 return canHaveActionMessage;
162 }
163 }
164 }