View Javadoc

1   /*
2    * $Id: ComponentTest.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 java.util.Iterator;
25  import java.util.Locale;
26  import java.util.Stack;
27  
28  import javax.servlet.jsp.tagext.TagSupport;
29  
30  import org.apache.struts2.views.jsp.AbstractTagTest;
31  import org.apache.struts2.views.jsp.BeanTag;
32  import org.apache.struts2.views.jsp.ElseIfTag;
33  import org.apache.struts2.views.jsp.ElseTag;
34  import org.apache.struts2.views.jsp.I18nTag;
35  import org.apache.struts2.views.jsp.IfTag;
36  import org.apache.struts2.views.jsp.IteratorTag;
37  import org.apache.struts2.views.jsp.PropertyTag;
38  import org.apache.struts2.views.jsp.PushTag;
39  import org.apache.struts2.views.jsp.SetTag;
40  import org.apache.struts2.views.jsp.TextTag;
41  import org.apache.struts2.views.jsp.URLTag;
42  import org.apache.struts2.views.jsp.iterator.AppendIteratorTag;
43  import org.apache.struts2.views.jsp.iterator.MergeIteratorTag;
44  import org.apache.struts2.views.jsp.ui.TextFieldTag;
45  import org.apache.struts2.views.jsp.ui.UpDownSelectTag;
46  
47  import com.opensymphony.xwork2.ActionContext;
48  import com.opensymphony.xwork2.util.LocalizedTextUtil;
49  
50  /***
51   * Test case for method findAncestor(Class) in Component and some commons
52   * test cases for Component in general.
53   *
54   */
55  public class ComponentTest extends AbstractTagTest {
56  
57      public void testFindAncestorTest() throws Exception {
58          Property property = new Property(stack);
59          Form form = new Form(stack, request, response);
60          ActionComponent actionComponent = new ActionComponent(stack, request, response);
61          Anchor anchor = new Anchor(stack, request, response);
62          Form form2 = new Form(stack, request, response);
63          TextField textField = new TextField(stack, request, response);
64  
65  
66          Stack stack = property.getComponentStack();
67          Iterator i = stack.iterator();
68  
69  
70          try {
71              // component stack
72              assertEquals(property.getComponentStack().size(), 6);
73              assertEquals(i.next(), property);
74              assertEquals(i.next(), form);
75              assertEquals(i.next(), actionComponent);
76              assertEquals(i.next(), anchor);
77              assertEquals(i.next(), form2);
78              assertEquals(i.next(), textField);
79  
80  
81              // property
82              assertNull(property.findAncestor(Component.class));
83  
84              // form
85              assertEquals(form.findAncestor(Component.class), property);
86              assertEquals(form.findAncestor(Property.class), property);
87  
88              // action
89              assertEquals(actionComponent.findAncestor(Component.class), form);
90              assertEquals(actionComponent.findAncestor(Property.class), property);
91              assertEquals(actionComponent.findAncestor(Form.class), form);
92  
93              // anchor
94              assertEquals(anchor.findAncestor(Component.class), actionComponent);
95              assertEquals(anchor.findAncestor(ActionComponent.class), actionComponent);
96              assertEquals(anchor.findAncestor(Form.class), form);
97              assertEquals(anchor.findAncestor(Property.class), property);
98  
99              // form2
100             assertEquals(form2.findAncestor(Component.class), anchor);
101             assertEquals(form2.findAncestor(Anchor.class), anchor);
102             assertEquals(form2.findAncestor(ActionComponent.class), actionComponent);
103             assertEquals(form2.findAncestor(Form.class), form);
104             assertEquals(form2.findAncestor(Property.class), property);
105 
106             // textField
107             assertEquals(textField.findAncestor(Component.class), form2);
108             assertEquals(textField.findAncestor(Form.class), form2);
109             assertEquals(textField.findAncestor(Anchor.class), anchor);
110             assertEquals(textField.findAncestor(ActionComponent.class), actionComponent);
111             assertEquals(textField.findAncestor(Property.class), property);
112         }
113         finally {
114             property.getComponentStack().pop();
115             property.getComponentStack().pop();
116             property.getComponentStack().pop();
117             property.getComponentStack().pop();
118             property.getComponentStack().pop();
119         }
120     }
121 
122     // Action Component
123     /*
124     public void testActionComponentDisposeItselfFromComponentStack() throws Exception {
125         ConfigurationManager.clearConfigurationProviders();
126         ConfigurationManager.addConfigurationProvider(new TestConfigurationProvider());
127         ConfigurationManager.getConfiguration().reload();
128 
129         ActionContext actionContext = new ActionContext(context);
130         actionContext.setValueStack(stack);
131         ActionContext.setContext(actionContext);
132 
133         request.setupGetServletPath(TestConfigurationProvider.TEST_NAMESPACE + "/" + "foo.action");
134         try {
135             TextFieldTag t = new TextFieldTag();
136             t.setName("textFieldName");
137             t.setPageContext(pageContext);
138             t.doStartTag();
139 
140             ActionTag tag = new ActionTag();
141             tag.setPageContext(pageContext);
142             tag.setName(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
143             tag.setId(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
144             tag.doStartTag();
145             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
146             tag.doEndTag();
147             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
148 
149             t.doEndTag();
150         }
151         catch(Exception e) {
152             e.printStackTrace();
153             fail(e.toString());
154         }
155     }
156     */
157 
158 
159     // AppendInterator
160     public void testAppendIteratorDisposeItselfFromComponentStack() throws Exception {
161         TextFieldTag t = new TextFieldTag();
162         t.setPageContext(pageContext);
163         t.setName("textFieldName");
164 
165         AppendIteratorTag tag = new AppendIteratorTag();
166         tag.setPageContext(pageContext);
167 
168         try {
169             t.doStartTag();
170             tag.doStartTag();
171             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
172             tag.doEndTag();
173             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
174             t.doEndTag();
175         }
176         catch(Exception e) {
177             e.printStackTrace();
178             fail(e.toString());
179         }
180     }
181 
182 
183     // Bean
184     public void testBeanComponentDisposeItselfFromComponentStack() throws Exception {
185         TextFieldTag t = new TextFieldTag();
186         t.setPageContext(pageContext);
187         t.setName("textFieldName");
188 
189         BeanTag tag = new BeanTag();
190         tag.setName("org.apache.struts2.util.Counter");
191         tag.setPageContext(pageContext);
192 
193         try {
194             t.doStartTag();
195             tag.doStartTag();
196             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
197             tag.doEndTag();
198             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
199             t.doEndTag();
200         }
201         catch(Exception e) {
202             e.printStackTrace();
203             fail();
204         }
205     }
206 
207 
208     // ElseIf
209     public void testElseIfComponentDisposeItselfFromComponentStack() throws Exception {
210         TextFieldTag t = new TextFieldTag();
211         t.setPageContext(pageContext);
212         t.setName("textFieldName");
213 
214         ElseIfTag tag = new ElseIfTag();
215         tag.setPageContext(pageContext);
216 
217         try {
218             t.doStartTag();
219             tag.doStartTag();
220             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
221             tag.doEndTag();
222             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
223             t.doEndTag();
224         }
225         catch(Exception e) {
226             e.printStackTrace();
227             fail(e.toString());
228         }
229     }
230 
231 
232     // Else
233     public void testElseComponentDisposeItselfFromComponentStack() throws Exception {
234         TextFieldTag t = new TextFieldTag();
235         t.setPageContext(pageContext);
236         t.setName("textFieldName");
237 
238         ElseTag tag = new ElseTag();
239         tag.setPageContext(pageContext);
240 
241         try {
242             t.doStartTag();
243             tag.doStartTag();
244             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
245             tag.doEndTag();
246             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
247             t.doEndTag();
248         }
249         catch(Exception e) {
250             e.printStackTrace();
251             fail(e.toString());
252         }
253     }
254 
255 
256     // If
257     public void testIfComponentDisposeItselfFromComponentStack() throws Exception {
258         TextFieldTag t = new TextFieldTag();
259         t.setPageContext(pageContext);
260         t.setName("textFieldName");
261 
262         IfTag tag = new IfTag();
263         tag.setTest("false");
264         tag.setPageContext(pageContext);
265 
266         try {
267             t.doStartTag();
268             tag.doStartTag();
269             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
270             tag.doEndTag();
271             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
272             t.doEndTag();
273         }
274         catch(Exception e) {
275             e.printStackTrace();
276             fail(e.toString());
277         }
278     }
279 
280 
281     // Iterator
282     public void testIteratorComponentDisposeItselfFromComponentStack() throws Exception {
283         TextFieldTag t = new TextFieldTag();
284         t.setPageContext(pageContext);
285         t.setName("textFieldName");
286 
287         IteratorTag tag = new IteratorTag();
288         tag.setValue("{1,2}");
289         tag.setPageContext(pageContext);
290 
291         try {
292             t.doStartTag();
293             tag.doStartTag();
294             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
295             int endIt = tag.doAfterBody();
296             while(TagSupport.EVAL_BODY_AGAIN == endIt) {
297                 assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
298                 endIt = tag.doAfterBody();
299             }
300             tag.doEndTag();
301             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
302             t.doEndTag();
303         }
304         catch(Exception e) {
305             e.printStackTrace();
306             fail(e.toString());
307         }
308     }
309 
310 
311     // MergeIterator
312     public void testMergeIteratorComponentDisposeItselfFromComponentStack() throws Exception {
313         TextFieldTag t = new TextFieldTag();
314         t.setPageContext(pageContext);
315         t.setName("textFieldName");
316 
317         MergeIteratorTag tag = new MergeIteratorTag();
318         tag.setPageContext(pageContext);
319 
320         try {
321             t.doStartTag();
322             tag.doStartTag();
323             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
324             tag.doEndTag();
325             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
326             t.doEndTag();
327         }
328         catch(Exception e) {
329             e.printStackTrace();
330             fail(e.toString());
331         }
332     }
333 
334 
335     // Property
336     public void testPropertyComponentDisposeItselfFromComponentStack() throws Exception {
337         TextFieldTag t = new TextFieldTag();
338         t.setPageContext(pageContext);
339         t.setName("textFieldName");
340 
341         PropertyTag tag = new PropertyTag();
342         tag.setPageContext(pageContext);
343 
344         try {
345             t.doStartTag();
346             tag.doStartTag();
347             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
348             tag.doEndTag();
349             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
350             t.doEndTag();
351         }
352         catch(Exception e) {
353             e.printStackTrace();
354             fail(e.toString());
355         }
356     }
357 
358 
359     // Push
360     public void testPushComponentDisposeItselfFromComponentStack() throws Exception {
361         TextFieldTag t = new TextFieldTag();
362         t.setPageContext(pageContext);
363         t.setName("textFieldName");
364 
365         PushTag tag = new PushTag();
366         tag.setValue("'aaaa'");
367         tag.setPageContext(pageContext);
368 
369         try {
370             t.doStartTag();
371             tag.doStartTag();
372             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
373             tag.doEndTag();
374             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
375             t.doEndTag();
376         }
377         catch(Exception e) {
378             e.printStackTrace();
379             fail(e.toString());
380         }
381     }
382 
383 
384     // Set
385     public void testSetComponentDisposeItselfFromComponentStack() throws Exception {
386         TextFieldTag t = new TextFieldTag();
387         t.setPageContext(pageContext);
388         t.setName("textFieldName");
389 
390         SetTag tag = new SetTag();
391         tag.setName("name");
392         tag.setValue("'value'");
393         tag.setPageContext(pageContext);
394 
395         try {
396             t.doStartTag();
397             tag.doStartTag();
398             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
399             tag.doEndTag();
400             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
401             t.doEndTag();
402         }
403         catch(Exception e) {
404             e.printStackTrace();
405             fail(e.toString());
406         }
407     }
408 
409 
410     // Text
411     public void testTextComponentDisposeItselfFromComponentStack() throws Exception {
412         TextFieldTag t = new TextFieldTag();
413         t.setPageContext(pageContext);
414         t.setName("textFieldName");
415 
416         TextTag tag = new TextTag();
417         tag.setName("some.i18n.key");
418         tag.setPageContext(pageContext);
419 
420         try {
421             t.doStartTag();
422             tag.doStartTag();
423             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
424             tag.doEndTag();
425             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
426             t.doEndTag();
427         }
428         catch(Exception e) {
429             e.printStackTrace();
430             fail(e.toString());
431         }
432     }
433 
434 
435     public void testI18nComponentDisposeItselfFromComponentStack() throws Exception {
436         stack.getContext().put(ActionContext.LOCALE, Locale.getDefault());
437 
438         TextFieldTag t = new TextFieldTag();
439         t.setPageContext(pageContext);
440         t.setName("textFieldName");
441 
442         LocalizedTextUtil.addDefaultResourceBundle("org.apache.struts2.components.temp");
443 
444         I18nTag tag = new I18nTag();
445         tag.setName("org.apache.struts2.components.tempo");
446         tag.setPageContext(pageContext);
447 
448         try {
449             t.doStartTag();
450             tag.doStartTag();
451             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
452             tag.doEndTag();
453             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
454             t.doEndTag();
455         }
456         catch(Exception e) {
457             e.printStackTrace();
458             fail(e.toString());
459         }
460     }
461 
462     // URL
463     public void testURLComponentDisposeItselfFromComponentStack() throws Exception {
464         TextFieldTag t = new TextFieldTag();
465         t.setPageContext(pageContext);
466         t.setName("textFieldName");
467 
468         URLTag tag = new URLTag();
469         tag.setPageContext(pageContext);
470 
471         try {
472             t.doStartTag();
473             tag.doStartTag();
474             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
475             tag.doEndTag();
476             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
477             t.doEndTag();
478         }
479         catch(Exception e) {
480             e.printStackTrace();
481             fail(e.toString());
482         }
483     }
484 
485 
486     // updownselect
487     public void testUpDownSelectDisposeItselfFromComponentStack() throws Exception {
488         TextFieldTag t = new TextFieldTag();
489         t.setPageContext(pageContext);
490         t.setName("textFieldName");
491 
492         UpDownSelectTag tag = new UpDownSelectTag();
493         tag.setId("myId");
494         tag.setPageContext(pageContext);
495         tag.setName("updownselectName");
496         tag.setList("{}");
497 
498         try {
499             t.doStartTag();
500             tag.doStartTag();
501             assertEquals(tag.getComponent().getComponentStack().peek(), tag.getComponent());
502             tag.doEndTag();
503             assertEquals(t.getComponent().getComponentStack().peek(), t.getComponent());
504             t.doEndTag();
505         }
506         catch(Exception e) {
507             e.printStackTrace();
508             fail(e.toString());
509         }
510     }
511 }