1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.renderkit.html;
20
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23
24 import javax.faces.component.behavior.AjaxBehavior;
25 import javax.faces.component.html.HtmlForm;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.myfaces.shared.renderkit.html.HTML;
31 import org.apache.myfaces.test.base.AbstractJsfTestCase;
32 import org.apache.myfaces.test.mock.MockRenderKitFactory;
33 import org.apache.myfaces.test.mock.MockResponseWriter;
34 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
35 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
36
37 public class HtmlFormRendererTest extends AbstractJsfTestCase
38 {
39 private MockResponseWriter writer ;
40 private HtmlForm form;
41
42 public HtmlFormRendererTest(String name)
43 {
44 super(name);
45 }
46
47 public static Test suite() {
48 return new TestSuite(HtmlFormRendererTest.class);
49 }
50
51 public void setUp() throws Exception
52 {
53 super.setUp();
54
55
56 form = new HtmlForm();
57
58 writer = new MockResponseWriter(new StringWriter(), null, null);
59 facesContext.setResponseWriter(writer);
60
61 facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
62 facesContext.getRenderKit().addRenderer(
63 form.getFamily(),
64 form.getRendererType(),
65 new HtmlFormRenderer());
66
67 facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
68 }
69
70 public void tearDown() throws Exception
71 {
72 super.tearDown();
73 form = null;
74 writer = null;
75 }
76
77 public void testHtmlPropertyPassTru() throws Exception
78 {
79 HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicReadOnlyAttrs();
80
81 try {
82 HtmlCheckAttributesUtil.checkRenderedAttributes(
83 form, facesContext, writer, attrs);
84 } catch(Exception e) {
85 StringWriter sw = new StringWriter();
86 e.printStackTrace(new PrintWriter(sw));
87 fail(sw.toString() + "\nHTML.FORM_PASSTHROUGH_ATTRIBUTES: " + printHTMLAttrs(HTML.FORM_PASSTHROUGH_ATTRIBUTES));
88 }
89 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
90 fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
91 }
92 }
93
94 public void testHtmlPropertyPassTruNotRendered() throws Exception
95 {
96 HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateAttrsNotRenderedForReadOnly();
97
98 try {
99 HtmlCheckAttributesUtil.checkRenderedAttributes(
100 form, facesContext, writer, attrs);
101 } catch(Exception e) {
102 StringWriter sw = new StringWriter();
103 e.printStackTrace(new PrintWriter(sw));
104 fail(sw.toString() + "\nHTML.FORM_PASSTHROUGH_ATTRIBUTES: " + printHTMLAttrs(HTML.FORM_PASSTHROUGH_ATTRIBUTES));
105 }
106 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
107 fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
108 }
109 }
110
111 private String printHTMLAttrs(String[] attrs) {
112 StringBuffer buffer = new StringBuffer();
113 for(int i = 0; i < attrs.length; i++) {
114 buffer.append(attrs[i]);
115 if(i+1 < attrs.length) {
116 buffer.append(", ");
117 }
118 }
119 return buffer.toString();
120 }
121
122
123
124
125 public void testClientBehaviorHolderRendersIdAndName()
126 {
127 form.addClientBehavior("focus", new AjaxBehavior());
128 try
129 {
130 form.encodeAll(facesContext);
131 String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
132 assertTrue(output.matches(".+id=\".+\".+"));
133 assertTrue(output.matches(".+name=\".+\".+"));
134 }
135 catch (Exception e)
136 {
137 fail(e.getMessage());
138 }
139
140 }
141 }