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              new HtmlRenderedAttr("role"),
80              //_FocusBlurProperties
81              new HtmlRenderedAttr("onfocus"), 
82              new HtmlRenderedAttr("onblur"),
83              //_EventProperties
84              new HtmlRenderedAttr("onclick"), 
85              new HtmlRenderedAttr("ondblclick"), 
86              new HtmlRenderedAttr("onkeydown"), 
87              new HtmlRenderedAttr("onkeypress"),
88              new HtmlRenderedAttr("onkeyup"), 
89              new HtmlRenderedAttr("onmousedown"), 
90              new HtmlRenderedAttr("onmousemove"), 
91              new HtmlRenderedAttr("onmouseout"),
92              new HtmlRenderedAttr("onmouseover"), 
93              new HtmlRenderedAttr("onmouseup"),
94              //_StyleProperties
95              new HtmlRenderedAttr("style"), 
96              new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
97          };
98          
99          label.setValue("outputdata");
100         label.setFor("compId");
101         
102         HtmlCheckAttributesUtil.checkRenderedAttributes(
103                 label, facesContext, writer, attrs);
104         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
105             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
106         }
107     }
108     
109     /**
110      * Gets the page contents.
111      * @return the page contents
112      */
113     protected String getPageContents()
114     {
115         return ((StringWriter) writer.getWriter()).toString();
116     }
117     
118     public void testEscapeUntouched() throws IOException
119     {
120         label.setId("labelId");
121         label.setValue("<span class=\"required\">field label</span>");
122 
123         // render label
124         label.encodeAll(facesContext);
125 
126         String page = getPageContents();
127         assertEquals("<label id=\"labelId\">&lt;span class=&quot;required&quot;&gt;field label&lt;/span&gt;</label>", page);
128     }
129 
130     public void testEscapeSetToFalse() throws IOException
131     {
132         label.setId("labelId");
133         label.setValue("<span class=\"required\">field label</span>");
134         label.setEscape(false);
135 
136         // render label
137         label.encodeAll(facesContext);
138 
139         String page = getPageContents();
140         assertEquals("<label id=\"labelId\"><span class=\"required\">field label</span></label>", page);
141     }
142     
143     /**
144      * Components that render client behaviors should always render "id" and "name" attribute
145      */
146     public void testClientBehaviorHolderRendersIdAndName() 
147     {
148         label.addClientBehavior("keypress", new AjaxBehavior());
149         try 
150         {
151             label.encodeAll(facesContext);
152             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
153             assertTrue(output.matches(".+id=\".+\".+"));
154             assertTrue(output.matches(".+name=\".+\".+"));
155         }
156         catch (Exception e)
157         {
158             fail(e.getMessage());
159         }
160         
161     }
162     
163 }