View Javadoc

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