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.IOException;
22  import java.io.StringWriter;
23  import java.util.Collection;
24  
25  import javax.el.ELContext;
26  import javax.el.ValueExpression;
27  import javax.faces.component.UISelectItem;
28  
29  import org.apache.myfaces.component.html.ext.HtmlSelectManyCheckbox;
30  import org.apache.myfaces.test.base.AbstractJsfTestCase;
31  import org.apache.myfaces.test.el.MockValueExpression;
32  import org.apache.myfaces.test.mock.MockResponseWriter;
33  
34  /**
35   * Test cases for HtmlCheckboxRenderer.
36   * 
37   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
38   * @version $Revision: 963899 $ $Date: 2010-07-13 17:57:38 -0500 (Tue, 13 Jul 2010) $
39   */
40  public class HtmlCheckboxRendererValueTest extends AbstractJsfTestCase
41  {
42  
43      private HtmlCheckboxRenderer _renderer;
44      private MockResponseWriter _writer;
45      private StringWriter _stringWriter;
46      
47      public HtmlCheckboxRendererValueTest(String name)
48      {
49          super(name);
50      }
51      
52      @Override
53      protected void setUp() throws Exception
54      {
55          super.setUp();
56          
57          _renderer = new HtmlCheckboxRenderer();
58          _stringWriter = new StringWriter();
59          _writer = new MockResponseWriter(_stringWriter, "text/html", "utf-8");
60          
61          facesContext.setResponseWriter(_writer);
62      }
63  
64      @Override
65      protected void tearDown() throws Exception
66      {
67          _renderer = null;
68          _stringWriter = null;
69          _writer = null;
70          
71          super.tearDown();
72      }
73  
74      @SuppressWarnings("unchecked")
75      public void testValueTypeRender() throws IOException
76      {
77          MockBean bean = new MockBean();
78          externalContext.getApplicationMap().put("bean", bean);
79          ValueExpression beanVE = new MockValueExpression("#{bean.values}", Object.class);
80          
81          // create UISelectMany component
82          HtmlSelectManyCheckbox selectMany = new HtmlSelectManyCheckbox();
83          selectMany.setValueExpression("value", beanVE);
84          selectMany.setValueType(Integer.class.getName());
85          
86          // create the select item
87          UISelectItem item = new UISelectItem();
88          item.setItemValue("1");
89          selectMany.getChildren().add(item);
90          
91          // register the converter
92          application.addConverter(Integer.class, MockIntegerConverter.class.getName());
93          
94          // Render the component (only encodeEnd is used in this renderer)
95          _renderer.encodeEnd(facesContext, selectMany);
96          final String output = _stringWriter.toString();
97          
98          // we expect a rendered value of 11, because the Converter adds 10 to
99          // the given value in getAsString(). Thus we verify that the converter was called.
100         assertTrue(output.contains("value=\"11\""));
101     }
102     
103     @SuppressWarnings({ "unchecked", "serial" })
104     public void testValueTypeSubmit() throws IOException
105     {
106         MockBean bean = new MockBean();
107         externalContext.getApplicationMap().put("bean", bean);
108         ValueExpression beanVE = new MockValueExpression("#{bean.values}", Object.class)
109         {
110 
111             @Override
112             public Class getType(ELContext context)
113             {
114                 // to simulate the behavior when a bean property has a null value,
115                 // but the getter has a return value of Collection
116                 return Collection.class;
117             }
118               
119         };
120         
121         // create UISelectMany component
122         HtmlSelectManyCheckbox selectMany = new HtmlSelectManyCheckbox();
123         selectMany.setValueExpression("value", beanVE);
124         selectMany.setValueType(Integer.class.getName());
125         
126         // create the select item
127         UISelectItem item = new UISelectItem();
128         item.setItemValue("1");
129         selectMany.getChildren().add(item);
130         
131         // get the converted value
132         Object convertedValue = _renderer.getConvertedValue(facesContext, selectMany, new String[] {"1"});
133         
134         // the value must be a Collection
135         assertTrue(convertedValue instanceof Collection);
136         
137         // the first element of the Collection must be the _Integer_ 1
138         // (without the valueType attribute it would be the String "1")
139         assertEquals(1, ((Collection) convertedValue).iterator().next());
140     }
141     
142 }