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.shale.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 (Tue, 09 Jun 2009) $
38   */
39  public class UISelectItems2Test 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 UISelectItems2Test(String testName) {
52          super(testName);
53      }
54      
55      public static Test suite() {
56          return new TestSuite(UISelectItems2Test.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          ValueBinding itemDisabledVb =  facesContext.getApplication().createValueBinding("#{Movie.disabled}");
70          ValueBinding itemLabelEscapedVb =  facesContext.getApplication().createValueBinding("#{Movie.escaped}");
71          
72          selectItems.setValueBinding("itemLabel", itemLabelVb);
73          selectItems.setValueBinding("itemValue", itemValueVb);
74          selectItems.setValueBinding("itemDisabled", itemDisabledVb);
75          selectItems.setValueBinding("itemLabelEscaped", itemLabelEscapedVb);
76          selectItems.getAttributes().put("var", "Movie");
77          
78          //entities
79          Movie movie1 = new Movie("Godfather", "Francis Ford Coppola");
80          Movie movie2 = new Movie("Goodfellas", "Martin Scorsese");
81          Movie movie3 = new Movie("Casino", "Martin Scorsese");
82          Movie movie4 = new Movie("Scarface", "Brian De Palma");
83          
84          //different value types
85          movieSelectItems = new SelectItem[3];
86          movieSelectItems[0] = new SelectItem(movie2.getDirector(), movie2.getName());
87          movieSelectItems[1] = new SelectItem(movie3.getDirector(), movie3.getName());
88          movieSelectItems[2] = new SelectItem(movie4.getDirector(), movie4.getName());
89          
90          movieCollection = new ArrayList();
91          movieCollection.add(movie1);
92          movieCollection.add(movie2);
93          movieCollection.add(movie3);
94          movieCollection.add(movie4);
95          
96          movieMap = new HashMap();
97          movieMap.put(movie3.getName(), movie3);
98          movieMap.put(movie4.getName(), movie4);
99  
100         movieSelectItemsGroupCollection = new ArrayList();
101         movieSelectItemsGroupCollection.add(createSelectItemGroup("group1", movieSelectItems));
102         movieSelectItemsGroupCollection.add(createSelectItemGroup("group2", movieSelectItems));
103     }
104     
105     private SelectItemGroup createSelectItemGroup(String groupName, SelectItem[] items) {
106         SelectItemGroup group = new SelectItemGroup();
107         group.setLabel(groupName);
108         group.setSelectItems(items);
109          return group;
110     }
111     
112     public void tearDown() throws Exception{
113         selectItems = null;
114         super.tearDown();
115     }
116     
117     public void testCreateSelectItemsBySelectItems() {
118         selectItems.setValue(movieSelectItems);
119         SelectItem[] items = (SelectItem[]) selectItems.getValue();
120         assertEquals(items[1].getValue(), new String("Martin Scorsese"));
121         assertEquals(items[1].getLabel(), new String("Casino"));
122         assertEquals(items.length, 3);
123     }
124     
125     public void testCreateSelectItemsFromCollection() {
126         selectItems.setValue(movieCollection);
127         SelectItem[] items = (SelectItem[]) selectItems.getValue();
128         assertEquals(items[0].getValue(), "Francis Ford Coppola");
129         assertEquals(items[0].getLabel(), "Godfather");
130         assertEquals(items.length, 4);
131     }
132     
133     public void testCreateSelectItemsFromMap() {
134         selectItems.setValue(movieMap);
135         SelectItem[] items = (SelectItem[]) selectItems.getValue();
136         assertEquals(items.length, 2);
137     }
138     
139     public void testCreateSelectItemsFromSelectItemsGroupCollection() {
140         selectItems.setValue(movieSelectItemsGroupCollection);
141         SelectItem[] items = (SelectItem[]) selectItems.getValue();
142         assertEquals(items[5].getValue(), "Brian De Palma");
143         assertEquals(items[5].getLabel(),"Scarface");
144         assertEquals(items.length, 6);
145     }
146 }