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