View Javadoc

    1   /*
    2    * $Id: SelectTest.java 508509 2007-02-16 17:35:12Z hermanns $
    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.ui;
    22
    23  import java.math.BigDecimal;
    24  import java.util.ArrayList;
    25  import java.util.Collection;
    26  import java.util.List;
    27
    28  import org.apache.struts2.TestAction;
    29  import org.apache.struts2.views.jsp.AbstractUITagTest;
    30
    31
    32  /***
    33   */
    34  public class SelectTest extends AbstractUITagTest {
    35
    36
    37      public void testHeaderCanBePreselected() throws Exception {
    38          SelectTag tag = new SelectTag();
    39          tag.setPageContext(pageContext);
    40          tag.setLabel("myLabel");
    41          tag.setList("#{1:'Cat',2:'Dog'}");
    42          tag.setName("myPet");
    43          tag.setHeaderKey("-1");
    44          tag.setHeaderValue("--- Please Select ---");
    45          tag.setValue("%{'-1'}");
    46
    47          tag.doStartTag();
    48          tag.doEndTag();
    49
    50          verify(SelectTag.class.getResource("Select-8.txt"));
    51      }
    52
    53      /***
    54       * Tests WW-1601: Select tag template does not work properly for Object like Byte.
    55       */
    56      public void testByte() throws Exception {
    57          ByteObject hello = new ByteObject(new Byte((byte)1), "hello");
    58          ByteObject foo = new ByteObject(new Byte((byte)2), "foo");
    59
    60          TestAction testAction = (TestAction) action;
    61
    62          Collection collection = new ArrayList(2);
    63          // expect strings to be returned, we're still dealing with HTTP here!
    64          collection.add("1");
    65          collection.add("2");
    66          testAction.setCollection(collection);
    67
    68          List list2 = new ArrayList();
    69          list2.add(hello);
    70          list2.add(foo);
    71          testAction.setList2(list2);
    72
    73          SelectTag tag = new SelectTag();
    74          tag.setPageContext(pageContext);
    75          tag.setLabel("mylabel");
    76          tag.setName("collection");
    77          tag.setList("list2");
    78          tag.setListKey("byte");
    79          tag.setListValue("name");
    80          tag.setMultiple("true");
    81          tag.setTitle("mytitle");
    82          tag.setOnmousedown("alert('onmousedown');");
    83          tag.setOnmousemove("alert('onmousemove');");
    84          tag.setOnmouseout("alert('onmouseout');");
    85          tag.setOnmouseover("alert('onmouseover');");
    86          tag.setOnmouseup("alert('onmouseup');");
    87
    88          tag.doStartTag();
    89          tag.doEndTag();
    90
    91          verify(SelectTag.class.getResource("Select-10.txt"));
    92      }
    93
    94
    95      /***
    96       * Tests WW-455: Select tag template does not work properly for Object like BigDecimal.
    97       */
    98      public void testBigDecimal() throws Exception {
    99          BigDecimalObject hello = new BigDecimalObject("hello", new BigDecimal(1));
    100         BigDecimalObject foo = new BigDecimalObject("foo", new BigDecimal(2));
    101
    102         TestAction testAction = (TestAction) action;
    103
    104         Collection collection = new ArrayList(2);
    105         // expect strings to be returned, we're still dealing with HTTP here!
    106         collection.add("hello");
    107         collection.add("foo");
    108         testAction.setCollection(collection);
    109
    110         List list2 = new ArrayList();
    111         list2.add(hello);
    112         list2.add(foo);
    113         list2.add(new BigDecimalObject("<cat>", new BigDecimal(1.500)));
    114         testAction.setList2(list2);
    115
    116         SelectTag tag = new SelectTag();
    117         tag.setPageContext(pageContext);
    118         tag.setLabel("mylabel");
    119         tag.setName("collection");
    120         tag.setList("list2");
    121         tag.setListKey("name");
    122         tag.setListValue("bigDecimal");
    123         tag.setMultiple("true");
    124         tag.setTitle("mytitle");
    125         tag.setOnmousedown("alert('onmousedown');");
    126         tag.setOnmousemove("alert('onmousemove');");
    127         tag.setOnmouseout("alert('onmouseout');");
    128         tag.setOnmouseover("alert('onmouseover');");
    129         tag.setOnmouseup("alert('onmouseup');");
    130
    131         tag.doStartTag();
    132         tag.doEndTag();
    133
    134         verify(SelectTag.class.getResource("Select-3.txt"));
    135     }
    136
    137     public class BigDecimalObject {
    138         private String name;
    139         private BigDecimal bigDecimal;
    140
    141         public BigDecimalObject(String name, BigDecimal bigDecimal) {
    142             this.name = name;
    143             this.bigDecimal = bigDecimal;
    144         }
    145
    146         public String getName() {
    147             return name;
    148         }
    149
    150         public BigDecimal getBigDecimal() {
    151             return bigDecimal;
    152         }
    153     }
    154
    155     public class ByteObject {
    156         private String name;
    157         private Byte byteValue;
    158
    159         public ByteObject(Byte byteValue, String name) {
    160             this.name = name;
    161             this.byteValue = byteValue;
    162         }
    163
    164         public String getName() {
    165             return name;
    166         }
    167
    168         public Byte getByte() {
    169             return byteValue;
    170         }
    171     }
    172
    173     public void testNullList() throws Exception {
    174         TestAction testAction = (TestAction) action;
    175         testAction.setList2(null);
    176
    177         SelectTag tag = new SelectTag();
    178         tag.setName("collection");
    179         tag.setList("list2");
    180         tag.setLabel("tmjee_name");
    181
    182         tag.setPageContext(pageContext);
    183         try {
    184             tag.doStartTag();
    185             tag.doEndTag();
    186             fail("exception should have been thrown value of select tag is null");
    187         }
    188         catch(Exception e) {
    189             assertTrue(true);
    190         }
    191     }
    192
    193
    194     public void testEmptyList() throws Exception {
    195         TestAction testAction = (TestAction) action;
    196         testAction.setList2(new ArrayList());
    197
    198         SelectTag tag = new SelectTag();
    199         tag.setName("collection");
    200         tag.setList("list2");
    201         tag.setLabel("tmjee_name");
    202
    203         tag.setPageContext(pageContext);
    204         tag.doStartTag();
    205         tag.doEndTag();
    206
    207         verify(SelectTag.class.getResource("Select-4.txt"));
    208     }
    209
    210     public void testMultiple() throws Exception {
    211         TestAction testAction = (TestAction) action;
    212         Collection collection = new ArrayList(2);
    213         collection.add("hello");
    214         collection.add("foo");
    215         testAction.setCollection(collection);
    216         testAction.setList(new String[][]{
    217                 {"hello", "world"},
    218                 {"foo", "bar"},
    219                 {"cat", "dog"}
    220         });
    221
    222         SelectTag tag = new SelectTag();
    223         tag.setPageContext(pageContext);
    224         tag.setLabel("mylabel");
    225         tag.setName("collection");
    226         tag.setList("list");
    227         tag.setListKey("top[0]");
    228         tag.setListValue("top[1]");
    229         tag.setMultiple("true");
    230         tag.setOnmousedown("alert('onmousedown');");
    231         tag.setOnmousemove("alert('onmousemove');");
    232         tag.setOnmouseout("alert('onmouseout');");
    233         tag.setOnmouseover("alert('onmouseover');");
    234         tag.setOnmouseup("alert('onmouseup');");
    235
    236         tag.doStartTag();
    237         tag.doEndTag();
    238
    239         verify(SelectTag.class.getResource("Select-2.txt"));
    240     }
    241
    242     public void testSimple() throws Exception {
    243         TestAction testAction = (TestAction) action;
    244         testAction.setFoo("hello");
    245         testAction.setList(new String[][]{
    246                 {"hello", "world"},
    247                 {"foo", "bar"}
    248         });
    249
    250         SelectTag tag = new SelectTag();
    251         tag.setPageContext(pageContext);
    252         tag.setEmptyOption("true");
    253         tag.setLabel("mylabel");
    254         tag.setName("foo");
    255         tag.setList("list");
    256         tag.setListKey("top[0]");
    257         tag.setListValue("top[1]");
    258
    259         // header stuff
    260         tag.setHeaderKey("headerKey");
    261         tag.setHeaderValue("headerValue");
    262
    263         // empty option
    264         tag.setEmptyOption("true");
    265
    266         tag.doStartTag();
    267         tag.doEndTag();
    268
    269         verify(SelectTag.class.getResource("Select-1.txt"));
    270     }
    271
    272     public void testSimpleWithNulls() throws Exception {
    273         TestAction testAction = (TestAction) action;
    274         testAction.setFoo("hello");
    275         testAction.setList(new String[][]{
    276                 {"hello", null},
    277                 {null, "bar"}
    278         });
    279
    280         SelectTag tag = new SelectTag();
    281         tag.setPageContext(pageContext);
    282         tag.setEmptyOption("true");
    283         tag.setLabel("mylabel");
    284         tag.setName("foo");
    285         tag.setList("list");
    286         tag.setListKey("top[0]");
    287         tag.setListValue("top[1]");
    288
    289         // header stuff
    290         tag.setHeaderKey("headerKey");
    291         tag.setHeaderValue("headerValue");
    292
    293         // empty option
    294         tag.setEmptyOption("true");
    295
    296         tag.doStartTag();
    297         tag.doEndTag();
    298
    299         verify(SelectTag.class.getResource("Select-9.txt"));
    300     }
    301
    302     public void testExtended() throws Exception {
    303         TestAction testAction = (TestAction) action;
    304         testAction.setFoo("hello");
    305         testAction.setList(new String[][]{
    306                 {"hello", "world"},
    307                 {"foo", "bar"}
    308         });
    309
    310         SelectTag tag = new SelectTag();
    311         tag.setPageContext(pageContext);
    312         tag.setEmptyOption("true");
    313         tag.setLabel("mylabel");
    314         tag.setName("foo");
    315         tag.setList("list");
    316         tag.setListKey("top[0]");
    317         tag.setListValue("%{top[0] + ' - ' + top[1]}");
    318
    319         // header stuff
    320         tag.setHeaderKey("headerKey");
    321         tag.setHeaderValue("%{foo + ': headerValue'}");
    322
    323         // empty option
    324         tag.setEmptyOption("true");
    325
    326         tag.doStartTag();
    327         tag.doEndTag();
    328
    329         verify(SelectTag.class.getResource("Select-7.txt"));
    330      }
    331
    332     public void testGenericSimple() throws Exception {
    333         SelectTag tag = new SelectTag();
    334         prepareTagGeneric(tag);
    335         verifyGenericProperties(tag, "simple", new String[]{"value"});
    336     }
    337
    338     public void testGenericXhtml() throws Exception {
    339         SelectTag tag = new SelectTag();
    340         prepareTagGeneric(tag);
    341         verifyGenericProperties(tag, "xhtml", new String[]{"value"});
    342     }
    343
    344     public void testGenericAjax() throws Exception {
    345         SelectTag tag = new SelectTag();
    346         prepareTagGeneric(tag);
    347         verifyGenericProperties(tag, "ajax", new String[]{"value"});
    348     }
    349
    350     public void testMultipleOn() throws Exception {
    351         SelectTag tag = new SelectTag();
    352         tag.setPageContext(pageContext);
    353         tag.setLabel("media1");
    354         tag.setId("myId");
    355         tag.setEmptyOption("true");
    356         tag.setName("myName");
    357         tag.setMultiple("true");
    358         tag.setList("{'aaa','bbb'}");
    359
    360         tag.doStartTag();
    361         tag.doEndTag();
    362
    363         verify(SelectTag.class.getResource("Select-5.txt"));
    364     }
    365
    366     public void testMultipleOff() throws Exception {
    367         SelectTag tag = new SelectTag();
    368         tag.setPageContext(pageContext);
    369         tag.setLabel("media2");
    370         tag.setId("myId");
    371         tag.setEmptyOption("true");
    372         tag.setName("myName");
    373         tag.setMultiple("false");
    374         tag.setList("{'aaa','bbb'}");
    375
    376         tag.doStartTag();
    377         tag.doEndTag();
    378
    379         verify(SelectTag.class.getResource("Select-6.txt"));
    380     }
    381
    382     public void testSimpleInteger() throws Exception {
    383         TestAction testAction = (TestAction) action;
    384
    385         IdName hello = new IdName(new Integer(1), "hello");
    386         IdName world = new IdName(new Integer(2), "world");
    387         List list2 = new ArrayList();
    388         list2.add(hello);
    389         list2.add(world);
    390         testAction.setList2(list2);
    391
    392         testAction.setFooInt(new Integer(1));
    393
    394         SelectTag tag = new SelectTag();
    395         tag.setPageContext(pageContext);
    396         tag.setEmptyOption("true");
    397         tag.setLabel("mylabel");
    398         tag.setName("fooInt");
    399         tag.setList("list2");
    400         tag.setListKey("id");
    401         tag.setListValue("name");
    402
    403         // header stuff
    404         tag.setHeaderKey("headerKey");
    405         tag.setHeaderValue("headerValue");
    406
    407         // empty option
    408         tag.setEmptyOption("true");
    409
    410         tag.doStartTag();
    411         tag.doEndTag();
    412
    413         verify(SelectTag.class.getResource("Select-11.txt"));
    414     }
    415
    416     public void testSimpleIntegerWithValueWorkaround() throws Exception {
    417         TestAction testAction = (TestAction) action;
    418
    419         IdName hello = new IdName(new Integer(1), "hello");
    420         IdName world = new IdName(new Integer(2), "world");
    421         List list2 = new ArrayList();
    422         list2.add(hello);
    423         list2.add(world);
    424         testAction.setList2(list2);
    425
    426         testAction.setFooInt(new Integer(1));
    427
    428         SelectTag tag = new SelectTag();
    429         tag.setPageContext(pageContext);
    430         tag.setEmptyOption("true");
    431         tag.setLabel("mylabel");
    432         tag.setName("fooInt");
    433         tag.setList("list2");
    434         tag.setListKey("id");
    435         tag.setListValue("name");
    436         tag.setValue("fooInt.toString()");
    437
    438         // header stuff
    439         tag.setHeaderKey("headerKey");
    440         tag.setHeaderValue("headerValue");
    441
    442         // empty option
    443         tag.setEmptyOption("true");
    444
    445         tag.doStartTag();
    446         tag.doEndTag();
    447
    448         verify(SelectTag.class.getResource("Select-11.txt"));
    449     }
    450
    451     public class IdName {
    452         private String name;
    453         private Integer id;
    454
    455         public IdName(Integer id, String name) {
    456             this.name = name;
    457             this.id = id;
    458         }
    459
    460         public String getName() {
    461             return name;
    462         }
    463
    464         public Integer getId() {
    465             return id;
    466         }
    467     }
    468
    469     private void prepareTagGeneric(SelectTag tag) {
    470         TestAction testAction = (TestAction) action;
    471         ArrayList collection = new ArrayList();
    472         collection.add("foo");
    473         collection.add("bar");
    474         collection.add("baz");
    475
    476         testAction.setCollection(collection);
    477
    478         tag.setList("collection");
    479     }
    480
    481 }