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.IOException;
22  import java.io.StringWriter;
23  
24  import javax.el.ValueExpression;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIForm;
27  import javax.faces.component.UIViewRoot;
28  import javax.faces.component.html.HtmlInputText;
29  import javax.faces.component.html.HtmlOutputText;
30  import javax.faces.context.FacesContext;
31  import javax.faces.convert.Converter;
32  import javax.faces.convert.ConverterException;
33  
34  import junit.framework.Test;
35  import junit.framework.TestSuite;
36  
37  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
38  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
39  import org.apache.shale.test.base.AbstractJsfTestCase;
40  import org.apache.shale.test.el.MockValueExpression;
41  import org.apache.shale.test.mock.MockRenderKitFactory;
42  import org.apache.shale.test.mock.MockResponseWriter;
43  
44  /**
45   * @author Bruno Aranda (latest modification by $Author: grantsmith $)
46   * @version $Revision: 472618 $ $Date: 2006-11-09 04:06:54 +0800 (Thu, 09 Nov 2006) $
47   */
48  public class HtmlTextRendererTest extends AbstractJsfTestCase
49  {
50  
51      public static Test suite()
52      {
53          return new TestSuite(HtmlTextRendererTest.class); // needed in maven
54      }
55  
56      private MockResponseWriter writer ;
57      private HtmlOutputText outputText;
58      private HtmlInputText inputText;
59  
60      public HtmlTextRendererTest(String name)
61      {
62          super(name);
63      }
64  
65      public void setUp() throws Exception
66      {
67          super.setUp();
68  
69          outputText = new HtmlOutputText();
70          inputText = new HtmlInputText();
71  
72          writer = new MockResponseWriter(new StringWriter(), null, null);
73          facesContext.setResponseWriter(writer);
74          // TODO remove these two lines once shale-test goes alpha, see MYFACES-1155
75          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
76          facesContext.getRenderKit().addRenderer(
77                  outputText.getFamily(),
78                  outputText.getRendererType(),
79                  new HtmlTextRenderer());
80          facesContext.getRenderKit().addRenderer(
81                  inputText.getFamily(),
82                  inputText.getRendererType(),
83                  new HtmlTextRenderer());
84      }
85  
86      public void tearDown() throws Exception
87      {
88          super.tearDown();
89          outputText = null;
90          inputText = null;
91          writer = null;
92      }
93  
94      public void testStyleClassAttr() throws IOException
95      {
96          outputText.setValue("Output");
97          outputText.setStyleClass("myStyleClass");
98  
99          outputText.encodeEnd(facesContext);
100         facesContext.renderResponse();
101 
102         String output = writer.getWriter().toString();
103 
104         assertEquals("<span class=\"myStyleClass\">Output</span>", output);
105         assertNotSame("Output", output);
106     }
107 
108     public void testHtmlPropertyPassTru() throws Exception
109     {
110         HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicAttrs();
111         
112 
113         HtmlCheckAttributesUtil.checkRenderedAttributes(
114                 inputText, facesContext, writer, attrs);
115         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
116             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
117         }
118     }
119     
120     public void testWhenSubmittedValueIsNullDefaultShouldDissapearFromRendering() {
121         //See MYFACES-2161 and MYFACES-1549 for details
122         UIViewRoot root = new UIViewRoot();
123         UIForm form = new UIForm();
124         form.setId("formId");
125         
126         form.getChildren().add(inputText);
127         root.getChildren().add(form);
128         
129         Converter converter = new Converter()
130         {
131             public Object getAsObject(FacesContext context,
132                     UIComponent component, String value)
133                     throws ConverterException
134             {
135                 if (value == null || "".equals(value))
136                     return null;
137                 else
138                     return value;
139             }
140 
141             public String getAsString(FacesContext context,
142                     UIComponent component, Object value)
143                     throws ConverterException
144             {
145                 if (value == null)
146                     return "";
147                 else
148                     return value.toString();
149             }
150         };
151         
152         inputText.setConverter(converter);
153         
154         ValueExpression expression = new MockValueExpression("#{requestScope.someDefaultValueOnBean}",String.class);
155         expression.setValue(facesContext.getELContext(), "defaultValue");
156         inputText.setValueExpression("value", expression);
157         
158         // 1) user enters an empty string in an input-component: ""
159         //Call to setSubmittedValue on HtmlRendererUtils.decodeUIInput(facesContext, component), 
160         //that is called from renderer decode()
161         facesContext.getExternalContext().getRequestParameterMap().put(inputText.getClientId(facesContext), "");
162         
163         inputText.decode(facesContext);
164         
165         // 2) conversion and validation phase: "" --> setValue(null);
166         // isLocalValueSet = true; setSubmittedValue(null);
167         inputText.validate(facesContext);
168         
169         // 3) validation fails in some component on the page --> update model
170         // phase is skipped
171         // No OP
172         
173         // 4) renderer calls getValue(); --> getValue() evaluates the
174         // value-binding, as the local-value is 'null', and I get the
175         // default-value of the bean shown again
176         assertNotSame(expression.getValue(facesContext.getELContext()), inputText.getValue());
177         assertNull(inputText.getValue());
178     }
179 
180 }