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