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.UIParameter;
24  import javax.faces.component.behavior.AjaxBehavior;
25  import javax.faces.component.html.HtmlForm;
26  import javax.faces.component.html.HtmlOutcomeTargetButton;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.myfaces.application.NavigationHandlerImpl;
32  import org.apache.myfaces.shared.renderkit.JSFAttr;
33  import org.apache.myfaces.shared.renderkit.html.HTML;
34  import org.apache.myfaces.test.base.AbstractJsfTestCase;
35  import org.apache.myfaces.test.mock.MockRenderKitFactory;
36  import org.apache.myfaces.test.mock.MockResponseWriter;
37  
38  /**
39   * Tests for HtmlOutcomeTargetButtonRenderer.
40   * 
41   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
42   * @version $Revision: 1151650 $ $Date: 2011-07-27 17:14:17 -0500 (Wed, 27 Jul 2011) $
43   */
44  public class HtmlOutcomeTargetButtonRendererTest extends AbstractJsfTestCase 
45  {
46  
47      private MockResponseWriter writer;
48      private HtmlOutcomeTargetButton outcomeTargetButton;
49      private HtmlForm form;
50      
51      public HtmlOutcomeTargetButtonRendererTest(String name) 
52      {
53          super(name);
54      }
55      
56      public static Test suite() 
57      {
58          return new TestSuite(HtmlOutcomeTargetButtonRendererTest.class);
59      }
60      
61      public void setUp() throws Exception 
62      {
63          super.setUp();
64          writer = new MockResponseWriter(new StringWriter(), null, null);
65          facesContext.setResponseWriter(writer);
66          facesContext.getApplication().setNavigationHandler(new NavigationHandlerImpl());
67          outcomeTargetButton = new HtmlOutcomeTargetButton();
68          form = new HtmlForm();
69          outcomeTargetButton.setParent(form);
70          
71          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
72          facesContext.getRenderKit().addRenderer(
73                  outcomeTargetButton.getFamily(),
74                  outcomeTargetButton.getRendererType(),
75                  new HtmlOutcomeTargetButtonRenderer());
76          facesContext.getRenderKit().addRenderer(
77                  form.getFamily(),
78                  form.getRendererType(),
79                  new HtmlFormRenderer());
80          
81          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
82      }
83      
84      public void tearDown() throws Exception 
85      {
86          super.tearDown();
87          writer = null;
88          form = null;
89          outcomeTargetButton = null;
90      }
91      
92      /**
93       * Components that render client behaviors should always render "id" and "name" attribute
94       */
95      public void testClientBehaviorHolderRendersIdAndName() 
96      {
97          outcomeTargetButton.addClientBehavior("keypress", new AjaxBehavior());
98          try 
99          {
100             outcomeTargetButton.encodeAll(facesContext);
101             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
102             assertTrue(output.matches(".+id=\".+\".+"));
103             assertTrue(output.matches(".+name=\".+\".+"));
104         }
105         catch (Exception e)
106         {
107             fail(e.getMessage());
108         }
109         
110     }
111     
112     /**
113      * Tests if h:button correctly includes all parameters of the implicit
114      * navigation case created from the outcome.
115      * 
116      * @throws Exception
117      */
118     public void testOutcomeTargetRendersNavigationCaseParameters() throws Exception
119     {
120         // configure the button
121         outcomeTargetButton.getAttributes().put("includeViewParams", false);
122         outcomeTargetButton.getAttributes().put("outcome", 
123                 "test.xhtml?param1=value1&param2=value2");
124         
125         // render the button
126         outcomeTargetButton.encodeAll(facesContext);
127         String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
128         
129         // make sure the parameters are rendered
130         assertTrue(output.contains("param1=value1"));
131         assertTrue(output.contains("param2=value2"));
132     }
133     
134     /**
135      * Tests if the fragment attribute is correctly rendered.
136      * @throws Exception
137      */
138     public void testFragment() throws Exception
139     {
140         // configure the button
141         final String fragment = "end";
142         outcomeTargetButton.getAttributes().put("fragment", fragment);
143         outcomeTargetButton.getAttributes().put("outcome", 
144                 "test.xhtml?param1=value1");
145         
146         // render the button
147         outcomeTargetButton.encodeAll(facesContext);
148         String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
149         
150         // make sure the fragment is rendered
151         assertTrue(output.contains("param1=value1#" + fragment));
152     }
153     
154     /**
155      * Tests if the h:button correctly includes an UIParameter
156      * with a non-null-name when creating the URL.
157      */
158     public void testIncludesUIParameterInURL()
159     {
160         // create the UIParameter and attach it
161         UIParameter param = new UIParameter();
162         param.setName("myParameter");
163         param.setValue("myValue");
164         outcomeTargetButton.getChildren().add(param);
165         
166         try
167         {
168             outcomeTargetButton.encodeAll(facesContext);
169             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
170             assertTrue(output.contains("myParameter=myValue"));
171         }
172         catch (Exception e)
173         {
174             fail(e.getMessage());
175         }
176     }
177     
178     /**
179      * Tests if the h:button correctly skips an UIParameter
180      * with a null-name when creating the URL.
181      */
182     public void testSkipsNullValueOfUIParameterInURL()
183     {
184         // create the UIParameter with value = null and attach it
185         UIParameter param = new UIParameter();
186         param.setName("myNullParameter");
187         param.setValue(null);
188         outcomeTargetButton.getChildren().add(param);
189         
190         try
191         {
192             outcomeTargetButton.encodeAll(facesContext);
193             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
194             assertFalse(output.contains("myNullParameter"));
195         }
196         catch (Exception e)
197         {
198             fail(e.getMessage());
199         }
200     }
201     
202     /**
203      * Tests if the h:button is rendered accordingly if disabled is true.
204      */
205     public void testDisabledAttribute() 
206     {
207         outcomeTargetButton.getAttributes().put(JSFAttr.DISABLED_ATTR, "true");
208         try 
209         {
210             outcomeTargetButton.encodeAll(facesContext);
211             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
212             
213             // Assertions
214             assertFalse(output.contains(HTML.ONCLICK_ATTR)); // the output must not contain onclick 
215             assertTrue(output.contains(HTML.DISABLED_ATTR)); // the ouput must contain disabled
216         }
217         catch (Exception e)
218         {
219             fail(e.getMessage());
220         }
221         
222     }
223     
224 }