View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Bruno Aranda (latest modification by $Author: bommel $)
39   * @version $Revision: 1187701 $ $Date: 2011-10-22 07:21:54 -0500 (Sat, 22 Oct 2011) $
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      * Components that render client behaviors should always render "id" and "name" attribute
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 }