KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > logic > TestIterateTag


1 /*
2  * $Id: TestIterateTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 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.struts.taglib.logic;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.apache.cactus.WebResponse;
33 import org.apache.struts.taglib.SimpleBeanForTesting;
34 import org.apache.struts.taglib.TaglibTestBase;
35
36
37
38 /**
39  * Suite of unit tests for the
40  * <code>org.apache.struts.taglib.logic.IterateTag</code> class.
41  *
42  */

43 public class TestIterateTag extends TaglibTestBase {
44     
45     private int iterations = 2;
46     
47     /**
48      * Defines the testcase name for JUnit.
49      *
50      * @param theName the testcase's name.
51      */

52     public TestIterateTag(String JavaDoc theName) {
53         super(theName);
54     }
55
56     /**
57      * Start the tests.
58      *
59      * @param theArgs the arguments. Not used
60      */

61     public static void main(String JavaDoc[] theArgs) {
62         junit.awtui.TestRunner.main(new String JavaDoc[] {TestIterateTag.class.getName()});
63     }
64
65     /**
66      * @return a test suite (<code>TestSuite</code>) that includes all methods
67      * starting with "test"
68      */

69     public static Test suite() {
70         // All methods starting with "test" will be executed in the test suite.
71
return new TestSuite(TestIterateTag.class);
72     }
73
74
75    /**
76      * Testing <code>IterateTag</code> using name attribute in
77      * the application scope.
78      *
79      * Tests the equivalent of this tag in a jsp:
80      * <logic:iterate id="theId" name="testApplicationScopeNameIterateList"
81      * scope="application">
82      *
83      */

84
85     // ========= Application
86
public void testApplicationScopeNameIterateList()
87         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
88         
89         String JavaDoc testKey = "testApplicationScopeNameIterateList";
90
91         ArrayList JavaDoc lst = new ArrayList JavaDoc();
92         for (int i = 0; i < iterations; i++) {
93             lst.add("test" + i);
94         }
95         
96         pageContext.setAttribute(testKey, lst,
97                                     PageContext.APPLICATION_SCOPE);
98
99         IterateTag tag = new IterateTag();
100         tag.setPageContext(pageContext);
101         tag.setId("theId");
102         tag.setName(testKey);
103         tag.setScope("application");
104         
105         int iteration = 0;
106         tag.doStartTag();
107         tag.doInitBody();
108         do
109         {
110             out.print((String JavaDoc)pageContext.getAttribute("theId"));
111             iteration++;
112         
113         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
114         tag.doEndTag();
115         assertEquals(iterations, iteration);
116     }
117
118     public void endApplicationScopeNameIterateList (WebResponse response){
119         String JavaDoc output = response.getText();
120         String JavaDoc compare = "";
121         for (int i = 0; i < iterations; i++) {
122             compare += "test" + i;
123         }
124
125         //fix for introduced carriage return / line feeds
126
output = replace(output,"\r","");
127         output = replace(output,"\n","");
128
129         assertEquals(compare, output);
130     }
131     
132     // ========= Session
133
public void testSessionScopeNameIterateList()
134         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
135         
136         String JavaDoc testKey = "testSessionScopeNameIterateList";
137
138         ArrayList JavaDoc lst = new ArrayList JavaDoc();
139         for (int i = 0; i < iterations; i++) {
140             lst.add("test" + i);
141         }
142         
143         pageContext.setAttribute(testKey, lst,
144                                     PageContext.SESSION_SCOPE);
145
146         IterateTag tag = new IterateTag();
147         tag.setPageContext(pageContext);
148         tag.setId("theId");
149         tag.setName(testKey);
150         tag.setScope("session");
151         
152         int iteration = 0;
153         tag.doStartTag();
154         tag.doInitBody();
155         do
156         {
157             out.print((String JavaDoc)pageContext.getAttribute("theId"));
158             iteration++;
159         
160         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
161         tag.doEndTag();
162         assertEquals(iterations, iteration);
163     }
164
165     public void endSessionScopeNameIterateList (WebResponse response){
166         String JavaDoc output = response.getText();
167         String JavaDoc compare = "";
168         for (int i = 0; i < iterations; i++) {
169             compare += "test" + i;
170         }
171
172         //fix for introduced carriage return / line feeds
173
output = replace(compare,"\r","");
174         output = replace(output,"\n","");
175
176         assertEquals(compare, output);
177     }
178
179     // ========= Request
180
public void testRequestScopeNameIterateList()
181         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
182         
183         String JavaDoc testKey = "testRequestScopeNameIterateList";
184
185         ArrayList JavaDoc lst = new ArrayList JavaDoc();
186         for (int i = 0; i < iterations; i++) {
187             lst.add("test" + i);
188         }
189         
190         pageContext.setAttribute(testKey, lst,
191                                     PageContext.REQUEST_SCOPE);
192
193         IterateTag tag = new IterateTag();
194         tag.setPageContext(pageContext);
195         tag.setId("theId");
196         tag.setName(testKey);
197         tag.setScope("request");
198         
199         int iteration = 0;
200         tag.doStartTag();
201         tag.doInitBody();
202         do
203         {
204             out.print((String JavaDoc)pageContext.getAttribute("theId"));
205             iteration++;
206         
207         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
208         tag.doEndTag();
209         assertEquals(iterations, iteration);
210     }
211
212     public void endRequestScopeNameIterateList (WebResponse response){
213         String JavaDoc output = response.getText();
214         String JavaDoc compare = "";
215         for (int i = 0; i < iterations; i++) {
216             compare += "test" + i;
217         }
218
219         //fix for introduced carriage return / line feeds
220
output = replace(compare,"\r","");
221         output = replace(output,"\r","");
222
223         assertEquals(compare, output);
224     }
225
226
227    /**
228      * Testing <code>IterateTag</code> using name attribute in
229      * the application scope.
230      *
231      * Tests the equivalent of this tag in a jsp:
232      * <logic:iterate id="theId" name="testApplicationScopeNameIterateList"
233      * property="list" scope="application">
234      *
235      */

236     
237     // ========= Application
238
public void testApplicationScopePropertyIterateList()
239         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
240         
241         
242         String JavaDoc testKey = "testApplicationScopePropertyIterate";
243
244         ArrayList JavaDoc lst = new ArrayList JavaDoc();
245         for (int i = 0; i < iterations; i++) {
246             lst.add("test" + i);
247         }
248         
249         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
250         sbft.setList(lst);
251         
252         pageContext.setAttribute(testKey, sbft,
253                                     PageContext.APPLICATION_SCOPE);
254
255         IterateTag tag = new IterateTag();
256         tag.setPageContext(pageContext);
257         /*
258          * Tests the equivalent of this tag in a jsp:
259          * <logic:iterate id="theId" name="testApplicationScopePropertyIterate"
260          * scope="application">
261          */

262         tag.setId("theId");
263         tag.setName(testKey);
264         tag.setScope("application");
265         tag.setProperty("list");
266         
267         int iteration = 0;
268         tag.doStartTag();
269         tag.doInitBody();
270         do
271         {
272             out.print((String JavaDoc)pageContext.getAttribute("theId"));
273             iteration++;
274         
275         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
276         tag.doEndTag();
277         assertEquals(iterations, iteration);
278     }
279
280     public void endApplicationScopePropertyIterateList (WebResponse response){
281         String JavaDoc output = response.getText();
282         String JavaDoc compare = "";
283         for (int i = 0; i < iterations; i++) {
284             compare += "test" + i;
285         }
286         
287         //fix for introduced carriage return / line feeds
288
output = replace(compare,"\r","");
289         output = replace(output,"\n","");
290         
291         assertEquals(compare, output);
292     }
293
294     
295     // ========= Session
296
public void testSessionScopePropertyIteratesList()
297         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
298         
299         
300         String JavaDoc testKey = "testSessionScopePropertyIterate";
301
302         ArrayList JavaDoc lst = new ArrayList JavaDoc();
303         for (int i = 0; i < iterations; i++) {
304             lst.add("test" + i);
305         }
306         
307         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
308         sbft.setList(lst);
309         
310         pageContext.setAttribute(testKey, sbft,
311                                     PageContext.SESSION_SCOPE);
312
313         IterateTag tag = new IterateTag();
314         tag.setPageContext(pageContext);
315         /*
316          * Tests the equivalent of this tag in a jsp:
317          * <logic:iterate id="theId" name="testSessionScopePropertyIterate"
318          * scope="session">
319          */

320         tag.setId("theId");
321         tag.setName(testKey);
322         tag.setScope("session");
323         tag.setProperty("list");
324         
325         int iteration = 0;
326         tag.doStartTag();
327         tag.doInitBody();
328         do
329         {
330             out.print((String JavaDoc)pageContext.getAttribute("theId"));
331             iteration++;
332         
333         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
334         tag.doEndTag();
335         assertEquals(iterations, iteration);
336     }
337
338     public void endSessionScopePropertyIterateList (WebResponse response){
339         String JavaDoc output = response.getText();
340         String JavaDoc compare = "";
341         for (int i = 0; i < iterations; i++) {
342             compare += "test" + i;
343         }
344         
345         //fix for introduced carriage return / line feeds
346
output = replace(compare,"\r","");
347         output = replace(output,"\n","");
348         
349         assertEquals(compare, output);
350     }
351
352     
353     // ========= Request
354
public void testRequestScopePropertyIteratesList()
355         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
356         
357         
358         String JavaDoc testKey = "testRequestScopePropertyIterate";
359
360         ArrayList JavaDoc lst = new ArrayList JavaDoc();
361         for (int i = 0; i < iterations; i++) {
362             lst.add("test" + i);
363         }
364         
365         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
366         sbft.setList(lst);
367         
368         pageContext.setAttribute(testKey, sbft,
369                                     PageContext.REQUEST_SCOPE);
370
371         IterateTag tag = new IterateTag();
372         tag.setPageContext(pageContext);
373         /*
374          * Tests the equivalent of this tag in a jsp:
375          * <logic:iterate id="theId" name="testRequestScopePropertyIterate"
376          * scope="request">
377          */

378         tag.setId("theId");
379         tag.setName(testKey);
380         tag.setScope("request");
381         tag.setProperty("list");
382         
383         int iteration = 0;
384         tag.doStartTag();
385         tag.doInitBody();
386         do
387         {
388             out.print((String JavaDoc)pageContext.getAttribute("theId"));
389             iteration++;
390         
391         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
392         tag.doEndTag();
393         assertEquals(iterations, iteration);
394     }
395
396     public void endRequestScopePropertyIterateList (WebResponse response){
397         String JavaDoc output = response.getText();
398         String JavaDoc compare = "";
399         for (int i = 0; i < iterations; i++) {
400             compare += "test" + i;
401         }
402         
403         //fix for introduced carriage return / line feeds
404
output = replace(compare,"\r","");
405         output = replace(output,"\n","");
406         
407         assertEquals(compare, output);
408     }
409
410
411
412    /**
413      * Testing <code>IterateTag</code> using name attribute in
414      * the application scope.
415      *
416      * Tests the equivalent of this tag in a jsp:
417      * <logic:iterate id="theId" name="testApplicationScopeNameIterateEnumeration"
418      * scope="application">
419      *
420      */

421
422     // ========= Application
423
public void testApplicationScopeNameIterateEnumeration()
424         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
425         
426         String JavaDoc testKey = "testApplicationScopeNameIterateEnumeration";
427         
428         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Application Scope Name Iterate Enumeration");
429
430         pageContext.setAttribute(testKey, st,
431                                     PageContext.APPLICATION_SCOPE);
432
433         IterateTag tag = new IterateTag();
434         tag.setPageContext(pageContext);
435         tag.setId("theId");
436         tag.setName(testKey);
437         tag.setScope("application");
438         
439         tag.doStartTag();
440         tag.doInitBody();
441         do
442         {
443             out.print((String JavaDoc)pageContext.getAttribute("theId"));
444         
445         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
446         tag.doEndTag();
447
448     }
449
450     public void endApplicationScopeNameIterateEnumeration (WebResponse response){
451         String JavaDoc output = response.getText();
452         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Application Scope Name Iterate Enumeration");
453         String JavaDoc compare = "";
454         
455         while (st.hasMoreTokens()) {
456             compare += st.nextToken();
457         }
458         
459         //fix for introduced carriage return / line feeds
460
output = replace(compare,"\r","");
461         output = replace(output,"\n","");
462
463         assertEquals(compare, output);
464     }
465
466     // ========= Session
467
public void testSessionScopeNameIterateEnumeration()
468         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
469         
470         String JavaDoc testKey = "testSessionScopeNameIterateEnumeration";
471         
472         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Session Scope Name Iterate Enumeration");
473
474         pageContext.setAttribute(testKey, st,
475                                     PageContext.SESSION_SCOPE);
476
477         IterateTag tag = new IterateTag();
478         tag.setPageContext(pageContext);
479         tag.setId("theId");
480         tag.setName(testKey);
481         tag.setScope("session");
482         
483         tag.doStartTag();
484         tag.doInitBody();
485         do
486         {
487             out.print((String JavaDoc)pageContext.getAttribute("theId"));
488         
489         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
490         tag.doEndTag();
491
492     }
493
494     public void endSessionScopeNameIterateEnumeration (WebResponse response){
495         String JavaDoc output = response.getText();
496         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Session Scope Name Iterate Enumeration");
497         String JavaDoc compare = "";
498         
499         while (st.hasMoreTokens()) {
500             compare += st.nextToken();
501         }
502         
503         //fix for introduced carriage return / line feeds
504
output = replace(compare,"\r","");
505         output = replace(output,"\n","");
506
507         assertEquals(compare, output);
508     }
509
510     // ========= Request
511
public void testRequestScopeNameIterateEnumeration()
512         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
513         
514         String JavaDoc testKey = "testRequestScopeNameIterateEnumeration";
515         
516         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Request Scope Name Iterate Enumeration");
517
518         pageContext.setAttribute(testKey, st,
519                                     PageContext.REQUEST_SCOPE);
520
521         IterateTag tag = new IterateTag();
522         tag.setPageContext(pageContext);
523         tag.setId("theId");
524         tag.setName(testKey);
525         tag.setScope("request");
526         
527         tag.doStartTag();
528         tag.doInitBody();
529         do
530         {
531             out.print((String JavaDoc)pageContext.getAttribute("theId"));
532         
533         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
534         tag.doEndTag();
535
536     }
537
538     public void endRequestScopeNameIterateEnumeration (WebResponse response){
539         String JavaDoc output = response.getText();
540         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Request Scope Name Iterate Enumeration");
541         String JavaDoc compare = "";
542         
543         while (st.hasMoreTokens()) {
544             compare += st.nextToken();
545         }
546         
547         //fix for introduced carriage return / line feeds
548
output = replace(compare,"\r","");
549         output = replace(output,"\n","");
550
551         assertEquals(compare, output);
552     }
553
554
555    /**
556      * Testing <code>IterateTag</code> using property attribute in
557      * the application scope.
558      *
559      * Tests the equivalent of this tag in a jsp:
560      * <logic:iterate id="theId" name="testApplicationScopePropertyIterateEnumeration"
561      * scope="application">
562      *
563      */

564
565     // ========= Application
566
public void testApplicationScopePropertyIterateEnumeration()
567         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
568         
569         String JavaDoc testKey = "testApplicationScopePropertyIterateEnumeration";
570         
571         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Application Scope Property Iterate Enumeration");
572
573         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
574         sbft.setEnumeration(st);
575
576         pageContext.setAttribute(testKey, sbft,
577                                     PageContext.APPLICATION_SCOPE);
578
579         IterateTag tag = new IterateTag();
580         tag.setPageContext(pageContext);
581         tag.setId("theId");
582         tag.setName(testKey);
583         tag.setScope("application");
584         tag.setProperty("enumeration");
585         
586         tag.doStartTag();
587         tag.doInitBody();
588         do
589         {
590             out.print((String JavaDoc)pageContext.getAttribute("theId"));
591         
592         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
593         tag.doEndTag();
594
595     }
596
597     public void endApplicationScopePropertyIterateEnumeration (WebResponse response){
598         String JavaDoc output = response.getText();
599         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Application Scope Property Iterate Enumeration");
600         String JavaDoc compare = "";
601         
602         while (st.hasMoreTokens()) {
603             compare += st.nextToken();
604         }
605         
606         //fix for introduced carriage return / line feeds
607
output = replace(compare,"\r","");
608         output = replace(output,"\n","");
609
610         assertEquals(compare, output);
611     }
612
613     // ========= Session
614
public void testSessionScopePropertyIterateEnumeration()
615         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
616         
617         String JavaDoc testKey = "testSessionScopePropertyIterateEnumeration";
618         
619         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Session Scope Property Iterate Enumeration");
620
621         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
622         sbft.setEnumeration(st);
623
624         pageContext.setAttribute(testKey, sbft,
625                                     PageContext.SESSION_SCOPE);
626
627         IterateTag tag = new IterateTag();
628         tag.setPageContext(pageContext);
629         tag.setId("theId");
630         tag.setName(testKey);
631         tag.setScope("session");
632         tag.setProperty("enumeration");
633         
634         tag.doStartTag();
635         tag.doInitBody();
636         do
637         {
638             out.print((String JavaDoc)pageContext.getAttribute("theId"));
639         
640         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
641         tag.doEndTag();
642
643     }
644
645     public void endSessionScopePropertyIterateEnumeration (WebResponse response){
646         String JavaDoc output = response.getText();
647         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Session Scope Property Iterate Enumeration");
648         String JavaDoc compare = "";
649         
650         while (st.hasMoreTokens()) {
651             compare += st.nextToken();
652         }
653         
654         //fix for introduced carriage return / line feeds
655
output = replace(compare,"\r","");
656         output = replace(output,"\n","");
657
658         assertEquals(compare, output);
659     }
660
661     // ========= Request
662
public void testRequestScopePropertyIterateEnumeration()
663         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
664         
665         String JavaDoc testKey = "testRequestScopePropertyIterateEnumeration";
666         
667         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Request Scope Property Iterate Enumeration");
668
669         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
670         sbft.setEnumeration(st);
671
672         pageContext.setAttribute(testKey, sbft,
673                                     PageContext.REQUEST_SCOPE);
674
675         IterateTag tag = new IterateTag();
676         tag.setPageContext(pageContext);
677         tag.setId("theId");
678         tag.setName(testKey);
679         tag.setScope("request");
680         tag.setProperty("enumeration");
681         
682         tag.doStartTag();
683         tag.doInitBody();
684         do
685         {
686             out.print((String JavaDoc)pageContext.getAttribute("theId"));
687         
688         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
689         tag.doEndTag();
690
691     }
692
693     public void endRequestScopePropertyIterateEnumeration (WebResponse response){
694         String JavaDoc output = response.getText();
695         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc("Request Scope Property Iterate Enumeration");
696         String JavaDoc compare = "";
697         
698         while (st.hasMoreTokens()) {
699             compare += st.nextToken();
700         }
701         
702         //fix for introduced carriage return / line feeds
703
output = replace(compare,"\r","");
704         output = replace(output,"\n","");
705
706         assertEquals(compare, output);
707     }
708
709
710
711
712
713    /**
714      * Testing <code>IterateTag</code> using name attribute in
715      * the application scope.
716      *
717      * Tests the equivalent of this tag in a jsp:
718      * <logic:iterate id="theId" name="testApplicationScopeNameIterateMap"
719      * scope="application">
720      *
721      */

722
723     // ========= Application
724
public void testApplicationScopeNameIterateMap()
725         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
726         
727         String JavaDoc testKey = "testApplicationScopeNameIterateMap";
728
729         HashMap JavaDoc map = new HashMap JavaDoc();
730         for (int i = 0; i < iterations; i++) {
731             map.put("test" + i,"test" + i);
732         }
733         
734         pageContext.setAttribute(testKey, map,
735                                     PageContext.APPLICATION_SCOPE);
736
737         IterateTag tag = new IterateTag();
738         tag.setPageContext(pageContext);
739         tag.setId("theId");
740         tag.setName(testKey);
741         tag.setScope("application");
742         
743         int iteration = 0;
744         tag.doStartTag();
745         tag.doInitBody();
746         do
747         {
748             out.print(pageContext.getAttribute("theId"));
749             iteration++;
750         
751         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
752         tag.doEndTag();
753         assertEquals(iterations, iteration);
754     }
755
756     public void endApplicationScopeNameIterateMap (WebResponse response){
757         String JavaDoc output = response.getText();
758         String JavaDoc compare = "";
759         for (int i = 0; i < iterations; i++) {
760             compare += "test" + i;
761         }
762
763         //fix for introduced carriage return / line feeds
764
output = replace(compare,"\r","");
765         output = replace(output,"\n","");
766
767         assertEquals(compare, output);
768     }
769     
770     // ========= Session
771
public void testSessionScopeNameIterateMap()
772         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
773         
774         String JavaDoc testKey = "testSessionScopeNameIterateMap";
775
776         HashMap JavaDoc map = new HashMap JavaDoc();
777         for (int i = 0; i < iterations; i++) {
778             map.put("test" + i,"test" + i);
779         }
780         
781         pageContext.setAttribute(testKey, map,
782                                     PageContext.SESSION_SCOPE);
783
784         IterateTag tag = new IterateTag();
785         tag.setPageContext(pageContext);
786         tag.setId("theId");
787         tag.setName(testKey);
788         tag.setScope("session");
789         
790         int iteration = 0;
791         tag.doStartTag();
792         tag.doInitBody();
793         do
794         {
795             out.print(pageContext.getAttribute("theId"));
796             iteration++;
797         
798         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
799         tag.doEndTag();
800         assertEquals(iterations, iteration);
801     }
802
803     public void endSessionScopeNameIterateMap (WebResponse response){
804         String JavaDoc output = response.getText();
805         String JavaDoc compare = "";
806         for (int i = 0; i < iterations; i++) {
807             compare += "test" + i;
808         }
809
810         //fix for introduced carriage return / line feeds
811
output = replace(compare,"\r","");
812         output = replace(output,"\n","");
813
814         assertEquals(compare, output);
815     }
816     
817     // ========= Request
818
public void testRequestScopeNameIterateMap()
819         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
820         
821         String JavaDoc testKey = "testRequestScopeNameIterateMap";
822
823         HashMap JavaDoc map = new HashMap JavaDoc();
824         for (int i = 0; i < iterations; i++) {
825             map.put("test" + i,"test" + i);
826         }
827         
828         pageContext.setAttribute(testKey, map,
829                                     PageContext.REQUEST_SCOPE);
830
831         IterateTag tag = new IterateTag();
832         tag.setPageContext(pageContext);
833         tag.setId("theId");
834         tag.setName(testKey);
835         tag.setScope("request");
836         
837         int iteration = 0;
838         tag.doStartTag();
839         tag.doInitBody();
840         do
841         {
842             out.print(pageContext.getAttribute("theId"));
843             iteration++;
844         
845         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
846         tag.doEndTag();
847         assertEquals(iterations, iteration);
848     }
849
850     public void endRequestScopeNameIterateMap (WebResponse response){
851         String JavaDoc output = response.getText();
852         String JavaDoc compare = "";
853         for (int i = 0; i < iterations; i++) {
854             compare += "test" + i;
855         }
856
857         //fix for introduced carriage return / line feeds
858
output = replace(compare,"\r","");
859         output = replace(output,"\n","");
860
861         assertEquals(compare, output);
862     }
863     
864
865
866
867    /**
868      * Testing <code>IterateTag</code> using property attribute in
869      * the application scope.
870      *
871      * Tests the equivalent of this tag in a jsp:
872      * <logic:iterate id="theId" name="testApplicationScopePropertyIterateMap"
873      * scope="application">
874      *
875      */

876
877     // ========= Application
878
public void testApplicationScopePropertyIterateMap()
879         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
880         
881         String JavaDoc testKey = "testApplicationScopePropertyIterateMap";
882
883         HashMap JavaDoc map = new HashMap JavaDoc();
884         for (int i = 0; i < iterations; i++) {
885             map.put("test" + i,"test" + i);
886         }
887         
888         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
889         sbft.setMap(map);
890         
891         pageContext.setAttribute(testKey, sbft,
892                                     PageContext.APPLICATION_SCOPE);
893
894         IterateTag tag = new IterateTag();
895         tag.setPageContext(pageContext);
896         tag.setId("theId");
897         tag.setName(testKey);
898         tag.setScope("application");
899         tag.setProperty("map");
900         
901         int iteration = 0;
902         tag.doStartTag();
903         tag.doInitBody();
904         do
905         {
906             out.print(pageContext.getAttribute("theId"));
907             iteration++;
908         
909         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
910         tag.doEndTag();
911         assertEquals(iterations, iteration);
912     }
913
914     public void endApplicationScopePropertyIterateMap (WebResponse response){
915         String JavaDoc output = response.getText();
916         String JavaDoc compare = "";
917         for (int i = 0; i < iterations; i++) {
918             compare += "test" + i;
919         }
920
921         //fix for introduced carriage return / line feeds
922
output = replace(compare,"\r","");
923         output = replace(output,"\n","");
924
925         assertEquals(compare, output);
926     }
927     
928     // ========= Session
929
public void testSessionScopePropertyIterateMap()
930         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
931         
932         String JavaDoc testKey = "testSessionScopePropertyIterateMap";
933
934         HashMap JavaDoc map = new HashMap JavaDoc();
935         for (int i = 0; i < iterations; i++) {
936             map.put("test" + i,"test" + i);
937         }
938
939         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
940         sbft.setMap(map);
941         
942         pageContext.setAttribute(testKey, sbft,
943                                     PageContext.SESSION_SCOPE);
944
945         IterateTag tag = new IterateTag();
946         tag.setPageContext(pageContext);
947         tag.setId("theId");
948         tag.setName(testKey);
949         tag.setScope("session");
950         tag.setProperty("map");
951         
952         int iteration = 0;
953         tag.doStartTag();
954         tag.doInitBody();
955         do
956         {
957             out.print(pageContext.getAttribute("theId"));
958             iteration++;
959         
960         } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
961         tag.doEndTag();
962         assertEquals(iterations, iteration);
963     }
964
965     public void endSessionScopePropertyIterateMap (WebResponse response){
966         String JavaDoc output = response.getText();
967         String JavaDoc compare = "";
968         for (int i = 0; i < iterations; i++) {
969             compare += "test" + i;
970         }
971
972         //fix for introduced carriage return / line feeds
973
output = replace(compare,"\r","");
974         output = replace(output,"\n","");
975
976         assertEquals(compare, output);
977     }
978     
979     // ========= Request
980
public void testRequestScopePropertyIterateMap()
981         throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
982         
983         String JavaDoc testKey = "testRequestScopePropertyIterateMap";
984
985         HashMap JavaDoc map = new HashMap JavaDoc();
986         for (int i = 0; i < iterations; i++) {
987             map.put("test" + i,"test" + i);
988         }
989         
990         SimpleBeanForTesting sbft = new SimpleBeanForTesting();
991         sbft.setMap(map);
992         
993         pageContext.setAttribute(testKey, sbft,
994                                     PageContext.REQUEST_SCOPE);
995
996         IterateTag tag = new IterateTag();
997         tag.setPageContext(pageContext);
998         tag.setId("theId");
999         tag.setName(testKey);
1000        tag.setScope("request");
1001        tag.setProperty("map");
1002        
1003        int iteration = 0;
1004        tag.doStartTag();
1005        tag.doInitBody();
1006        do
1007        {
1008            out.print(pageContext.getAttribute("theId"));
1009            iteration++;
1010        
1011        } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
1012        tag.doEndTag();
1013        assertEquals(iterations, iteration);
1014    }
1015
1016    public void endRequestScopePropertyIterateMap (WebResponse response){
1017        String JavaDoc output = response.getText();
1018        String JavaDoc compare = "";
1019        for (int i = 0; i < iterations; i++) {
1020            compare += "test" + i;
1021        }
1022
1023        //fix for introduced carriage return / line feeds
1024
output = replace(compare,"\r","");
1025        output = replace(output,"\n","");
1026
1027        assertEquals(compare, output);
1028    }
1029    
1030
1031
1032
1033
1034   /**
1035     * Testing <code>IterateTag</code> using name attribute in
1036     * the application scope.
1037     *
1038     * Tests the equivalent of this tag in a jsp:
1039     * <logic:iterate id="theId" name="testApplicationScopeNameIterateArray"
1040     * scope="application">
1041     *
1042     */

