View Javadoc

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