View Javadoc

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