1043
1044    // ========= Application
1045
public void testApplicationScopeNameIterateArray()
1046        throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
1047        
1048        String JavaDoc testKey = "testApplicationScopeNameIterateArray";
1049
1050        String JavaDoc[] tst = new String JavaDoc[iterations];
1051        for (int i = 0; i < tst.length; i++) {
1052            tst[i] = "test" + i;
1053        }
1054        
1055        pageContext.setAttribute(testKey, tst,
1056                                    PageContext.APPLICATION_SCOPE);
1057
1058        IterateTag tag = new IterateTag();
1059        tag.setPageContext(pageContext);
1060        tag.setId("theId");
1061        tag.setName(testKey);
1062        tag.setScope("application");
1063        
1064        int iteration = 0;
1065        tag.doStartTag();
1066        tag.doInitBody();
1067        do
1068        {
1069            out.print(pageContext.getAttribute("theId"));
1070            iteration++;
1071        
1072        } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
1073        tag.doEndTag();
1074        assertEquals(iterations, iteration);
1075    }
1076
1077    public void endApplicationScopeNameIterateArray (WebResponse response){
1078        String JavaDoc output = response.getText();
1079        String JavaDoc[] tst = new String JavaDoc[iterations];
1080        for (int i = 0; i < tst.length; i++) {
1081            tst[i] = "test" + i;
1082        }
1083
1084        String JavaDoc compare = "";
1085        for (int i = 0; i < iterations; i++) {
1086            compare += tst[i];
1087        }
1088
1089        //fix for introduced carriage return / line feeds
1090
output = replace(compare,"\r","");
1091        output = replace(output,"\n","");
1092                
1093        assertEquals(compare, output);
1094    }
1095    
1096    // ========= Session
1097
public void testSessionScopeNameIterateArray()
1098        throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
1099        
1100        String JavaDoc testKey = "testSessionScopeNameIterateArray";
1101
1102        String JavaDoc[] tst = new String JavaDoc[iterations];
1103        for (int i = 0; i < tst.length; i++) {
1104            tst[i] = "test" + i;
1105        }
1106        
1107        pageContext.setAttribute(testKey, tst,
1108                                    PageContext.SESSION_SCOPE);
1109
1110        IterateTag tag = new IterateTag();
1111        tag.setPageContext(pageContext);
1112        tag.setId("theId");
1113        tag.setName(testKey);
1114        tag.setScope("session");
1115        
1116        int iteration = 0;
1117        tag.doStartTag();
1118        tag.doInitBody();
1119        do
1120        {
1121            out.print(pageContext.getAttribute("theId"));
1122            iteration++;
1123        
1124        } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
1125        tag.doEndTag();
1126        assertEquals(iterations, iteration);
1127    }
1128
1129    public void endSessionScopeNameIterateArray (WebResponse response){
1130        String JavaDoc output = response.getText();
1131        String JavaDoc[] tst = new String JavaDoc[iterations];
1132        for (int i = 0; i < tst.length; i++) {
1133            tst[i] = "test" + i;
1134        }
1135
1136        String JavaDoc compare = "";
1137        for (int i = 0; i < iterations; i++) {
1138            compare += tst[i];
1139        }
1140
1141        //fix for introduced carriage return / line feeds
1142
output = replace(compare,"\r","");
1143        output = replace(output,"\n","");
1144
1145        assertEquals(compare, output);
1146    }
1147    
1148    // ========= Request
1149
public void testRequestScopeNameIterateArray()
1150        throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
1151        
1152        String JavaDoc testKey = "testRequestScopeNameIterateArray";
1153
1154        String JavaDoc[] tst = new String JavaDoc[iterations];
1155        for (int i = 0; i < tst.length; i++) {
1156            tst[i] = "test" + i;
1157        }
1158        
1159        pageContext.setAttribute(testKey, tst,
1160                                    PageContext.REQUEST_SCOPE);
1161
1162        IterateTag tag = new IterateTag();
1163        tag.setPageContext(pageContext);
1164        tag.setId("theId");
1165        tag.setName(testKey);
1166        tag.setScope("request");
1167        
1168        int iteration = 0;
1169        tag.doStartTag();
1170        tag.doInitBody();
1171        do
1172        {
1173            out.print(pageContext.getAttribute("theId"));
1174            iteration++;
1175        
1176        } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
1177        tag.doEndTag();
1178        assertEquals(iterations, iteration);
1179    }
1180
1181    public void endRequestScopeNameIterateArray (WebResponse response){
1182        String JavaDoc output = response.getText();
1183        String JavaDoc[] tst = new String JavaDoc[iterations];
1184        for (int i = 0; i < tst.length; i++) {
1185            tst[i] = "test" + i;
1186        }
1187
1188        String JavaDoc compare = "";
1189        for (int i = 0; i < iterations; i++) {
1190            compare += tst[i];
1191        }
1192
1193        //fix for introduced carriage return / line feeds
1194
output = replace(compare,"\r","");
1195        output = replace(output,"\n","");
1196
1197        assertEquals(compare, output);
1198    }
1199    
1200
1201   /**
1202     * Testing <code>IterateTag</code> using property attribute in
1203     * the application scope.
1204     *
1205     * Tests the equivalent of this tag in a jsp:
1206     * <logic:iterate id="theId" name="testApplicationScopePropertyIterateArray"
1207     * scope="application">
1208     *
1209     */

