1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.jsp;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.servlet.jsp.JspException;
31 import javax.servlet.jsp.tagext.TagSupport;
32
33 import com.mockobjects.servlet.MockBodyContent;
34 import com.mockobjects.servlet.MockJspWriter;
35
36
37 /***
38 * Test Case for Iterator Tag
39 *
40 */
41 public class IteratorTagTest extends AbstractUITagTest {
42
43 IteratorTag tag;
44
45
46 public void testIteratingWithIdSpecified() throws Exception {
47 List list = new ArrayList();
48 list.add("one");
49 list.add("two");
50 list.add("three");
51 list.add("four");
52 list.add("five");
53
54 Foo foo = new Foo();
55 foo.setList(list);
56
57 stack.push(foo);
58
59 tag.setValue("list");
60 tag.setId("myId");
61
62
63 int result = tag.doStartTag();
64 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
65 assertEquals(stack.peek(), "one");
66 assertEquals(stack.getContext().get("myId"), "one");
67
68
69 tag.doInitBody();
70
71
72 result = tag.doAfterBody();
73 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
74 assertEquals(stack.peek(), "two");
75 assertEquals(stack.getContext().get("myId"), "two");
76
77
78
79 result = tag.doAfterBody();
80 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
81 assertEquals(stack.peek(), "three");
82 assertEquals(stack.getContext().get("myId"), "three");
83
84
85
86 result = tag.doAfterBody();
87 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
88 assertEquals(stack.peek(), "four");
89 assertEquals(stack.getContext().get("myId"), "four");
90
91
92
93 result = tag.doAfterBody();
94 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
95 assertEquals(stack.peek(), "five");
96 assertEquals(stack.getContext().get("myId"), "five");
97
98
99 result = tag.doAfterBody();
100 assertEquals(result, TagSupport.SKIP_BODY);
101
102 result = tag.doEndTag();
103 assertEquals(result, TagSupport.EVAL_PAGE);
104 }
105
106 public void testIteratingWithIdSpecifiedAndNullElementOnCollection() throws Exception {
107 List list = new ArrayList();
108 list.add("one");
109 list.add(null);
110 list.add("three");
111
112 Foo foo = new Foo();
113 foo.setList(list);
114
115 stack.push(foo);
116
117 tag.setValue("list");
118 tag.setId("myId");
119
120
121 int result = tag.doStartTag();
122 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
123 assertEquals(stack.peek(), "one");
124 assertEquals(stack.getContext().get("myId"), "one");
125
126
127 tag.doInitBody();
128
129
130 result = tag.doAfterBody();
131 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
132 assertNull(stack.peek());
133 assertNull(stack.getContext().get("myId"));
134
135
136
137 result = tag.doAfterBody();
138 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
139 assertEquals(stack.peek(), "three");
140 assertEquals(stack.getContext().get("myId"), "three");
141
142 result = tag.doAfterBody();
143 assertEquals(result, TagSupport.SKIP_BODY);
144
145 result = tag.doEndTag();
146 assertEquals(result, TagSupport.EVAL_PAGE);
147 }
148
149
150 public void testArrayIterator() {
151 Foo foo = new Foo();
152 foo.setArray(new String[]{"test1", "test2", "test3"});
153
154 stack.push(foo);
155
156 tag.setValue("array");
157
158 iterateThreeStrings();
159 }
160
161 public void testCollectionIterator() {
162 Foo foo = new Foo();
163 ArrayList list = new ArrayList();
164 list.add("test1");
165 list.add("test2");
166 list.add("test3");
167 foo.setList(list);
168
169 stack.push(foo);
170
171 tag.setValue("list");
172
173 iterateThreeStrings();
174 }
175
176 public void testIteratorWithDefaultValue() {
177 stack.push(new String[]{"test1", "test2", "test3"});
178 iterateThreeStrings();
179 }
180
181 public void testMapIterator() {
182 Foo foo = new Foo();
183 HashMap map = new HashMap();
184 map.put("test1", "123");
185 map.put("test2", "456");
186 map.put("test3", "789");
187 foo.setMap(map);
188
189 stack.push(foo);
190
191 tag.setValue("map");
192
193 int result = 0;
194
195 try {
196 result = tag.doStartTag();
197 } catch (JspException e) {
198 e.printStackTrace();
199 fail();
200 }
201
202 assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
203 assertEquals(4, stack.size());
204 assertTrue(stack.getRoot().peek() instanceof Map.Entry);
205
206 try {
207 result = tag.doAfterBody();
208 } catch (JspException e) {
209 e.printStackTrace();
210 fail();
211 }
212
213 assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
214 assertEquals(4, stack.size());
215 assertTrue(stack.getRoot().peek() instanceof Map.Entry);
216
217 try {
218 result = tag.doAfterBody();
219 } catch (JspException e) {
220 e.printStackTrace();
221 fail();
222 }
223
224 assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
225 assertEquals(4, stack.size());
226 assertTrue(stack.getRoot().peek() instanceof Map.Entry);
227
228 try {
229 result = tag.doAfterBody();
230 } catch (JspException e) {
231 e.printStackTrace();
232 fail();
233 }
234
235 assertEquals(TagSupport.SKIP_BODY, result);
236 assertEquals(3, stack.size());
237 }
238
239 public void testStatus() {
240 Foo foo = new Foo();
241 foo.setArray(new String[]{"test1", "test2", "test3"});
242
243 stack.push(foo);
244
245 tag.setValue("array");
246 tag.setStatus("fooStatus");
247
248 int result = 0;
249
250 try {
251 result = tag.doStartTag();
252 } catch (JspException e) {
253 e.printStackTrace();
254 fail();
255 }
256
257 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
258 assertEquals("test1", stack.getRoot().peek());
259 assertEquals(4, stack.size());
260
261 IteratorStatus status = (IteratorStatus) context.get("fooStatus");
262 assertNotNull(status);
263 assertFalse(status.isLast());
264 assertTrue(status.isFirst());
265 assertEquals(0, status.getIndex());
266 assertEquals(1, status.getCount());
267 assertTrue(status.isOdd());
268 assertFalse(status.isEven());
269
270 try {
271 result = tag.doAfterBody();
272 } catch (JspException e) {
273 e.printStackTrace();
274 fail();
275 }
276
277 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
278 assertEquals("test2", stack.getRoot().peek());
279 assertEquals(4, stack.size());
280
281 status = (IteratorStatus) context.get("fooStatus");
282 assertNotNull(status);
283 assertFalse(status.isLast());
284 assertFalse(status.isFirst());
285 assertEquals(1, status.getIndex());
286 assertEquals(2, status.getCount());
287 assertFalse(status.isOdd());
288 assertTrue(status.isEven());
289
290 try {
291 result = tag.doAfterBody();
292 } catch (JspException e) {
293 e.printStackTrace();
294 fail();
295 }
296
297 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
298 assertEquals("test3", stack.getRoot().peek());
299 assertEquals(4, stack.size());
300
301 status = (IteratorStatus) context.get("fooStatus");
302 assertNotNull(status);
303 assertTrue(status.isLast());
304 assertFalse(status.isFirst());
305 assertEquals(2, status.getIndex());
306 assertEquals(3, status.getCount());
307 assertTrue(status.isOdd());
308 assertFalse(status.isEven());
309 }
310
311 public void testEmptyArray() {
312 Foo foo = new Foo();
313 foo.setArray(new String[]{});
314
315 stack.push(foo);
316
317 tag.setValue("array");
318
319 validateSkipBody();
320 }
321
322 public void testNullArray() {
323 Foo foo = new Foo();
324 foo.setArray(null);
325
326 stack.push(foo);
327
328 tag.setValue("array");
329
330 validateSkipBody();
331 }
332
333 public void testEmptyCollection() {
334 Foo foo = new Foo();
335 foo.setList(new ArrayList());
336
337 stack.push(foo);
338
339 tag.setValue("list");
340
341 validateSkipBody();
342 }
343
344 public void testNullCollection() {
345 Foo foo = new Foo();
346 foo.setList(null);
347
348 stack.push(foo);
349
350 tag.setValue("list");
351
352 validateSkipBody();
353 }
354
355 protected void setUp() throws Exception {
356 super.setUp();
357
358
359 tag = new IteratorTag();
360
361 MockBodyContent mockBodyContent = new TestMockBodyContent();
362 mockBodyContent.setupGetEnclosingWriter(new MockJspWriter());
363 tag.setBodyContent(mockBodyContent);
364
365
366 tag.setPageContext(pageContext);
367 }
368
369 private void iterateThreeStrings() {
370 int result = 0;
371
372 try {
373 result = tag.doStartTag();
374 } catch (JspException e) {
375 e.printStackTrace();
376 fail();
377 }
378
379 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
380 assertEquals("test1", stack.getRoot().peek());
381 assertEquals(4, stack.size());
382
383 try {
384 result = tag.doAfterBody();
385 } catch (JspException e) {
386 e.printStackTrace();
387 fail();
388 }
389
390 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
391 assertEquals("test2", stack.getRoot().peek());
392 assertEquals(4, stack.size());
393
394 try {
395 result = tag.doAfterBody();
396 } catch (JspException e) {
397 e.printStackTrace();
398 fail();
399 }
400
401 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
402 assertEquals("test3", stack.getRoot().peek());
403 assertEquals(4, stack.size());
404
405 try {
406 result = tag.doAfterBody();
407 } catch (JspException e) {
408 e.printStackTrace();
409 fail();
410 }
411
412 assertEquals(result, TagSupport.SKIP_BODY);
413 assertEquals(3, stack.size());
414 }
415
416 private void validateSkipBody() {
417 int result = 0;
418
419 try {
420 result = tag.doStartTag();
421 } catch (JspException e) {
422 e.printStackTrace();
423 fail();
424 }
425
426 assertEquals(result, TagSupport.SKIP_BODY);
427 try {
428 result = tag.doEndTag();
429 } catch (JspException e) {
430 e.printStackTrace();
431 fail();
432 }
433 }
434
435 class Foo {
436 private Collection list;
437 private Map map;
438 private String[] array;
439
440 public void setArray(String[] array) {
441 this.array = array;
442 }
443
444 public String[] getArray() {
445 return array;
446 }
447
448 public void setList(Collection list) {
449 this.list = list;
450 }
451
452 public Collection getList() {
453 return list;
454 }
455
456 public void setMap(Map map) {
457 this.map = map;
458 }
459
460 public Map getMap() {
461 return map;
462 }
463 }
464
465 class TestMockBodyContent extends MockBodyContent {
466 public String getString() {
467 return ".-.";
468 }
469 }
470 }