View Javadoc

1   /*
2    * $Id: DoubleSelectTest.java 768476 2009-04-25 06:07:58Z nilsga $
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.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   * Test case for DoubleSelectTag.
33   *
34   */
35  public class DoubleSelectTest extends AbstractUITagTest {
36  
37      public void testDouble() throws Exception {
38          TestAction testAction = (TestAction) action;
39  
40          Region antwerp = new Region("Antwerp", "AN");
41          Region gent = new Region("Gent", "GN");
42          Region brugge = new Region("Brugge", "BRG");
43          ArrayList belgiumRegions = new ArrayList();
44          belgiumRegions.add(antwerp);
45          belgiumRegions.add(gent);
46          belgiumRegions.add(brugge);
47          Country belgium = new Country("Belgium", "BE", belgiumRegions);
48  
49          Region paris = new Region("Paris", "PA");
50          Region bordeaux = new Region("Bordeaux", "BOR");
51          ArrayList franceRegions = new ArrayList();
52          franceRegions.add(paris);
53          franceRegions.add(bordeaux);
54          Country france = new Country("France", "FR", franceRegions);
55  
56          Collection collection = new ArrayList(2);
57          collection.add("AN");
58          testAction.setCollection(collection);
59  
60          List countries = new ArrayList();
61          countries.add(belgium);
62          countries.add(france);
63  
64          testAction.setList2(countries);
65  
66          DoubleSelectTag tag = new DoubleSelectTag();
67          tag.setPageContext(pageContext);
68          tag.setLabel("mylabel");
69          tag.setName("foo");
70          tag.setDoubleName("region");
71  
72          tag.setList("list2");
73          tag.setDoubleList("regions");
74  
75          tag.setListKey("iso");
76          tag.setDoubleListKey("key");
77          tag.setListValue("name");
78          tag.setDoubleListValue("name");
79  
80          tag.setFormName("inputForm");
81  
82          tag.setOnmousedown("window.status='onmousedown';");
83          tag.setOnmousemove("window.status='onmousemove';");
84          tag.setOnmouseout("window.status='onmouseout';");
85          tag.setOnmouseover("window.status='onmouseover';");
86          tag.setOnmouseup("window.status='onmouseup';");
87  
88          //css style and class
89          tag.setCssClass("c1");
90          tag.setCssStyle("s1");
91          tag.setDoubleCssClass("c2");
92          tag.setDoubleCssStyle("s2");
93          
94          tag.doStartTag();
95          tag.doEndTag();
96  
97          verify(SelectTag.class.getResource("DoubleSelect-1.txt"));
98      }
99      
100     public void testOnchange() throws Exception {
101         TestAction testAction = (TestAction) action;
102 
103         Region antwerp = new Region("Antwerp", "AN");
104         Region gent = new Region("Gent", "GN");
105         Region brugge = new Region("Brugge", "BRG");
106         ArrayList belgiumRegions = new ArrayList();
107         belgiumRegions.add(antwerp);
108         belgiumRegions.add(gent);
109         belgiumRegions.add(brugge);
110         Country belgium = new Country("Belgium", "BE", belgiumRegions);
111 
112         Region paris = new Region("Paris", "PA");
113         Region bordeaux = new Region("Bordeaux", "BOR");
114         ArrayList franceRegions = new ArrayList();
115         franceRegions.add(paris);
116         franceRegions.add(bordeaux);
117         Country france = new Country("France", "FR", franceRegions);
118 
119         Collection collection = new ArrayList(2);
120         collection.add("AN");
121         testAction.setCollection(collection);
122 
123         List countries = new ArrayList();
124         countries.add(belgium);
125         countries.add(france);
126 
127         testAction.setList2(countries);
128 
129         DoubleSelectTag tag = new DoubleSelectTag();
130         tag.setPageContext(pageContext);
131         tag.setLabel("mylabel");
132         tag.setName("foo");
133         tag.setDoubleName("region");
134 
135         tag.setList("list2");
136         tag.setDoubleList("regions");
137 
138         tag.setListKey("iso");
139         tag.setDoubleListKey("key");
140         tag.setListValue("name");
141         tag.setDoubleListValue("name");
142 
143         tag.setFormName("inputForm");
144 
145         tag.setOnmousedown("window.status='onmousedown';");
146         tag.setOnmousemove("window.status='onmousemove';");
147         tag.setOnmouseout("window.status='onmouseout';");
148         tag.setOnmouseover("window.status='onmouseover';");
149         tag.setOnmouseup("window.status='onmouseup';");
150         tag.setOnchange("window.status='onchange';");
151 
152         //css style and class
153         tag.setCssClass("c1");
154         tag.setCssStyle("s1");
155         tag.setDoubleCssClass("c2");
156         tag.setDoubleCssStyle("s2");
157         
158         tag.doStartTag();
159         tag.doEndTag();
160 
161         verify(SelectTag.class.getResource("DoubleSelect-4.txt"));
162     }
163 
164 
165     public void testDoubleWithDefaultSelectedValues() throws Exception {
166 
167         TestAction testAction = (TestAction) action;
168 
169         Region antwerp = new Region("Antwerp", "AN");
170         Region gent = new Region("Gent", "GN");
171         Region brugge = new Region("Brugge", "BRG");
172         ArrayList belgiumRegions = new ArrayList();
173         belgiumRegions.add(antwerp);
174         belgiumRegions.add(gent);
175         belgiumRegions.add(brugge);
176         Country belgium = new Country("Belgium", "BE", belgiumRegions);
177 
178         Region paris = new Region("Paris", "PA");
179         Region bordeaux = new Region("Bordeaux", "BOR");
180         ArrayList franceRegions = new ArrayList();
181         franceRegions.add(paris);
182         franceRegions.add(bordeaux);
183         Country france = new Country("France", "FR", franceRegions);
184 
185         Collection collection = new ArrayList(2);
186         collection.add("AN");
187         testAction.setCollection(collection);
188 
189         List countries = new ArrayList();
190         countries.add(belgium);
191         countries.add(france);
192 
193         testAction.setList2(countries);
194 
195         DoubleSelectTag tag = new DoubleSelectTag();
196         tag.setPageContext(pageContext);
197         tag.setLabel("mylabel");
198         tag.setName("foo");
199         tag.setDoubleName("region");
200 
201         tag.setValue("'FR'");
202         tag.setDoubleValue("'BOR'");
203 
204         tag.setList("list2");
205         tag.setDoubleList("regions");
206 
207         tag.setListKey("iso");
208         tag.setDoubleListKey("key");
209         tag.setListValue("name");
210         tag.setDoubleListValue("name");
211 
212         tag.setFormName("inputForm");
213 
214         tag.setOnmousedown("window.status='onmousedown';");
215         tag.setOnmousemove("window.status='onmousemove';");
216         tag.setOnmouseout("window.status='onmouseout';");
217         tag.setOnmouseover("window.status='onmouseover';");
218         tag.setOnmouseup("window.status='onmouseup';");
219 
220         tag.doStartTag();
221         tag.doEndTag();
222 
223         verify(SelectTag.class.getResource("DoubleSelect-2.txt"));
224 
225 
226     }
227     
228     public void testDoubleWithDotName() throws Exception {
229         TestAction testAction = (TestAction) action;
230 
231         Region antwerp = new Region("Antwerp", "AN");
232         Region gent = new Region("Gent", "GN");
233         Region brugge = new Region("Brugge", "BRG");
234         ArrayList belgiumRegions = new ArrayList();
235         belgiumRegions.add(antwerp);
236         belgiumRegions.add(gent);
237         belgiumRegions.add(brugge);
238         Country belgium = new Country("Belgium", "BE", belgiumRegions);
239 
240         Region paris = new Region("Paris", "PA");
241         Region bordeaux = new Region("Bordeaux", "BOR");
242         ArrayList franceRegions = new ArrayList();
243         franceRegions.add(paris);
244         franceRegions.add(bordeaux);
245         Country france = new Country("France", "FR", franceRegions);
246 
247         Collection collection = new ArrayList(2);
248         collection.add("AN");
249         testAction.setCollection(collection);
250 
251         List countries = new ArrayList();
252         countries.add(belgium);
253         countries.add(france);
254 
255         testAction.setList2(countries);
256 
257         DoubleSelectTag tag = new DoubleSelectTag();
258         tag.setPageContext(pageContext);
259         tag.setLabel("mylabel");
260         tag.setName("foo.bar");
261         tag.setDoubleName("region");
262 
263         tag.setList("list2");
264         tag.setDoubleList("regions");
265 
266         tag.setListKey("iso");
267         tag.setDoubleListKey("key");
268         tag.setListValue("name");
269         tag.setDoubleListValue("name");
270 
271         tag.setFormName("inputForm");
272 
273         tag.setOnmousedown("window.status='onmousedown';");
274         tag.setOnmousemove("window.status='onmousemove';");
275         tag.setOnmouseout("window.status='onmouseout';");
276         tag.setOnmouseover("window.status='onmouseover';");
277         tag.setOnmouseup("window.status='onmouseup';");
278 
279         tag.doStartTag();
280         tag.doEndTag();
281 
282         verify(SelectTag.class.getResource("DoubleSelect-3.txt"));
283     }
284 
285     public void testGenericSimple() throws Exception {
286         DoubleSelectTag tag = new DoubleSelectTag();
287         prepareTagGeneric(tag);
288         verifyGenericProperties(tag, "simple", new String[]{"value"});
289     }
290 
291     public void testGenericXhtml() throws Exception {
292         DoubleSelectTag tag = new DoubleSelectTag();
293         prepareTagGeneric(tag);
294         verifyGenericProperties(tag, "xhtml", new String[]{"value"});
295     }
296 
297     private void prepareTagGeneric(DoubleSelectTag tag) {
298         TestAction testAction = (TestAction) action;
299         Region antwerp = new Region("Antwerp", "AN");
300         Region gent = new Region("Gent", "GN");
301         Region brugge = new Region("Brugge", "BRG");
302         ArrayList belgiumRegions = new ArrayList();
303         belgiumRegions.add(antwerp);
304         belgiumRegions.add(gent);
305         belgiumRegions.add(brugge);
306         Country belgium = new Country("Belgium", "BE", belgiumRegions);
307 
308         Region paris = new Region("Paris", "PA");
309         Region bordeaux = new Region("Bordeaux", "BOR");
310         ArrayList franceRegions = new ArrayList();
311         franceRegions.add(paris);
312         franceRegions.add(bordeaux);
313         Country france = new Country("France", "FR", franceRegions);
314 
315         Collection collection = new ArrayList(2);
316         collection.add("AN");
317         testAction.setCollection(collection);
318 
319         tag.setList("collection");
320 
321         List countries = new ArrayList();
322         countries.add(belgium);
323         countries.add(france);
324 
325         testAction.setList2(countries);
326 
327         tag.setValue("'FR'");
328         tag.setDoubleValue("'BOR'");
329 
330         tag.setList("list2");
331         tag.setDoubleList("regions");
332         tag.setDoubleName("region");
333 
334         tag.setListKey("iso");
335         tag.setDoubleListKey("key");
336         tag.setListValue("name");
337         tag.setDoubleListValue("name");
338 
339         tag.setFormName("inputForm");
340     }
341 
342     public class Country {
343         String name;
344         String iso;
345         Collection regions;
346 
347         public Country(String name, String iso, Collection regions) {
348             this.name = name;
349             this.iso = iso;
350             this.regions = regions;
351         }
352 
353         public String getName() {
354             return name;
355         }
356 
357         public String getIso() {
358             return iso;
359         }
360 
361         public Collection getRegions() {
362             return regions;
363         }
364     }
365 
366     public class Region {
367         String name;
368         String key;
369 
370         public Region(String name, String key) {
371             this.name = name;
372             this.key = key;
373         }
374 
375         public String getName() {
376             return name;
377         }
378 
379         public String getKey() {
380             return key;
381         }
382     }
383 }