1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.jsf.html;
20
21 import java.io.StringWriter;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UIForm;
24 import javax.faces.component.UIInput;
25 import javax.faces.component.UIOutput;
26 import javax.faces.component.UIViewRoot;
27 import javax.faces.component.html.HtmlBody;
28 import javax.faces.component.html.HtmlCommandButton;
29 import javax.faces.component.html.HtmlCommandLink;
30 import javax.faces.component.html.HtmlGraphicImage;
31 import javax.faces.component.html.HtmlHead;
32 import javax.faces.component.html.HtmlInputFile;
33 import javax.faces.component.html.HtmlInputHidden;
34 import javax.faces.component.html.HtmlInputSecret;
35 import javax.faces.component.html.HtmlInputText;
36 import javax.faces.component.html.HtmlInputTextarea;
37 import javax.faces.component.html.HtmlOutcomeTargetButton;
38 import javax.faces.component.html.HtmlOutcomeTargetLink;
39 import javax.faces.component.html.HtmlOutputLabel;
40 import javax.faces.component.html.HtmlOutputLink;
41 import javax.faces.component.html.HtmlSelectBooleanCheckbox;
42 import javax.faces.component.html.HtmlSelectManyListbox;
43 import javax.faces.component.html.HtmlSelectOneListbox;
44 import javax.faces.context.ResponseWriter;
45 import javax.faces.view.Location;
46 import javax.faces.view.facelets.Tag;
47 import javax.faces.view.facelets.TagAttribute;
48 import javax.faces.view.facelets.TagDecorator;
49 import org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl;
50 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
51 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
52 import org.apache.myfaces.view.facelets.FaceletTestCase;
53 import org.apache.myfaces.view.facelets.tag.TagAttributeImpl;
54 import org.apache.myfaces.view.facelets.tag.TagAttributesImpl;
55 import org.junit.Assert;
56 import org.junit.Test;
57
58
59
60
61
62 public class DefaultHtmlDecoratorTestCase extends FaceletTestCase
63 {
64
65 @Override
66 protected void setUpServletObjects() throws Exception
67 {
68 super.setUpServletObjects();
69
70
71 }
72
73 @Test
74 public void testHtmlPassthrough1() throws Exception
75 {
76 request.getSession().setAttribute("test", new MockBean());
77
78 UIViewRoot root = facesContext.getViewRoot();
79 vdl.buildView(facesContext, root, "testHtmlPassthrough1.xhtml");
80
81 checkTags();
82
83
84
85
86
87 }
88
89 @Test
90 public void testHtmlPassthrough2() throws Exception
91 {
92 request.getSession().setAttribute("test", new MockBean());
93
94 UIViewRoot root = facesContext.getViewRoot();
95 vdl.buildView(facesContext, root, "testHtmlPassthrough2.xhtml");
96
97 checkTags();
98
99
100
101
102
103 }
104
105 private void checkTags() throws Exception
106 {
107 UIViewRoot root = facesContext.getViewRoot();
108
109 HtmlHead head = (HtmlHead) root.findComponent("idHead");
110 int linksFound = 0;
111 int scriptsFound = 0;
112 for (UIComponent child : head.getChildren())
113 {
114 if (child instanceof UIOutput)
115 {
116 if ("javax.faces.resource.Script".equals(child.getRendererType()))
117 {
118 Assert.assertEquals("osc", child.getId());
119 scriptsFound++;
120 }
121 if ("javax.faces.resource.Stylesheet".equals(child.getRendererType()))
122 {
123 Assert.assertEquals("osh", child.getId());
124 linksFound++;
125 }
126 }
127 }
128 for (UIComponent child : root.getComponentResources(facesContext, "head"))
129 {
130 if ("javax.faces.resource.Stylesheet".equals(child.getRendererType()))
131 {
132 Assert.assertEquals("osh", child.getId());
133 linksFound++;
134 }
135 }
136
137 Assert.assertEquals(1, linksFound);
138 Assert.assertEquals(1, scriptsFound);
139
140 HtmlBody body = (HtmlBody) root.findComponent("idBody");
141 Assert.assertNotNull(body);
142
143 UIForm form = (UIForm) root.findComponent("myForm");
144 Assert.assertNotNull(form);
145
146 HtmlCommandLink link1 = (HtmlCommandLink) form.findComponent("link1");
147 Assert.assertNotNull(link1);
148 Assert.assertEquals("#{test.testAction}", link1.getActionExpression().getExpressionString());
149
150 HtmlCommandLink link2 = (HtmlCommandLink) form.findComponent("link2");
151 Assert.assertNotNull(link2);
152 Assert.assertEquals(1, link2.getActionListeners().length);
153
154 HtmlOutputLink link3 = (HtmlOutputLink) form.findComponent("link3");
155 Assert.assertNotNull(link3);
156 Assert.assertEquals("/my/new/location.txt", link3.getValue());
157
158 HtmlOutcomeTargetLink link4 = (HtmlOutcomeTargetLink) form.findComponent("link4");
159 Assert.assertNotNull(link4);
160 Assert.assertEquals("rollback", link4.getOutcome());
161
162 HtmlCommandButton button = (HtmlCommandButton) form.findComponent("button1");
163 Assert.assertNotNull(button);
164 Assert.assertEquals("#{test.testAction}", button.getActionExpression().getExpressionString());
165
166 HtmlOutcomeTargetButton button2 = (HtmlOutcomeTargetButton) form.findComponent("button2");
167 Assert.assertNotNull(button2);
168 Assert.assertEquals("rollback", button2.getOutcome());
169
170 HtmlGraphicImage img1 = (HtmlGraphicImage) form.findComponent("img1");
171 Assert.assertNotNull(img1);
172 Assert.assertEquals("/my/image.png", img1.getUrl());
173
174 HtmlCommandButton input1 = (HtmlCommandButton) form.findComponent("input1");
175 Assert.assertNotNull(input1);
176
177 HtmlSelectBooleanCheckbox input2 = (HtmlSelectBooleanCheckbox) form.findComponent("input2");
178 Assert.assertNotNull(input2);
179
180 HtmlInputText input3 = (HtmlInputText) form.findComponent("input3");
181 Assert.assertNotNull(input3);
182
183 HtmlInputText input4 = (HtmlInputText) form.findComponent("input4");
184 Assert.assertNotNull(input4);
185
186 HtmlInputText input5 = (HtmlInputText) form.findComponent("input5");
187 Assert.assertNotNull(input5);
188
189 HtmlInputText input6 = (HtmlInputText) form.findComponent("input6");
190 Assert.assertNotNull(input6);
191
192 HtmlInputText input7 = (HtmlInputText) form.findComponent("input7");
193 Assert.assertNotNull(input7);
194
195 HtmlInputText input8 = (HtmlInputText) form.findComponent("input8");
196 Assert.assertNotNull(input8);
197
198 HtmlInputText input9 = (HtmlInputText) form.findComponent("input9");
199 Assert.assertNotNull(input9);
200
201 HtmlInputText input10 = (HtmlInputText) form.findComponent("input10");
202 Assert.assertNotNull(input10);
203
204 HtmlInputText input11 = (HtmlInputText) form.findComponent("input11");
205 Assert.assertNotNull(input11);
206
207 HtmlInputText input12 = (HtmlInputText) form.findComponent("input12");
208 Assert.assertNotNull(input12);
209
210 HtmlInputText input13 = (HtmlInputText) form.findComponent("input13");
211 Assert.assertNotNull(input13);
212
213 HtmlInputText input14 = (HtmlInputText) form.findComponent("input14");
214 Assert.assertNotNull(input14);
215
216 HtmlInputFile input15 = (HtmlInputFile) form.findComponent("input15");
217 Assert.assertNotNull(input15);
218
219 HtmlInputHidden input16 = (HtmlInputHidden) form.findComponent("input16");
220 Assert.assertNotNull(input16);
221
222 HtmlInputSecret input17 = (HtmlInputSecret) form.findComponent("input17");
223 Assert.assertNotNull(input17);
224
225 HtmlCommandButton input18 = (HtmlCommandButton) form.findComponent("input18");
226 Assert.assertNotNull(input18);
227
228 HtmlCommandButton input19 = (HtmlCommandButton) form.findComponent("input19");
229 Assert.assertNotNull(input19);
230
231 HtmlInputText input20 = (HtmlInputText) form.findComponent("input20");
232 Assert.assertNotNull(input20);
233
234 HtmlOutputLabel label1 = (HtmlOutputLabel) form.findComponent("label1");
235 Assert.assertNotNull(label1);
236
237 HtmlSelectOneListbox select1 = (HtmlSelectOneListbox) form.findComponent("select1");
238 Assert.assertNotNull(select1);
239
240 HtmlSelectManyListbox select2 = (HtmlSelectManyListbox) form.findComponent("select2");
241 Assert.assertNotNull(select2);
242
243 HtmlInputTextarea textarea1 = (HtmlInputTextarea) form.findComponent("textarea1");
244 Assert.assertNotNull(textarea1);
245 }
246
247 @Test
248 public void testDefaultTagDecorator1() throws Exception
249 {
250 Location location = new Location("/test.xhtml", 20, 5);
251 Tag tag = new Tag(location, DefaultTagDecorator.XHTML_NAMESPACE, "body", "body",
252 new TagAttributesImpl(new TagAttribute[]
253 {
254 new TagAttributeImpl(location, DefaultTagDecorator.JSF_NAMESPACE, "id", "jsf:id", "idBody")
255 }
256 ));
257
258 TagDecorator tagDecorator = new DefaultTagDecorator();
259 Tag decoratedTag = tagDecorator.decorate(tag);
260
261 Assert.assertNotNull(decoratedTag);
262 }
263
264 @Test
265 public void testDefaultTagDecorator2() throws Exception
266 {
267 Location location = new Location("/test.xhtml", 20, 5);
268 Tag tag = new Tag(location, DefaultTagDecorator.XHTML_NAMESPACE, "a", "a",
269 new TagAttributesImpl(new TagAttribute[]
270 {
271 new TagAttributeImpl(location, DefaultTagDecorator.JSF_NAMESPACE, "action", "jsf:action", "#{test.testAction}")
272 }
273 ));
274
275 TagDecorator tagDecorator = new DefaultTagDecorator();
276 Tag decoratedTag = tagDecorator.decorate(tag);
277
278 Assert.assertNotNull(decoratedTag);
279 }
280
281 @Test
282 public void testDefaultTagDecorator3() throws Exception
283 {
284 Location location = new Location("/test.xhtml", 20, 5);
285 Tag tag = new Tag(location, DefaultTagDecorator.XHTML_NAMESPACE, "body", "body",
286 new TagAttributesImpl(new TagAttribute[]
287 {
288 new TagAttributeImpl(location, DefaultTagDecorator.JSF_ALIAS_NAMESPACE, "id", "jsf:id", "idBody")
289 }
290 ));
291
292 TagDecorator tagDecorator = new DefaultTagDecorator();
293 Tag decoratedTag = tagDecorator.decorate(tag);
294
295 Assert.assertNotNull(decoratedTag);
296 }
297
298 @Test
299 public void testDefaultTagDecorator4() throws Exception
300 {
301 Location location = new Location("/test.xhtml", 20, 5);
302 Tag tag = new Tag(location, DefaultTagDecorator.XHTML_NAMESPACE, "a", "a",
303 new TagAttributesImpl(new TagAttribute[]
304 {
305 new TagAttributeImpl(location, DefaultTagDecorator.JSF_ALIAS_NAMESPACE, "action", "jsf:action", "#{test.testAction}")
306 }
307 ));
308
309 TagDecorator tagDecorator = new DefaultTagDecorator();
310 Tag decoratedTag = tagDecorator.decorate(tag);
311
312 Assert.assertNotNull(decoratedTag);
313 }
314
315 @Test
316 public void testNoMatchJSFElement1() throws Exception
317 {
318 request.getSession().setAttribute("test", new MockBean());
319
320 UIViewRoot root = facesContext.getViewRoot();
321 vdl.buildView(facesContext, root, "testNoMatchJSFElement1.xhtml");
322
323 UIComponent box1 = root.findComponent("myForm:box1");
324 Assert.assertNotNull(box1);
325 Assert.assertEquals(box1.getRendererType(), "javax.faces.passthrough.Element");
326
327
328
329
330
331 }
332
333 @Test
334 public void testConvertTagAttributes1() throws Exception
335 {
336 request.getSession().setAttribute("test", new MockBean());
337
338 UIViewRoot root = facesContext.getViewRoot();
339 vdl.buildView(facesContext, root, "testConvertTagAttributes1.xhtml");
340
341
342
343
344
345
346 UIInput input1 = (UIInput) root.findComponent("myForm:box1");
347 Assert.assertNotNull(input1);
348
349 Assert.assertEquals(input1.getPassThroughAttributes().get("placeholder"), "Enter text");
350 Assert.assertNull(input1.getAttributes().get("placeholder"));
351
352
353 Assert.assertEquals(input1.getAttributes().get("customAttr"), "SomeValue");
354
355
356 Assert.assertNull(input1.getPassThroughAttributes().get("customAttr"));
357
358 Assert.assertEquals(input1.getPassThroughAttributes().get("data_up"), "Going Up");
359 Assert.assertNull(input1.getAttributes().get("data_up"));
360
361 Assert.assertNotNull(input1.getValueExpression("value"));
362
363 Assert.assertNull(input1.getPassThroughAttributes().get("value"));
364 Assert.assertEquals(input1.getValue(), "value1");
365 Assert.assertEquals(input1.getAttributes().get("value"), "value1");
366
367
368
369
370
371
372 UIComponent input2 = root.findComponent("myForm:box2");
373 Assert.assertFalse(input2 instanceof UIInput);
374 Assert.assertEquals(input2.getRendererType(), "javax.faces.passthrough.Element");
375
376 Assert.assertEquals(input2.getPassThroughAttributes().get("placeholder"), "Enter text");
377
378 Assert.assertNull(input2.getAttributes().get("placeholder"));
379
380 Assert.assertEquals(input2.getAttributes().get("customAttr"), "SomeValue");
381
382
383 Assert.assertNull(input2.getPassThroughAttributes().get("customAttr"));
384
385 Assert.assertEquals(input2.getPassThroughAttributes().get("data_up"), "Going Up");
386 Assert.assertNull(input2.getAttributes().get("data_up"));
387
388
389
390 Assert.assertNotNull(input2.getValueExpression("value"));
391 Assert.assertNull(input2.getPassThroughAttributes().get("value"));
392 Assert.assertNotNull(input2.getAttributes().get("value"));
393
394
395
396
397
398
399
400
401 UIComponent input3 = root.findComponent("myForm:box3");
402 Assert.assertFalse(input3 instanceof UIInput);
403 Assert.assertEquals(input3.getRendererType(), "javax.faces.passthrough.Element");
404
405 Assert.assertEquals(input3.getAttributes().get("placeholder"), "Enter text");
406 Assert.assertNull(input3.getPassThroughAttributes().get("placeholder"));
407
408 Assert.assertNull(input3.getPassThroughAttributes().get("customAttr"));
409 Assert.assertEquals(input3.getAttributes().get("customAttr"), "SomeValue");
410
411 Assert.assertEquals(input3.getPassThroughAttributes().get("data_up"), "Going Up");
412 Assert.assertNull(input3.getAttributes().get("data_up"));
413
414 Assert.assertNotNull(input3.getValueExpression("value"));
415 Assert.assertNull(input3.getPassThroughAttributes().get("value"));
416 Assert.assertNotNull(input3.getAttributes().get("value"));
417
418
419
420
421
422
423
424
425 UIComponent box4 = root.findComponent("myForm:box4");
426 Assert.assertNotNull(box4);
427 UIComponent boxDiv4 = box4.getChildren().get(0);
428 Assert.assertNotNull(boxDiv4);
429 Assert.assertEquals(boxDiv4.getAttributes().get("styleClass"), "noprint");
430
431 Assert.assertNull(boxDiv4.getPassThroughAttributes().get("class"));
432
433
434
435
436
437
438 UIComponent box5 = root.findComponent("myForm:box5");
439 Assert.assertNotNull(box5);
440 UIComponent boxDiv5 = box5.getChildren().get(0);
441 Assert.assertNotNull(boxDiv5);
442 Assert.assertNotNull(boxDiv5.getAttributes().get("style"));
443
444 Assert.assertNull(boxDiv5.getPassThroughAttributes().get("style"));
445
446 StringWriter sw = new StringWriter();
447
448 ResponseWriter mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
449 facesContext.setResponseWriter(mrw);
450
451 HtmlRenderedAttr[] attrs = {
452 new HtmlRenderedAttr("data_up", "Going Up"),
453 new HtmlRenderedAttr("placeholder", "Enter text"),
454 new HtmlRenderedAttr("onclick", "alert('hello')"),
455
456 new HtmlRenderedAttr("value", "value1")
457 };
458
459 input1.encodeAll(facesContext);
460
461 sw.flush();
462
463 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
464 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs))
465 {
466 Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, sw.toString()));
467 }
468
469 sw = new StringWriter();
470 mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
471 facesContext.setResponseWriter(mrw);
472
473 input2.encodeAll(facesContext);
474
475 sw.flush();
476
477 attrs = new HtmlRenderedAttr[]{
478 new HtmlRenderedAttr("data_up", "Going Up"),
479 new HtmlRenderedAttr("onclick", "alert('hello')"),
480
481 new HtmlRenderedAttr("placeholder", "Enter text")
482 };
483
484 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
485 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs))
486 {
487 Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, sw.toString()));
488 }
489 Assert.assertTrue(sw.toString().contains("<meter "));
490 Assert.assertTrue(sw.toString().contains("</meter>"));
491
492 sw = new StringWriter();
493 mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
494 facesContext.setResponseWriter(mrw);
495
496 input3.encodeAll(facesContext);
497
498 sw.flush();
499
500 attrs = new HtmlRenderedAttr[]{
501 new HtmlRenderedAttr("data_up", "Going Up"),
502
503
504
505 new HtmlRenderedAttr("onclick", "alert('hello')"),
506 };
507
508 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
509 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs))
510 {
511 Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, sw.toString()));
512 }
513 Assert.assertTrue(sw.toString().contains("<meter "));
514 Assert.assertTrue(sw.toString().contains("</meter>"));
515
516
517 sw = new StringWriter();
518 mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
519 facesContext.setResponseWriter(mrw);
520
521 boxDiv4.encodeAll(facesContext);
522
523 sw.flush();
524
525 attrs = new HtmlRenderedAttr[]{
526 new HtmlRenderedAttr("class", "noprint"),
527 };
528
529 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
530 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs))
531 {
532 Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, sw.toString()));
533 }
534 Assert.assertTrue(sw.toString().contains("MYBOX4"));
535 Assert.assertTrue(sw.toString().contains("<div "));
536 Assert.assertTrue(sw.toString().contains("</div>"));
537
538
539 sw = new StringWriter();
540 mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
541 facesContext.setResponseWriter(mrw);
542
543 boxDiv5.encodeAll(facesContext);
544
545 sw.flush();
546
547 attrs = new HtmlRenderedAttr[]{
548 new HtmlRenderedAttr("style", "noprint"),
549 };
550
551 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
552 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs))
553 {
554 Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, sw.toString()));
555 }
556 Assert.assertTrue(sw.toString().contains("MYBOX5"));
557 Assert.assertTrue(sw.toString().contains("<div "));
558 Assert.assertTrue(sw.toString().contains("</div>"));
559 }
560
561 @Test
562 public void testConvertTagAttributes6() throws Exception
563 {
564 request.getSession().setAttribute("test", new MockBean());
565
566 UIViewRoot root = facesContext.getViewRoot();
567 vdl.buildView(facesContext, root, "testConvertTagAttributes6.xhtml");
568
569
570
571
572
573
574 UIComponent box6 = root.findComponent("myForm:box6");
575 Assert.assertNotNull(box6);
576 Assert.assertEquals(box6.getAttributes().get("onclick"), "alert('hello')");
577
578 StringWriter sw = new StringWriter();
579 ResponseWriter mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
580 facesContext.setResponseWriter(mrw);
581
582 box6.encodeAll(facesContext);
583
584 sw.flush();
585 HtmlRenderedAttr[] attrs = new HtmlRenderedAttr[]{
586 new HtmlRenderedAttr("onclick",
587 "jsf.util.chain(this, event,'alert(\\'hello\\')', "
588 + "'jsf.ajax.request(this,event,{render:\\'myForm:box5 \\',"
589 + "\\'javax.faces.behavior.event\\':\\'click\\'})');"),
590 };
591
592 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
593 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs))
594 {
595 Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, sw.toString()));
596 }
597 Assert.assertTrue(sw.toString().contains("MYBOX6"));
598 Assert.assertTrue(sw.toString().contains("<div "));
599 Assert.assertTrue(sw.toString().contains("</div>"));
600 }
601
602 @Test
603 public void testConvertTagAttributes7() throws Exception
604 {
605 request.getSession().setAttribute("test", new MockBean());
606
607 UIViewRoot root = facesContext.getViewRoot();
608 vdl.buildView(facesContext, root, "testConvertTagAttributes7.xhtml");
609
610
611
612
613
614 UIComponent box7 = root.findComponent("myForm:box7");
615 Assert.assertNotNull(box7);
616 UIComponent boxDiv7 = box7.getChildren().get(0);
617 Assert.assertNotNull(boxDiv7);
618
619 StringWriter sw = new StringWriter();
620
621 ResponseWriter mrw = new HtmlResponseWriterImpl(sw, "text/html", "UTF-8");
622 facesContext.setResponseWriter(mrw);
623
624 boxDiv7.encodeAll(facesContext);
625
626 sw.flush();
627 HtmlRenderedAttr[] attrs = new HtmlRenderedAttr[]{
628 new HtmlRenderedAttr("alt", "Some Logo"),
629 };
630
631 HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
632 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs))
633 {
634 Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, sw.toString()));
635 }
636 Assert.assertTrue(sw.toString().contains("<img "));
637 Assert.assertTrue(sw.toString().contains("javax.faces.resource/external.png"));
638 }
639 }