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.html.HtmlOutputText;
25  import javax.faces.component.html.HtmlPanelGrid;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
31  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
32  import org.apache.shale.test.base.AbstractJsfTestCase;
33  import org.apache.shale.test.mock.MockRenderKitFactory;
34  import org.apache.shale.test.mock.MockResponseWriter;
35  
36  /**
37   * @author Bruno Aranda (latest modification by $Author: baranda $)
38   * @version $Revision: 451814 $ $Date: 2006-10-01 22:28:42 +0100 (dom, 01 oct 2006) $
39   */
40  public class HtmlGridRendererTest extends AbstractJsfTestCase
41  {
42      private static final String LINE_SEPARATOR = System.getProperty(
43              "line.separator", "\r\n");
44  
45      private MockResponseWriter writer ;
46      private HtmlPanelGrid panelGrid;
47      private HtmlOutputText colText;
48  
49      public HtmlGridRendererTest(String name)
50      {
51          super(name);
52      }
53      
54      public static Test suite() {
55          return new TestSuite(HtmlGridRendererTest.class);
56      }
57  
58      public void setUp() throws Exception
59      {
60          super.setUp();
61  
62          panelGrid = new HtmlPanelGrid();
63          colText = new HtmlOutputText();
64  
65          writer = new MockResponseWriter(new StringWriter(), null, null);
66          facesContext.setResponseWriter(writer);
67  
68          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
69          facesContext.getRenderKit().addRenderer(
70                  panelGrid.getFamily(),
71                  panelGrid.getRendererType(),
72                  new HtmlGridRenderer());
73          facesContext.getRenderKit().addRenderer(
74                  colText.getFamily(),
75                  colText.getRendererType(),
76                  new HtmlTextRenderer());
77  
78      }
79  
80      public void tearDown() throws Exception
81      {
82          super.tearDown();
83          panelGrid = null;
84          writer = null;
85      }
86  
87      public void testRenderTable() throws Exception
88      {
89          UIColumn col1 = new UIColumn();
90          HtmlOutputText col1Text = new HtmlOutputText();
91          col1Text.setValue("col1Text");
92  
93          UIColumn col2 = new UIColumn();
94          HtmlOutputText col2Text = new HtmlOutputText();
95          col2Text.setValue("col2Text");
96  
97          col1.getChildren().add(col1Text);
98          col2.getChildren().add(col2Text);
99          panelGrid.getChildren().add(col1);
100         panelGrid.getChildren().add(col2);
101 
102         panelGrid.encodeBegin(facesContext);
103         panelGrid.encodeChildren(facesContext);
104         panelGrid.encodeEnd(facesContext);
105         facesContext.renderResponse();
106 
107         String output = writer.getWriter().toString();
108         assertEquals("<table><tbody><tr><td>col1Text</td></tr>" + LINE_SEPARATOR +
109                 "<tr><td>col2Text</td></tr>" + LINE_SEPARATOR +
110                 "</tbody></table>", output);
111     }
112 
113     public void testHtmlPropertyPassTru() throws Exception 
114     { 
115         HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicReadOnlyAttrs();
116 
117         HtmlCheckAttributesUtil.checkRenderedAttributes(
118                 panelGrid, facesContext, writer, attrs);
119         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
120             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
121         }
122     }
123     
124     public void testHtmlPropertyPassTruNotRendered() throws Exception 
125     { 
126         HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateAttrsNotRenderedForReadOnly();
127 
128         HtmlCheckAttributesUtil.checkRenderedAttributes(
129                 panelGrid, facesContext, writer, attrs);
130         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
131             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
132         }
133     }
134 }