View Javadoc

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