View Javadoc

    1   /*
    2    * $Id: IteratorGeneratorTagTest.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.Iterator;
    24
    25  import org.apache.struts2.util.IteratorGenerator.Converter;
    26  import org.apache.struts2.views.jsp.iterator.IteratorGeneratorTag;
    27
    28  import com.opensymphony.xwork2.Action;
    29  import com.opensymphony.xwork2.ActionSupport;
    30
    31  /***
    32   * Test case for IteratorGeneratorTag.
    33   *
    34   */
    35  public class IteratorGeneratorTagTest extends AbstractTagTest {
    36
    37      public void testGeneratorBasic() throws Exception {
    38          IteratorGeneratorTag tag = new IteratorGeneratorTag();
    39
    40          tag.setPageContext(pageContext);
    41          tag.setVal("%{'aaa,bbb,ccc,ddd,eee'}");
    42          tag.doStartTag();
    43          Object topOfStack = stack.findValue("top");
    44
    45
    46          assertTrue(topOfStack instanceof Iterator);
    47          // 1
    48          assertTrue(((Iterator)topOfStack).hasNext());
    49          assertEquals(((Iterator)topOfStack).next(), "aaa");
    50          // 2
    51          assertTrue(((Iterator)topOfStack).hasNext());
    52          assertEquals(((Iterator)topOfStack).next(), "bbb");
    53          // 3
    54          assertTrue(((Iterator)topOfStack).hasNext());
    55          assertEquals(((Iterator)topOfStack).next(), "ccc");
    56          // 4
    57          assertTrue(((Iterator)topOfStack).hasNext());
    58          assertEquals(((Iterator)topOfStack).next(), "ddd");
    59          // 5
    60          assertTrue(((Iterator)topOfStack).hasNext());
    61          assertEquals(((Iterator)topOfStack).next(),"eee");
    62
    63          assertFalse(((Iterator)topOfStack).hasNext());
    64
    65          tag.doEndTag();
    66          Object afterTopOfStack = stack.findValue("top");
    67
    68
    69          assertNotSame(afterTopOfStack, topOfStack);
    70      }
    71
    72      public void testGeneratorWithSeparator() throws Exception {
    73          IteratorGeneratorTag tag = new IteratorGeneratorTag();
    74
    75          tag.setPageContext(pageContext);
    76          tag.setVal("%{'aaa|bbb|ccc|ddd|eee'}");
    77          tag.setSeparator("|");
    78          tag.doStartTag();
    79          Object topOfStack = stack.findValue("top");
    80          tag.doEndTag();
    81          Object afterTopOfStack = stack.findValue("top");
    82
    83          assertTrue(topOfStack instanceof Iterator);
    84          // 1
    85          assertTrue(((Iterator)topOfStack).hasNext());
    86          assertEquals(((Iterator)topOfStack).next(), "aaa");
    87          // 2
    88          assertTrue(((Iterator)topOfStack).hasNext());
    89          assertEquals(((Iterator)topOfStack).next(), "bbb");
    90          // 3
    91          assertTrue(((Iterator)topOfStack).hasNext());
    92          assertEquals(((Iterator)topOfStack).next(), "ccc");
    93          // 4
    94          assertTrue(((Iterator)topOfStack).hasNext());
    95          assertEquals(((Iterator)topOfStack).next(), "ddd");
    96          // 5
    97          assertTrue(((Iterator)topOfStack).hasNext());
    98          assertEquals(((Iterator)topOfStack).next(), "eee");
    99
    100         assertFalse(((Iterator)topOfStack).hasNext());
    101         assertNotSame(afterTopOfStack, topOfStack);
    102     }
    103
    104     public void testGeneratorWithConverter() throws Exception {
    105         IteratorGeneratorTag tag = new IteratorGeneratorTag();
    106
    107         tag.setPageContext(pageContext);
    108         tag.setVal("%{'aaa, bbb, ccc, ddd, eee'}");
    109         tag.setConverter("myConverter");
    110         tag.doStartTag();
    111         Object topOfStack = stack.findValue("top");
    112         tag.doEndTag();
    113         Object afterTopOfStack = stack.findValue("top");
    114
    115         assertTrue(topOfStack instanceof Iterator);
    116         // 1.
    117         assertTrue(((Iterator)topOfStack).hasNext());
    118         assertEquals(((Iterator)topOfStack).next(), "myConverter-aaa");
    119         // 2
    120         assertTrue(((Iterator)topOfStack).hasNext());
    121         assertEquals(((Iterator)topOfStack).next(), "myConverter-bbb");
    122         // 3
    123         assertTrue(((Iterator)topOfStack).hasNext());
    124         assertEquals(((Iterator)topOfStack).next(), "myConverter-ccc");
    125         // 4.
    126         assertTrue(((Iterator)topOfStack).hasNext());
    127         assertEquals(((Iterator)topOfStack).next(), "myConverter-ddd");
    128         // 5.
    129         assertTrue(((Iterator)topOfStack).hasNext());
    130         assertEquals(((Iterator)topOfStack).next(), "myConverter-eee");
    131
    132         assertFalse(((Iterator)topOfStack).hasNext());
    133         assertNotSame(afterTopOfStack, topOfStack);
    134     }
    135
    136     public void testGeneratorWithId() throws Exception {
    137         IteratorGeneratorTag tag = new IteratorGeneratorTag();
    138         tag.setPageContext(pageContext);
    139         tag.setVal("%{'aaa,bbb,ccc,ddd,eee'}");
    140         tag.setId("myPageContextAttId");
    141         tag.doStartTag();
    142         tag.doEndTag();
    143
    144         Object pageContextIterator = pageContext.getAttribute("myPageContextAttId");
    145
    146         assertTrue(pageContextIterator instanceof Iterator);
    147         // 1
    148         assertTrue(((Iterator)pageContextIterator).hasNext());
    149         assertEquals(((Iterator)pageContextIterator).next(), "aaa");
    150         // 2.
    151         assertTrue(((Iterator)pageContextIterator).hasNext());
    152         assertEquals(((Iterator)pageContextIterator).next(), "bbb");
    153         // 3.
    154         assertTrue(((Iterator)pageContextIterator).hasNext());
    155         assertEquals(((Iterator)pageContextIterator).next(), "ccc");
    156         // 4
    157         assertTrue(((Iterator)pageContextIterator).hasNext());
    158         assertEquals(((Iterator)pageContextIterator).next(), "ddd");
    159         // 5
    160         assertTrue(((Iterator)pageContextIterator).hasNext());
    161         assertEquals(((Iterator)pageContextIterator).next(), "eee");
    162
    163         assertFalse(((Iterator)pageContextIterator).hasNext());
    164     }
    165
    166     public void testGeneratorWithCount() throws Exception {
    167         IteratorGeneratorTag tag = new IteratorGeneratorTag();
    168
    169         tag.setPageContext(pageContext);
    170         tag.setVal("%{'aaa,bbb,ccc,ddd,eee'}");
    171         tag.setCount("myCount");
    172         tag.doStartTag();
    173         Object topOfStack = stack.findValue("top");
    174         tag.doEndTag();
    175         Object afterTopOfStack = stack.findValue("top");
    176
    177
    178         assertTrue(topOfStack instanceof Iterator);
    179         // 1
    180         assertTrue(((Iterator)topOfStack).hasNext());
    181         assertEquals(((Iterator)topOfStack).next(), "aaa");
    182         // 2
    183         assertTrue(((Iterator)topOfStack).hasNext());
    184         assertEquals(((Iterator)topOfStack).next(), "bbb");
    185         // 3.
    186         assertTrue(((Iterator)topOfStack).hasNext());
    187         assertEquals(((Iterator)topOfStack).next(), "ccc");
    188
    189         assertFalse(((Iterator)topOfStack).hasNext());
    190         assertNotSame(topOfStack, afterTopOfStack);
    191     }
    192
    193
    194     public Action getAction() {
    195         return new ActionSupport() {
    196             public Converter getMyConverter() {
    197                 return new Converter() {
    198                     public Object convert(String value) throws Exception {
    199                         return "myConverter-"+value;
    200                     }
    201                 };
    202             }
    203
    204             public int getMyCount() {
    205                 return 3;
    206             }
    207         };
    208     }
    209 }
    210