1210
1211    // ========= Application
1212
public void testApplicationScopePropertyIterateArray()
1213        throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
1214        
1215        String JavaDoc testKey = "testApplicationScopePropertyIterateArray";
1216
1217        String JavaDoc[] tst = new String JavaDoc[iterations];
1218        for (int i = 0; i < tst.length; i++) {
1219            tst[i] = "test" + i;
1220        }
1221        SimpleBeanForTesting sbft = new SimpleBeanForTesting();
1222        sbft.setArray(tst);
1223        
1224        pageContext.setAttribute(testKey, sbft,
1225                                    PageContext.APPLICATION_SCOPE);
1226
1227        IterateTag tag = new IterateTag();
1228        tag.setPageContext(pageContext);
1229        tag.setId("theId");
1230        tag.setName(testKey);
1231        tag.setScope("application");
1232        tag.setProperty("array");
1233        
1234        int iteration = 0;
1235        tag.doStartTag();
1236        tag.doInitBody();
1237        do
1238        {
1239            out.print(pageContext.getAttribute("theId"));
1240            iteration++;
1241        
1242        } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
1243        tag.doEndTag();
1244        assertEquals(iterations, iteration);
1245    }
1246
1247    public void endApplicationScopePropertyIterateArray (WebResponse response){
1248        String JavaDoc output = response.getText();
1249        String JavaDoc[] tst = new String JavaDoc[iterations];
1250        for (int i = 0; i < tst.length; i++) {
1251            tst[i] = "test" + i;
1252        }
1253
1254        String JavaDoc compare = "";
1255        for (int i = 0; i < iterations; i++) {
1256            compare += tst[i];
1257        }
1258
1259        //fix for introduced carriage return / line feeds
1260
output = replace(compare,"\r","");
1261        output = replace(output,"\n","");
1262
1263        assertEquals(compare, output);
1264    }
1265    
1266    // ========= Session
1267
public void testSessionScopePropertyIterateArray()
1268        throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
1269        
1270        String JavaDoc testKey = "testSessionScopePropertyIterateArray";
1271
1272        String JavaDoc[] tst = new String JavaDoc[iterations];
1273        for (int i = 0; i < tst.length; i++) {
1274            tst[i] = "test" + i;
1275        }
1276
1277        SimpleBeanForTesting sbft = new SimpleBeanForTesting();
1278        sbft.setArray(tst);
1279
1280        pageContext.setAttribute(testKey, sbft,
1281                                    PageContext.SESSION_SCOPE);
1282
1283        IterateTag tag = new IterateTag();
1284        tag.setPageContext(pageContext);
1285        tag.setId("theId");
1286        tag.setName(testKey);
1287        tag.setScope("session");
1288        tag.setProperty("array");
1289        
1290        int iteration = 0;
1291        tag.doStartTag();
1292        tag.doInitBody();
1293        do
1294        {
1295            out.print(pageContext.getAttribute("theId"));
1296            iteration++;
1297        
1298        } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
1299        tag.doEndTag();
1300        assertEquals(iterations, iteration);
1301    }
1302
1303    public void endSessionScopePropertyIterateArray (WebResponse response){
1304        String JavaDoc output = response.getText();
1305        String JavaDoc[] tst = new String JavaDoc[iterations];
1306        for (int i = 0; i < tst.length; i++) {
1307            tst[i] = "test" + i;
1308        }
1309
1310        String JavaDoc compare = "";
1311        for (int i = 0; i < iterations; i++) {
1312            compare += tst[i];
1313        }
1314
1315        //fix for introduced carriage return / line feeds
1316
output = replace(compare,"\r","");
1317        output = replace(output,"\n","");
1318                
1319        assertEquals(compare, output);
1320    }
1321    
1322    // ========= Request
1323
public void testRequestScopePropertyIterateArray()
1324        throws ServletException JavaDoc, JspException JavaDoc, IOException JavaDoc {
1325        
1326        String JavaDoc testKey = "testRequestScopePropertyIterateArray";
1327
1328        String JavaDoc[] tst = new String JavaDoc[iterations];
1329        for (int i = 0; i < tst.length; i++) {
1330            tst[i] = "test" + i;
1331        }
1332        
1333        SimpleBeanForTesting sbft = new SimpleBeanForTesting();
1334        sbft.setArray(tst);
1335
1336        pageContext.setAttribute(testKey, sbft,
1337                                    PageContext.REQUEST_SCOPE);
1338
1339        IterateTag tag = new IterateTag();
1340        tag.setPageContext(pageContext);
1341        tag.setId("theId");
1342        tag.setName(testKey);
1343        tag.setScope("request");
1344        tag.setProperty("array");
1345        
1346        int iteration = 0;
1347        tag.doStartTag();
1348        tag.doInitBody();
1349        do
1350        {
1351            out.print(pageContext.getAttribute("theId"));
1352            iteration++;
1353        
1354        } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG);
1355        tag.doEndTag();
1356        assertEquals(iterations, iteration);
1357    }
1358
1359    public void endRequestScopePropertyIterateArray (WebResponse response){
1360        String JavaDoc output = response.getText();
1361        String JavaDoc[] tst = new String JavaDoc[iterations];
1362        for (int i = 0; i < tst.length; i++) {
1363            tst[i] = "test" + i;
1364        }
1365
1366        String JavaDoc compare = "";
1367        for (int i = 0; i < iterations; i++) {
1368            compare += tst[i];
1369        }
1370
1371        //fix for introduced carriage return / line feeds
1372
output = replace(compare,"\r","");
1373        output = replace(output,"\n","");
1374
1375        assertEquals(compare, output);
1376    }
1377    
1378
1379}
1380
Popular Tags