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$)
47   * @version $Revision$ $Date$
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 testOutputLink() 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("onkeyup"), 
170             new HtmlRenderedAttr("onmousedown"), 
171             new HtmlRenderedAttr("onmousemove"), 
172             new HtmlRenderedAttr("onmouseout"),
173             new HtmlRenderedAttr("onmouseover"), 
174             new HtmlRenderedAttr("onmouseup"),
175             //_StyleProperties
176             new HtmlRenderedAttr("style"), 
177             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
178             //_TabindexProperty
179             new HtmlRenderedAttr("tabindex")
180         };
181 
182         
183         HtmlCheckAttributesUtil.checkRenderedAttributes(
184                 outputLink, facesContext, writer, attrs);
185         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
186             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
187         }
188     }
189     
190     /**
191      * Components that render client behaviors should always render "id" and "name" attribute
192      */
193     public void testClientBehaviorHolderRendersIdAndNameOutputLink() 
194     {
195         outputLink.addClientBehavior("keypress", new AjaxBehavior());
196         try 
197         {
198             outputLink.encodeAll(facesContext);
199             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
200             assertTrue(output.matches(".+id=\".+\".+"));
201             assertTrue(output.matches(".+name=\".+\".+"));
202         }
203         catch (Exception e)
204         {
205             fail(e.getMessage());
206         }
207         
208     }
209     
210     /**
211      * Components that render client behaviors should always render "id" and "name" attribute
212      */
213     public void testClientBehaviorHolderRendersIdAndNameCommandLink() 
214     {
215         commandLink.addClientBehavior("keypress", new AjaxBehavior());
216         try 
217         {
218             commandLink.encodeAll(facesContext);
219             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
220             System.out.println("----OUTPUT----"+output);
221             assertTrue(output.matches("(?s).+id=\".+\".+"));
222             assertTrue(output.matches("(?s).+name=\".+\".+"));
223         }
224         catch (Exception e)
225         {
226             fail(e.getMessage());
227         }
228         
229     }
230     
231     /**
232      * Components that render client behaviors should always render "id" and "name" attribute
233      */
234     public void testClientBehaviorHolderRendersIdAndNameOutcomeTargetLink() 
235     {
236         outcomeTargetLink.addClientBehavior("keypress", new AjaxBehavior());
237         try 
238         {
239             outcomeTargetLink.encodeAll(facesContext);
240             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
241             assertTrue(output.matches(".+id=\".+\".+"));
242             assertTrue(output.matches(".+name=\".+\".+"));
243         }
244         catch (Exception e)
245         {
246             fail(e.getMessage());
247         }
248         
249     }
250     
251     /**
252      * Tests if h:link correctly includes all parameters of the implicit
253      * navigation case created from the outcome.
254      * 
255      * @throws Exception
256      */
257     public void testOutcomeTargetRendersNavigationCaseParameters() throws Exception
258     {
259         // configure the link
260         outcomeTargetLink.getAttributes().put("includeViewParams", false);
261         outcomeTargetLink.getAttributes().put("outcome", 
262                 "test.xhtml?param1=value1&param2=value2");
263         
264         // render the link
265         outcomeTargetLink.encodeAll(facesContext);
266         String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
267         
268         // make sure the parameters are rendered
269         assertTrue(output.contains("param1=value1"));
270         assertTrue(output.contains("param2=value2"));
271     }
272     
273     /**
274      * Tests if the fragment attribute is correctly rendered.
275      * @throws Exception
276      */
277     public void testOutcomeTargetLinkFragment() throws Exception
278     {
279         // configure the link
280         final String fragment = "end";
281         outcomeTargetLink.getAttributes().put("fragment", fragment);
282         outcomeTargetLink.getAttributes().put("outcome", 
283                 "test.xhtml?param1=value1");
284         
285         // render the link
286         outcomeTargetLink.encodeAll(facesContext);
287         String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
288         
289         // make sure the fragment is rendered
290         assertTrue(output.contains("param1=value1#" + fragment));
291     }
292     
293     /**
294      * Test for the right use of the fragment attribute.
295      * The value of the fragment attribute is appended to the end of target URL following a hash (#) mark.
296      * @throws Exception
297      */
298     public void testOutputLinkFragment() throws Exception
299     {
300         outputLink.setFragment("fragment");
301         outputLink.setValue("http://www.irian.at");
302         outputLink.encodeAll(facesContext);
303         String output = writer.getWriter().toString();
304         assertEquals("<a href=\"http://www.irian.at#fragment\"></a>", output);
305     }
306     
307     /**
308      * If the disable attribute of a child UIParameter is true,
309      * he should be ignored.
310      * @throws Exception
311      */
312     public void testDisabledUIParameterNotRenderedCommandLink() throws Exception
313     {
314         UIParameter param1 = new UIParameter();
315         param1.setName("param1");
316         param1.setValue("value1");
317         param1.setDisable(true);
318         UIParameter param2 = new UIParameter();
319         param2.setName("param2");
320         param2.setValue("value2");
321         commandLink.getChildren().add(param1);
322         commandLink.getChildren().add(param2);
323         
324         commandLink.encodeAll(facesContext);
325         String output = writer.getWriter().toString();
326         assertFalse(output.contains("param1"));
327         assertFalse(output.contains("value1"));
328         assertTrue(output.contains("param2"));
329         assertTrue(output.contains("value2"));
330     }
331     
332     /**
333      * If the disable attribute of a child UIParameter is true,
334      * he should be ignored.
335      * @throws Exception
336      */
337     public void testDisabledUIParameterNotRenderedOutputLink() throws Exception
338     {
339         UIParameter param1 = new UIParameter();
340         param1.setName("param1");
341         param1.setValue("value1");
342         param1.setDisable(true);
343         UIParameter param2 = new UIParameter();
344         param2.setName("param2");
345         param2.setValue("value2");
346         outputLink.getChildren().add(param1);
347         outputLink.getChildren().add(param2);
348         
349         outputLink.encodeAll(facesContext);
350         String output = writer.getWriter().toString();
351         assertFalse(output.contains("param1"));
352         assertFalse(output.contains("value1"));
353         assertTrue(output.contains("param2"));
354         assertTrue(output.contains("value2"));
355     }
356     
357     /**
358      * Tests if the h:link correctly includes an UIParameter
359      * with a non-null-name when creating the URL.
360      */
361     public void testOutcomeTargetLinkIncludesUIParameterInURL()
362     {
363         // create the UIParameter and attach it
364         UIParameter param = new UIParameter();
365         param.setName("myParameter");
366         param.setValue("myValue");
367         outcomeTargetLink.getChildren().add(param);
368         
369         try
370         {
371             outcomeTargetLink.encodeAll(facesContext);
372             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
373             assertTrue(output.contains("myParameter=myValue"));
374         }
375         catch (Exception e)
376         {
377             fail(e.getMessage());
378         }
379     }
380     
381     /**
382      * Tests if the h:link correctly skips an UIParameter
383      * with a null-name when creating the URL.
384      */
385     public void testOutcomeTargetLinkSkipsNullValueOfUIParameterInURL()
386     {
387         // create the UIParameter with value = null and attach it
388         UIParameter param = new UIParameter();
389         param.setName("myNullParameter");
390         param.setValue(null);
391         outcomeTargetLink.getChildren().add(param);
392         
393         try
394         {
395             outcomeTargetLink.encodeAll(facesContext);
396             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
397             assertFalse(output.contains("myNullParameter"));
398         }
399         catch (Exception e)
400         {
401             fail(e.getMessage());
402         }
403     }
404     
405 }