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.UIForm;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.myfaces.component.html.ext.HtmlCommandLink;
27  import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
28  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
29  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
30  import org.apache.myfaces.test.mock.MockExternalContext;
31  import org.apache.myfaces.test.mock.MockHttpServletRequest;
32  import org.apache.myfaces.test.mock.MockHttpServletResponse;
33  import org.apache.myfaces.test.mock.MockResponseWriter;
34  import org.apache.myfaces.test.mock.MockServletContext;
35  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
36  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
37  
38  
39  public class HtmlLinkRendererTest extends AbstractTomahawkViewControllerTestCase
40  {
41  
42      private MockResponseWriter writer;
43      private HtmlCommandLink link;
44  
45      public HtmlLinkRendererTest(String name)
46      {
47          super(name);
48      }
49      
50      public static Test suite() {
51          return new TestSuite(HtmlLinkRendererTest.class);
52      }
53  
54      public void setUp() throws Exception
55      {
56          super.setUp();
57          link = new HtmlCommandLink();
58          writer = (MockResponseWriter)facesContext.getResponseWriter();
59  
60          UIForm form = new UIForm();
61          form.getChildren().add(link);
62      }
63  
64      public void tearDown() throws Exception
65      {
66          super.tearDown();
67          writer = null;
68      }
69       
70      public void testJSAllowedHtmlPropertyPassTru() 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", "onclick", "return jsf.util.chain(document.getElementById('j_id0:j_id0'), event,'onclick', 'return myfaces.oam.submitForm(\\'j_id0\\',\\'j_id0:j_id0\\');');"), 
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         link.setValue("outputdata");
101         
102         HtmlCheckAttributesUtil.checkRenderedAttributes(
103                 link, facesContext, writer, attrs);
104         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
105             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
106         }
107     }
108     
109     public void testJSNotAllowedHtmlPropertyPassTru() throws Exception
110     {
111         HtmlRenderedAttr[] attrs = {
112             //_AccesskeyProperty
113             new HtmlRenderedAttr("accesskey"),
114             //_UniversalProperties
115             new HtmlRenderedAttr("dir"), 
116             new HtmlRenderedAttr("lang"), 
117             new HtmlRenderedAttr("title"),
118             //_FocusBlurProperties
119             new HtmlRenderedAttr("onfocus"), 
120             new HtmlRenderedAttr("onblur"),
121             //_EventProperties
122             //new HtmlRenderedAttr("onclick"), 
123             new HtmlRenderedAttr("ondblclick"), 
124             new HtmlRenderedAttr("onkeydown"), 
125             new HtmlRenderedAttr("onkeypress"),
126             new HtmlRenderedAttr("onkeyup"), 
127             new HtmlRenderedAttr("onmousedown"), 
128             new HtmlRenderedAttr("onmousemove"), 
129             new HtmlRenderedAttr("onmouseout"),
130             new HtmlRenderedAttr("onmouseover"), 
131             new HtmlRenderedAttr("onmouseup"),
132             //_StyleProperties
133             new HtmlRenderedAttr("style"), 
134             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
135             //_TabindexProperty
136             new HtmlRenderedAttr("tabindex")
137         };
138 
139         
140         link.setValue("outputdata");
141         
142         MockServletContext servletContext = new MockServletContext();
143         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "false");
144         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
145                 new MockHttpServletRequest(), new MockHttpServletResponse());
146         //MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
147         //facesContext.setExternalContext(mockExtCtx);
148         
149         HtmlCheckAttributesUtil.checkRenderedAttributes(
150                 link, facesContext, writer, attrs);
151         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
152             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
153         }
154     }
155 }