View Javadoc

1   /*
2    * $Id: SelectTest.java 539825 2007-05-20 04:28:35Z mrdon $
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 class LongObject {
174         private Long id;
175         private String value;
176 
177 
178         public LongObject(Long id, String value) {
179             this.id = id;
180             this.value = value;
181         }
182 
183         public Long getId() {
184             return id;
185         }
186 
187         public void setId(Long id) {
188             this.id = id;
189         }
190 
191         public String getValue() {
192             return value;
193         }
194 
195         public void setValue(String value) {
196             this.value = value;
197         }
198     }
199 
200     public void testNullList() throws Exception {
201         TestAction testAction = (TestAction) action;
202         testAction.setList2(null);
203 
204         SelectTag tag = new SelectTag();
205         tag.setName("collection");
206         tag.setList("list2");
207         tag.setLabel("tmjee_name");
208 
209         tag.setPageContext(pageContext);
210         try {
211             tag.doStartTag();
212             tag.doEndTag();
213             fail("exception should have been thrown value of select tag is null");
214         }
215         catch(Exception e) {
216             assertTrue(true);
217         }
218     }
219 
220 
221     public void testEmptyList() throws Exception {
222         TestAction testAction = (TestAction) action;
223         testAction.setList2(new ArrayList());
224 
225         SelectTag tag = new SelectTag();
226         tag.setName("collection");
227         tag.setList("list2");
228         tag.setLabel("tmjee_name");
229 
230         tag.setPageContext(pageContext);
231         tag.doStartTag();
232         tag.doEndTag();
233 
234         verify(SelectTag.class.getResource("Select-4.txt"));
235     }
236 
237     public void testMultiple() throws Exception {
238         TestAction testAction = (TestAction) action;
239         Collection collection = new ArrayList(2);
240         collection.add("hello");
241         collection.add("foo");
242         testAction.setCollection(collection);
243         testAction.setList(new String[][]{
244                 {"hello", "world"},
245                 {"foo", "bar"},
246                 {"cat", "dog"}
247         });
248 
249         SelectTag tag = new SelectTag();
250         tag.setPageContext(pageContext);
251         tag.setLabel("mylabel");
252         tag.setName("collection");
253         tag.setList("list");
254         tag.setListKey("top[0]");
255         tag.setListValue("top[1]");
256         tag.setMultiple("true");
257         tag.setOnmousedown("alert('onmousedown');");
258         tag.setOnmousemove("alert('onmousemove');");
259         tag.setOnmouseout("alert('onmouseout');");
260         tag.setOnmouseover("alert('onmouseover');");
261         tag.setOnmouseup("alert('onmouseup');");
262 
263         tag.doStartTag();
264         tag.doEndTag();
265 
266         verify(SelectTag.class.getResource("Select-2.txt"));
267     }
268 
269     /***
270      * WW-1747 - should be a valid test case for the described issue
271      * @throws Exception
272      */
273     public void testMultipleWithLists() throws Exception {
274         TestAction testAction = (TestAction) action;
275         Collection collection = new ArrayList(2);
276 
277         collection.add(1l);
278         collection.add(300000000l);
279         testAction.setCollection(collection);
280 
281         List selectList = new ArrayList();
282         selectList.add(new LongObject(1l, "foo"));
283         selectList.add(new LongObject(2l, "bar"));
284         selectList.add(new LongObject(300000000l, "foobar"));
285         testAction.setList2(selectList);
286 
287         SelectTag tag = new SelectTag();
288         tag.setPageContext(pageContext);
289         tag.setLabel("mylabel");
290         tag.setName("collection");
291         tag.setList("list2");
292         tag.setListKey("id");
293         tag.setListValue("value");
294         tag.setMultiple("true");
295         tag.setOnmousedown("alert('onmousedown');");
296         tag.setOnmousemove("alert('onmousemove');");
297         tag.setOnmouseout("alert('onmouseout');");
298         tag.setOnmouseover("alert('onmouseover');");
299         tag.setOnmouseup("alert('onmouseup');");
300 
301         tag.doStartTag();
302         tag.doEndTag();
303 
304         verify(SelectTag.class.getResource("Select-12.txt"));
305     }
306 
307     public void testSimple() throws Exception {
308         TestAction testAction = (TestAction) action;
309         testAction.setFoo("hello");
310         testAction.setList(new String[][]{
311                 {"hello", "world"},
312                 {"foo", "bar"}
313         });
314 
315         SelectTag tag = new SelectTag();
316         tag.setPageContext(pageContext);
317         tag.setEmptyOption("true");
318         tag.setLabel("mylabel");
319         tag.setName("foo");
320         tag.setList("list");
321         tag.setListKey("top[0]");
322         tag.setListValue("top[1]");
323 
324         // header stuff
325         tag.setHeaderKey("headerKey");
326         tag.setHeaderValue("headerValue");
327 
328         // empty option
329         tag.setEmptyOption("true");
330 
331         tag.doStartTag();
332         tag.doEndTag();
333 
334         verify(SelectTag.class.getResource("Select-1.txt"));
335     }
336     
337     public void testSimpleWithNulls() throws Exception {
338         TestAction testAction = (TestAction) action;
339         testAction.setFoo("hello");
340         testAction.setList(new String[][]{
341                 {"hello", null},
342                 {null, "bar"}
343         });
344 
345         SelectTag tag = new SelectTag();
346         tag.setPageContext(pageContext);
347         tag.setEmptyOption("true");
348         tag.setLabel("mylabel");
349         tag.setName("foo");
350         tag.setList("list");
351         tag.setListKey("top[0]");
352         tag.setListValue("top[1]");
353 
354         // header stuff
355         tag.setHeaderKey("headerKey");
356         tag.setHeaderValue("headerValue");
357 
358         // empty option
359         tag.setEmptyOption("true");
360 
361         tag.doStartTag();
362         tag.doEndTag();
363 
364         verify(SelectTag.class.getResource("Select-9.txt"));
365     }
366 
367     public void testExtended() throws Exception {
368         TestAction testAction = (TestAction) action;
369         testAction.setFoo("hello");
370         testAction.setList(new String[][]{
371                 {"hello", "world"},
372                 {"foo", "bar"}
373         });
374 
375         SelectTag tag = new SelectTag();
376         tag.setPageContext(pageContext);
377         tag.setEmptyOption("true");
378         tag.setLabel("mylabel");
379         tag.setName("foo");
380         tag.setList("list");
381         tag.setListKey("top[0]");
382         tag.setListValue("%{top[0] + ' - ' + top[1]}");
383 
384         // header stuff
385         tag.setHeaderKey("headerKey");
386         tag.setHeaderValue("%{foo + ': headerValue'}");
387 
388         // empty option
389         tag.setEmptyOption("true");
390 
391         tag.doStartTag();
392         tag.doEndTag();
393 
394         verify(SelectTag.class.getResource("Select-7.txt"));
395      }
396 
397     public void testGenericSimple() throws Exception {
398         SelectTag tag = new SelectTag();
399         prepareTagGeneric(tag);
400         verifyGenericProperties(tag, "simple", new String[]{"value"});
401     }
402 
403     public void testGenericXhtml() throws Exception {
404         SelectTag tag = new SelectTag();
405         prepareTagGeneric(tag);
406         verifyGenericProperties(tag, "xhtml", new String[]{"value"});
407     }
408 
409     public void testGenericAjax() throws Exception {
410         SelectTag tag = new SelectTag();
411         prepareTagGeneric(tag);
412         verifyGenericProperties(tag, "ajax", new String[]{"value"});
413     }
414 
415     public void testMultipleOn() throws Exception {
416         SelectTag tag = new SelectTag();
417         tag.setPageContext(pageContext);
418         tag.setLabel("media1");
419         tag.setId("myId");
420         tag.setEmptyOption("true");
421         tag.setName("myName");
422         tag.setMultiple("true");
423         tag.setList("{'aaa','bbb'}");
424 
425         tag.doStartTag();
426         tag.doEndTag();
427 
428         verify(SelectTag.class.getResource("Select-5.txt"));
429     }
430 
431     public void testMultipleOff() throws Exception {
432         SelectTag tag = new SelectTag();
433         tag.setPageContext(pageContext);
434         tag.setLabel("media2");
435         tag.setId("myId");
436         tag.setEmptyOption("true");
437         tag.setName("myName");
438         tag.setMultiple("false");
439         tag.setList("{'aaa','bbb'}");
440 
441         tag.doStartTag();
442         tag.doEndTag();
443 
444         verify(SelectTag.class.getResource("Select-6.txt"));
445     }
446 
447     public void testSimpleInteger() throws Exception {
448         TestAction testAction = (TestAction) action;
449 
450         IdName hello = new IdName(new Integer(1), "hello");
451         IdName world = new IdName(new Integer(2), "world");
452         List list2 = new ArrayList();
453         list2.add(hello);
454         list2.add(world);
455         testAction.setList2(list2);
456 
457         testAction.setFooInt(new Integer(1));
458 
459         SelectTag tag = new SelectTag();
460         tag.setPageContext(pageContext);
461         tag.setEmptyOption("true");
462         tag.setLabel("mylabel");
463         tag.setName("fooInt");
464         tag.setList("list2");
465         tag.setListKey("id");
466         tag.setListValue("name");
467 
468         // header stuff
469         tag.setHeaderKey("headerKey");
470         tag.setHeaderValue("headerValue");
471 
472         // empty option
473         tag.setEmptyOption("true");
474 
475         tag.doStartTag();
476         tag.doEndTag();
477 
478         verify(SelectTag.class.getResource("Select-11.txt"));
479     }
480 
481     public void testSimpleIntegerWithValueWorkaround() throws Exception {
482         TestAction testAction = (TestAction) action;
483 
484         IdName hello = new IdName(new Integer(1), "hello");
485         IdName world = new IdName(new Integer(2), "world");
486         List list2 = new ArrayList();
487         list2.add(hello);
488         list2.add(world);
489         testAction.setList2(list2);
490 
491         testAction.setFooInt(new Integer(1));
492 
493         SelectTag tag = new SelectTag();
494         tag.setPageContext(pageContext);
495         tag.setEmptyOption("true");
496         tag.setLabel("mylabel");
497         tag.setName("fooInt");
498         tag.setList("list2");
499         tag.setListKey("id");
500         tag.setListValue("name");
501         tag.setValue("fooInt.toString()");
502 
503         // header stuff
504         tag.setHeaderKey("headerKey");
505         tag.setHeaderValue("headerValue");
506 
507         // empty option
508         tag.setEmptyOption("true");
509 
510         tag.doStartTag();
511         tag.doEndTag();
512 
513         verify(SelectTag.class.getResource("Select-11.txt"));
514     }
515     
516     public void testEnumList() throws Exception {
517 
518         SelectTag tag = new SelectTag();
519         tag.setPageContext(pageContext);
520         tag.setLabel("mylabel");
521         tag.setName("status");
522         tag.setList("statusList");
523         tag.setListKey("name");
524         tag.setListValue("displayName");
525 
526         tag.doStartTag();
527         tag.doEndTag();
528 
529         verify(SelectTag.class.getResource("Select-13.txt"));
530     }
531 
532     public class IdName {
533         private String name;
534         private Integer id;
535 
536         public IdName(Integer id, String name) {
537             this.name = name;
538             this.id = id;
539         }
540 
541         public String getName() {
542             return name;
543         }
544 
545         public Integer getId() {
546             return id;
547         }
548     }
549 
550     private void prepareTagGeneric(SelectTag tag) {
551         TestAction testAction = (TestAction) action;
552         ArrayList collection = new ArrayList();
553         collection.add("foo");
554         collection.add("bar");
555         collection.add("baz");
556 
557         testAction.setCollection(collection);
558 
559         tag.setList("collection");
560     }
561 
562 }