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_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  
38  public class HtmlLinkRendererTest extends AbstractTomahawkViewControllerTestCase
39  {
40  
41      private MockResponseWriter writer;
42      private HtmlCommandLink link;
43  
44      public HtmlLinkRendererTest(String name)
45      {
46          super(name);
47      }
48      
49      public static Test suite() {
50          return new TestSuite(HtmlLinkRendererTest.class);
51      }
52  
53      public void setUp() throws Exception
54      {
55          super.setUp();
56          link = new HtmlCommandLink();
57          writer = (MockResponseWriter)facesContext.getResponseWriter();
58  
59          UIForm form = new UIForm();
60          form.getChildren().add(link);
61  
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"), 
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 }