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