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.HtmlInputSecret;
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 HtmlSecretRendererTest extends AbstractJsfTestCase
40  {
41      private MockResponseWriter writer ;
42      private HtmlInputSecret inputText;
43  
44      public HtmlSecretRendererTest(String name)
45      {
46          super(name);
47      }
48      
49      public static Test suite() {
50          return new TestSuite(HtmlSecretRendererTest.class);
51      }
52  
53      public void setUp() throws Exception
54      {
55          super.setUp();
56  
57          inputText = new HtmlInputSecret();
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                  inputText.getFamily(),
65                  inputText.getRendererType(),
66                  new HtmlSecretRenderer());
67          
68          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
69      }
70  
71      public void tearDown() throws Exception
72      {
73          super.tearDown();
74          inputText = null;
75          writer = null;
76      }
77  
78      public void testInputTextDefault() throws Exception
79      {
80          inputText.encodeEnd(facesContext);
81          facesContext.renderResponse();
82  
83          String output = writer.getWriter().toString();
84          assertEquals("<input type=\"password\" name=\"j_id__v_0\"/>", output);
85      }
86  
87      public void testHtmlPropertyPassTru() throws Exception
88      {
89          HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicAttrs();
90          
91          HtmlCheckAttributesUtil.checkRenderedAttributes(
92                  inputText, facesContext, writer, attrs);
93          if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
94              fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
95          }
96      }
97      
98      /**
99       * Components that render client behaviors should always render "id" and "name" attribute
100      */
101     public void testClientBehaviorHolderRendersIdAndName() 
102     {
103         inputText.addClientBehavior("keypress", new AjaxBehavior());
104         try 
105         {
106             inputText.encodeAll(facesContext);
107             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
108             assertTrue(output.matches(".+id=\".+\".+"));
109             assertTrue(output.matches(".+name=\".+\".+"));
110         }
111         catch (Exception e)
112         {
113             fail(e.getMessage());
114         }
115         
116     }
117 }