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 javax.faces.component;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Ignore;
28  import org.junit.Test;
29  
30  /**
31   * 
32   * @author martinkoci
33   */
34  public class _SelectItemsUtilTest extends AbstractJsfTestCase
35  {
36  
37      private static final String NO_SELECTION_ITEM_VALUE = "1.0";
38      private UISelectOne uiComponent;
39      private Float value;
40      private _SelectItemsIterator iterator;
41      private UISelectItem noSelectionOption;
42      private UISelectItem selectItem1;
43      private UISelectItem selectItem2;
44      private UISelectItem selectItem3;
45  
46      @Before
47      public void setUp() throws Exception
48      {
49          super.setUp();
50          
51          uiComponent = new UISelectOne();
52          
53          value = Float.valueOf("1.2");
54  
55          noSelectionOption = new UISelectItem();
56          noSelectionOption.setNoSelectionOption(true);
57          noSelectionOption.setItemValue(NO_SELECTION_ITEM_VALUE);
58          uiComponent.getChildren().add(noSelectionOption);
59          
60          selectItem1 = new UISelectItem();
61          selectItem1.setItemValue("1.1");
62          uiComponent.getChildren().add(selectItem1);
63          selectItem2 = new UISelectItem();
64          selectItem2.setItemValue("1.2");
65          uiComponent.getChildren().add(selectItem2);
66          selectItem3 = new UISelectItem();
67          selectItem3.setItemValue("1.3");
68          uiComponent.getChildren().add(selectItem3);
69          
70          iterator = new _SelectItemsIterator(uiComponent, facesContext);
71      }
72  
73      @After
74      public void tearDown() throws Exception
75      {
76          super.tearDown();
77          uiComponent = null;
78          value = null;
79          iterator = null;
80          noSelectionOption = null;
81          selectItem1 = null;
82          selectItem2 = null;
83          selectItem3 = null;
84      }
85  
86      @Test
87      public void testMatchValue()
88      {
89          
90          boolean matchValue = _SelectItemsUtil.matchValue(facesContext, uiComponent, value, iterator, null);
91          
92          assertTrue("Value Float 1.2 must match SelectItem.value \"1.2\" (type of String)", matchValue);
93          
94          Float valueNotInSelectItems = Float.valueOf("2.0");
95          matchValue = _SelectItemsUtil.matchValue(facesContext, uiComponent, valueNotInSelectItems, iterator, null);
96          assertFalse(matchValue);
97      }
98      
99      @Test
100     public void testMatchValueWithEnums() throws Exception
101     {
102         noSelectionOption.setItemValue("NONE");
103         selectItem1.setItemValue("ONE");
104         selectItem2.setItemValue("TWO");
105         selectItem3.setItemValue("THREE");
106         iterator = new _SelectItemsIterator(uiComponent, facesContext);
107         
108         Object enumValue = MockEnum.THREE;
109         boolean matchValue = _SelectItemsUtil.matchValue(facesContext, uiComponent, enumValue, iterator, null);
110         
111         assertTrue("Value Enum THREE must match SelectItem.value \"THREE\" (type of String)", matchValue);
112         
113         enumValue = MockEnum.FOUR;
114         matchValue = _SelectItemsUtil.matchValue(facesContext, uiComponent, enumValue, iterator, null);
115         assertFalse(matchValue);
116     }
117     
118     @Test 
119     public void testMatchValueWithEnumsNoExtends() throws Exception
120     {
121         noSelectionOption.setItemValue("NONE");
122         selectItem1.setItemValue("ONE");
123         selectItem2.setItemValue("TWO");
124         selectItem3.setItemValue("THREE");
125         iterator = new _SelectItemsIterator(uiComponent, facesContext);
126         
127         Object enumValue = MockEnum.TWO;
128         boolean matchValue = _SelectItemsUtil.matchValue(facesContext, uiComponent, enumValue, iterator, null);
129         
130         assertTrue("Value Enum TWO must match SelectItem.value \"TWO\" (type of String)", matchValue);
131     }
132     
133     private static enum MockEnum {
134         NONE,
135         ONE {
136 
137             @Override
138             public String toString()
139             {
140                 return "ONE";
141             } 
142             
143         },TWO,
144         THREE {
145  
146             @Override
147             public String toString()
148             {
149                 return "THREE";
150             } 
151             
152         }, FOUR
153     }
154 
155     @Test
156     public void testIsNoSelectionOption()
157     {
158         Float value = Float.parseFloat(NO_SELECTION_ITEM_VALUE);
159         boolean noSelectionOption = _SelectItemsUtil.isNoSelectionOption(facesContext, uiComponent, value, iterator, null);
160         assertTrue(noSelectionOption);
161         
162         Float valueNotInSelectItems = Float.valueOf("2.0");
163         noSelectionOption = _SelectItemsUtil.isNoSelectionOption(facesContext, uiComponent, valueNotInSelectItems, iterator, null);
164         assertFalse(noSelectionOption);
165         
166     }
167 
168 }