2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.validator.validator;
19  
20  import java.util.Locale;
21  
22  import javax.faces.component.UIForm;
23  import javax.faces.component.UIInput;
24  import javax.faces.validator.ValidatorException;
25  import javax.servlet.ServletContextEvent;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.shale.test.base.AbstractJsfTestCase;
31  import org.apache.shale.validator.faces.ValidatorLifecycleListener;
32  
33  /***
34   * <p>Test case for <code>FloatValidator</code>.</p>
35   */
36  public class FloatValidatorTestCase extends AbstractJsfTestCase {
37  
38  
39      // ------------------------------------------------------------ Constructors
40  
41  
42      // Construct a new instance of this test case.
43      public FloatValidatorTestCase(String name) {
44          super(name);
45      }
46  
47  
48      // ---------------------------------------------------- Overall Test Methods
49  
50  
51      // Set up instance variables required by this test case.
52      protected void setUp() throws Exception {
53  
54          super.setUp();
55          facesContext.getViewRoot().setLocale(Locale.US);
56  
57          listener = new ValidatorLifecycleListener();
58          listener.contextInitialized(new ServletContextEvent(servletContext));
59  
60          form = new UIForm();
61          form.setId("form");
62          facesContext.getViewRoot().getChildren().add(form);
63  
64          input = new UIInput();
65          input.setId("input");
66          form.getChildren().add(input);
67  
68          validator = new FloatValidator();
69          input.addValidator(validator);
70  
71      }
72  
73  
74      // Return the tests included in this test case.
75      public static Test suite() {
76  
77          return (new TestSuite(FloatValidatorTestCase.class));
78  
79      }
80  
81  
82      // Tear down instance variables required by this test case.
83      protected void tearDown() throws Exception {
84  
85          validator = null;
86          input = null;
87          form = null;
88  
89          listener.contextDestroyed(new ServletContextEvent(servletContext));
90          listener = null;
91  
92          super.tearDown();
93  
94      }
95  
96  
97      // -------------------------------------------------------- Static Variables
98  
99  
100     // ------------------------------------------------------ Instance Variables
101 
102 
103     /***
104      * <p>The form component for our input form.</p>
105      */
106     private UIForm form = null;
107 
108 
109     /***
110      * <p>The text field component for our input form.</p>
111      */
112     private UIInput input = null;
113 
114 
115     /***
116      * <p>ValidatorLifecycleListener used to load configuration resources</p>
117      */
118     private ValidatorLifecycleListener listener = null;
119 
120 
121     /***
122      * <p>Validator instance under test.</p>
123      */
124     private FloatValidator validator = null;
125 
126 
127     // ------------------------------------------------- Individual Test Methods
128 
129 
130     /***
131      * <p>Tests for invalid input with no range limits.</p>
132      */
133     public void testInvalidInput() {
134 
135         // NOTE - null and zero-length string are irrelevant inputs, because
136         // JSF will not call validators in that scenario
137 
138         try {
139             validator.validate(facesContext, input, "abc");
140             fail("Should have thrown ValidatorException");
141         } catch (ValidatorException e) {
142             ; // Expected result
143 //            System.err.println("a: " + e.getFacesMessage().getSummary());
144         }
145 
146     }
147 
148 
149     /***
150      * <p>Test cases where a maximum range value has been specified
151      * with an invalid value.</p>
152      */
153     public void testInvalidMaximum() {
154 
155         validator.setMaximum((float) 123);
156         try {
157             validator.validate(facesContext, input, new Float((float) 234));
158             fail("Should have thrown ValidatorException");
159         } catch (ValidatorException e) {
160             ; // Expected result
161 //            System.err.print("234: " + e.getFacesMessage().getSummary());
162         }
163 
164     }
165 
166 
167     /***
168      * <p>Test cases where a minimum range value has been specified
169      * with an invalid value.</p>
170      */
171     public void testInvalidMinimum() {
172 
173         validator.setMinimum((float) 234);
174         try {
175             validator.validate(facesContext, input, new Float((float) 123));
176             fail("Should have thrown ValidatorException");
177         } catch (ValidatorException e) {
178             ; // Expected result
179 //            System.err.print("123: " + e.getFacesMessage().getSummary());
180         }
181 
182     }
183 
184 
185     /***
186      * <p>Test cases where a minimum and minimum range value has been specified
187      * with an invalid value.</p>
188      */
189     public void testInvalidRange() {
190 
191         validator.setMinimum((float) 0);
192         validator.setMaximum((float) 234);
193         try {
194             validator.validate(facesContext, input, new Float((float) -1));
195             fail("Should have thrown ValidatorException");
196         } catch (ValidatorException e) {
197             ; // Expected result
198 //            System.err.println("-1: " + e.getFacesMessage().getSummary());
199         }
200 
201         validator.setMinimum((float) 0);
202         validator.setMaximum((float) 234);
203         try {
204             validator.validate(facesContext, input, new Float((float) 235));
205             fail("Should have thrown ValidatorException");
206         } catch (ValidatorException e) {
207             ; // Expected result
208 //            System.err.println("-1: " + e.getFacesMessage().getSummary());
209         }
210 
211     }
212 
213 
214     /***
215      * <p>Tests for valid input with no range limits.</p>
216      */
217     public void testValidInput() {
218 
219         // NOTE - null and zero-length string are irrelevant inputs, because
220         // JSF will not call validators in that scenario
221 
222         try {
223             validator.validate(facesContext, input, new Float((float) 0));
224         } catch (ValidatorException e) {
225             fail("Should not have thrown ValidatorException: " +
226                     e.getFacesMessage().getSummary());
227         }
228 
229         try {
230             validator.validate(facesContext, input, new Float((float) 123));
231         } catch (ValidatorException e) {
232             fail("Should not have thrown ValidatorException: " +
233                     e.getFacesMessage().getSummary());
234         }
235 
236         try {
237             validator.validate(facesContext, input, new Float((float) -456));
238         } catch (ValidatorException e) {
239             fail("Should not have thrown ValidatorException: " +
240                     e.getFacesMessage().getSummary());
241         }
242 
243     }
244 
245 
246     /***
247      * <p>Test cases where a maximum range value has been specified
248      * with a valid value.</p>
249      */
250     public void testValidMaximum() {
251 
252         validator.setMaximum((float) 234);
253         try {
254             validator.validate(facesContext, input, new Float((float) 123));
255         } catch (ValidatorException e) {
256             fail("Should not have thrown ValidatorException: " +
257                     e.getFacesMessage().getSummary());
258         }
259 
260         validator.setMaximum((float) 234);
261         try {
262             validator.validate(facesContext, input, new Float((float) 234));
263         } catch (ValidatorException e) {
264             fail("Should not have thrown ValidatorException: " +
265                     e.getFacesMessage().getSummary());
266         }
267 
268     }
269 
270 
271     /***
272      * <p>Test cases where a minimum range value has been specified
273      * with a valid value.</p>
274      */
275     public void testValidMinimum() {
276 
277         validator.setMinimum((float) 0);
278         try {
279             validator.validate(facesContext, input, new Float((float) 0));
280         } catch (ValidatorException e) {
281             fail("Should not have thrown ValidatorException: " +
282                     e.getFacesMessage().getSummary());
283         }
284 
285         validator.setMinimum((float) 0);
286         try {
287             validator.validate(facesContext, input, new Float((float) 123));
288         } catch (ValidatorException e) {
289             fail("Should not have thrown ValidatorException: " +
290                     e.getFacesMessage().getSummary());
291         }
292 
293     }
294 
295 
296     /***
297      * <p>Test cases where a minimum and minimum range value has been specified
298      * with a valid value.</p>
299      */
300     public void testValidRange() {
301 
302         validator.setMinimum((float) 0);
303         validator.setMaximum((float) 234);
304         try {
305             validator.validate(facesContext, input, new Float((float) 0));
306         } catch (ValidatorException e) {
307             fail("Should not have thrown ValidatorException: " +
308                     e.getFacesMessage().getSummary());
309         }
310 
311         validator.setMinimum((float) 0);
312         validator.setMaximum((float) 234);
313         try {
314             validator.validate(facesContext, input, new Float((float) 123));
315         } catch (ValidatorException e) {
316             fail("Should not have thrown ValidatorException: " +
317                     e.getFacesMessage().getSummary());
318         }
319 
320         validator.setMinimum((float) 0);
321         validator.setMaximum((float) 234);
322         try {
323             validator.validate(facesContext, input, new Float((float) 234));
324         } catch (ValidatorException e) {
325             fail("Should not have thrown ValidatorException: " +
326                     e.getFacesMessage().getSummary());
327         }
328 
329     }
330 
331 
332 }