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