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.converter;
19  
20  import java.util.Locale;
21  import javax.faces.component.UIForm;
22  import javax.faces.component.UIInput;
23  import javax.faces.convert.ConverterException;
24  import javax.servlet.ServletContextEvent;
25  import org.apache.shale.test.base.AbstractJsfTestCase;
26  import org.apache.shale.validator.faces.ValidatorLifecycleListener;
27  
28  /***
29   * <p>Test case for <code>FloatConverter</code>.</p>
30   */
31  public class FloatConverterTestCase extends AbstractJsfTestCase {
32  
33  
34      // ------------------------------------------------------------ Constructors
35  
36  
37      // Construct a new instance of this test case.
38      public FloatConverterTestCase(String name) {
39          super(name);
40      }
41  
42  
43      // ---------------------------------------------------- Overall Test Methods
44  
45  
46      // Set up instance variables required by this test case.
47      protected void setUp() throws Exception {
48  
49          super.setUp();
50          facesContext.getViewRoot().setLocale(Locale.US);
51  
52          listener = new ValidatorLifecycleListener();
53          listener.contextInitialized(new ServletContextEvent(servletContext));
54  
55          form = new UIForm();
56          form.setId("form");
57          facesContext.getViewRoot().getChildren().add(form);
58  
59          input = new UIInput();
60          input.setId("input");
61          form.getChildren().add(input);
62  
63          converter = new FloatConverter();
64          input.setConverter(converter);
65  
66      }
67  
68  
69      // Tear down instance variables required by this test case.
70      protected void tearDown() throws Exception {
71  
72          converter = null;
73          input = null;
74          form = null;
75  
76          listener.contextDestroyed(new ServletContextEvent(servletContext));
77          listener = null;
78  
79          super.tearDown();
80  
81      }
82  
83  
84      // -------------------------------------------------------- Static Variables
85  
86  
87      // Valid object values to be tested
88      private static final Float[] VALID_OBJECTS =
89      { new Float(Float.MIN_VALUE),
90        new Float((float) -123456),
91        new Float((float) -123.456),
92        new Float((float) -123),
93        new Float((float) 0),
94        new Float((float) 123),
95        new Float((float) 123.456),
96        new Float((float) 123456),
97        new Float(Float.MAX_VALUE),
98      };
99  
100 
101     // Valid string values to be tested
102     private static final String[] VALID_STRINGS =
103     {// String.valueOf(Float.MIN_VALUE), // JDK format routines do not like this
104       "-123456",
105       "-123.456",
106       "-123",
107       "0",
108       "123",
109       "123.456",
110       "123456",
111       // String.valueOf(Float.MAX_VALUE), // JDK format routines do not like this
112     };
113 
114 
115     // Valid string values with grouping (Locale.GERMANY)
116     private static final String[] VALID_STRINGS_DE =
117     { "-123.456",
118       "-123,456",
119       "123",
120       "123",
121       "123,456",
122       "123.456",
123     };
124 
125 
126     // Valid string values with grouping (Locale.US)
127     private static final String[] VALID_STRINGS_US =
128     { "-123,456",
129       "-123.456",
130       "123",
131       "123",
132       "123.456",
133       "123,456",
134     };
135 
136 
137     // ------------------------------------------------------ Instance Variables
138 
139 
140     /***
141      * <p>Converter instance under test.</p>
142      */
143     private FloatConverter converter = null;
144 
145 
146     /***
147      * <p>The form component for our input form.</p>
148      */
149     private UIForm form = null;
150 
151 
152     /***
153      * <p>The text field component for our input form.</p>
154      */
155     private UIInput input = null;
156 
157 
158     /***
159      * <p>ValidatorLifecycleListener used to load configuration resources</p>
160      */
161     private ValidatorLifecycleListener listener = null;
162 
163 
164     // ------------------------------------------------- Individual Test Methods
165 
166 
167     /***
168      * <p>Tests for invalid Object input.</p>
169      */
170     public void testInvalidObject() {
171 
172 
173     }
174 
175 
176     /***
177      * <p>Tests for valid Object input.</p>
178      */
179     public void testValidObject() {
180 
181         for (int i = 0; i < VALID_OBJECTS.length; i++) {
182             Float value = VALID_OBJECTS[i];
183             try {
184                 String string = converter.getAsString(facesContext, input, value);
185                 assertEquals("Float value " + value
186                              + " equals String value " + string,
187                   value.floatValue(), Float.valueOf(strip(string)).floatValue(),
188                   (float) 0.001);
189             } catch (ConverterException e) {
190                 fail("Should not have thrown ConverterException for value " + value);
191             }
192         }
193 
194     }
195 
196 
197     /***
198      * <p>Tests for invalid String input.</p>
199      */
200     public void testInvalidString() {
201 
202 
203     }
204 
205 
206     /***
207      * <p>Tests for valid String input.</p>
208      */
209     public void testValidString() {
210 
211         for (int i = 0; i < VALID_STRINGS.length; i++) {
212             String string = VALID_STRINGS[i];
213             try {
214                 Object value = converter.getAsObject(facesContext, input, string);
215                 assertEquals("String value " + string
216                              + " equals Float value " + value,
217                              Float.parseFloat(string),
218                              ((Float) value).floatValue(),
219                              (float) 0.001);
220             } catch (ConverterException e) {
221                 fail("Should not have thrown ConverterException for value " + string);
222             }
223         }
224 
225     }
226 
227 
228     /***
229      * <p>Tests for valid String input with grouping (Locale.GERMANY).</p>
230      */
231     public void testValidStringDE() {
232 
233         facesContext.getViewRoot().setLocale(Locale.GERMANY);
234         for (int i = 0; i < VALID_STRINGS_DE.length; i++) {
235             String string = VALID_STRINGS_DE[i];
236             Object value = converter.getAsObject(facesContext, input, string);
237             assertTrue(value instanceof Float);
238             assertEquals("String value " + string
239                          + " equals Float value " + value,
240                          Float.parseFloat(strip(string)),
241                          ((Float) value).floatValue(),
242                          (float) 0.001);
243         }
244 
245     }
246 
247 
248     /***
249      * <p>Tests for valid String input with grouping (Locale.US).</p>
250      */
251     public void testValidStringUS() {
252 
253         facesContext.getViewRoot().setLocale(Locale.US);
254         for (int i = 0; i < VALID_STRINGS_US.length; i++) {
255             String string = VALID_STRINGS_US[i];
256             Object value = converter.getAsObject(facesContext, input, string);
257             assertTrue(value instanceof Float);
258             assertEquals("String value " + string
259                          + " equals Float value " + value,
260                          Float.parseFloat(strip(string)),
261                          ((Float) value).floatValue(),
262                          (float) 0.001);
263         }
264 
265     }
266 
267 
268     private String strip(String value) {
269         char grouping =
270           Locale.US == facesContext.getViewRoot().getLocale() ? ',' : '.';
271         while (true) {
272             int index = value.indexOf(grouping);
273             if (index < 0) {
274                 break;
275             }
276             value = value.substring(0, index) + value.substring(index + 1);
277         }
278         // Switch the decimal point back as well if not US
279         if (Locale.US != facesContext.getViewRoot().getLocale()) {
280             int index = value.indexOf(',');
281             if (index >= 0) {
282                 value = value.substring(0, index) + '.' + value.substring(index + 1);
283             }
284         }
285         return value;
286     }
287 
288 
289 }