1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.view.facelets.tag.composite;
21
22 import java.beans.BeanDescriptor;
23 import java.beans.FeatureDescriptor;
24 import java.beans.PropertyDescriptor;
25 import java.io.StringWriter;
26 import java.util.Map;
27
28 import javax.el.MethodExpression;
29 import javax.el.ValueExpression;
30 import javax.faces.application.ProjectStage;
31 import javax.faces.component.UICommand;
32 import javax.faces.component.UIComponent;
33 import javax.faces.component.UIForm;
34 import javax.faces.component.UIInput;
35 import javax.faces.component.UINamingContainer;
36 import javax.faces.component.UIViewRoot;
37 import javax.faces.component.html.HtmlCommandButton;
38 import javax.faces.component.html.HtmlInputText;
39 import javax.faces.component.html.HtmlOutputText;
40 import javax.faces.event.ActionEvent;
41 import javax.faces.event.ValueChangeEvent;
42
43 import org.apache.myfaces.test.mock.MockResponseWriter;
44 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
45 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
46 import org.apache.myfaces.view.facelets.FaceletTestCase;
47 import org.apache.myfaces.view.facelets.bean.DummyBean;
48 import org.apache.myfaces.view.facelets.bean.HelloWorld;
49 import org.junit.Assert;
50 import org.junit.Test;
51
52 public class CompositeComponentAttributeTestCase extends FaceletTestCase
53 {
54
55 @Override
56 protected void setupComponents() throws Exception
57 {
58 super.setupComponents();
59 application.addComponent(CompositeTestComponent.class.getName(),
60 CompositeTestComponent.class.getName());
61 }
62
63
64
65
66
67
68 @Test
69 public void testSimpleCompositeAttribute() throws Exception
70 {
71 MockAttributeBean bean = new MockAttributeBean();
72
73 facesContext.getExternalContext().getRequestMap().put("bean",
74 bean);
75
76 UIViewRoot root = facesContext.getViewRoot();
77 vdl.buildView(facesContext, root, "testSimpleAttributeVE.xhtml");
78
79 UIComponent panelGroup1 = root.findComponent("testGroup1");
80 Assert.assertNotNull(panelGroup1);
81 CompositeTestComponent compositeComponent1 = (CompositeTestComponent) panelGroup1.getChildren().get(0);
82 Assert.assertNotNull(compositeComponent1);
83 UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
84 Assert.assertNotNull(facet1);
85 HtmlOutputText text1 = (HtmlOutputText) facet1.findComponent("text");
86 Assert.assertNotNull(text1);
87 HtmlCommandButton button1 = (HtmlCommandButton) facet1.findComponent("button");
88 Assert.assertNotNull(button1);
89 HtmlInputText input1 = (HtmlInputText) facet1.findComponent("input");
90 Assert.assertNotNull(input1);
91
92 compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
93 facet1.pushComponentToEL(facesContext, facet1);
94 text1.pushComponentToEL(facesContext, text1);
95
96 Assert.assertEquals(bean.getStyleClass(), text1.getStyleClass());
97
98 Assert.assertEquals(bean.getStyle(), text1.getStyle());
99
100 Assert.assertEquals(bean.getJavaProperty(), text1.getValue());
101
102 Assert.assertEquals(bean.getValue(), input1.getValue());
103 Assert.assertEquals(true, input1.isRequired());
104
105 text1.popComponentFromEL(facesContext);
106 button1.pushComponentToEL(facesContext, button1);
107 MethodExpression method = button1.getActionExpression();
108 Assert.assertEquals(bean.doSomethingFunny("xysj"), method.invoke(facesContext.getELContext(), new Object[]{"xysj"}));
109 button1.popComponentFromEL(facesContext);
110 facet1.popComponentFromEL(facesContext);
111 compositeComponent1.popComponentFromEL(facesContext);
112
113 StringWriter sw = new StringWriter();
114 MockResponseWriter mrw = new MockResponseWriter(sw);
115 facesContext.setResponseWriter(mrw);
116
117 compositeComponent1.encodeAll(facesContext);
118 sw.flush();
119
120 HtmlRenderedAttr[] attrs = new HtmlRenderedAttr[]{
121 new HtmlRenderedAttr("style")
122 };
123
124 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
125 }
126
127
128
129
130
131
132 @Test
133 public void testSimpleCompositeAttributeInsertChildren() throws Exception
134 {
135 MockAttributeBean bean = new MockAttributeBean();
136
137 facesContext.getExternalContext().getRequestMap().put("bean",
138 bean);
139
140 UIViewRoot root = facesContext.getViewRoot();
141 vdl.buildView(facesContext, root, "testSimpleAttributeVEInsertChildren.xhtml");
142
143 UIComponent panelGroup1 = root.findComponent("testGroup1");
144 Assert.assertNotNull(panelGroup1);
145 CompositeTestComponent compositeComponent1 = (CompositeTestComponent) panelGroup1.getChildren().get(0);
146 Assert.assertNotNull(compositeComponent1);
147 UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
148 Assert.assertNotNull(facet1);
149 UIComponent compositeComponent2 = facet1.getChildren().get(0);
150 Assert.assertNotNull(compositeComponent2);
151 UIComponent facet2 = compositeComponent2.getFacet(UIComponent.COMPOSITE_FACET_NAME);
152 Assert.assertNotNull(facet2);
153 HtmlOutputText text1 = (HtmlOutputText) facet2.findComponent("text");
154 Assert.assertNotNull(text1);
155 HtmlCommandButton button1 = (HtmlCommandButton) facet2.findComponent("button");
156 Assert.assertNotNull(button1);
157
158 compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
159 facet1.pushComponentToEL(facesContext, facet1);
160 text1.pushComponentToEL(facesContext, text1);
161
162 Assert.assertEquals(bean.getStyleClass(), text1.getStyleClass());
163
164 Assert.assertEquals(bean.getStyle(), text1.getStyle());
165
166 Assert.assertEquals(bean.getJavaProperty(), text1.getValue());
167
168 text1.popComponentFromEL(facesContext);
169 button1.pushComponentToEL(facesContext, button1);
170 MethodExpression method = button1.getActionExpression();
171 Assert.assertEquals(bean.doSomethingFunny("xysj"), method.invoke(facesContext.getELContext(), new Object[]{"xysj"}));
172 button1.popComponentFromEL(facesContext);
173 facet1.popComponentFromEL(facesContext);
174 compositeComponent1.popComponentFromEL(facesContext);
175
176 StringWriter sw = new StringWriter();
177 MockResponseWriter mrw = new MockResponseWriter(sw);
178 facesContext.setResponseWriter(mrw);
179
180 compositeComponent1.encodeAll(facesContext);
181 sw.flush();
182
183 HtmlRenderedAttr[] attrs = new HtmlRenderedAttr[]{
184 new HtmlRenderedAttr("style")
185 };
186
187 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
188 }
189
190
191 @Test
192 public void testSimpleMethodInvocation() throws Exception
193 {
194 DummyBean dummyBean = new DummyBean();
195
196 facesContext.getExternalContext().getRequestMap().put("dummyBean",
197 dummyBean);
198
199 UIViewRoot root = facesContext.getViewRoot();
200 vdl.buildView(facesContext, root, "testSimpleMethodInvocation.xhtml");
201
202 UIComponent panelGroup1 = root.findComponent("testGroup1");
203 Assert.assertNotNull(panelGroup1);
204 UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
205 Assert.assertNotNull(compositeComponent1);
206 UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
207 Assert.assertNotNull(facet1);
208 UINamingContainer compositeComponent2 = (UINamingContainer) facet1.getChildren().get(0);
209 Assert.assertNotNull(compositeComponent2);
210 UIComponent facet2 = compositeComponent2.getFacet(UIComponent.COMPOSITE_FACET_NAME);
211 Assert.assertNotNull(facet2);
212 UIForm form = (UIForm) facet2.findComponent("mainForm");
213 Assert.assertNotNull(form);
214 UICommand button1 = (UICommand) form.findComponent("button1");
215 Assert.assertNotNull(button1);
216 UICommand button2 = (UICommand) form.findComponent("button2");
217 Assert.assertNotNull(button2);
218 UICommand button3 = (UICommand) form.findComponent("button3");
219 Assert.assertNotNull(button3);
220 UIInput text1 = (UIInput) form.findComponent("text1");
221 Assert.assertNotNull(text1);
222 UIInput text2 = (UIInput) form.findComponent("text2");
223 Assert.assertNotNull(text2);
224
225 compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
226 facet1.pushComponentToEL(facesContext, facet1);
227 compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
228 facet2.pushComponentToEL(facesContext, facet2);
229 form.pushComponentToEL(facesContext, form);
230
231 button1.pushComponentToEL(facesContext, button1);
232 button1.getActionExpression().invoke(facesContext.getELContext(), new Object[]{});
233 button1.popComponentFromEL(facesContext);
234
235 button2.pushComponentToEL(facesContext, button2);
236 button2.getActionListeners()[0].processAction(new ActionEvent(button2));
237 button2.popComponentFromEL(facesContext);
238
239 button3.pushComponentToEL(facesContext, button3);
240 button3.getActionExpression().invoke(facesContext.getELContext(), new Object[]{});
241 button3.popComponentFromEL(facesContext);
242
243 text1.pushComponentToEL(facesContext, text1);
244 text1.getValidators()[0].validate(facesContext, text1, "");
245 text1.popComponentFromEL(facesContext);
246
247 text2.pushComponentToEL(facesContext, text2);
248 text2.getValueChangeListeners()[0].processValueChange(new ValueChangeEvent(text2, "old", "new"));
249 text2.popComponentFromEL(facesContext);
250
251 form.popComponentFromEL(facesContext);
252 facet2.popComponentFromEL(facesContext);
253 compositeComponent2.popComponentFromEL(facesContext);
254 facet1.popComponentFromEL(facesContext);
255 compositeComponent1.popComponentFromEL(facesContext);
256
257
258 StringWriter sw = new StringWriter();
259 MockResponseWriter mrw = new MockResponseWriter(sw);
260 facesContext.setResponseWriter(mrw);
261
262 compositeComponent1.encodeAll(facesContext);
263
264 sw.flush();
265
266 String resp = sw.toString();
267 }
268
269 @Test
270 public void testCompositeActionMethodInvocation() throws Exception
271 {
272 HelloWorld helloWorld = new HelloWorld();
273
274 facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
275 helloWorld);
276
277 UIViewRoot root = facesContext.getViewRoot();
278 vdl.buildView(facesContext, root, "testCompositeActionMethodInvocation.xhtml");
279
280 UIComponent form = root.findComponent("testForm1");
281 Assert.assertNotNull(form);
282 UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
283 Assert.assertNotNull(compositeComponent);
284 UINamingContainer compositeComponent2 = (UINamingContainer) compositeComponent.findComponent("button1");
285 Assert.assertNotNull(compositeComponent2);
286 UICommand button = (UICommand) compositeComponent2.findComponent("button2");
287 Assert.assertNotNull(button);
288
289 Assert.assertNotNull(button.getActionExpression());
290
291 Assert.assertEquals("success", button.getActionExpression().invoke(facesContext.getELContext(), null));
292
293 Assert.assertEquals(1, button.getActionListeners().length);
294
295
296
297
298
299
300
301
302 }
303
304
305
306
307
308
309 @Test
310 @SuppressWarnings("unchecked")
311 public void testUnspecifiedAttributes() throws Exception
312 {
313 UIViewRoot root = facesContext.getViewRoot();
314 vdl.buildView(facesContext, root, "testInterfaceDescriptorAttributes.xhtml");
315
316
317 UIComponent composite = root.findComponent("panel").getChildren().get(0);
318 CompositeComponentBeanInfo beanInfo =
319 (CompositeComponentBeanInfo) composite.getAttributes()
320 .get(UIComponent.BEANINFO_KEY);
321 Assert.assertNotNull(beanInfo);
322
323
324 BeanDescriptor interfaceDescriptor = beanInfo.getBeanDescriptor();
325 _checkUnspecifiedAttribute(interfaceDescriptor,
326 "unspecifiedInterfaceAttribute", "unspecifiedInterfaceValue");
327
328
329 Assert.assertEquals("Expecting one <composite:attribute>",
330 1, beanInfo.getPropertyDescriptors().length);
331 PropertyDescriptor attributeDescriptor = beanInfo.getPropertyDescriptors()[0];
332 _checkUnspecifiedAttribute(attributeDescriptor,
333 "unspecifiedAttributeAttribute", "unspecifiedAttributeValue");
334
335
336 Map<String, PropertyDescriptor> facetPropertyDescriptorMap =
337 (Map<String, PropertyDescriptor>) interfaceDescriptor.getValue(UIComponent.FACETS_KEY);
338 Assert.assertNotNull(facetPropertyDescriptorMap);
339 PropertyDescriptor facetDescriptor = facetPropertyDescriptorMap.get("facet");
340 _checkUnspecifiedAttribute(facetDescriptor,
341 "unspecifiedFacetAttribute", "unspecifiedFacetValue");
342 }
343
344
345
346
347
348
349
350 private void _checkUnspecifiedAttribute(FeatureDescriptor descriptor,
351 final String attributeName, final String attributeValue)
352 {
353 Object value = descriptor.getValue(attributeName);
354 Assert.assertTrue("Unspecified attributes must be stored as a ValueExpression",
355 value instanceof ValueExpression);
356 Assert.assertEquals(attributeValue,
357 ((ValueExpression) value).getValue(facesContext.getELContext()));
358 }
359
360
361
362
363
364
365
366 @Test
367 public void testDevelopmentValuesDevelopmentStage() throws Exception
368 {
369 _testDevelopmentValues(ProjectStage.Development);
370 }
371
372
373
374
375
376
377
378
379 @Test
380 public void testDevelopmentValuesProductionStage() throws Exception
381 {
382 _testDevelopmentValues(ProjectStage.Production);
383 }
384
385
386
387
388
389
390
391 @SuppressWarnings("unchecked")
392 private void _testDevelopmentValues(ProjectStage stage) throws Exception
393 {
394 final boolean development = stage.equals(ProjectStage.Development);
395
396
397 setProjectStage(stage);
398
399 UIViewRoot root = facesContext.getViewRoot();
400 vdl.buildView(facesContext, root, "testInterfaceDescriptorAttributes.xhtml");
401
402
403 UIComponent composite = root.findComponent("panel").getChildren().get(0);
404 CompositeComponentBeanInfo beanInfo =
405 (CompositeComponentBeanInfo) composite.getAttributes()
406 .get(UIComponent.BEANINFO_KEY);
407 Assert.assertNotNull(beanInfo);
408
409
410 BeanDescriptor interfaceDescriptor = beanInfo.getBeanDescriptor();
411 _checkDevelopmentValues(interfaceDescriptor, "interfaceDisplayName",
412 "interfaceShortDescription", development);
413
414
415 Assert.assertEquals("Expecting one <composite:attribute>",
416 1, beanInfo.getPropertyDescriptors().length);
417 PropertyDescriptor attributeDescriptor = beanInfo.getPropertyDescriptors()[0];
418 _checkDevelopmentValues(attributeDescriptor, "attributeDisplayName",
419 "attributeShortDescription", development);
420
421
422 Map<String, PropertyDescriptor> facetPropertyDescriptorMap =
423 (Map<String, PropertyDescriptor>) interfaceDescriptor.getValue(UIComponent.FACETS_KEY);
424 Assert.assertNotNull(facetPropertyDescriptorMap);
425 PropertyDescriptor facetDescriptor = facetPropertyDescriptorMap.get("facet");
426 _checkDevelopmentValues(facetDescriptor, "facetDisplayName",
427 "facetShortDescription", development);
428 }
429
430
431
432
433
434
435
436
437 private void _checkDevelopmentValues(FeatureDescriptor descriptor,
438 String displayName, String shortDescription,
439 final boolean development)
440 {
441 boolean booleanPropertiesValue;
442
443
444 if (development)
445 {
446
447
448 booleanPropertiesValue = true;
449
450
451 }
452 else
453 {
454
455 booleanPropertiesValue = false;
456
457
458 displayName = descriptor.getName();
459
460
461 shortDescription = displayName;
462 }
463
464
465 Assert.assertEquals(displayName, descriptor.getDisplayName());
466 Assert.assertEquals(shortDescription, descriptor.getShortDescription());
467 Assert.assertEquals(booleanPropertiesValue, descriptor.isExpert());
468 Assert.assertEquals(booleanPropertiesValue, descriptor.isHidden());
469 Assert.assertEquals(booleanPropertiesValue, descriptor.isPreferred());
470 }
471
472 @Test
473 public void testSimpleActionTargetAttributeName() throws Exception
474 {
475 MockAttributeBean bean = new MockAttributeBean();
476
477 facesContext.getExternalContext().getRequestMap().put("bean",
478 bean);
479
480 UIViewRoot root = facesContext.getViewRoot();
481 vdl.buildView(facesContext, root, "testActionTargetAttributeName.xhtml");
482
483 UIComponent panelGroup1 = root.findComponent("mainForm:testGroup1");
484 Assert.assertNotNull(panelGroup1);
485 UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.findComponent("cc1");
486 Assert.assertNotNull(compositeComponent1);
487 UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
488 Assert.assertNotNull(facet1);
489 HtmlCommandButton button1 = (HtmlCommandButton) facet1.findComponent("submitButton");
490 Assert.assertNotNull(button1);
491 HtmlCommandButton button2 = (HtmlCommandButton) facet1.findComponent("cancelAction");
492 Assert.assertNotNull(button2);
493
494 compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
495 facet1.pushComponentToEL(facesContext, facet1);
496 button1.pushComponentToEL(facesContext, button1);
497
498 MethodExpression method = button1.getActionExpression();
499 Assert.assertEquals("testActionMethodTypeSubmit", method.invoke(facesContext.getELContext(), null));
500
501 Assert.assertEquals(1, button1.getActionListeners().length);
502 button1.getActionListeners()[0].processAction(new ActionEvent(button1));
503 Assert.assertTrue(bean.isSubmitActionListenerCalled());
504
505 button1.popComponentFromEL(facesContext);
506 button2.pushComponentToEL(facesContext, button2);
507
508 method = button2.getActionExpression();
509 Assert.assertEquals(bean.cancelAction(), method.invoke(facesContext.getELContext(), null));
510
511 Assert.assertEquals(1, button2.getActionListeners().length);
512 button2.getActionListeners()[0].processAction(new ActionEvent(button2));
513 Assert.assertTrue(bean.isCancelActionListenerCalled());
514
515 button2.popComponentFromEL(facesContext);
516 facet1.popComponentFromEL(facesContext);
517 compositeComponent1.popComponentFromEL(facesContext);
518
519 StringWriter sw = new StringWriter();
520 MockResponseWriter mrw = new MockResponseWriter(sw);
521 facesContext.setResponseWriter(mrw);
522
523 compositeComponent1.encodeAll(facesContext);
524 sw.flush();
525
526
527
528
529
530
531
532
533 }
534
535 @Test
536 public void testCompositeActionTargetAttributeName() throws Exception
537 {
538 MockAttributeBean bean = new MockAttributeBean();
539
540 facesContext.getExternalContext().getRequestMap().put("bean",
541 bean);
542
543 UIViewRoot root = facesContext.getViewRoot();
544 vdl.buildView(facesContext, root, "testCompositeActionTargetAttributeName.xhtml");
545
546 UIComponent panelGroup1 = root.findComponent("mainForm:testGroup1");
547 Assert.assertNotNull(panelGroup1);
548 UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.findComponent("cc1");
549 Assert.assertNotNull(compositeComponent1);
550 UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
551 Assert.assertNotNull(facet1);
552 UINamingContainer compositeComponent2 = (UINamingContainer) facet1.getChildren().get(0);
553 Assert.assertNotNull(compositeComponent2);
554 UIComponent facet2 = compositeComponent2.getFacet(UIComponent.COMPOSITE_FACET_NAME);
555 Assert.assertNotNull(facet2);
556
557
558 HtmlCommandButton button1 = (HtmlCommandButton) facet2.findComponent("submitButton");
559 Assert.assertNotNull(button1);
560 HtmlCommandButton button2 = (HtmlCommandButton) facet2.findComponent("cancelAction");
561 Assert.assertNotNull(button2);
562
563 compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
564 facet1.pushComponentToEL(facesContext, facet1);
565 compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
566 facet2.pushComponentToEL(facesContext, facet1);
567 button1.pushComponentToEL(facesContext, button1);
568
569 MethodExpression method = button1.getActionExpression();
570 Assert.assertEquals("testActionMethodTypeSubmit", method.invoke(facesContext.getELContext(), null));
571
572 Assert.assertEquals(1, button1.getActionListeners().length);
573 button1.getActionListeners()[0].processAction(new ActionEvent(button1));
574 Assert.assertTrue(bean.isSubmitActionListenerCalled());
575
576 button1.popComponentFromEL(facesContext);
577 button2.pushComponentToEL(facesContext, button2);
578
579 method = button2.getActionExpression();
580 Assert.assertEquals(bean.cancelAction(), method.invoke(facesContext.getELContext(), null));
581
582 Assert.assertEquals(1, button2.getActionListeners().length);
583 button2.getActionListeners()[0].processAction(new ActionEvent(button2));
584 Assert.assertTrue(bean.isCancelActionListenerCalled());
585
586 button2.popComponentFromEL(facesContext);
587 facet2.popComponentFromEL(facesContext);
588 compositeComponent2.popComponentFromEL(facesContext);
589 facet1.popComponentFromEL(facesContext);
590 compositeComponent1.popComponentFromEL(facesContext);
591
592 StringWriter sw = new StringWriter();
593 MockResponseWriter mrw = new MockResponseWriter(sw);
594 facesContext.setResponseWriter(mrw);
595
596 compositeComponent1.encodeAll(facesContext);
597 sw.flush();
598
599
600
601
602
603
604
605
606 }
607 }