View Javadoc

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