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.StringWriter;
22  
23  import javax.faces.component.UIForm;
24  import javax.faces.component.UIParameter;
25  import javax.faces.component.behavior.AjaxBehavior;
26  import javax.faces.component.html.HtmlCommandLink;
27  import javax.faces.component.html.HtmlOutcomeTargetLink;
28  import javax.faces.component.html.HtmlOutputLink;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  
33  import org.apache.myfaces.application.NavigationHandlerImpl;
34  import org.apache.myfaces.shared.config.MyfacesConfig;
35  import org.apache.myfaces.test.base.AbstractJsfTestCase;
36  import org.apache.myfaces.test.mock.MockExternalContext;
37  import org.apache.myfaces.test.mock.MockHttpServletRequest;
38  import org.apache.myfaces.test.mock.MockHttpServletResponse;
39  import org.apache.myfaces.test.mock.MockRenderKitFactory;
40  import org.apache.myfaces.test.mock.MockResponseWriter;
41  import org.apache.myfaces.test.mock.MockServletContext;
42  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
43  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
44  
45  /**
46   * @author Bruno Aranda (latest modification by $Author: lu4242 $)
47   * @version $Revision: 1151650 $ $Date: 2011-07-27 17:14:17 -0500 (Wed, 27 Jul 2011) $
48   */
49  public class HtmlLinkRendererTest extends AbstractJsfTestCase
50  {
51  
52      private MockResponseWriter writer;
53      private HtmlCommandLink commandLink;
54      private HtmlOutputLink outputLink;
55      private HtmlOutcomeTargetLink outcomeTargetLink;
56  
57      public HtmlLinkRendererTest(String name)
58      {
59          super(name);
60      }
61      
62      public static Test suite() {
63          return new TestSuite(HtmlLinkRendererTest.class);
64      }
65  
66      public void setUp() throws Exception
67      {
68          super.setUp();
69  
70          UIForm form = new UIForm();
71  
72          commandLink = new HtmlCommandLink();
73          outputLink = new HtmlOutputLink();
74          outputLink.setValue("http://someurl");
75          outcomeTargetLink = new HtmlOutcomeTargetLink();
76  
77          form.getChildren().add(commandLink);
78  
79          writer = new MockResponseWriter(new StringWriter(), null, "UTF-8");
80          facesContext.setResponseWriter(writer);
81          facesContext.getApplication().setNavigationHandler(new NavigationHandlerImpl());
82         
83  
84  
85          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
86          facesContext.getRenderKit().addRenderer(
87                  commandLink.getFamily(),
88                  commandLink.getRendererType(),
89                  new HtmlLinkRenderer());
90          facesContext.getRenderKit().addRenderer(
91                  form.getFamily(),
92                  form.getRendererType(),
93                  new HtmlFormRenderer());
94          facesContext.getRenderKit().addRenderer(
95                  outputLink.getFamily(),
96                  outputLink.getRendererType(),
97                  new HtmlLinkRenderer());
98          facesContext.getRenderKit().addRenderer(
99                  outcomeTargetLink.getFamily(),
100                 outcomeTargetLink.getRendererType(),
101                 new HtmlLinkRenderer());
102         
103         facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
104     }
105 
106     public void tearDown() throws Exception
107     {
108         super.tearDown();
109         writer = null;
110     }
111      
112     public void testHtmlPropertyPassTru() throws Exception
113     {
114         HtmlRenderedAttr[] attrs = {
115             //_AccesskeyProperty
116             new HtmlRenderedAttr("accesskey"),
117             //_UniversalProperties
118             new HtmlRenderedAttr("dir"), 
119             new HtmlRenderedAttr("lang"), 
120             new HtmlRenderedAttr("title"),
121             //_FocusBlurProperties
122             new HtmlRenderedAttr("onfocus"), 
123             new HtmlRenderedAttr("onblur"),
124             //_EventProperties
125             //new HtmlRenderedAttr("onclick",1) "onclick", 
126             //        "onclick=\"var cf = function(){onclick};var oamSF = function(){return oamSubmitForm('j_id1','j_id1:j_id0');};return (cf()==false)? false : oamSF();\"")
127             //, 
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         commandLink.setValue("outputdata");
145         
146         HtmlCheckAttributesUtil.checkRenderedAttributes(
147                 commandLink, facesContext, writer, attrs);
148         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
149             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
150         }
151     }
152     
153     public void testJSNotAllowedHtmlPropertyPassTru() throws Exception
154     {
155         HtmlRenderedAttr[] attrs = {
156             //_AccesskeyProperty
157             new HtmlRenderedAttr("accesskey"),
158             //_UniversalProperties
159             new HtmlRenderedAttr("dir"), 
160             new HtmlRenderedAttr("lang"), 
161             new HtmlRenderedAttr("title"),
162             //_FocusBlurProperties
163             new HtmlRenderedAttr("onfocus"), 
164             new HtmlRenderedAttr("onblur"),
165             //_EventProperties
166             new HtmlRenderedAttr("onclick"), 
167             new HtmlRenderedAttr("ondblclick"), 
168             new HtmlRenderedAttr("onkeydown"), 
169             new HtmlRenderedAttr("onkeypress"),
170             new HtmlRenderedAttr("onkeyup"), 
171             new HtmlRenderedAttr("onmousedown"), 
172             new HtmlRenderedAttr("onmousemove"), 
173             new HtmlRenderedAttr("onmouseout"),
174             new HtmlRenderedAttr("onmouseover"), 
175             new HtmlRenderedAttr("onmouseup"),
176             //_StyleProperties
177             new HtmlRenderedAttr("style"), 
178             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
179             //_TabindexProperty
180             new HtmlRenderedAttr("tabindex")
181         };
182 
183         
184         commandLink.setValue("outputdata");
185         
186         MockServletContext servletContext = new MockServletContext();
187         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "false");
188         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
189                 new MockHttpServletRequest(), new MockHttpServletResponse());
190         MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
191         facesContext.setExternalContext(mockExtCtx);
192         
193         HtmlCheckAttributesUtil.checkRenderedAttributes(
194                 commandLink, facesContext, writer, attrs);
195         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
196             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
197         }
198     }
199     
200     public void testOutputLink() throws Exception 
201     {
202         HtmlRenderedAttr[] attrs = {
203             //_AccesskeyProperty
204             new HtmlRenderedAttr("accesskey"),
205             //_UniversalProperties
206             new HtmlRenderedAttr("dir"), 
207             new HtmlRenderedAttr("lang"), 
208             new HtmlRenderedAttr("title"),
209             //_FocusBlurProperties
210             new HtmlRenderedAttr("onfocus"), 
211             new HtmlRenderedAttr("onblur"),
212             //_EventProperties
213             new HtmlRenderedAttr("onclick"), 
214             new HtmlRenderedAttr("ondblclick"), 
215             new HtmlRenderedAttr("onkeydown"), 
216             new HtmlRenderedAttr("onkeyup"), 
217             new HtmlRenderedAttr("onmousedown"), 
218             new HtmlRenderedAttr("onmousemove"), 
219             new HtmlRenderedAttr("onmouseout"),
220             new HtmlRenderedAttr("onmouseover"), 
221             new HtmlRenderedAttr("onmouseup"),
222             //_StyleProperties
223             new HtmlRenderedAttr("style"), 
224             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
225             //_TabindexProperty
226             new HtmlRenderedAttr("tabindex")
227         };
228 
229         
230         HtmlCheckAttributesUtil.checkRenderedAttributes(
231                 outputLink, facesContext, writer, attrs);
232         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
233             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
234         }
235     }
236     
237     /**
238      * Components that render client behaviors should always render "id" and "name" attribute
239      */
240     public void testClientBehaviorHolderRendersIdAndNameOutputLink() 
241     {
242         outputLink.addClientBehavior("keypress", new AjaxBehavior());
243         try 
244         {
245             outputLink.encodeAll(facesContext);
246             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
247             assertTrue(output.matches(".+id=\".+\".+"));
248             assertTrue(output.matches(".+name=\".+\".+"));
249         }
250         catch (Exception e)
251         {
252             fail(e.getMessage());
253         }
254         
255     }
256     
257     /**
258      * Components that render client behaviors should always render "id" and "name" attribute
259      */
260     public void testClientBehaviorHolderRendersIdAndNameCommandLink() 
261     {
262         commandLink.addClientBehavior("keypress", new AjaxBehavior());
263         try 
264         {
265             commandLink.encodeAll(facesContext);
266             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
267             System.out.println("----OUTPUT----"+output);
268             assertTrue(output.matches("(?s).+id=\".+\".+"));
269             assertTrue(output.matches("(?s).+name=\".+\".+"));
270         }
271         catch (Exception e)
272         {
273             fail(e.getMessage());
274         }
275         
276     }
277     
278     /**
279      * Components that render client behaviors should always render "id" and "name" attribute
280      */
281     public void testClientBehaviorHolderRendersIdAndNameOutcomeTargetLink() 
282     {
283         outcomeTargetLink.addClientBehavior("keypress", new AjaxBehavior());
284         try 
285         {
286             outcomeTargetLink.encodeAll(facesContext);
287             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
288             assertTrue(output.matches(".+id=\".+\".+"));
289             assertTrue(output.matches(".+name=\".+\".+"));
290         }
291         catch (Exception e)
292         {
293             fail(e.getMessage());
294         }
295         
296     }
297     
298     /**
299      * Tests if h:link correctly includes all parameters of the implicit
300      * navigation case created from the outcome.
301      * 
302      * @throws Exception
303      */
304     public void testOutcomeTargetRendersNavigationCaseParameters() throws Exception
305     {
306         // configure the link
307         outcomeTargetLink.getAttributes().put("includeViewParams", false);
308         outcomeTargetLink.getAttributes().put("outcome", 
309                 "test.xhtml?param1=value1&param2=value2");
310         
311         // render the link
312         outcomeTargetLink.encodeAll(facesContext);
313         String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
314         
315         // make sure the parameters are rendered
316         assertTrue(output.contains("param1=value1"));
317         assertTrue(output.contains("param2=value2"));
318     }
319     
320     /**
321      * Tests if the fragment attribute is correctly rendered.
322      * @throws Exception
323      */
324     public void testOutcomeTargetLinkFragment() throws Exception
325     {
326         // configure the link
327         final String fragment = "end";
328         outcomeTargetLink.getAttributes().put("fragment", fragment);
329         outcomeTargetLink.getAttributes().put("outcome", 
330                 "test.xhtml?param1=value1");
331         
332         // render the link
333         outcomeTargetLink.encodeAll(facesContext);
334         String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
335         
336         // make sure the fragment is rendered
337         assertTrue(output.contains("param1=value1#" + fragment));
338     }
339     
340     /**
341      * Test for the right use of the fragment attribute.
342      * The value of the fragment attribute is appended to the end of target URL following a hash (#) mark.
343      * @throws Exception
344      */
345     public void testOutputLinkFragment() throws Exception
346     {
347         outputLink.setFragment("fragment");
348         outputLink.setValue("http://www.irian.at");
349         outputLink.encodeAll(facesContext);
350         String output = writer.getWriter().toString();
351         assertEquals("<a href=\"http://www.irian.at#fragment\"></a>", output);
352     }
353     
354     /**
355      * If the disable attribute of a child UIParameter is true,
356      * he should be ignored.
357      * @throws Exception
358      */
359     public void testDisabledUIParameterNotRenderedCommandLink() throws Exception
360     {
361         UIParameter param1 = new UIParameter();
362         param1.setName("param1");
363         param1.setValue("value1");
364         param1.setDisable(true);
365         UIParameter param2 = new UIParameter();
366         param2.setName("param2");
367         param2.setValue("value2");
368         commandLink.getChildren().add(param1);
369         commandLink.getChildren().add(param2);
370         
371         commandLink.encodeAll(facesContext);
372         String output = writer.getWriter().toString();
373         assertFalse(output.contains("param1"));
374         assertFalse(output.contains("value1"));
375         assertTrue(output.contains("param2"));
376         assertTrue(output.contains("value2"));
377     }
378     
379     /**
380      * If the disable attribute of a child UIParameter is true,
381      * he should be ignored.
382      * @throws Exception
383      */
384     public void testDisabledUIParameterNotRenderedOutputLink() throws Exception
385     {
386         UIParameter param1 = new UIParameter();
387         param1.setName("param1");
388         param1.setValue("value1");
389         param1.setDisable(true);
390         UIParameter param2 = new UIParameter();
391         param2.setName("param2");
392         param2.setValue("value2");
393         outputLink.getChildren().add(param1);
394         outputLink.getChildren().add(param2);
395         
396         outputLink.encodeAll(facesContext);
397         String output = writer.getWriter().toString();
398         assertFalse(output.contains("param1"));
399         assertFalse(output.contains("value1"));
400         assertTrue(output.contains("param2"));
401         assertTrue(output.contains("value2"));
402     }
403     
404     /**
405      * Tests if the h:link correctly includes an UIParameter
406      * with a non-null-name when creating the URL.
407      */
408     public void testOutcomeTargetLinkIncludesUIParameterInURL()
409     {
410         // create the UIParameter and attach it
411         UIParameter param = new UIParameter();
412         param.setName("myParameter");
413         param.setValue("myValue");
414         outcomeTargetLink.getChildren().add(param);
415         
416         try
417         {
418             outcomeTargetLink.encodeAll(facesContext);
419             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
420             assertTrue(output.contains("myParameter=myValue"));
421         }
422         catch (Exception e)
423         {
424             fail(e.getMessage());
425         }
426     }
427     
428     /**
429      * Tests if the h:link correctly skips an UIParameter
430      * with a null-name when creating the URL.
431      */
432     public void testOutcomeTargetLinkSkipsNullValueOfUIParameterInURL()
433     {
434         // create the UIParameter with value = null and attach it
435         UIParameter param = new UIParameter();
436         param.setName("myNullParameter");
437         param.setValue(null);
438         outcomeTargetLink.getChildren().add(param);
439         
440         try
441         {
442             outcomeTargetLink.encodeAll(facesContext);
443             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
444             assertFalse(output.contains("myNullParameter"));
445         }
446         catch (Exception e)
447         {
448             fail(e.getMessage());
449         }
450     }
451     
452 }