View Javadoc

1   /*
2    * $Id: ActionErrorTagTest.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   * ActionErrorTag test case.
35   *
36   */
37  public class ActionErrorTagTest extends AbstractUITagTest {
38  
39      boolean shouldActionHaveError = false;
40  
41      public void testNoActionErrors() throws Exception {
42          ActionErrorTag tag = new ActionErrorTag();
43          ((InternalActionSupport)action).setHasActionErrors(false);
44          tag.setPageContext(pageContext);
45          tag.doStartTag();
46          tag.doEndTag();
47  
48          //assertEquals("", writer.toString());
49          verify(ActionErrorTagTest.class.getResource("actionerror-1.txt"));
50      }
51  
52       public void testActionErrorsEscape() throws Exception {
53  
54          ActionErrorTag tag = new ActionErrorTag();
55          TestAction testAction = new TestAction();
56          testAction.addActionError("<p>hey</p>");
57          stack.pop();
58          stack.push(testAction);
59          tag.setEscape(true);
60          tag.setPageContext(pageContext);
61          tag.doStartTag();
62          tag.doEndTag();
63  
64          assertEquals(normalize("<ul class=\"errorMessage\"><li><span>&lt;p&gt;hey&lt;/p&gt;</span></li></ul>", true),
65                  normalize(writer.toString(), true));
66      }
67  
68      public void testActionErrorsDontEscape() throws Exception {
69  
70          ActionErrorTag tag = new ActionErrorTag();
71          TestAction testAction = new TestAction();
72          testAction.addActionError("<p>hey</p>");
73          stack.pop();
74          stack.push(testAction);
75          tag.setEscape(false);
76          tag.setPageContext(pageContext);
77          tag.doStartTag();
78          tag.doEndTag();
79  
80          assertEquals(normalize("<ul class=\"errorMessage\"><li><span><p>hey</p></span></li></ul>", true),
81                  normalize(writer.toString(), true));
82      }
83  
84      public void testHaveActionErrors() throws Exception {
85  
86          ActionErrorTag tag = new ActionErrorTag();
87          tag.setId("someid");
88          ((InternalActionSupport)action).setHasActionErrors(true);
89          tag.setPageContext(pageContext);
90          tag.doStartTag();
91          tag.doEndTag();
92  
93          verify(ActionErrorTagTest.class.getResource("actionerror-2.txt"));
94      }
95  
96      public void testNullError() throws Exception {
97  
98          ActionErrorTag tag = new ActionErrorTag();
99          tag.setId("someid");
100         ((InternalActionSupport)action).setHasActionErrors(true);
101         ((InternalActionSupport)action).addActionError(null);
102         tag.setPageContext(pageContext);
103         tag.doStartTag();
104         tag.doEndTag();
105 
106         verify(ActionErrorTagTest.class.getResource("actionerror-2.txt"));
107     }
108 
109      public void testEmptyErrorList() throws Exception {
110 
111         ActionErrorTag tag = new ActionErrorTag();
112         tag.setId("someid");
113         ((InternalActionSupport)action).setHasActionErrors(true);
114         ((InternalActionSupport)action).setJustNullElement(true);
115         tag.setPageContext(pageContext);
116         tag.doStartTag();
117         tag.doEndTag();
118 
119         assertTrue(StringUtils.isBlank(writer.toString()));
120     }
121 
122 
123     public Action getAction() {
124         return new InternalActionSupport();
125     }
126 
127 
128     public class InternalActionSupport extends ActionSupport {
129 
130         private static final long serialVersionUID = -4777466640658557661L;
131 
132         private boolean yesActionErrors;
133         private boolean justNullElement;
134 
135         public void setJustNullElement(boolean justNullElement) {
136             this.justNullElement = justNullElement;
137         }
138 
139         public void setHasActionErrors(boolean aYesActionErrors) {
140             yesActionErrors = aYesActionErrors;
141         }
142 
143         public boolean hasActionErrors() {
144             return yesActionErrors;
145         }
146 
147         public Collection getActionErrors() {
148              if (justNullElement) {
149                 return Arrays.asList(null);
150             } else if (yesActionErrors) {
151                 List errors = new ArrayList();
152                 errors.add("action error number 1");
153                 errors.add("action error number 2");
154                 errors.add("action error number 3");
155                 return errors;
156             }
157             else {
158                 return Collections.EMPTY_LIST;
159             }
160         }
161     }
162 }