View Javadoc

    1   /*
    2    * $Id: MergeIteratorTagTest.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.views.jsp.iterator.MergeIteratorTag;
    28
    29  import com.opensymphony.xwork2.Action;
    30  import com.opensymphony.xwork2.ActionSupport;
    31
    32  /***
    33   * Test case for MergeIteratorTag.
    34   *
    35   */
    36  public class MergeIteratorTagTest extends AbstractTagTest {
    37
    38      public void testMergingIteratorWithArrayAsSource() throws Exception {
    39          MergeIteratorTag tag = new MergeIteratorTag();
    40          tag.setPageContext(pageContext);
    41          tag.setId("myMergedIterator");
    42
    43          ParamTag iterator1ParamTag = new ParamTag();
    44          iterator1ParamTag.setPageContext(pageContext);
    45          iterator1ParamTag.setValue("myArr1");
    46
    47          ParamTag iterator2ParamTag = new ParamTag();
    48          iterator2ParamTag.setPageContext(pageContext);
    49          iterator2ParamTag.setValue("myArr2");
    50
    51          ParamTag iterator3ParamTag = new ParamTag();
    52          iterator3ParamTag.setPageContext(pageContext);
    53          iterator3ParamTag.setValue("myArr3");
    54
    55
    56          tag.doStartTag();
    57          iterator1ParamTag.doStartTag();
    58          iterator1ParamTag.doEndTag();
    59          iterator2ParamTag.doStartTag();
    60          iterator2ParamTag.doEndTag();
    61          iterator3ParamTag.doStartTag();
    62          iterator3ParamTag.doEndTag();
    63          tag.doEndTag();
    64
    65          Iterator mergedIterator = (Iterator) stack.findValue("#myMergedIterator"); // if not iterator, let CCE surface
    66
    67          assertNotNull(mergedIterator);
    68          assertTrue(mergedIterator.hasNext());
    69          assertEquals(mergedIterator.next(), "1");
    70          assertTrue(mergedIterator.hasNext());
    71          assertEquals(mergedIterator.next(), "a");
    72          assertTrue(mergedIterator.hasNext());
    73          assertEquals(mergedIterator.next(), "A");
    74          assertTrue(mergedIterator.hasNext());
    75          assertEquals(mergedIterator.next(), "2");
    76          assertTrue(mergedIterator.hasNext());
    77          assertEquals(mergedIterator.next(), "b");
    78          assertTrue(mergedIterator.hasNext());
    79          assertEquals(mergedIterator.next(), "B");
    80          assertTrue(mergedIterator.hasNext());
    81          assertEquals(mergedIterator.next(), "3");
    82          assertTrue(mergedIterator.hasNext());
    83          assertEquals(mergedIterator.next(), "c");
    84          assertTrue(mergedIterator.hasNext());
    85          assertEquals(mergedIterator.next(), "C");
    86          assertFalse(mergedIterator.hasNext());
    87      }
    88
    89
    90      public void testMergingIteratorsWithListAsSource() throws Exception {
    91          MergeIteratorTag tag = new MergeIteratorTag();
    92          tag.setPageContext(pageContext);
    93          tag.setId("myMergedIterator");
    94
    95          ParamTag iterator1ParamTag = new ParamTag();
    96          iterator1ParamTag.setPageContext(pageContext);
    97          iterator1ParamTag.setValue("myList1");
    98
    99          ParamTag iterator2ParamTag = new ParamTag();
    100         iterator2ParamTag.setPageContext(pageContext);
    101         iterator2ParamTag.setValue("myList2");
    102
    103         ParamTag iterator3ParamTag = new ParamTag();
    104         iterator3ParamTag.setPageContext(pageContext);
    105         iterator3ParamTag.setValue("myList3");
    106
    107
    108         tag.doStartTag();
    109         iterator1ParamTag.doStartTag();
    110         iterator1ParamTag.doEndTag();
    111         iterator2ParamTag.doStartTag();
    112         iterator2ParamTag.doEndTag();
    113         iterator3ParamTag.doStartTag();
    114         iterator3ParamTag.doEndTag();
    115         tag.doEndTag();
    116
    117         Iterator mergedIterator = (Iterator) stack.findValue("#myMergedIterator"); // if not iterator, let CCE surface
    118
    119         assertNotNull(mergedIterator);
    120         assertTrue(mergedIterator.hasNext());
    121         assertEquals(mergedIterator.next(), "1");
    122         assertTrue(mergedIterator.hasNext());
    123         assertEquals(mergedIterator.next(), "a");
    124         assertTrue(mergedIterator.hasNext());
    125         assertEquals(mergedIterator.next(), "A");
    126         assertTrue(mergedIterator.hasNext());
    127         assertEquals(mergedIterator.next(), "2");
    128         assertTrue(mergedIterator.hasNext());
    129         assertEquals(mergedIterator.next(), "b");
    130         assertTrue(mergedIterator.hasNext());
    131         assertEquals(mergedIterator.next(), "B");
    132         assertTrue(mergedIterator.hasNext());
    133         assertEquals(mergedIterator.next(), "3");
    134         assertTrue(mergedIterator.hasNext());
    135         assertEquals(mergedIterator.next(), "c");
    136         assertTrue(mergedIterator.hasNext());
    137         assertEquals(mergedIterator.next(), "C");
    138         assertFalse(mergedIterator.hasNext());
    139     }
    140
    141
    142
    143
    144     public Action getAction() {
    145         return new ActionSupport() {
    146             public List getMyList1() {
    147                 List l = new ArrayList();
    148                 l.add("1");
    149                 l.add("2");
    150                 l.add("3");
    151                 return l;
    152             }
    153
    154             public List getMyList2() {
    155                 List l = new ArrayList();
    156                 l.add("a");
    157                 l.add("b");
    158                 l.add("c");
    159                 return l;
    160             }
    161
    162             public List getMyList3() {
    163                 List l = new ArrayList();
    164                 l.add("A");
    165                 l.add("B");
    166                 l.add("C");
    167                 return l;
    168             }
    169
    170             public String[] getMyArr1() {
    171                 return new String[] { "1", "2", "3" };
    172             }
    173
    174             public String[] getMyArr2() {
    175                 return new String[] { "a", "b", "c" };
    176             }
    177
    178             public String[] getMyArr3() {
    179                 return new String[] {"A", "B", "C"};
    180             }
    181         };
    182     }
    183 }