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.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          //application.setViewHandler(new MockTestViewHandler());
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      * Components that render client behaviors should always render "id" and "name" attribute
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 }