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.behavior.AjaxBehavior;
24  import javax.faces.component.html.HtmlInputTextarea;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
30  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
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  
35  /**
36   * @author Bruno Aranda (latest modification by $Author: lu4242 $)
37   * @version $Revision: 1421655 $ $Date: 2012-12-13 22:54:49 -0500 (Thu, 13 Dec 2012) $
38   */
39  public class HtmlTextareaRendererTest extends AbstractJsfTestCase
40  {
41      private MockResponseWriter writer ;
42      private HtmlInputTextarea inputTextarea;
43  
44      public HtmlTextareaRendererTest(String name)
45      {
46          super(name);
47      }
48      
49      public static Test suite() {
50          return new TestSuite(HtmlTextareaRendererTest.class);
51      }
52  
53      public void setUp() throws Exception
54      {
55          super.setUp();
56  
57          inputTextarea = new HtmlInputTextarea();
58  
59          writer = new MockResponseWriter(new StringWriter(), null, null);
60          facesContext.setResponseWriter(writer);
61  
62          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
63          facesContext.getRenderKit().addRenderer(
64                  inputTextarea.getFamily(),
65                  inputTextarea.getRendererType(),
66                  new HtmlTextareaRenderer());
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          inputTextarea = null;
74          writer = null;
75      }
76  
77      public void testRenderDefault() throws Exception
78      {
79          inputTextarea.encodeEnd(facesContext);
80          facesContext.renderResponse();
81  
82          String output = writer.getWriter().toString();
83          assertEquals("<textarea name=\"j_id__v_0\"></textarea>", output);
84      }
85  
86      public void testRenderColsRows() throws Exception
87      {
88          inputTextarea.setCols(5);
89          inputTextarea.setRows(10);
90          inputTextarea.encodeEnd(facesContext);
91          facesContext.renderResponse();
92  
93          String output = writer.getWriter().toString();
94          assertEquals("<textarea name=\"j_id__v_0\" cols=\"5\" rows=\"10\"></textarea>", output);
95      }
96      
97      public void testHtmlPropertyPassTru() throws Exception
98      {
99          HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicAttrs();
100         
101         HtmlCheckAttributesUtil.checkRenderedAttributes(
102                 inputTextarea, facesContext, writer, attrs);
103         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
104             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
105         }
106     }
107     
108     /**
109      * Components that render client behaviors should always render "id" and "name" attribute
110      */
111     public void testClientBehaviorHolderRendersIdAndName() 
112     {
113         inputTextarea.addClientBehavior("keypress", new AjaxBehavior());
114         try 
115         {
116             inputTextarea.encodeAll(facesContext);
117             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
118             assertTrue(output.matches("(?s).+id=\".+\".+"));
119             assertTrue(output.matches("(?s).+name=\".+\".+"));
120         }
121         catch (Exception e)
122         {
123             fail(e.getMessage());
124         }
125         
126     }
127 }