001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import com.mockobjects.servlet.MockBodyContent;
008: import com.mockobjects.servlet.MockJspWriter;
009:
010: import javax.servlet.jsp.JspException;
011: import javax.servlet.jsp.tagext.TagSupport;
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Map;
017:
018: /**
019: * Test Case for Iterator Tag
020: *
021: * @author $Author: tmjee $
022: * @version $Date: 2006-01-25 17:58:03 +0100 (Wed, 25 Jan 2006) $ $Id: IteratorTagTest.java 2028 2006-01-25 16:58:03Z tmjee $
023: */
024: public class IteratorTagTest extends AbstractUITagTest {
025:
026: IteratorTag tag;
027:
028: public void testIteratingWithIdSpecified() throws Exception {
029: List list = new ArrayList();
030: list.add("one");
031: list.add("two");
032: list.add("three");
033: list.add("four");
034: list.add("five");
035:
036: Foo foo = new Foo();
037: foo.setList(list);
038:
039: stack.push(foo);
040:
041: tag.setValue("list");
042: tag.setId("myId");
043:
044: // one
045: int result = tag.doStartTag();
046: assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
047: assertEquals(stack.peek(), "one");
048: assertEquals(stack.getContext().get("myId"), "one");
049:
050: tag.doInitBody();
051:
052: // two
053: result = tag.doAfterBody();
054: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
055: assertEquals(stack.peek(), "two");
056: assertEquals(stack.getContext().get("myId"), "two");
057:
058: // three
059: result = tag.doAfterBody();
060: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
061: assertEquals(stack.peek(), "three");
062: assertEquals(stack.getContext().get("myId"), "three");
063:
064: // four
065: result = tag.doAfterBody();
066: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
067: assertEquals(stack.peek(), "four");
068: assertEquals(stack.getContext().get("myId"), "four");
069:
070: // five
071: result = tag.doAfterBody();
072: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
073: assertEquals(stack.peek(), "five");
074: assertEquals(stack.getContext().get("myId"), "five");
075:
076: result = tag.doAfterBody();
077: assertEquals(result, TagSupport.SKIP_BODY);
078:
079: result = tag.doEndTag();
080: assertEquals(result, TagSupport.EVAL_PAGE);
081: }
082:
083: public void testArrayIterator() {
084: Foo foo = new Foo();
085: foo.setArray(new String[] { "test1", "test2", "test3" });
086:
087: stack.push(foo);
088:
089: tag.setValue("array");
090:
091: iterateThreeStrings();
092: }
093:
094: public void testCollectionIterator() {
095: Foo foo = new Foo();
096: ArrayList list = new ArrayList();
097: list.add("test1");
098: list.add("test2");
099: list.add("test3");
100: foo.setList(list);
101:
102: stack.push(foo);
103:
104: tag.setValue("list");
105:
106: iterateThreeStrings();
107: }
108:
109: public void testIteratorWithDefaultValue() {
110: stack.push(new String[] { "test1", "test2", "test3" });
111: iterateThreeStrings();
112: }
113:
114: public void testMapIterator() {
115: Foo foo = new Foo();
116: HashMap map = new HashMap();
117: map.put("test1", "123");
118: map.put("test2", "456");
119: map.put("test3", "789");
120: foo.setMap(map);
121:
122: stack.push(foo);
123:
124: tag.setValue("map");
125:
126: int result = 0;
127:
128: try {
129: result = tag.doStartTag();
130: } catch (JspException e) {
131: e.printStackTrace();
132: fail();
133: }
134:
135: assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
136: assertEquals(4, stack.size());
137: assertTrue(stack.getRoot().peek() instanceof Map.Entry);
138:
139: try {
140: result = tag.doAfterBody();
141: } catch (JspException e) {
142: e.printStackTrace();
143: fail();
144: }
145:
146: assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
147: assertEquals(4, stack.size());
148: assertTrue(stack.getRoot().peek() instanceof Map.Entry);
149:
150: try {
151: result = tag.doAfterBody();
152: } catch (JspException e) {
153: e.printStackTrace();
154: fail();
155: }
156:
157: assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
158: assertEquals(4, stack.size());
159: assertTrue(stack.getRoot().peek() instanceof Map.Entry);
160:
161: try {
162: result = tag.doAfterBody();
163: } catch (JspException e) {
164: e.printStackTrace();
165: fail();
166: }
167:
168: assertEquals(TagSupport.SKIP_BODY, result);
169: assertEquals(3, stack.size());
170: }
171:
172: public void testStatus() {
173: Foo foo = new Foo();
174: foo.setArray(new String[] { "test1", "test2", "test3" });
175:
176: stack.push(foo);
177:
178: tag.setValue("array");
179: tag.setStatus("fooStatus");
180:
181: int result = 0;
182:
183: try {
184: result = tag.doStartTag();
185: } catch (JspException e) {
186: e.printStackTrace();
187: fail();
188: }
189:
190: assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
191: assertEquals("test1", stack.getRoot().peek());
192: assertEquals(4, stack.size());
193:
194: IteratorStatus status = (IteratorStatus) context
195: .get("fooStatus");
196: assertNotNull(status);
197: assertFalse(status.isLast());
198: assertTrue(status.isFirst());
199: assertEquals(0, status.getIndex());
200: assertEquals(1, status.getCount());
201: assertTrue(status.isOdd());
202: assertFalse(status.isEven());
203:
204: try {
205: result = tag.doAfterBody();
206: } catch (JspException e) {
207: e.printStackTrace();
208: fail();
209: }
210:
211: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
212: assertEquals("test2", stack.getRoot().peek());
213: assertEquals(4, stack.size());
214:
215: status = (IteratorStatus) context.get("fooStatus");
216: assertNotNull(status);
217: assertFalse(status.isLast());
218: assertFalse(status.isFirst());
219: assertEquals(1, status.getIndex());
220: assertEquals(2, status.getCount());
221: assertFalse(status.isOdd());
222: assertTrue(status.isEven());
223:
224: try {
225: result = tag.doAfterBody();
226: } catch (JspException e) {
227: e.printStackTrace();
228: fail();
229: }
230:
231: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
232: assertEquals("test3", stack.getRoot().peek());
233: assertEquals(4, stack.size());
234:
235: status = (IteratorStatus) context.get("fooStatus");
236: assertNotNull(status);
237: assertTrue(status.isLast());
238: assertFalse(status.isFirst());
239: assertEquals(2, status.getIndex());
240: assertEquals(3, status.getCount());
241: assertTrue(status.isOdd());
242: assertFalse(status.isEven());
243: }
244:
245: public void testEmptyArray() {
246: Foo foo = new Foo();
247: foo.setArray(new String[] {});
248:
249: stack.push(foo);
250:
251: tag.setValue("array");
252:
253: validateSkipBody();
254: }
255:
256: public void testNullArray() {
257: Foo foo = new Foo();
258: foo.setArray(null);
259:
260: stack.push(foo);
261:
262: tag.setValue("array");
263:
264: validateSkipBody();
265: }
266:
267: public void testEmptyCollection() {
268: Foo foo = new Foo();
269: foo.setList(new ArrayList());
270:
271: stack.push(foo);
272:
273: tag.setValue("list");
274:
275: validateSkipBody();
276: }
277:
278: public void testNullCollection() {
279: Foo foo = new Foo();
280: foo.setList(null);
281:
282: stack.push(foo);
283:
284: tag.setValue("list");
285:
286: validateSkipBody();
287: }
288:
289: protected void setUp() throws Exception {
290: super .setUp();
291:
292: // create the needed objects
293: tag = new IteratorTag();
294:
295: MockBodyContent mockBodyContent = new TestMockBodyContent();
296: mockBodyContent.setupGetEnclosingWriter(new MockJspWriter());
297: tag.setBodyContent(mockBodyContent);
298:
299: // associate the tag with the mock page request
300: tag.setPageContext(pageContext);
301: }
302:
303: private void iterateThreeStrings() {
304: int result = 0;
305:
306: try {
307: result = tag.doStartTag();
308: } catch (JspException e) {
309: e.printStackTrace();
310: fail();
311: }
312:
313: assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
314: assertEquals("test1", stack.getRoot().peek());
315: assertEquals(4, stack.size());
316:
317: try {
318: result = tag.doAfterBody();
319: } catch (JspException e) {
320: e.printStackTrace();
321: fail();
322: }
323:
324: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
325: assertEquals("test2", stack.getRoot().peek());
326: assertEquals(4, stack.size());
327:
328: try {
329: result = tag.doAfterBody();
330: } catch (JspException e) {
331: e.printStackTrace();
332: fail();
333: }
334:
335: assertEquals(result, TagSupport.EVAL_BODY_AGAIN);
336: assertEquals("test3", stack.getRoot().peek());
337: assertEquals(4, stack.size());
338:
339: try {
340: result = tag.doAfterBody();
341: } catch (JspException e) {
342: e.printStackTrace();
343: fail();
344: }
345:
346: assertEquals(result, TagSupport.SKIP_BODY);
347: assertEquals(3, stack.size());
348: }
349:
350: private void validateSkipBody() {
351: int result = 0;
352:
353: try {
354: result = tag.doStartTag();
355: } catch (JspException e) {
356: e.printStackTrace();
357: fail();
358: }
359:
360: assertEquals(result, TagSupport.SKIP_BODY);
361: try {
362: result = tag.doEndTag();
363: } catch (JspException e) {
364: e.printStackTrace();
365: fail();
366: }
367: }
368:
369: class Foo {
370: private Collection list;
371: private Map map;
372: private String[] array;
373:
374: public void setArray(String[] array) {
375: this .array = array;
376: }
377:
378: public String[] getArray() {
379: return array;
380: }
381:
382: public void setList(Collection list) {
383: this .list = list;
384: }
385:
386: public Collection getList() {
387: return list;
388: }
389:
390: public void setMap(Map map) {
391: this .map = map;
392: }
393:
394: public Map getMap() {
395: return map;
396: }
397: }
398:
399: class TestMockBodyContent extends MockBodyContent {
400: public String getString() {
401: return ".-.";
402: }
403: }
404: }
|