View Javadoc

    1   /*
    2    * $Id: SortIteratorTagTest.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.Comparator;
    25  import java.util.Iterator;
    26  import java.util.List;
    27
    28  import javax.servlet.jsp.JspException;
    29
    30  import org.apache.struts2.views.jsp.iterator.SortIteratorTag;
    31
    32  import com.opensymphony.xwork2.Action;
    33  import com.opensymphony.xwork2.ActionSupport;
    34
    35  /***
    36   * Test case to test SortIteratorTag.
    37   *
    38   */
    39  public class SortIteratorTagTest extends AbstractTagTest {
    40
    41      public void testSortWithoutId() throws Exception {
    42          SortIteratorTag tag = new SortIteratorTag();
    43
    44          tag.setComparator("comparator");
    45          tag.setSource("source");
    46
    47          tag.setPageContext(pageContext);
    48          tag.doStartTag();
    49
    50          // if not an Iterator, just let the ClassCastException be thrown as error instead of failure
    51          Iterator sortedIterator = (Iterator) stack.findValue("top");
    52
    53          assertNotNull(sortedIterator);
    54          // 1
    55          assertTrue(sortedIterator.hasNext());
    56          assertEquals(sortedIterator.next(), new Integer(1));
    57          // 2
    58          assertTrue(sortedIterator.hasNext());
    59          assertEquals(sortedIterator.next(), new Integer(2));
    60          // 3.
    61          assertTrue(sortedIterator.hasNext());
    62          assertEquals(sortedIterator.next(), new Integer(3));
    63          // 4.
    64          assertTrue(sortedIterator.hasNext());
    65          assertEquals(sortedIterator.next(), new Integer(4));
    66          // 5
    67          assertTrue(sortedIterator.hasNext());
    68          assertEquals(sortedIterator.next(), new Integer(5));
    69
    70          assertFalse(sortedIterator.hasNext());
    71          tag.doEndTag();
    72      }
    73
    74      public void testSortWithIdIteratorAvailableInStackTop() throws Exception {
    75
    76          SortIteratorTag tag = new SortIteratorTag();
    77
    78          tag.setId("myId");
    79          tag.setComparator("comparator");
    80          tag.setSource("source");
    81
    82          tag.setPageContext(pageContext);
    83          tag.doStartTag();
    84
    85          {
    86              Iterator sortedIterator = (Iterator) stack.findValue("top");
    87
    88              assertNotNull(sortedIterator);
    89              // 1
    90              assertTrue(sortedIterator.hasNext());
    91              assertEquals(sortedIterator.next(), new Integer(1));
    92              // 2
    93              assertTrue(sortedIterator.hasNext());
    94              assertEquals(sortedIterator.next(), new Integer(2));
    95              // 3
    96              assertTrue(sortedIterator.hasNext());
    97              assertEquals(sortedIterator.next(), new Integer(3));
    98              // 4
    99              assertTrue(sortedIterator.hasNext());
    100             assertEquals(sortedIterator.next(), new Integer(4));
    101             // 5
    102             assertTrue(sortedIterator.hasNext());
    103             assertEquals(sortedIterator.next(), new Integer(5));
    104
    105             assertFalse(sortedIterator.hasNext());
    106         }
    107
    108         tag.doEndTag();
    109     }
    110
    111
    112     public void testSortWithIdIteratorAvailableInPageContext() throws Exception {
    113         SortIteratorTag tag = new SortIteratorTag();
    114
    115         tag.setId("myId");
    116         tag.setComparator("comparator");
    117         tag.setSource("source");
    118
    119         tag.setPageContext(pageContext);
    120         tag.doStartTag();
    121
    122         {
    123             Iterator sortedIterator = (Iterator) pageContext.getAttribute("myId");
    124
    125             assertNotNull(sortedIterator);
    126             // 1
    127             assertTrue(sortedIterator.hasNext());
    128             assertEquals(sortedIterator.next(), new Integer(1));
    129             // 2
    130             assertTrue(sortedIterator.hasNext());
    131             assertEquals(sortedIterator.next(), new Integer(2));
    132             // 3
    133             assertTrue(sortedIterator.hasNext());
    134             assertEquals(sortedIterator.next(), new Integer(3));
    135             // 4
    136             assertTrue(sortedIterator.hasNext());
    137             assertEquals(sortedIterator.next(), new Integer(4));
    138             // 5
    139             assertTrue(sortedIterator.hasNext());
    140             assertEquals(sortedIterator.next(), new Integer(5));
    141
    142             assertFalse(sortedIterator.hasNext());
    143         }
    144
    145         tag.doEndTag();
    146     }
    147
    148     public void testSortWithIllegalSource() throws Exception {
    149         SortIteratorTag tag = new SortIteratorTag();
    150
    151         tag.setComparator("comparator");
    152         tag.setSource("badSource");
    153
    154         try {
    155             tag.setPageContext(pageContext);
    156             tag.doStartTag();
    157             tag.doEndTag();
    158             fail("JspException expected");
    159         }
    160         catch (JspException e) {
    161             // ok
    162             assertTrue(true);
    163         }
    164     }
    165
    166     public void testSortWithIllegalComparator() throws Exception {
    167         SortIteratorTag tag = new SortIteratorTag();
    168
    169         tag.setComparator("badComparator");
    170         tag.setSource("source");
    171
    172         try {
    173             tag.setPageContext(pageContext);
    174             tag.doStartTag();
    175             tag.doEndTag();
    176             fail("JspException expected");
    177         }
    178         catch (JspException e) {
    179             // good
    180             assertTrue(true);
    181         }
    182
    183     }
    184
    185     public Action getAction() {
    186         return new ActionSupport() {
    187             public Comparator getComparator() {
    188                 return new Comparator() {
    189                     public int compare(Object o1, Object o2) {
    190                         Integer i1 = (Integer) o1;
    191                         Integer i2 = (Integer) o2;
    192
    193                         return (i1.intValue() - i2.intValue());
    194                     }
    195                 };
    196             }
    197
    198             public List getSource() {
    199                 List l = new ArrayList();
    200                 l.add(new Integer(3));
    201                 l.add(new Integer(1));
    202                 l.add(new Integer(2));
    203                 l.add(new Integer(5));
    204                 l.add(new Integer(4));
    205                 return l;
    206             }
    207
    208             public Object getBadComparator() {
    209                 return new Object();
    210             }
    211
    212             public Object getBadSource() {
    213                 return new Object();
    214             }
    215         };
    216     }
    217 }