View Javadoc

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