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.ext;
20  
21  import javax.faces.component.html.HtmlForm;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.myfaces.component.html.ext.HtmlCommandButton;
27  import org.apache.myfaces.shared_impl.config.MyfacesConfig;
28  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
29  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
30  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
31  import org.apache.shale.test.mock.MockExternalContext;
32  import org.apache.shale.test.mock.MockHttpServletRequest;
33  import org.apache.shale.test.mock.MockHttpServletResponse;
34  import org.apache.shale.test.mock.MockResponseWriter;
35  import org.apache.shale.test.mock.MockServletContext;
36  
37  public class HtmlButtonRendererTest extends AbstractTomahawkViewControllerTestCase {
38  
39      private MockResponseWriter writer;
40      private HtmlCommandButton commandButton;
41      private HtmlForm form;
42      
43      public HtmlButtonRendererTest(String name) {
44          super(name);
45      }
46      
47      public static Test suite() {
48          return new TestSuite(HtmlButtonRendererTest.class);
49      }
50      
51      public void setUp() throws Exception {
52          super.setUp();
53          writer = (MockResponseWriter)facesContext.getResponseWriter();
54          commandButton = new HtmlCommandButton();
55          form = new HtmlForm();
56          commandButton.setParent(form);
57      }
58      
59      public void tearDown() throws Exception {
60          super.tearDown();
61          writer = null;
62          form = null;
63          commandButton = null;
64      }
65  
66      public void testJSAllowedHtmlPropertyPassTru() throws Exception 
67      {
68          HtmlRenderedAttr[] attrs = {
69                  //_AccesskeyProperty
70                  new HtmlRenderedAttr("accesskey"),
71                  //_UniversalProperties
72                  new HtmlRenderedAttr("dir"), 
73                  new HtmlRenderedAttr("lang"), 
74                  new HtmlRenderedAttr("title"),
75                  //_FocusBlurProperties
76                  new HtmlRenderedAttr("onfocus"), 
77                  new HtmlRenderedAttr("onblur"),
78                  //_ChangeSelectProperties
79                  new HtmlRenderedAttr("onchange"), 
80                  new HtmlRenderedAttr("onselect"),
81                  //_EventProperties
82                  new HtmlRenderedAttr("onclick", "onclick", 
83                          "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                  //_TabindexProperty
97                  new HtmlRenderedAttr("tabindex")
98              };
99          
100         MockServletContext servletContext = new MockServletContext();
101         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "true");
102         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
103                 new MockHttpServletRequest(), new MockHttpServletResponse());
104         MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
105         facesContext.setExternalContext(mockExtCtx);
106     
107         HtmlCheckAttributesUtil.checkRenderedAttributes(
108                 commandButton, facesContext, writer, attrs);
109         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) 
110         {
111             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
112         }
113     }
114     
115     public void testJSNotAllowedHtmlPropertyPassTru() throws Exception 
116     {
117         HtmlRenderedAttr[] attrs = {
118             //_AccesskeyProperty
119             new HtmlRenderedAttr("accesskey"),
120             //_UniversalProperties
121             new HtmlRenderedAttr("dir"), 
122             new HtmlRenderedAttr("lang"), 
123             new HtmlRenderedAttr("title"),
124             //_FocusBlurProperties
125             new HtmlRenderedAttr("onfocus"), 
126             new HtmlRenderedAttr("onblur"),
127             //_ChangeSelectProperties
128             new HtmlRenderedAttr("onchange"), 
129             new HtmlRenderedAttr("onselect"),
130             //_EventProperties
131             new HtmlRenderedAttr("onclick"), 
132             new HtmlRenderedAttr("ondblclick"), 
133             new HtmlRenderedAttr("onkeydown"), 
134             new HtmlRenderedAttr("onkeypress"),
135             new HtmlRenderedAttr("onkeyup"), 
136             new HtmlRenderedAttr("onmousedown"), 
137             new HtmlRenderedAttr("onmousemove"), 
138             new HtmlRenderedAttr("onmouseout"),
139             new HtmlRenderedAttr("onmouseover"), 
140             new HtmlRenderedAttr("onmouseup"),
141             //_StyleProperties
142             new HtmlRenderedAttr("style"), 
143             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
144             //_TabindexProperty
145             new HtmlRenderedAttr("tabindex")
146         };
147         
148         HtmlCheckAttributesUtil.checkRenderedAttributes(
149                 commandButton, facesContext, writer, attrs);
150         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) 
151         {
152             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
153         }
154 
155     }
156 }