View Javadoc

    1   /*
    2    * $Id: SubsetIteratorTagTest.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.Iterator;
    25  import java.util.List;
    26
    27  import org.apache.struts2.util.SubsetIteratorFilter.Decider;
    28  import org.apache.struts2.views.jsp.iterator.SubsetIteratorTag;
    29
    30  import com.opensymphony.xwork2.Action;
    31  import com.opensymphony.xwork2.ActionSupport;
    32
    33
    34  /***
    35   * Test case for SubsetIteratorTag.
    36   *
    37   */
    38  public class SubsetIteratorTagTest extends AbstractTagTest {
    39
    40
    41      public void testBasic() throws Exception {
    42          { // List as Source
    43              SubsetIteratorTag tag = new SubsetIteratorTag();
    44              tag.setPageContext(pageContext);
    45              tag.setSource("myList");
    46
    47              tag.doStartTag();
    48              Iterator subsetIterator = (Iterator) stack.findValue("top");
    49              tag.doEndTag();
    50
    51              assertEquals(subsetIterator.next(), new Integer(1));
    52              assertEquals(subsetIterator.next(), new Integer(2));
    53              assertEquals(subsetIterator.next(), new Integer(3));
    54              assertEquals(subsetIterator.next(), new Integer(4));
    55              assertEquals(subsetIterator.next(), new Integer(5));
    56          }
    57
    58          { // Array as Source
    59              SubsetIteratorTag tag = new SubsetIteratorTag();
    60              tag.setPageContext(pageContext);
    61              tag.setSource("myArray");
    62
    63              tag.doStartTag();
    64              Iterator subsetIterator = (Iterator) stack.findValue("top");
    65              tag.doEndTag();
    66
    67              assertEquals(subsetIterator.next(), new Integer(1));
    68              assertEquals(subsetIterator.next(), new Integer(2));
    69              assertEquals(subsetIterator.next(), new Integer(3));
    70              assertEquals(subsetIterator.next(), new Integer(4));
    71              assertEquals(subsetIterator.next(), new Integer(5));
    72          }
    73      }
    74
    75      public void testWithStartAttribute() throws Exception {
    76          { // List as source
    77              SubsetIteratorTag tag = new SubsetIteratorTag();
    78              tag.setPageContext(pageContext);
    79              tag.setSource("myList");
    80              tag.setStart("3");
    81
    82              tag.doStartTag();
    83              Iterator subsetIterator = (Iterator) stack.findValue("top");
    84              tag.doEndTag();
    85
    86              assertEquals(subsetIterator.next(), new Integer(4));
    87              assertEquals(subsetIterator.next(), new Integer(5));
    88          }
    89
    90          { // Array as source
    91              SubsetIteratorTag tag = new SubsetIteratorTag();
    92              tag.setPageContext(pageContext);
    93              tag.setSource("myArray");
    94              tag.setStart("3");
    95
    96              tag.doStartTag();
    97              Iterator subsetIterator = (Iterator) stack.findValue("top");
    98              tag.doEndTag();
    99
    100             assertEquals(subsetIterator.next(), new Integer(4));
    101             assertEquals(subsetIterator.next(), new Integer(5));
    102         }
    103     }
    104
    105     public void testWithCountAttribute() throws Exception {
    106         { // List as source
    107             SubsetIteratorTag tag = new SubsetIteratorTag();
    108             tag.setPageContext(pageContext);
    109             tag.setSource("myList");
    110             tag.setCount("3");
    111
    112             tag.doStartTag();
    113             Iterator subsetIterator = (Iterator) stack.findValue("top");
    114             tag.doEndTag();
    115
    116             assertEquals(subsetIterator.next(), new Integer(1));
    117             assertEquals(subsetIterator.next(), new Integer(2));
    118             assertEquals(subsetIterator.next(), new Integer(3));
    119         }
    120
    121         { // array as source
    122             SubsetIteratorTag tag = new SubsetIteratorTag();
    123             tag.setPageContext(pageContext);
    124             tag.setSource("myArray");
    125             tag.setCount("3");
    126
    127             tag.doStartTag();
    128             Iterator subsetIterator = (Iterator) stack.findValue("top");
    129             tag.doEndTag();
    130
    131             assertEquals(subsetIterator.next(), new Integer(1));
    132             assertEquals(subsetIterator.next(), new Integer(2));
    133             assertEquals(subsetIterator.next(), new Integer(3));
    134         }
    135     }
    136
    137     public void testWIthStartAndCountAttribute() throws Exception {
    138         { // List as source
    139             SubsetIteratorTag tag = new SubsetIteratorTag();
    140             tag.setPageContext(pageContext);
    141             tag.setSource("myList");
    142             tag.setStart("3");
    143             tag.setCount("3");
    144
    145             tag.doStartTag();
    146             Iterator subsetIterator = (Iterator) stack.findValue("top");
    147             tag.doEndTag();
    148
    149             assertEquals(subsetIterator.next(), new Integer("4"));
    150             assertEquals(subsetIterator.next(), new Integer("5"));
    151         }
    152
    153         {   // Array as source
    154             SubsetIteratorTag tag = new SubsetIteratorTag();
    155             tag.setPageContext(pageContext);
    156             tag.setSource("myArray");
    157             tag.setStart("3");
    158             tag.setCount("3");
    159
    160             tag.doStartTag();
    161             Iterator subsetIterator = (Iterator) stack.findValue("top");
    162             tag.doEndTag();
    163
    164             assertEquals(subsetIterator.next(), new Integer("4"));
    165             assertEquals(subsetIterator.next(), new Integer("5"));
    166         }
    167     }
    168
    169     public void testWithId() throws Exception {
    170         {   // List as Source
    171             SubsetIteratorTag tag = new SubsetIteratorTag();
    172             tag.setPageContext(pageContext);
    173             tag.setSource("myList");
    174             tag.setId("myPageContextId1");
    175
    176             tag.doStartTag();
    177             Iterator subsetIterator1 = (Iterator) stack.findValue("top");
    178             tag.doEndTag();
    179
    180             Iterator subsetIterator2 = (Iterator) pageContext.getAttribute("myPageContextId1");
    181
    182             assertNotNull(subsetIterator1);
    183             assertNotNull(subsetIterator2);
    184             assertEquals(subsetIterator1, subsetIterator2);
    185             assertEquals(subsetIterator2.next(), new Integer(1));
    186             assertEquals(subsetIterator2.next(), new Integer(2));
    187             assertEquals(subsetIterator2.next(), new Integer(3));
    188             assertEquals(subsetIterator2.next(), new Integer(4));
    189             assertEquals(subsetIterator2.next(), new Integer(5));
    190         }
    191
    192         {   // Array as source
    193             SubsetIteratorTag tag = new SubsetIteratorTag();
    194             tag.setPageContext(pageContext);
    195             tag.setSource("myArray");
    196             tag.setId("myPageContextId2");
    197
    198             tag.doStartTag();
    199             Iterator subsetIterator1 = (Iterator) stack.findValue("top");
    200             tag.doEndTag();
    201
    202             Iterator subsetIterator2 = (Iterator) pageContext.getAttribute("myPageContextId2");
    203
    204             assertNotNull(subsetIterator1);
    205             assertNotNull(subsetIterator2);
    206             assertEquals(subsetIterator1, subsetIterator2);
    207             assertEquals(subsetIterator2.next(), new Integer(1));
    208             assertEquals(subsetIterator2.next(), new Integer(2));
    209             assertEquals(subsetIterator2.next(), new Integer(3));
    210             assertEquals(subsetIterator2.next(), new Integer(4));
    211             assertEquals(subsetIterator2.next(), new Integer(5));
    212         }
    213     }
    214
    215     public void testWithDecider() throws Exception {
    216         {   // List as source
    217             SubsetIteratorTag tag = new SubsetIteratorTag();
    218             tag.setPageContext(pageContext);
    219             tag.setSource("myList");
    220             tag.setDecider("myDecider");
    221
    222             tag.doStartTag();
    223             Iterator subsetIterator = (Iterator) stack.findValue("top");
    224             tag.doEndTag();
    225
    226             assertEquals(subsetIterator.next(), new Integer(2));
    227             assertEquals(subsetIterator.next(), new Integer(4));
    228         }
    229
    230         {   // Array As source
    231             SubsetIteratorTag tag = new SubsetIteratorTag();
    232             tag.setPageContext(pageContext);
    233             tag.setSource("myList");
    234             tag.setDecider("myDecider");
    235
    236             tag.doStartTag();
    237             Iterator subsetIterator = (Iterator) stack.findValue("top");
    238             tag.doEndTag();
    239
    240             assertEquals(subsetIterator.next(), new Integer(2));
    241             assertEquals(subsetIterator.next(), new Integer(4));
    242         }
    243     }
    244
    245
    246
    247     public Action getAction() {
    248         return new ActionSupport() {
    249             public List getMyList() {
    250                 List l = new ArrayList();
    251                 l.add(new Integer(1));
    252                 l.add(new Integer(2));
    253                 l.add(new Integer(3));
    254                 l.add(new Integer(4));
    255                 l.add(new Integer(5));
    256                 return l;
    257             }
    258
    259             public Integer[] getMyArray() {
    260                 Integer[] integers = new Integer[5];
    261                 integers[0] = new Integer(1);
    262                 integers[1] = new Integer(2);
    263                 integers[2] = new Integer(3);
    264                 integers[3] = new Integer(4);
    265                 integers[4] = new Integer(5);
    266                 return integers;
    267             }
    268
    269             public Decider getMyDecider() {
    270                 return new Decider() {
    271                     public boolean decide(Object element) throws Exception {
    272                         int integer = ((Integer)element).intValue();
    273                         return (((integer % 2) == 0)?true:false);
    274                     }
    275                 };
    276             }
    277         };
    278     }
    279 }