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.selectitems;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.faces.el.ValueBinding;
27  import javax.faces.model.SelectItem;
28  import javax.faces.model.SelectItemGroup;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  
33  import org.apache.myfaces.test.base.AbstractJsfTestCase;
34  
35  /**
36   * @author cagatay (latest modification by $Author: lu4242 $)
37   * @version $Revision: 783163 $ $Date: 2009-06-09 18:37:19 -0500 (Mar, 09 Jun 2009) $
38   */
39  public class UISelectItemsTest extends AbstractJsfTestCase{
40      
41      private UISelectItems selectItems;
42      
43      private Collection movieCollection;
44      
45      private Collection movieSelectItemsGroupCollection;
46      
47      private Map movieMap;
48      
49      private SelectItem[] movieSelectItems;
50  
51      public UISelectItemsTest(String testName) {
52          super(testName);
53      }
54      
55      public static Test suite() {
56          return new TestSuite(UISelectItemsTest.class);
57      }
58  
59      /**
60       * Sets up the test environment for <s:selectItems value="#{PossibleValueHere}" var="Movie" itemLabel="#{Movie.name} itemValue="#{Movie.director}" />
61       * Accepted possible values for the component can be a SelectItems array, a collection, a map and SelectItems group collection.
62       */
63      public void setUp() throws Exception{
64          super.setUp();
65          //component
66          selectItems = new UISelectItems();
67          ValueBinding itemLabelVb =  facesContext.getApplication().createValueBinding("#{Movie.name}");
68          ValueBinding itemValueVb =  facesContext.getApplication().createValueBinding("#{Movie.director}");
69          
70          selectItems.setValueBinding("itemLabel", itemLabelVb);
71          selectItems.setValueBinding("itemValue", itemValueVb);
72          selectItems.getAttributes().put("var", "Movie");
73          
74          //entities
75          Movie movie1 = new Movie("Godfather", "Francis Ford Coppola");
76          Movie movie2 = new Movie("Goodfellas", "Martin Scorsese");
77          Movie movie3 = new Movie("Casino", "Martin Scorsese");
78          Movie movie4 = new Movie("Scarface", "Brian De Palma");
79          
80          //different value types
81          movieSelectItems = new SelectItem[3];
82          movieSelectItems[0] = new SelectItem(movie2.getDirector(), movie2.getName());
83          movieSelectItems[1] = new SelectItem(movie3.getDirector(), movie3.getName());
84          movieSelectItems[2] = new SelectItem(movie4.getDirector(), movie4.getName());
85          
86          movieCollection = new ArrayList();
87          movieCollection.add(movie1);
88          movieCollection.add(movie2);
89          movieCollection.add(movie3);
90          movieCollection.add(movie4);
91          
92          movieMap = new HashMap();
93          movieMap.put(movie3.getName(), movie3);
94          movieMap.put(movie4.getName(), movie4);
95  
96          movieSelectItemsGroupCollection = new ArrayList();
97          movieSelectItemsGroupCollection.add(createSelectItemGroup("group1", movieSelectItems));
98          movieSelectItemsGroupCollection.add(createSelectItemGroup("group2", movieSelectItems));
99      }
100     
101     private SelectItemGroup createSelectItemGroup(String groupName, SelectItem[] items) {
102         SelectItemGroup group = new SelectItemGroup();
103         group.setLabel(groupName);
104         group.setSelectItems(items);
105          return group;
106     }
107     
108     public void tearDown() throws Exception{
109         selectItems = null;
110         super.tearDown();
111     }
112     
113     public void testCreateSelectItemsBySelectItems() {
114         selectItems.setValue(movieSelectItems);
115         SelectItem[] items = (SelectItem[]) selectItems.getValue();
116         assertEquals(items[1].getValue(), new String("Martin Scorsese"));
117         assertEquals(items[1].getLabel(), new String("Casino"));
118         assertEquals(items.length, 3);
119     }
120     
121     public void testCreateSelectItemsFromCollection() {
122         selectItems.setValue(movieCollection);
123         SelectItem[] items = (SelectItem[]) selectItems.getValue();
124         assertEquals(items[0].getValue(), "Francis Ford Coppola");
125         assertEquals(items[0].getLabel(), "Godfather");
126         assertEquals(items.length, 4);
127     }
128     
129     public void testCreateSelectItemsFromMap() {
130         selectItems.setValue(movieMap);
131         SelectItem[] items = (SelectItem[]) selectItems.getValue();
132         assertEquals(items.length, 2);
133     }
134     
135     public void testCreateSelectItemsFromSelectItemsGroupCollection() {
136         selectItems.setValue(movieSelectItemsGroupCollection);
137         SelectItem[] items = (SelectItem[]) selectItems.getValue();
138         assertEquals(items[5].getValue(), "Brian De Palma");
139         assertEquals(items[5].getLabel(),"Scarface");
140         assertEquals(items.length, 6);
141     }
142 }