View Javadoc

1   /*
2    * $Id: UIComponentTest.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.components;
23  
24  import org.apache.struts2.TestConfigurationProvider;
25  import org.apache.struts2.views.jsp.AbstractUITagTest;
26  import org.apache.struts2.views.jsp.ui.ActionErrorTag;
27  import org.apache.struts2.views.jsp.ui.ActionMessageTag;
28  import org.apache.struts2.views.jsp.ui.AnchorTag;
29  import org.apache.struts2.views.jsp.ui.CheckboxTag;
30  import org.apache.struts2.views.jsp.ui.ComboBoxTag;
31  import org.apache.struts2.views.jsp.ui.DivTag;
32  import org.apache.struts2.views.jsp.ui.DoubleSelectTag;
33  import org.apache.struts2.views.jsp.ui.FieldErrorTag;
34  import org.apache.struts2.views.jsp.ui.FileTag;
35  import org.apache.struts2.views.jsp.ui.FormTag;
36  import org.apache.struts2.views.jsp.ui.HiddenTag;
37  import org.apache.struts2.views.jsp.ui.LabelTag;
38  import org.apache.struts2.views.jsp.ui.OptionTransferSelectTag;
39  import org.apache.struts2.views.jsp.ui.PasswordTag;
40  import org.apache.struts2.views.jsp.ui.RadioTag;
41  import org.apache.struts2.views.jsp.ui.SelectTag;
42  import org.apache.struts2.views.jsp.ui.SubmitTag;
43  import org.apache.struts2.views.jsp.ui.TextFieldTag;
44  import org.apache.struts2.views.jsp.ui.TextareaTag;
45  import org.apache.struts2.views.jsp.ui.TokenTag;
46  
47  import com.opensymphony.xwork2.ActionContext;
48  
49  
50  /***
51   * Test case common for all UI component in general.
52   */
53  public class UIComponentTest extends AbstractUITagTest {
54  
55      // actionError
56      public void testActionErrorComponentDisposeItselfFromComponentStack() throws Exception {
57          ActionMessageTag t = new ActionMessageTag();
58          t.setPageContext(pageContext);
59  
60          try {
61              t.doStartTag();
62              ActionErrorTag tag = new ActionErrorTag();
63              tag.setPageContext(pageContext);
64              tag.doStartTag();
65              assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
66              tag.doEndTag();
67              assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
68  
69              t.doEndTag();
70          }
71          catch (Exception e) {
72              e.printStackTrace();
73              fail(e.toString());
74          }
75  
76      }
77  
78      // actionMessage
79      public void testActionMessageDisposeItselfFromComponentStack() throws Exception {
80          ActionErrorTag t = new ActionErrorTag();
81          t.setPageContext(pageContext);
82  
83          try {
84              t.doStartTag();
85              ActionMessageTag tag = new ActionMessageTag();
86              tag.setPageContext(pageContext);
87              tag.doStartTag();
88              assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
89              tag.doEndTag();
90              assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
91  
92              t.doEndTag();
93          }
94          catch (Exception e) {
95              e.printStackTrace();
96              fail(e.toString());
97          }
98      }
99  
100     //   Anchor
101     public void testAnchorComponentDisposeItselfFromComponentStack() throws Exception {
102 
103         TextFieldTag t = new TextFieldTag();
104         t.setPageContext(pageContext);
105         t.setName("textFieldName");
106 
107         AnchorTag tag = new AnchorTag();
108         tag.setPageContext(pageContext);
109 
110         try {
111             t.doStartTag();
112             tag.doStartTag();
113             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
114             tag.doEndTag();
115             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
116 
117             t.doEndTag();
118         }
119         catch (Exception e) {
120             e.printStackTrace();
121             fail(e.toString());
122         }
123     }
124 
125     // checkbox
126     public void testCheckboxDisposeItselfFromComponentStack() throws Exception {
127         ActionErrorTag t = new ActionErrorTag();
128         t.setPageContext(pageContext);
129 
130         try {
131             t.doStartTag();
132             CheckboxTag tag = new CheckboxTag();
133             tag.setName("name");
134             tag.setLabel("label");
135             tag.setPageContext(pageContext);
136             tag.doStartTag();
137             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
138             tag.doEndTag();
139             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
140 
141             t.doEndTag();
142         }
143         catch (Exception e) {
144             e.printStackTrace();
145             fail(e.toString());
146         }
147     }
148 
149     // combobox
150     public void testComboboxDisposeItselfFromComponentStack() throws Exception {
151         ActionErrorTag t = new ActionErrorTag();
152         t.setPageContext(pageContext);
153 
154         try {
155             t.doStartTag();
156             ComboBoxTag tag = new ComboBoxTag();
157             tag.setName("name");
158             tag.setLabel("label");
159             tag.setList("{'aaa','bbb','ccc'}");
160             tag.setPageContext(pageContext);
161             tag.doStartTag();
162             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
163             tag.doEndTag();
164             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
165 
166             t.doEndTag();
167         }
168         catch (Exception e) {
169             e.printStackTrace();
170             fail(e.toString());
171         }
172     }
173 
174     // div
175     public void testDivComponentDisposeItselfFromComponentStack() throws Exception {
176         ActionErrorTag t = new ActionErrorTag();
177         t.setPageContext(pageContext);
178 
179         try {
180             t.doStartTag();
181             DivTag tag = new DivTag();
182             tag.setPageContext(pageContext);
183             tag.doStartTag();
184             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
185             tag.doEndTag();
186             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
187 
188             t.doEndTag();
189         }
190         catch (Exception e) {
191             e.printStackTrace();
192             fail(e.toString());
193         }
194     }
195 
196     // doubleselect
197     public void testDoubleselectComponentDisposeItselfFromComponentStack() throws Exception {
198         ActionErrorTag t = new ActionErrorTag();
199         t.setPageContext(pageContext);
200 
201         try {
202             t.doStartTag();
203             DoubleSelectTag tag = new DoubleSelectTag();
204             tag.setName("name");
205             tag.setLabel("label");
206             tag.setList("#{1:'one',2:'two'}");
207             tag.setDoubleName("doubleName");
208             tag.setDoubleList("1?({'aa','bb'}:{'cc','dd'}");
209             tag.setFormName("formName");
210             tag.setPageContext(pageContext);
211             tag.doStartTag();
212             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
213             tag.doEndTag();
214             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
215 
216             t.doEndTag();
217         }
218         catch (Exception e) {
219             e.printStackTrace();
220             fail(e.toString());
221         }
222     }
223 
224     // fieldError
225     public void testFielderrorComponentDisposeItselfFromComponentStack() throws Exception {
226         ActionErrorTag t = new ActionErrorTag();
227         t.setPageContext(pageContext);
228 
229         try {
230             t.doStartTag();
231             FieldErrorTag tag = new FieldErrorTag();
232             tag.setPageContext(pageContext);
233             tag.doStartTag();
234             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
235             tag.doEndTag();
236             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
237 
238             t.doEndTag();
239         }
240         catch (Exception e) {
241             e.printStackTrace();
242             fail(e.toString());
243         }
244     }
245 
246     // file
247     public void testFileDisposeItselfFromComponentStack() throws Exception {
248         ActionErrorTag t = new ActionErrorTag();
249         t.setPageContext(pageContext);
250 
251         try {
252             t.doStartTag();
253             FileTag tag = new FileTag();
254             tag.setName("name");
255             tag.setLabel("label");
256             tag.setPageContext(pageContext);
257             tag.doStartTag();
258             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
259             tag.doEndTag();
260             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
261 
262             t.doEndTag();
263         }
264         catch (Exception e) {
265             e.printStackTrace();
266             fail(e.toString());
267         }
268     }
269 
270     // form
271     public void testFormComponentDisposeItselfFromComponentStack() throws Exception {
272         configurationManager.clearConfigurationProviders();
273         configurationManager.addConfigurationProvider(new TestConfigurationProvider());
274         ActionContext.getContext().setValueStack(stack);
275 
276         request.setupGetServletPath("/testAction");
277 
278         ActionErrorTag t = new ActionErrorTag();
279         t.setPageContext(pageContext);
280 
281         try {
282             t.doStartTag();
283             FormTag tag = new FormTag();
284             tag.setName("myForm");
285             tag.setMethod("POST");
286             tag.setAction("myAction");
287             tag.setEnctype("myEncType");
288             tag.setTitle("mytitle");
289             tag.setPageContext(pageContext);
290             tag.doStartTag();
291             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
292             tag.doEndTag();
293             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
294 
295             t.doEndTag();
296         }
297         catch (Exception e) {
298             e.printStackTrace();
299             fail(e.toString());
300         }
301     }
302 
303     // hidden
304     public void testHiddenComponentDisposeItselfFromComponentStack() throws Exception {
305         ActionErrorTag t = new ActionErrorTag();
306         t.setPageContext(pageContext);
307 
308         try {
309             t.doStartTag();
310             HiddenTag tag = new HiddenTag();
311             tag.setPageContext(pageContext);
312             tag.doStartTag();
313             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
314             tag.doEndTag();
315             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
316 
317             t.doEndTag();
318         }
319         catch (Exception e) {
320             e.printStackTrace();
321             fail(e.toString());
322         }
323     }
324 
325     // label
326     public void testLabelComponentDisposeItselfFromComponentStack() throws Exception {
327         ActionErrorTag t = new ActionErrorTag();
328         t.setPageContext(pageContext);
329 
330         try {
331             t.doStartTag();
332             LabelTag tag = new LabelTag();
333             tag.setName("name");
334             tag.setLabel("label");
335             tag.setPageContext(pageContext);
336             tag.doStartTag();
337             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
338             tag.doEndTag();
339             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
340 
341             t.doEndTag();
342         }
343         catch (Exception e) {
344             e.printStackTrace();
345             fail(e.toString());
346         }
347     }
348 
349     // optiontransferselect
350     public void testOptiontransferselectComponentDisposeItselfFromComponentStack() throws Exception {
351         ActionErrorTag t = new ActionErrorTag();
352         t.setPageContext(pageContext);
353 
354         try {
355             t.doStartTag();
356             OptionTransferSelectTag tag = new OptionTransferSelectTag();
357             tag.setId("myId");
358             tag.setDoubleId("myDoubleId");
359             tag.setName("name");
360             tag.setLabel("label");
361             tag.setList("{}");
362             tag.setDoubleList("{}");
363             tag.setPageContext(pageContext);
364             tag.doStartTag();
365             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
366             tag.doEndTag();
367             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
368 
369             t.doEndTag();
370         }
371         catch (Exception e) {
372             e.printStackTrace();
373             fail(e.toString());
374         }
375     }
376 
377     // password
378     public void testPasswordComponentDisposeItselfFromComponentStack() throws Exception {
379         ActionErrorTag t = new ActionErrorTag();
380         t.setPageContext(pageContext);
381 
382         try {
383             t.doStartTag();
384             PasswordTag tag = new PasswordTag();
385             tag.setName("name");
386             tag.setLabel("label");
387             tag.setPageContext(pageContext);
388             tag.doStartTag();
389             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
390             tag.doEndTag();
391             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
392 
393             t.doEndTag();
394         }
395         catch (Exception e) {
396             e.printStackTrace();
397             fail(e.toString());
398         }
399     }
400 
401     // radio
402     public void testRadioComponentDisposeItselfFromComponentStack() throws Exception {
403         ActionErrorTag t = new ActionErrorTag();
404         t.setPageContext(pageContext);
405 
406         try {
407             t.doStartTag();
408             RadioTag tag = new RadioTag();
409             tag.setList("{}");
410             tag.setName("name");
411             tag.setLabel("label");
412             tag.setPageContext(pageContext);
413             tag.doStartTag();
414             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
415             tag.doEndTag();
416             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
417 
418             t.doEndTag();
419         }
420         catch (Exception e) {
421             e.printStackTrace();
422             fail(e.toString());
423         }
424     }
425 
426     // select
427     public void testSelectComponentDisposeItselfFromComponentStack() throws Exception {
428         ActionErrorTag t = new ActionErrorTag();
429         t.setPageContext(pageContext);
430 
431         try {
432             t.doStartTag();
433             SelectTag tag = new SelectTag();
434             tag.setList("{}");
435             tag.setName("name");
436             tag.setLabel("label");
437             tag.setPageContext(pageContext);
438             tag.doStartTag();
439             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
440             tag.doEndTag();
441 
442             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
443 
444             t.doEndTag();
445         }
446         catch (Exception e) {
447             e.printStackTrace();
448             fail(e.toString());
449         }
450     }
451 
452     // submit
453     public void testSubmitDisposeItselfFromComponentStack() throws Exception {
454         ActionErrorTag t = new ActionErrorTag();
455         t.setPageContext(pageContext);
456 
457         try {
458             t.doStartTag();
459             SubmitTag tag = new SubmitTag();
460             tag.setName("name");
461             tag.setPageContext(pageContext);
462             tag.doStartTag();
463             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
464             tag.doEndTag();
465             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
466 
467             t.doEndTag();
468         }
469         catch (Exception e) {
470             e.printStackTrace();
471             fail(e.toString());
472         }
473     }
474 
475     // textarea
476     public void testTextareaComponentDisposeItselfFromComponentStack() throws Exception {
477         ActionErrorTag t = new ActionErrorTag();
478         t.setPageContext(pageContext);
479 
480         try {
481             t.doStartTag();
482             TextareaTag tag = new TextareaTag();
483             tag.setName("name");
484             tag.setLabel("label");
485             tag.setPageContext(pageContext);
486             tag.doStartTag();
487             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
488             tag.doEndTag();
489             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
490 
491             t.doEndTag();
492         }
493         catch (Exception e) {
494             e.printStackTrace();
495             fail(e.toString());
496         }
497     }
498 
499     // textfield
500     public void testTextfieldComponentDisposeItselfFromComponentStack() throws Exception {
501         ActionErrorTag t = new ActionErrorTag();
502         t.setPageContext(pageContext);
503 
504         try {
505             t.doStartTag();
506             TextFieldTag tag = new TextFieldTag();
507             tag.setName("name");
508             tag.setLabel("label");
509             tag.setPageContext(pageContext);
510             tag.doStartTag();
511             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
512             tag.doEndTag();
513             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
514 
515             t.doEndTag();
516         }
517         catch (Exception e) {
518             e.printStackTrace();
519             fail(e.toString());
520         }
521     }
522 
523     // token
524     public void testTokenComponentDisposeItselfFromComponentStack() throws Exception {
525         ActionErrorTag t = new ActionErrorTag();
526         t.setPageContext(pageContext);
527 
528         try {
529             t.doStartTag();
530             TokenTag tag = new TokenTag();
531             tag.setPageContext(pageContext);
532             tag.doStartTag();
533             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
534             tag.doEndTag();
535             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
536 
537             t.doEndTag();
538         }
539         catch (Exception e) {
540             e.printStackTrace();
541             fail(e.toString());
542         }
543     }
544 }