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 com.mockobjects.servlet.MockBodyContent;
25 import com.mockobjects.servlet.MockJspWriter;
26 import org.apache.commons.collections.ListUtils;
27
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.tagext.TagSupport;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37
38 /***
39 * Test Case for Iterator Tag
40 *
41 */
42 public class IteratorTagTest extends AbstractUITagTest {
43
44 IteratorTag tag;
45
46
47 public void testIteratingWithIdSpecified() throws Exception {
48 List list = new ArrayList();
49 list.add("one");
50 list.add("two");
51 list.add("three");
52 list.add("four");
53 list.add("five");
54
55 Foo foo = new Foo();
56 foo.setList(list);
57
58 stack.push(foo);
59
60 tag.setValue("list");
61 tag.setId("myId");
62
63
64 int result = tag.doStartTag();
65 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
66 assertEquals(stack.peek(), "one");
67 assertEquals(stack.getContext().get("myId"), "one");
68
69
70 tag.doInitBody();
71
72
73 result = tag.doAfterBody();
74 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
75 assertEquals(stack.peek(), "two");
76 assertEquals(stack.getContext().get("myId"), "two");
77
78
79
80 result = tag.doAfterBody();
81 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
82 assertEquals(stack.peek(), "three");
83 assertEquals(stack.getContext().get("myId"), "three");
84
85
86
87 result = tag.doAfterBody();
88 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
89 assertEquals(stack.peek(), "four");
90 assertEquals(stack.getContext().get("myId"), "four");
91
92
93
94 result = tag.doAfterBody();
95 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
96 assertEquals(stack.peek(), "five");
97 assertEquals(stack.getContext().get("myId"), "five");
98
99
100 result = tag.doAfterBody();
101 assertEquals(result, TagSupport.SKIP_BODY);
102
103 result = tag.doEndTag();
104 assertEquals(result, TagSupport.EVAL_PAGE);
105 }
106
107 public void testIteratingWithIdSpecifiedAndNullElementOnCollection() throws Exception {
108 List list = new ArrayList();
109 list.add("one");
110 list.add(null);
111 list.add("three");
112
113 Foo foo = new Foo();
114 foo.setList(list);
115
116 stack.push(foo);
117
118 tag.setValue("list");
119 tag.setId("myId");
120
121
122 int result = tag.doStartTag();
123 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
124 assertEquals(stack.peek(), "one");
125 assertEquals(stack.getContext().get("myId"), "one");
126
127
128 tag.doInitBody();
129
130
131 result = tag.doAfterBody();
132 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
133 assertNull(stack.peek());
134 assertNull(stack.getContext().get("myId"));
135
136
137
138 result = tag.doAfterBody();
139 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
140 assertEquals(stack.peek(), "three");
141 assertEquals(stack.getContext().get("myId"), "three");
142
143 result = tag.doAfterBody();
144 assertEquals(result, TagSupport.SKIP_BODY);
145
146 result = tag.doEndTag();
147 assertEquals(result, TagSupport.EVAL_PAGE);
148 }
149
150
151 public void testArrayIterator() {
152 Foo foo = new Foo();
153 foo.setArray(new String[]{"test1", "test2", "test3"});
154
155 stack.push(foo);
156
157 tag.setValue("array");
158
159 iterateThreeStrings();
160 }
161
162 public void testCollectionIterator() {
163 Foo foo = new Foo();
164 ArrayList list = new ArrayList();
165 list.add("test1");
166 list.add("test2");
167 list.add("test3");
168 foo.setList(list);
169
170 stack.push(foo);
171
172 tag.setValue("list");
173
174 iterateThreeStrings();
175 }
176
177 public void testIteratorWithDefaultValue() {
178 stack.push(new String[]{"test1", "test2", "test3"});
179 iterateThreeStrings();
180 }
181
182 public void testMapIterator() {
183 Foo foo = new Foo();
184 HashMap map = new HashMap();
185 map.put("test1", "123");
186 map.put("test2", "456");
187 map.put("test3", "789");
188 foo.setMap(map);
189
190 stack.push(foo);
191
192 tag.setValue("map");
193
194 int result = 0;
195
196 try {
197 result = tag.doStartTag();
198 } catch (JspException e) {
199 e.printStackTrace();
200 fail();
201 }
202
203 assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
204 assertEquals(4, stack.size());
205 assertTrue(stack.getRoot().peek() instanceof Map.Entry);
206
207 try {
208 result = tag.doAfterBody();
209 } catch (JspException e) {
210 e.printStackTrace();
211 fail();
212 }
213
214 assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
215 assertEquals(4, stack.size());
216 assertTrue(stack.getRoot().peek() instanceof Map.Entry);
217
218 try {
219 result = tag.doAfterBody();
220 } catch (JspException e) {
221 e.printStackTrace();
222 fail();
223 }
224
225 assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
226 assertEquals(4, stack.size());
227 assertTrue(stack.getRoot().peek() instanceof Map.Entry);
228
229 try {
230 result = tag.doAfterBody();
231 } catch (JspException e) {
232 e.printStackTrace();
233 fail();
234 }
235
236 assertEquals(TagSupport.SKIP_BODY, result);
237 assertEquals(3, stack.size());
238 }
239
240 public void testStatus() {
241 Foo foo = new Foo();
242 foo.setArray(new String[]{"test1", "test2", "test3"});
243
244 stack.push(foo);
245
246 tag.setValue("array");
247 tag.setStatus("fooStatus");
248
249 int result = 0;
250
251 try {
252 result = tag.doStartTag();
253 } catch (JspException e) {
254 e.printStackTrace();
255 fail();
256 }
257
258 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
259 assertEquals("test1", stack.getRoot().peek());
260 assertEquals(4, stack.size());
261
262 IteratorStatus status = (IteratorStatus) context.get("fooStatus");
263 assertNotNull(status);
264 assertFalse(status.isLast());
265 assertTrue(status.isFirst());
266 assertEquals(0, status.getIndex());
267 assertEquals(1, status.getCount());
268 assertTrue(status.isOdd());
269 assertFalse(status.isEven());
270
271 try {
272 result = tag.doAfterBody();
273 } catch (JspException e) {
274 e.printStackTrace();
275 fail();
276 }
277
278 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
279 assertEquals("test2", stack.getRoot().peek());
280 assertEquals(4, stack.size());
281
282 status = (IteratorStatus) context.get("fooStatus");
283 assertNotNull(status);
284 assertFalse(status.isLast());
285 assertFalse(status.isFirst());
286 assertEquals(1, status.getIndex());
287 assertEquals(2, status.getCount());
288 assertFalse(status.isOdd());
289 assertTrue(status.isEven());
290
291 try {
292 result = tag.doAfterBody();
293 } catch (JspException e) {
294 e.printStackTrace();
295 fail();
296 }
297
298 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
299 assertEquals("test3", stack.getRoot().peek());
300 assertEquals(4, stack.size());
301
302 status = (IteratorStatus) context.get("fooStatus");
303 assertNotNull(status);
304 assertTrue(status.isLast());
305 assertFalse(status.isFirst());
306 assertEquals(2, status.getIndex());
307 assertEquals(3, status.getCount());
308 assertTrue(status.isOdd());
309 assertFalse(status.isEven());
310 }
311
312 public void testEmptyArray() {
313 Foo foo = new Foo();
314 foo.setArray(new String[]{});
315
316 stack.push(foo);
317
318 tag.setValue("array");
319
320 validateSkipBody();
321 }
322
323 public void testNullArray() {
324 Foo foo = new Foo();
325 foo.setArray(null);
326
327 stack.push(foo);
328
329 tag.setValue("array");
330
331 validateSkipBody();
332 }
333
334 public void testEmptyCollection() {
335 Foo foo = new Foo();
336 foo.setList(new ArrayList());
337
338 stack.push(foo);
339
340 tag.setValue("list");
341
342 validateSkipBody();
343 }
344
345 public void testNullCollection() {
346 Foo foo = new Foo();
347 foo.setList(null);
348
349 stack.push(foo);
350
351 tag.setValue("list");
352
353 validateSkipBody();
354 }
355
356 public void testCounter() throws JspException {
357 tag.setBegin("0");
358 tag.setEnd("5");
359 validateCounter(new Integer[]{0, 1, 2, 3, 4, 5});
360 }
361
362 public void testCounterWithStackValues() throws JspException {
363 stack.getContext().put("begin", 0);
364 stack.getContext().put("end", 5);
365 tag.setBegin("%{#begin}");
366 tag.setEnd("%{#end}");
367 validateCounter(new Integer[]{0, 1, 2, 3, 4, 5});
368 }
369
370 public void testCounterWithList() throws JspException {
371 Foo foo = new Foo();
372 ArrayList list = new ArrayList();
373 list.add("a");
374 list.add("b");
375 list.add("c");
376 list.add("d");
377 foo.setList(list);
378
379 stack.push(foo);
380
381 tag.setValue("list");
382
383 tag.setBegin("0");
384 tag.setEnd("2");
385 validateCounter(new String[]{"a", "b", "c"});
386 }
387
388 public void testCounterWithArray() throws JspException {
389 Foo foo = new Foo();
390 ArrayList list = new ArrayList();
391 foo.setArray(new String[]{"a", "b", "c", "d"});
392
393 stack.push(foo);
394
395 tag.setValue("array");
396
397 tag.setBegin("0");
398 tag.setEnd("2");
399 validateCounter(new String[]{"a", "b", "c"});
400 }
401
402
403 public void testCounterWithListNoEnd() throws JspException {
404 Foo foo = new Foo();
405 ArrayList list = new ArrayList();
406 list.add("a");
407 list.add("b");
408 list.add("c");
409 list.add("d");
410 foo.setList(list);
411
412 stack.push(foo);
413
414 tag.setValue("list");
415
416 tag.setBegin("0");
417 validateCounter(new String[]{"a", "b", "c", "d"});
418 }
419
420 public void testCounterWithArrayNoEnd() throws JspException {
421 Foo foo = new Foo();
422 ArrayList list = new ArrayList();
423 foo.setArray(new String[]{"a", "b", "c", "d"});
424
425 stack.push(foo);
426
427 tag.setValue("array");
428
429 tag.setBegin("0");
430 validateCounter(new String[]{"a", "b", "c", "d"});
431 }
432
433 public void testCounterWithList2() throws JspException {
434 Foo foo = new Foo();
435 ArrayList list = new ArrayList();
436 list.add("a");
437 list.add("b");
438 list.add("c");
439 list.add("d");
440 foo.setList(list);
441
442 stack.push(foo);
443
444 tag.setValue("list");
445
446 tag.setBegin("1");
447 tag.setEnd("2");
448 validateCounter(new String[]{"b", "c"});
449 }
450
451 public void testCounterWithArray2() throws JspException {
452 Foo foo = new Foo();
453 ArrayList list = new ArrayList();
454 foo.setArray(new String[]{"a", "b", "c", "d"});
455
456 stack.push(foo);
457
458 tag.setValue("array");
459
460 tag.setBegin("1");
461 tag.setEnd("2");
462 validateCounter(new String[]{"b", "c"});
463 }
464
465 public void testCounterWithListNoEnd2() throws JspException {
466 Foo foo = new Foo();
467 ArrayList list = new ArrayList();
468 list.add("a");
469 list.add("b");
470 list.add("c");
471 list.add("d");
472 foo.setList(list);
473
474 stack.push(foo);
475
476 tag.setValue("list");
477
478 tag.setBegin("2");
479 validateCounter(new String[]{"c", "d"});
480 }
481
482 public void testCounterWithArrayNoEnd2() throws JspException {
483 Foo foo = new Foo();
484 ArrayList list = new ArrayList();
485 foo.setArray(new String[]{"a", "b", "c", "d"});
486
487 stack.push(foo);
488
489 tag.setValue("array");
490
491 tag.setBegin("2");
492 validateCounter(new String[]{"c", "d"});
493 }
494
495 public void testCounter2() throws JspException {
496 tag.setBegin("2");
497 tag.setEnd("5");
498 validateCounter(new Integer[]{2, 3, 4, 5});
499 }
500
501 public void testCounterWithStep() throws JspException {
502 tag.setBegin("0");
503 tag.setEnd("5");
504 tag.setStep("2");
505 validateCounter(new Integer[]{0, 2, 4});
506 }
507
508 public void testCounterWithListAndStep() throws JspException {
509 Foo foo = new Foo();
510 ArrayList list = new ArrayList();
511 list.add("a");
512 list.add("b");
513 list.add("c");
514 list.add("d");
515 foo.setList(list);
516
517 stack.push(foo);
518
519 tag.setValue("list");
520
521 tag.setStep("2");
522 tag.setBegin("0");
523 tag.setEnd("3");
524
525 validateCounter(new String[]{"a", "c"});
526 }
527
528 public void testCounterWithArrayAndStep() throws JspException {
529 Foo foo = new Foo();
530 ArrayList list = new ArrayList();
531 foo.setArray(new String[]{"a", "b", "c", "d"});
532
533 stack.push(foo);
534
535 tag.setValue("array");
536
537 tag.setStep("2");
538 tag.setBegin("0");
539 tag.setEnd("3");
540
541 validateCounter(new String[]{"a", "c"});
542 }
543
544 public void testCounterWithListAndStepNoEnd() throws JspException {
545 Foo foo = new Foo();
546 ArrayList list = new ArrayList();
547 list.add("a");
548 list.add("b");
549 list.add("c");
550 list.add("d");
551 foo.setList(list);
552
553 stack.push(foo);
554
555 tag.setValue("list");
556
557 tag.setStep("2");
558 tag.setBegin("0");
559
560 validateCounter(new String[]{"a", "c"});
561 }
562
563 public void testCounterWithArrayAndStepNoEnd() throws JspException {
564 Foo foo = new Foo();
565 ArrayList list = new ArrayList();
566 foo.setArray(new String[]{"a", "b", "c", "d"});
567
568 stack.push(foo);
569
570 tag.setValue("array");
571
572 tag.setStep("2");
573 tag.setBegin("0");
574
575 validateCounter(new String[]{"a", "c"});
576 }
577
578 public void testCounterWithNegativeStep() throws JspException {
579 tag.setBegin("8");
580 tag.setEnd("5");
581 tag.setStep("-1");
582 validateCounter(new Integer[]{8, 7, 6, 5});
583 }
584
585 public void testCounterWithListAndNegativeStep() throws JspException {
586 Foo foo = new Foo();
587 ArrayList list = new ArrayList();
588 list.add("a");
589 list.add("b");
590 list.add("c");
591 list.add("d");
592 foo.setList(list);
593
594 stack.push(foo);
595
596 tag.setValue("list");
597
598 tag.setStep("-1");
599 tag.setBegin("3");
600 tag.setEnd("1");
601
602 validateCounter(new String[]{"d", "c", "b"});
603 }
604
605 public void testCounterWithListAndNegativeStepNoEnd() throws JspException {
606 Foo foo = new Foo();
607 ArrayList list = new ArrayList();
608 list.add("a");
609 list.add("b");
610 list.add("c");
611 list.add("d");
612 foo.setList(list);
613
614 stack.push(foo);
615
616 tag.setValue("list");
617
618 tag.setStep("-1");
619 tag.setBegin("3");
620
621 validateCounter(new String[]{"d", "c", "b", "a"});
622 }
623
624 public void testCounterWithArrayAndNegativeStep() throws JspException {
625 Foo foo = new Foo();
626 ArrayList list = new ArrayList();
627 list.add("a");
628 list.add("b");
629 list.add("c");
630 list.add("d");
631 foo.setList(list);
632
633 stack.push(foo);
634
635 tag.setValue("list");
636
637 tag.setStep("-1");
638 tag.setBegin("3");
639 tag.setEnd("1");
640
641 validateCounter(new String[]{"d", "c", "b"});
642 }
643
644 public void testCounterWithArrayAndNegativeStepNoEnd() throws JspException {
645 Foo foo = new Foo();
646 ArrayList list = new ArrayList();
647 list.add("a");
648 list.add("b");
649 list.add("c");
650 list.add("d");
651 foo.setList(list);
652
653 stack.push(foo);
654
655 tag.setValue("list");
656
657 tag.setStep("-1");
658 tag.setBegin("3");
659
660 validateCounter(new String[]{"d", "c", "b", "a"});
661 }
662
663 protected void validateCounter(Object[] expectedValues) throws JspException {
664 List values = new ArrayList();
665 try {
666 int result = tag.doStartTag();
667 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
668 values.add(stack.getRoot().peek());
669 } catch (JspException e) {
670 e.printStackTrace();
671 fail();
672 }
673
674 while (tag.doAfterBody() == TagSupport.EVAL_BODY_AGAIN) {
675 values.add(stack.getRoot().peek());
676 }
677
678 assertEquals(expectedValues.length, values.size());
679 ListUtils.isEqualList(Arrays.asList(expectedValues), values);
680 }
681
682 protected void setUp() throws Exception {
683 super.setUp();
684
685
686 tag = new IteratorTag();
687
688 MockBodyContent mockBodyContent = new TestMockBodyContent();
689 mockBodyContent.setupGetEnclosingWriter(new MockJspWriter());
690 tag.setBodyContent(mockBodyContent);
691
692
693 tag.setPageContext(pageContext);
694 }
695
696 private void iterateThreeStrings() {
697 int result = 0;
698
699 try {
700 result = tag.doStartTag();
701 } catch (JspException e) {
702 e.printStackTrace();
703 fail();
704 }
705
706 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
707 assertEquals("test1", stack.getRoot().peek());
708 assertEquals(4, stack.size());
709
710 try {
711 result = tag.doAfterBody();
712 } catch (JspException e) {
713 e.printStackTrace();
714 fail();
715 }
716
717 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
718 assertEquals("test2", stack.getRoot().peek());
719 assertEquals(4, stack.size());
720
721 try {
722 result = tag.doAfterBody();
723 } catch (JspException e) {
724 e.printStackTrace();
725 fail();
726 }
727
728 assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
729 assertEquals("test3", stack.getRoot().peek());
730 assertEquals(4, stack.size());
731
732 try {
733 result = tag.doAfterBody();
734 } catch (JspException e) {
735 e.printStackTrace();
736 fail();
737 }
738
739 assertEquals(result, TagSupport.SKIP_BODY);
740 assertEquals(3, stack.size());
741 }
742
743 private void validateSkipBody() {
744 int result = 0;
745
746 try {
747 result = tag.doStartTag();
748 } catch (JspException e) {
749 e.printStackTrace();
750 fail();
751 }
752
753 assertEquals(result, TagSupport.SKIP_BODY);
754 try {
755 result = tag.doEndTag();
756 } catch (JspException e) {
757 e.printStackTrace();
758 fail();
759 }
760 }
761
762 class Foo {
763 private Collection list;
764 private Map map;
765 private String[] array;
766
767 public void setArray(String[] array) {
768 this.array = array;
769 }
770
771 public String[] getArray() {
772 return array;
773 }
774
775 public void setList(Collection list) {
776 this.list = list;
777 }
778
779 public Collection getList() {
780 return list;
781 }
782
783 public void setMap(Map map) {
784 this.map = map;
785 }
786
787 public Map getMap() {
788 return map;
789 }
790 }
791
792 class TestMockBodyContent extends MockBodyContent {
793 public String getString() {
794 return ".-.";
795 }
796 }
797 }