View Javadoc

1   /*
2    * $Id: IfTagTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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;
23  
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.TagSupport;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.struts2.ServletActionContext;
30  import org.apache.struts2.StrutsTestCase;
31  
32  import com.mockobjects.servlet.MockJspWriter;
33  import com.mockobjects.servlet.MockPageContext;
34  import com.opensymphony.xwork2.ActionContext;
35  import com.opensymphony.xwork2.util.ValueStack;
36  import com.opensymphony.xwork2.util.ValueStackFactory;
37  
38  
39  /***
40   */
41  public class IfTagTest extends StrutsTestCase {
42  
43      IfTag tag;
44      MockPageContext pageContext;
45      ValueStack stack;
46  
47  
48      public void testNonBooleanTest() {
49          // set up the stack
50          Foo foo = new Foo();
51          foo.setNum(1);
52          stack.push(foo);
53  
54          // set up the test
55          tag.setTest("num");
56  
57          int result = 0;
58  
59          try {
60              result = tag.doStartTag();
61          } catch (JspException e) {
62              e.printStackTrace();
63              fail();
64          }
65  
66          assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
67  
68          try {
69              result = tag.doEndTag();
70          } catch (JspException e) {
71              e.printStackTrace();
72              fail();
73          }
74      }
75  
76      public void testTestError() {
77          // set up the stack
78          Foo foo = new Foo();
79          foo.setNum(2);
80          stack.push(foo);
81  
82          // set up the test
83          tag.setTest("nuuuuum == 2");
84  
85          int result = 0;
86  
87          try {
88              result = tag.doStartTag();
89          } catch (JspException e) {
90              e.printStackTrace();
91              fail();
92          }
93  
94          assertEquals(TagSupport.SKIP_BODY, result);
95  
96          try {
97              result = tag.doEndTag();
98          } catch (JspException e) {
99              e.printStackTrace();
100             fail();
101         }
102     }
103 
104     public void testTestFalse() {
105         // set up the stack
106         Foo foo = new Foo();
107         foo.setNum(2);
108         stack.push(foo);
109 
110         // set up the test
111         tag.setTest("num != 2");
112 
113         int result = 0;
114 
115         try {
116             result = tag.doStartTag();
117         } catch (JspException e) {
118             e.printStackTrace();
119             fail();
120         }
121 
122         assertEquals(TagSupport.SKIP_BODY, result);
123 
124         try {
125             result = tag.doEndTag();
126         } catch (JspException e) {
127             e.printStackTrace();
128             fail();
129         }
130     }
131 
132     public void testTestTrue() {
133         // set up the stack
134         Foo foo = new Foo();
135         foo.setNum(2);
136         stack.push(foo);
137 
138         // set up the test
139         tag.setTest("num == 2");
140 
141         int result = 0;
142         //tag.setPageContext(pageContext);
143 
144         try {
145             result = tag.doStartTag();
146         } catch (JspException e) {
147             e.printStackTrace();
148             fail();
149         }
150 
151         assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
152 
153         try {
154             result = tag.doEndTag();
155         } catch (JspException e) {
156             e.printStackTrace();
157             fail();
158         }
159     }
160 
161 
162     public void testIfElse1() throws Exception {
163         IfTag ifTag = new IfTag();
164         ifTag.setPageContext(pageContext);
165         ifTag.setTest("true");
166 
167         ElseTag elseTag = new ElseTag();
168         elseTag.setPageContext(pageContext);
169 
170         int r1 = ifTag.doStartTag();
171         ifTag.doEndTag();
172         int r2 = elseTag.doStartTag();
173         elseTag.doEndTag();
174 
175         assertEquals(TagSupport.EVAL_BODY_INCLUDE, r1);
176         assertEquals(TagSupport.SKIP_BODY, r2);
177     }
178 
179     public void testIfElse2() throws Exception {
180         IfTag ifTag = new IfTag();
181         ifTag.setPageContext(pageContext);
182         ifTag.setTest("false");
183 
184         ElseTag elseTag = new ElseTag();
185         elseTag.setPageContext(pageContext);
186 
187         int r1 = ifTag.doStartTag();
188         ifTag.doEndTag();
189         int r2 = elseTag.doStartTag();
190         elseTag.doEndTag();
191 
192         assertEquals(TagSupport.SKIP_BODY, r1);
193         assertEquals(TagSupport.EVAL_BODY_INCLUDE, r2);
194     }
195 
196     public void testIfElseIf() throws Exception {
197         IfTag ifTag = new IfTag();
198         ifTag.setPageContext(pageContext);
199         ifTag.setTest("false");
200 
201         ElseIfTag elseIfTag1 = new ElseIfTag();
202         elseIfTag1.setPageContext(pageContext);
203         elseIfTag1.setTest("false");
204 
205         ElseIfTag elseIfTag2 = new ElseIfTag();
206         elseIfTag2.setPageContext(pageContext);
207         elseIfTag2.setTest("true");
208 
209         ElseIfTag elseIfTag3 = new ElseIfTag();
210         elseIfTag3.setPageContext(pageContext);
211         elseIfTag3.setTest("true");
212 
213         int r1 = ifTag.doStartTag();
214         ifTag.doEndTag();
215         int r2 = elseIfTag1.doStartTag();
216         elseIfTag1.doEndTag();
217         int r3 = elseIfTag2.doStartTag();
218         elseIfTag2.doEndTag();
219         int r4 = elseIfTag3.doStartTag();
220         elseIfTag3.doEndTag();
221 
222         assertEquals(TagSupport.SKIP_BODY, r1);
223         assertEquals(TagSupport.SKIP_BODY, r2);
224         assertEquals(TagSupport.EVAL_BODY_INCLUDE, r3);
225         assertEquals(TagSupport.SKIP_BODY, r4);
226     }
227 
228     public void testIfElseIfElse() throws Exception {
229         IfTag ifTag = new IfTag();
230         ifTag.setPageContext(pageContext);
231         ifTag.setTest("false");
232 
233         ElseIfTag elseIfTag1 = new ElseIfTag();
234         elseIfTag1.setPageContext(pageContext);
235         elseIfTag1.setTest("false");
236 
237         ElseIfTag elseIfTag2 = new ElseIfTag();
238         elseIfTag2.setPageContext(pageContext);
239         elseIfTag2.setTest("false");
240 
241         ElseIfTag elseIfTag3 = new ElseIfTag();
242         elseIfTag3.setPageContext(pageContext);
243         elseIfTag3.setTest("false");
244 
245         ElseTag elseTag = new ElseTag();
246         elseTag.setPageContext(pageContext);
247 
248         int r1 = ifTag.doStartTag();
249         ifTag.doEndTag();
250         int r2 = elseIfTag1.doStartTag();
251         elseIfTag1.doEndTag();
252         int r3 = elseIfTag2.doStartTag();
253         elseIfTag2.doEndTag();
254         int r4 = elseIfTag3.doStartTag();
255         elseIfTag3.doEndTag();
256         int r5 = elseTag.doStartTag();
257         elseTag.doEndTag();
258 
259         assertEquals(TagSupport.SKIP_BODY, r1);
260         assertEquals(TagSupport.SKIP_BODY, r2);
261         assertEquals(TagSupport.SKIP_BODY, r3);
262         assertEquals(TagSupport.SKIP_BODY, r4);
263         assertEquals(TagSupport.EVAL_BODY_INCLUDE, r5);
264     }
265 
266 
267     public void testNestedIfElse1() throws Exception {
268         IfTag ifTag = new IfTag();
269         ifTag.setPageContext(pageContext);
270         ifTag.setTest("true");
271 
272         IfTag nestedIfTag = new IfTag();
273         nestedIfTag.setPageContext(pageContext);
274         nestedIfTag.setTest("true");
275 
276         ElseTag elseTag = new ElseTag();
277         elseTag.setPageContext(pageContext);
278 
279         int r1 = ifTag.doStartTag();
280         int r2 = nestedIfTag.doStartTag();
281         int r3 = nestedIfTag.doEndTag();
282         int r4 = ifTag.doEndTag();
283         int r5 = elseTag.doStartTag();
284         int r6 = elseTag.doEndTag();
285 
286         assertEquals(TagSupport.EVAL_BODY_INCLUDE, r1);
287         assertEquals(TagSupport.EVAL_BODY_INCLUDE, r2);
288         assertEquals(TagSupport.EVAL_PAGE, r3);
289         assertEquals(TagSupport.EVAL_PAGE, r4);
290         assertEquals(TagSupport.SKIP_BODY, r5);
291         assertEquals(TagSupport.EVAL_PAGE, r6);
292     }
293 
294     public void testNestedIfElse2() throws Exception {
295         IfTag ifTag = new IfTag();
296         ifTag.setPageContext(pageContext);
297         ifTag.setTest("true");
298 
299         IfTag nestedIfTag = new IfTag();
300         nestedIfTag.setPageContext(pageContext);
301         nestedIfTag.setTest("false");
302 
303         ElseTag elseTag = new ElseTag();
304         elseTag.setPageContext(pageContext);
305 
306         int r1 = ifTag.doStartTag();
307         int r2 = nestedIfTag.doStartTag();
308         int r3 = nestedIfTag.doEndTag();
309         int r4 = ifTag.doEndTag();
310         int r5 = elseTag.doStartTag();
311         int r6 = elseTag.doEndTag();
312 
313         assertEquals(TagSupport.EVAL_BODY_INCLUDE, r1);
314         assertEquals(TagSupport.SKIP_BODY, r2);
315         assertEquals(TagSupport.EVAL_PAGE, r3);
316         assertEquals(TagSupport.EVAL_PAGE, r4);
317         assertEquals(TagSupport.SKIP_BODY, r5);
318         assertEquals(TagSupport.EVAL_PAGE, r6);
319     }
320 
321 
322 
323 
324     protected void setUp() throws Exception {
325         super.setUp();
326         // create the needed objects
327         tag = new IfTag();
328         stack = ActionContext.getContext().getValueStack();
329 
330         // create the mock http servlet request
331         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
332         ActionContext.getContext().setValueStack(stack);
333         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
334 
335         // create the mock page context
336         pageContext = new MockPageContext();
337         pageContext.setRequest(request);
338         pageContext.setJspWriter(new MockJspWriter());
339 
340         // associate the tag with the mock page request
341         tag.setPageContext(pageContext);
342     }
343 
344 
345     class Foo {
346         int num;
347 
348         public void setNum(int num) {
349             this.num = num;
350         }
351 
352         public int getNum() {
353             return num;
354         }
355     }
356 }