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.component.html.ext;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import javax.faces.FactoryFinder;
27  import javax.faces.application.Application;
28  import javax.faces.application.ViewHandler;
29  import javax.faces.component.UISelectItems;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.FacesContextFactory;
33  import javax.faces.lifecycle.Lifecycle;
34  import javax.faces.lifecycle.LifecycleFactory;
35  import javax.faces.model.SelectItem;
36  
37  import org.apache.cactus.ServletTestCase;
38  import org.apache.cactus.WebRequest;
39  import org.apache.cactus.WebResponse;
40  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlResponseWriterImpl;
41  import org.apache.myfaces.renderkit.html.ext.HtmlCheckboxRenderer;
42  
43  /**
44   * Cactus test for the rendering of the
45   * {@link org.apache.myfaces.component.html.ext.HtmlSelectBooleanCheckbox}
46   * component.  Various combinations of layouts (lineDirection,pageDirection)
47   * and layoutWidths are tested.
48   * TODO: Test the markup generated when SelectItemGroup's are used in
49   *       combination with different layouts and layoutWidths.
50   * @author Ken Weiner
51   */
52  public class HtmlSelectManyCheckboxRendererCactus extends ServletTestCase
53  {
54      private FacesContext facesContext;
55      private UIViewRoot viewRoot;
56  
57      public void setUp() throws Exception
58      {
59          FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder
60                  .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
61          LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
62                  .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
63          Lifecycle lifecycle = lifecycleFactory
64                  .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
65          facesContext = facesContextFactory.getFacesContext(this.config
66                  .getServletContext(), request, response, lifecycle);
67          assertNotNull(facesContext);
68  
69          Writer htmlResponseWriter = response.getWriter();
70          facesContext.setResponseWriter(new HtmlResponseWriterImpl(
71                  htmlResponseWriter, "text/html", "ISO-8859-1"));
72  
73          Application application = facesContext.getApplication();
74          ViewHandler viewHandler = application.getViewHandler();
75          String viewId = "/index.jsp";
76          viewRoot = viewHandler.createView(facesContext, viewId);
77          viewRoot.setViewId(viewId);
78          facesContext.setViewRoot(viewRoot);
79      }
80  
81      public void beginEncodeWithLineDirection(WebRequest request)
82      {
83          // Nothing to do
84      }
85  
86      public void testEncodeWithLineDirection() throws IOException
87      {
88          // Make component and set its properties
89          HtmlSelectManyCheckbox selectMany = new HtmlSelectManyCheckbox();
90          selectMany.setId("test");
91          selectMany.setLayout("lineDirection");
92          selectMany.setValue(getSelections(2));
93          UISelectItems uiSelectItems = new UISelectItems();
94          uiSelectItems.setParent(selectMany);
95          uiSelectItems.setValue(getSelectManyCheckboxSelections(3));
96          selectMany.getChildren().add(uiSelectItems);
97  
98          // Make a renderer, associate it with component, and call encode
99          HtmlCheckboxRenderer renderer = new HtmlCheckboxRenderer();
100         renderer.encodeBegin(facesContext, selectMany);
101         renderer.encodeEnd(facesContext, selectMany);
102     }
103 
104     public void endEncodeWithLineDirection(WebResponse response)
105     {
106         // Check the markup produced by the component
107         String htmlMarkup = response.getText();
108         String expected =
109             "<table id=\"test\">" +
110             "<tr>" +
111             "<td>" +
112             "<label>" +
113             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-0\" />" +
114             "&#160;Label-0" +
115             "</label>" +
116             "</td>" +
117             "<td>" +
118             "<label>" +
119             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-1\" />" +
120             "&#160;Label-1" +
121             "</label>" +
122             "</td>" +
123             "<td>" +
124             "<label>" +
125             "<input type=\"checkbox\" name=\"test\" value=\"Value-2\" />" +
126             "&#160;Label-2" +
127             "</label>" +
128             "</td>" +
129             "</tr>" +
130             "</table>";
131         assertEquals(expected, htmlMarkup);
132     }
133 
134     public void beginEncodeWithPageDirection(WebRequest request)
135     {
136         // Nothing to do
137     }
138 
139     public void testEncodeWithPageDirection() throws IOException
140     {
141         // Make component and set its properties
142         HtmlSelectManyCheckbox selectMany = new HtmlSelectManyCheckbox();
143         selectMany.setId("test");
144         selectMany.setLayout("pageDirection");
145         selectMany.setValue(getSelections(2));
146         UISelectItems uiSelectItems = new UISelectItems();
147         uiSelectItems.setParent(selectMany);
148         uiSelectItems.setValue(getSelectManyCheckboxSelections(3));
149         selectMany.getChildren().add(uiSelectItems);
150 
151         // Make a renderer, associate it with component, and call encode
152         HtmlCheckboxRenderer renderer = new HtmlCheckboxRenderer();
153         renderer.encodeBegin(facesContext, selectMany);
154         renderer.encodeEnd(facesContext, selectMany);
155     }
156 
157     public void endEncodeWithPageDirection(WebResponse response)
158     {
159         // Check the markup produced by the component
160         String htmlMarkup = response.getText();
161         String expected =
162             "<table id=\"test\">" +
163             "<tr>" +
164             "<td>" +
165             "<label>" +
166             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-0\" />" +
167             "&#160;Label-0" +
168             "</label>" +
169             "</td>" +
170             "</tr>" +
171             "<tr>" +
172             "<td>" +
173             "<label>" +
174             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-1\" />" +
175             "&#160;Label-1" +
176             "</label>" +
177             "</td>" +
178             "</tr>" +
179             "<tr>" +
180             "<td>" +
181             "<label>" +
182             "<input type=\"checkbox\" name=\"test\" value=\"Value-2\" />" +
183             "&#160;Label-2" +
184             "</label>" +
185             "</td>" +
186             "</tr>" +
187             "</table>";
188         assertEquals(expected, htmlMarkup);
189     }
190 
191     public void beginEncodeWithLineDirectionAndLayoutWidth(WebRequest request)
192     {
193         // Nothing to do
194     }
195 
196     public void testEncodeWithLineDirectionAndLayoutWidth() throws IOException
197     {
198         // Make component and set its properties
199         HtmlSelectManyCheckbox selectMany = new HtmlSelectManyCheckbox();
200         selectMany.setId("test");
201         selectMany.setLayout("lineDirection");
202         selectMany.setLayoutWidth("2");
203         selectMany.setValue(getSelections(2));
204         UISelectItems uiSelectItems = new UISelectItems();
205         uiSelectItems.setParent(selectMany);
206         uiSelectItems.setValue(getSelectManyCheckboxSelections(5));
207         selectMany.getChildren().add(uiSelectItems);
208 
209         // Make a renderer, associate it with component, and call encode
210         HtmlCheckboxRenderer renderer = new HtmlCheckboxRenderer();
211         renderer.encodeBegin(facesContext, selectMany);
212         renderer.encodeEnd(facesContext, selectMany);
213     }
214 
215     public void endEncodeWithLineDirectionAndLayoutWidth(WebResponse response)
216     {
217         // Check the markup produced by the component
218         String htmlMarkup = response.getText();
219         String expected =
220             "<table id=\"test\">" +
221             "<tr>" +
222             "<td>" +
223             "<label>" +
224             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-0\" />" +
225             "&#160;Label-0" +
226             "</label>" +
227             "</td>" +
228             "<td>" +
229             "<label>" +
230             "<input type=\"checkbox\" name=\"test\" value=\"Value-2\" />" +
231             "&#160;Label-2" +
232             "</label>" +
233             "</td>" +
234             "<td>" +
235             "<label>" +
236             "<input type=\"checkbox\" name=\"test\" value=\"Value-4\" />" +
237             "&#160;Label-4" +
238             "</label>" +
239             "</td>" +
240             "</tr>" +
241             "<tr>" +
242             "<td>" +
243             "<label>" +
244             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-1\" />" +
245             "&#160;Label-1" +
246             "</label>" +
247             "</td>" +
248             "<td>" +
249             "<label>" +
250             "<input type=\"checkbox\" name=\"test\" value=\"Value-3\" />" +
251             "&#160;Label-3" +
252             "</label>" +
253             "</td>" +
254             "<td>" +
255             // empty cell
256             "</td>" +
257             "</tr>" +
258             "</table>";
259         assertEquals(expected, htmlMarkup);
260     }
261 
262     public void beginEncodeWithPageDirectionAndLayoutWidth(WebRequest request)
263     {
264         // Nothing to do
265     }
266 
267     public void testEncodeWithPageDirectionAndLayoutWidth() throws IOException
268     {
269         // Make component and set its properties
270         HtmlSelectManyCheckbox selectMany = new HtmlSelectManyCheckbox();
271         selectMany.setId("test");
272         selectMany.setLayout("pageDirection");
273         selectMany.setLayoutWidth("2");
274         selectMany.setValue(getSelections(2));
275         UISelectItems uiSelectItems = new UISelectItems();
276         uiSelectItems.setParent(selectMany);
277         uiSelectItems.setValue(getSelectManyCheckboxSelections(5));
278         selectMany.getChildren().add(uiSelectItems);
279 
280         // Make a renderer, associate it with component, and call encode
281         HtmlCheckboxRenderer renderer = new HtmlCheckboxRenderer();
282         renderer.encodeBegin(facesContext, selectMany);
283         renderer.encodeEnd(facesContext, selectMany);
284     }
285 
286     public void endEncodeWithPageDirectionAndLayoutWidth(WebResponse response)
287     {
288         // Check the markup produced by the component
289         String htmlMarkup = response.getText();
290         String expected =
291             "<table id=\"test\">" +
292             "<tr>" +
293             "<td>" +
294             "<label>" +
295             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-0\" />" +
296             "&#160;Label-0" +
297             "</label>" +
298             "</td>" +
299             "<td>" +
300             "<label>" +
301             "<input type=\"checkbox\" name=\"test\" checked=\"checked\" value=\"Value-1\" />" +
302             "&#160;Label-1" +
303             "</label>" +
304             "</td>" +
305             "</tr>" +
306             "<tr>" +
307             "<td>" +
308             "<label>" +
309             "<input type=\"checkbox\" name=\"test\" value=\"Value-2\" />" +
310             "&#160;Label-2" +
311             "</label>" +
312             "</td>" +
313             "<td>" +
314             "<label>" +
315             "<input type=\"checkbox\" name=\"test\" value=\"Value-3\" />" +
316             "&#160;Label-3" +
317             "</label>" +
318             "</td>" +
319             "</tr>" +
320             "<tr>" +
321             "<td>" +
322             "<label>" +
323             "<input type=\"checkbox\" name=\"test\" value=\"Value-4\" />" +
324             "&#160;Label-4" +
325             "</label>" +
326             "</td>" +
327             "<td>" +
328             // empty cell
329             "</td>" +
330             "</tr>" +
331             "</table>";
332         assertEquals(expected, htmlMarkup);
333     }
334 
335     /**
336      * Helper method to generate list of selected checkbox
337      * values.  The values will be strings in the form
338      * "Value-X" where X is the 0-based index of the value.
339      * @param numSelected the number of values requested
340      * @return the list of selected checkbox values
341      */
342     protected List getSelections(int numSelected)
343     {
344         List selectedValues = new ArrayList(numSelected);
345         for (int i = 0; i < numSelected; i++)
346         {
347             selectedValues.add("Value-" + i);
348         }
349         return selectedValues;
350     }
351 
352     /**
353      * Helper method to generate list of select items that
354      * represent the selection of checkboxes.
355      * The select items will have labels and values in the form
356      * "Label-X", "Value-X" where X is the 0-based index of
357      * the label or value.
358      * @param numItems the number of checkboxes requested
359      * @return the list of checkbox select items
360      */
361     protected List getSelectManyCheckboxSelections(int numItems)
362     {
363         List selectItems = new ArrayList(numItems);
364         for (int i = 0; i < numItems; i++)
365         {
366             selectItems.add(new SelectItem("Value-" + i, "Label-" + i));
367         }
368         return selectItems;
369     }
370 
371     public void tearDown()
372     {
373         facesContext = null;
374         viewRoot = null;
375     }
376 }