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.StringWriter;
22
23 import javax.faces.component.UIColumn;
24 import javax.faces.component.behavior.AjaxBehavior;
25 import javax.faces.component.html.HtmlOutputText;
26 import javax.faces.component.html.HtmlPanelGrid;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
32 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
33 import org.apache.myfaces.test.base.AbstractJsfTestCase;
34 import org.apache.myfaces.test.mock.MockRenderKitFactory;
35 import org.apache.myfaces.test.mock.MockResponseWriter;
36
37
38
39
40
41 public class HtmlGridRendererTest extends AbstractJsfTestCase
42 {
43 private static final String LINE_SEPARATOR = System.getProperty(
44 "line.separator", "\r\n");
45
46 private MockResponseWriter writer ;
47 private HtmlPanelGrid panelGrid;
48 private HtmlOutputText colText;
49
50 public HtmlGridRendererTest(String name)
51 {
52 super(name);
53 }
54
55 public static Test suite() {
56 return new TestSuite(HtmlGridRendererTest.class);
57 }
58
59 public void setUp() throws Exception
60 {
61 super.setUp();
62
63 panelGrid = new HtmlPanelGrid();
64 colText = new HtmlOutputText();
65
66 writer = new MockResponseWriter(new StringWriter(), null, null);
67 facesContext.setResponseWriter(writer);
68
69 facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
70 facesContext.getRenderKit().addRenderer(
71 panelGrid.getFamily(),
72 panelGrid.getRendererType(),
73 new HtmlGridRenderer());
74 facesContext.getRenderKit().addRenderer(
75 colText.getFamily(),
76 colText.getRendererType(),
77 new HtmlTextRenderer());
78
79 facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
80 }
81
82 public void tearDown() throws Exception
83 {
84 super.tearDown();
85 panelGrid = null;
86 writer = null;
87 }
88
89 public void testRenderTable() throws Exception
90 {
91 UIColumn col1 = new UIColumn();
92 HtmlOutputText col1Text = new HtmlOutputText();
93 col1Text.setValue("col1Text");
94
95 UIColumn col2 = new UIColumn();
96 HtmlOutputText col2Text = new HtmlOutputText();
97 col2Text.setValue("col2Text");
98
99 col1.getChildren().add(col1Text);
100 col2.getChildren().add(col2Text);
101 panelGrid.getChildren().add(col1);
102 panelGrid.getChildren().add(col2);
103
104 panelGrid.encodeBegin(facesContext);
105 panelGrid.encodeChildren(facesContext);
106 panelGrid.encodeEnd(facesContext);
107 facesContext.renderResponse();
108
109 String output = writer.getWriter().toString();
110 assertEquals("<table><tbody>"+LINE_SEPARATOR+
111 "<tr><td>col1Text</td></tr>" + LINE_SEPARATOR +
112 "<tr><td>col2Text</td></tr>" + LINE_SEPARATOR +
113 "</tbody>"+LINE_SEPARATOR+"</table>", output);
114 }
115
116 public void testHtmlPropertyPassTru() throws Exception
117 {
118 HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicReadOnlyAttrs();
119
120 HtmlCheckAttributesUtil.checkRenderedAttributes(
121 panelGrid, facesContext, writer, attrs);
122 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
123 fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
124 }
125 }
126
127 public void testHtmlPropertyPassTruNotRendered() throws Exception
128 {
129 HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateAttrsNotRenderedForReadOnly();
130
131 HtmlCheckAttributesUtil.checkRenderedAttributes(
132 panelGrid, facesContext, writer, attrs);
133 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
134 fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
135 }
136 }
137
138
139
140
141 public void testClientBehaviorHolderRendersIdAndName()
142 {
143 panelGrid.addClientBehavior("click", new AjaxBehavior());
144 try
145 {
146 panelGrid.encodeAll(facesContext);
147 String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
148 assertTrue(output.matches(".+id=\".+\".+"));
149 assertTrue(output.matches(".+name=\".+\".+"));
150 }
151 catch (Exception e)
152 {
153 fail(e.getMessage());
154 }
155
156 }
157 }