View Javadoc

1   /*
2    * $Id: ActionMessageTagTest.java 805635 2009-08-19 00:18:54Z musachy $
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 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>&lt;p&gt;hey&lt;/p&gt;</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 }