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.IOException;
22  import java.io.StringWriter;
23  
24  import javax.faces.component.behavior.AjaxBehavior;
25  import javax.faces.component.html.HtmlOutputLabel;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.myfaces.test.base.AbstractJsfTestCase;
31  import org.apache.myfaces.test.mock.MockRenderKitFactory;
32  import org.apache.myfaces.test.mock.MockResponseWriter;
33  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
34  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
35  
36  public class HtmlLabelRendererTest extends AbstractJsfTestCase
37  {
38      private MockResponseWriter writer;
39      private HtmlOutputLabel label;
40      
41      public HtmlLabelRendererTest(String name)
42      {
43          super(name);
44      }
45      
46      public static Test suite() {
47          return new TestSuite(HtmlLabelRendererTest.class);
48      }
49      
50      public void setUp() throws Exception {
51          super.setUp();
52          label = new HtmlOutputLabel();
53          writer = new MockResponseWriter(new StringWriter(), null, null);
54          facesContext.setResponseWriter(writer);
55          
56          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
57          facesContext.getRenderKit().addRenderer(
58                  label.getFamily(),
59                  label.getRendererType(),
60                  new HtmlLabelRenderer());
61          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
62      }
63      
64      public void tearDown() throws Exception {
65          super.tearDown();
66          writer = null;
67          label = null;
68      }
69  
70      public void testHtmlPropertyPassTru() throws Exception
71      {
72          HtmlRenderedAttr[] attrs = {
73              //_AccesskeyProperty
74              new HtmlRenderedAttr("accesskey"),
75              //_UniversalProperties
76              new HtmlRenderedAttr("dir"), 
77              new HtmlRenderedAttr("lang"), 
78              new HtmlRenderedAttr("title"),
79              //_FocusBlurProperties
80              new HtmlRenderedAttr("onfocus"), 
81              new HtmlRenderedAttr("onblur"),
82              //_EventProperties
83              new HtmlRenderedAttr("onclick"), 
84              new HtmlRenderedAttr("ondblclick"), 
85              new HtmlRenderedAttr("onkeydown"), 
86              new HtmlRenderedAttr("onkeypress"),
87              new HtmlRenderedAttr("onkeyup"), 
88              new HtmlRenderedAttr("onmousedown"), 
89              new HtmlRenderedAttr("onmousemove"), 
90              new HtmlRenderedAttr("onmouseout"),
91              new HtmlRenderedAttr("onmouseover"), 
92              new HtmlRenderedAttr("onmouseup"),
93              //_StyleProperties
94              new HtmlRenderedAttr("style"), 
95              new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
96          };
97          
98          label.setValue("outputdata");
99          label.setFor("compId");
100         
101         HtmlCheckAttributesUtil.checkRenderedAttributes(
102                 label, facesContext, writer, attrs);
103         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
104             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
105         }
106     }
107     
108     /**
109      * Gets the page contents.
110      * @return the page contents
111      */
112     protected String getPageContents()
113     {
114         return ((StringWriter) writer.getWriter()).toString();
115     }
116     
117     public void testEscapeUntouched() throws IOException
118     {
119         label.setId("labelId");
120         label.setValue("<span class=\"required\">field label</span>");
121 
122         // render label
123         label.encodeAll(facesContext);
124 
125         String page = getPageContents();
126         assertEquals("<label id=\"labelId\">&lt;span class=&quot;required&quot;&gt;field label&lt;/span&gt;</label>", page);
127     }
128 
129     public void testEscapeSetToFalse() throws IOException
130     {
131         label.setId("labelId");
132         label.setValue("<span class=\"required\">field label</span>");
133         label.setEscape(false);
134 
135         // render label
136         label.encodeAll(facesContext);
137 
138         String page = getPageContents();
139         assertEquals("<label id=\"labelId\"><span class=\"required\">field label</span></label>", page);
140     }
141     
142     /**
143      * Components that render client behaviors should always render "id" and "name" attribute
144      */
145     public void testClientBehaviorHolderRendersIdAndName() 
146     {
147         label.addClientBehavior("keypress", new AjaxBehavior());
148         try 
149         {
150             label.encodeAll(facesContext);
151             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
152             assertTrue(output.matches(".+id=\".+\".+"));
153             assertTrue(output.matches(".+name=\".+\".+"));
154         }
155         catch (Exception e)
156         {
157             fail(e.getMessage());
158         }
159         
160     }
161     
162 }