View Javadoc

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