View Javadoc

1   /*
2    * $Id: SelectTest.java 447901 2006-09-19 16:36:02Z tmjee $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp.ui;
19  
20  import java.math.BigDecimal;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.struts2.TestAction;
26  import org.apache.struts2.views.jsp.AbstractUITagTest;
27  
28  
29  /***
30   */
31  public class SelectTest extends AbstractUITagTest {
32  	
33  	
34  	public void testHeaderCanBePreselected() throws Exception {
35  		SelectTag tag = new SelectTag();
36  		tag.setPageContext(pageContext);
37  		tag.setLabel("myLabel");
38  		tag.setList("#{1:'Cat',2:'Dog'}");
39  		tag.setName("myPet");
40  		tag.setHeaderKey("-1");
41  		tag.setHeaderValue("--- Please Select ---");
42  		tag.setValue("%{'-1'}");
43  		
44  		tag.doStartTag();
45  		tag.doEndTag();
46  		
47  		verify(SelectTag.class.getResource("Select-8.txt"));
48  	}
49  
50      /***
51       * Tests WW-455: Select tag template does not work properly for Object like BigDecimal.
52       */
53      public void testBigDecimal() throws Exception {
54          BigDecimalObject hello = new BigDecimalObject("hello", new BigDecimal(1));
55          BigDecimalObject foo = new BigDecimalObject("foo", new BigDecimal(2));
56  
57          TestAction testAction = (TestAction) action;
58  
59          Collection collection = new ArrayList(2);
60          // expect strings to be returned, we're still dealing with HTTP here!
61          collection.add("hello");
62          collection.add("foo");
63          testAction.setCollection(collection);
64  
65          List list2 = new ArrayList();
66          list2.add(hello);
67          list2.add(foo);
68          list2.add(new BigDecimalObject("<cat>", new BigDecimal(1.500)));
69          testAction.setList2(list2);
70  
71          SelectTag tag = new SelectTag();
72          tag.setPageContext(pageContext);
73          tag.setLabel("mylabel");
74          tag.setName("collection");
75          tag.setList("list2");
76          tag.setListKey("name");
77          tag.setListValue("bigDecimal");
78          tag.setMultiple("true");
79          tag.setTitle("mytitle");
80          tag.setOnmousedown("alert('onmousedown');");
81          tag.setOnmousemove("alert('onmousemove');");
82          tag.setOnmouseout("alert('onmouseout');");
83          tag.setOnmouseover("alert('onmouseover');");
84          tag.setOnmouseup("alert('onmouseup');");
85  
86          tag.doStartTag();
87          tag.doEndTag();
88  
89          verify(SelectTag.class.getResource("Select-3.txt"));
90      }
91  
92      public class BigDecimalObject {
93          private String name;
94          private BigDecimal bigDecimal;
95  
96          public BigDecimalObject(String name, BigDecimal bigDecimal) {
97              this.name = name;
98              this.bigDecimal = bigDecimal;
99          }
100 
101         public String getName() {
102             return name;
103         }
104 
105         public BigDecimal getBigDecimal() {
106             return bigDecimal;
107         }
108     }
109     
110     public void testNullList() throws Exception {
111     	TestAction testAction = (TestAction) action;
112     	testAction.setList2(null);
113     	
114     	SelectTag tag = new SelectTag();
115     	tag.setName("collection");
116     	tag.setList("list2");
117     	tag.setLabel("tmjee_name");
118     	
119     	tag.setPageContext(pageContext);
120     	try {
121     		tag.doStartTag();
122     		tag.doEndTag();
123     		fail("exception should have been thrown value of select tag is null");
124     	}
125     	catch(Exception e) {
126     		assertTrue(true);
127     	}
128     }
129     
130 
131     public void testEmptyList() throws Exception {
132     	TestAction testAction = (TestAction) action;
133     	testAction.setList2(new ArrayList());
134     	
135     	SelectTag tag = new SelectTag();
136     	tag.setName("collection");
137     	tag.setList("list2");
138     	tag.setLabel("tmjee_name");
139     	
140     	tag.setPageContext(pageContext);
141     	tag.doStartTag();
142     	tag.doEndTag();
143     	
144     	verify(SelectTag.class.getResource("Select-4.txt"));
145     }
146     
147     public void testMultiple() throws Exception {
148         TestAction testAction = (TestAction) action;
149         Collection collection = new ArrayList(2);
150         collection.add("hello");
151         collection.add("foo");
152         testAction.setCollection(collection);
153         testAction.setList(new String[][]{
154                 {"hello", "world"},
155                 {"foo", "bar"},
156                 {"cat", "dog"}
157         });
158 
159         SelectTag tag = new SelectTag();
160         tag.setPageContext(pageContext);
161         tag.setLabel("mylabel");
162         tag.setName("collection");
163         tag.setList("list");
164         tag.setListKey("top[0]");
165         tag.setListValue("top[1]");
166         tag.setMultiple("true");
167         tag.setOnmousedown("alert('onmousedown');");
168         tag.setOnmousemove("alert('onmousemove');");
169         tag.setOnmouseout("alert('onmouseout');");
170         tag.setOnmouseover("alert('onmouseover');");
171         tag.setOnmouseup("alert('onmouseup');");
172 
173         tag.doStartTag();
174         tag.doEndTag();
175 
176         verify(SelectTag.class.getResource("Select-2.txt"));
177     }
178 
179     public void testSimple() throws Exception {
180         TestAction testAction = (TestAction) action;
181         testAction.setFoo("hello");
182         testAction.setList(new String[][]{
183                 {"hello", "world"},
184                 {"foo", "bar"}
185         });
186 
187         SelectTag tag = new SelectTag();
188         tag.setPageContext(pageContext);
189         tag.setEmptyOption("true");
190         tag.setLabel("mylabel");
191         tag.setName("foo");
192         tag.setList("list");
193         tag.setListKey("top[0]");
194         tag.setListValue("top[1]");
195 
196         // header stuff
197         tag.setHeaderKey("headerKey");
198         tag.setHeaderValue("headerValue");
199 
200         // empty option
201         tag.setEmptyOption("true");
202 
203         tag.doStartTag();
204         tag.doEndTag();
205 
206         verify(SelectTag.class.getResource("Select-1.txt"));
207     }
208 
209     public void testExtended() throws Exception {
210         TestAction testAction = (TestAction) action;
211         testAction.setFoo("hello");
212         testAction.setList(new String[][]{
213                 {"hello", "world"},
214                 {"foo", "bar"}
215         });
216 
217         SelectTag tag = new SelectTag();
218         tag.setPageContext(pageContext);
219         tag.setEmptyOption("true");
220         tag.setLabel("mylabel");
221         tag.setName("foo");
222         tag.setList("list");
223         tag.setListKey("top[0]");
224         tag.setListValue("%{top[0] + ' - ' + top[1]}");
225 
226         // header stuff
227         tag.setHeaderKey("headerKey");
228         tag.setHeaderValue("%{foo + ': headerValue'}");
229 
230         // empty option
231         tag.setEmptyOption("true");
232 
233         tag.doStartTag();
234         tag.doEndTag();
235 
236         verify(SelectTag.class.getResource("Select-7.txt"));
237      }
238 
239     public void testGenericSimple() throws Exception {
240         SelectTag tag = new SelectTag();
241         prepareTagGeneric(tag);
242         verifyGenericProperties(tag, "simple", new String[]{"value"});
243     }
244 
245     public void testGenericXhtml() throws Exception {
246         SelectTag tag = new SelectTag();
247         prepareTagGeneric(tag);
248         verifyGenericProperties(tag, "xhtml", new String[]{"value"});
249     }
250 
251     public void testGenericAjax() throws Exception {
252         SelectTag tag = new SelectTag();
253         prepareTagGeneric(tag);
254         verifyGenericProperties(tag, "ajax", new String[]{"value"});
255     }
256     
257     public void testMultipleOn() throws Exception {
258     	SelectTag tag = new SelectTag();
259     	tag.setPageContext(pageContext);
260     	tag.setLabel("media1");
261     	tag.setId("myId");
262     	tag.setEmptyOption("true");
263     	tag.setName("myName");
264     	tag.setMultiple("true");
265     	tag.setList("{'aaa','bbb'}");
266     	
267     	tag.doStartTag();
268     	tag.doEndTag();
269     	
270     	verify(SelectTag.class.getResource("Select-5.txt"));
271     }
272     
273     public void testMultipleOff() throws Exception {
274     	SelectTag tag = new SelectTag();
275     	tag.setPageContext(pageContext);
276     	tag.setLabel("media2");
277     	tag.setId("myId");
278     	tag.setEmptyOption("true");
279     	tag.setName("myName");
280     	tag.setMultiple("false");
281     	tag.setList("{'aaa','bbb'}");
282     	
283     	tag.doStartTag();
284     	tag.doEndTag();
285     	
286     	verify(SelectTag.class.getResource("Select-6.txt"));
287     }
288 
289     private void prepareTagGeneric(SelectTag tag) {
290         TestAction testAction = (TestAction) action;
291         ArrayList collection = new ArrayList();
292         collection.add("foo");
293         collection.add("bar");
294         collection.add("baz");
295 
296         testAction.setCollection(collection);
297 
298         tag.setList("collection");
299     }
300     
301 }