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