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 java.util.List;
22  import java.util.Locale;
23  
24  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  public class UISelectOneTest extends AbstractJsfTestCase
29  {
30      
31      @Test
32      public void testValidateRequiredNull()
33      {
34  
35          facesContext.getViewRoot().setLocale(_TEST_LOCALE);
36  
37          UISelectOne selectOne = new UISelectOne();
38          selectOne.setId("selectOne");
39          selectOne.setRendererType(null);
40          selectOne.setRequired(true);
41          List<UIComponent> children = selectOne.getChildren();
42  
43          UISelectItem one = new UISelectItem();
44          one.setItemValue(new Integer(1));
45          children.add(one);
46  
47          UISelectItem two = new UISelectItem();
48          two.setItemValue(new Integer(2));
49          children.add(two);
50  
51          UISelectItem three = new UISelectItem();
52          three.setItemValue(new Integer(3));
53          children.add(three);
54  
55          selectOne.validateValue(facesContext, null);
56  
57          Assert.assertFalse(selectOne.isValid());
58          Assert.assertEquals(1, facesContext.getMessageList().size());
59      }
60      
61      @Test
62      public void testValidateNotRequiredValid()
63      {
64  
65          facesContext.getViewRoot().setLocale(_TEST_LOCALE);
66  
67          UISelectOne selectOne = new UISelectOne();
68          selectOne.setId("selectOne");
69          selectOne.setRendererType(null);
70          selectOne.setRequired(false);
71          List<UIComponent> children = selectOne.getChildren();
72  
73          UISelectItem one = new UISelectItem();
74          one.setItemValue(new Integer(1));
75          children.add(one);
76  
77          UISelectItem two = new UISelectItem();
78          two.setItemValue(new Integer(2));
79          children.add(two);
80  
81          UISelectItem three = new UISelectItem();
82          three.setItemValue(new Integer(3));
83          children.add(three);
84  
85          selectOne.validateValue(facesContext, 1);
86  
87          Assert.assertTrue(selectOne.isValid());
88          Assert.assertEquals(0, facesContext.getMessageList().size());
89      }
90      
91      @Test
92      public void testValidateNotRequiredNotValid()
93      {
94  
95          facesContext.getViewRoot().setLocale(_TEST_LOCALE);
96  
97          UISelectOne selectOne = new UISelectOne();
98          selectOne.setId("selectOne");
99          selectOne.setRendererType(null);
100         selectOne.setRequired(false);
101         List<UIComponent> children = selectOne.getChildren();
102 
103         UISelectItem one = new UISelectItem();
104         one.setItemValue(new Integer(1));
105         children.add(one);
106 
107         UISelectItem two = new UISelectItem();
108         two.setItemValue(new Integer(2));
109         children.add(two);
110 
111         UISelectItem three = new UISelectItem();
112         three.setItemValue(new Integer(3));
113         children.add(three);
114 
115         selectOne.validateValue(facesContext, 4);
116 
117         Assert.assertFalse(selectOne.isValid());
118         Assert.assertEquals(1, facesContext.getMessageList().size());
119     }
120     
121     @Test
122     public void testValidateRequiredNotSelectOption()
123     {
124 
125         facesContext.getViewRoot().setLocale(_TEST_LOCALE);
126 
127         UISelectOne selectOne = new UISelectOne();
128         selectOne.setId("selectOne");
129         selectOne.setRendererType(null);
130         selectOne.setRequired(true);
131         List<UIComponent> children = selectOne.getChildren();
132 
133         UISelectItem one = new UISelectItem();
134         one.setItemValue(new Integer(1));
135         children.add(one);
136 
137         UISelectItem two = new UISelectItem();
138         two.setItemValue(new Integer(2));
139         children.add(two);
140 
141         UISelectItem three = new UISelectItem();
142         three.setItemValue(new Integer(3));
143         three.setNoSelectionOption(true);
144         children.add(three);
145 
146         selectOne.validateValue(facesContext, 3);
147 
148         Assert.assertFalse(selectOne.isValid());
149         Assert.assertEquals(1, facesContext.getMessageList().size());
150     }
151     
152     @Test
153     public void testValidateNotRequiredNotSelectOption()
154     {
155 
156         facesContext.getViewRoot().setLocale(_TEST_LOCALE);
157 
158         UISelectOne selectOne = new UISelectOne();
159         selectOne.setId("selectOne");
160         selectOne.setRendererType(null);
161         selectOne.setRequired(false);
162         List<UIComponent> children = selectOne.getChildren();
163 
164         UISelectItem one = new UISelectItem();
165         one.setItemValue(new Integer(1));
166         children.add(one);
167 
168         UISelectItem two = new UISelectItem();
169         two.setItemValue(new Integer(2));
170         children.add(two);
171 
172         UISelectItem three = new UISelectItem();
173         three.setItemValue(new Integer(3));
174         three.setNoSelectionOption(true);
175         children.add(three);
176 
177         selectOne.validateValue(facesContext, 3);
178 
179         Assert.assertTrue(selectOne.isValid());
180         Assert.assertEquals(0, facesContext.getMessageList().size());
181     }
182     
183     static private final Locale _TEST_LOCALE = new Locale("xx", "TEST");
184 
185 }