View Javadoc

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