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.commons.beanutils.locale.converters;
19  
20  import java.text.DecimalFormat;
21  import org.apache.commons.beanutils.ConversionException;
22  
23  /***
24   * Test Case for the FloatLocaleConverter class.
25   *
26   * @author Niall Pemberton
27   * @version $Revision: 555489 $ $Date: 2007-07-12 05:53:26 +0100 (Thu, 12 Jul 2007) $
28   */
29  
30  public class FloatLocaleConverterTestCase extends BaseLocaleConverterTestCase {
31  
32  
33  
34      // ---------------------------------------------------------- Constructors
35  
36      public FloatLocaleConverterTestCase(String name) {
37          super(name);
38      }
39      
40      // -------------------------------------------------- Overall Test Methods
41  
42      /***
43       * Set up instance variables required by this test case.
44       */
45      public void setUp() throws Exception {
46  
47          super.setUp();
48  
49          defaultValue  = new Float("9.99");
50          expectedValue = new Float(expectedDecimalValue);
51  
52      }
53  
54      /***
55       * Tear down instance variables required by this test case.
56       */
57      public void tearDown() {
58          super.tearDown();
59      }
60  
61  
62      // ------------------------------------------------------------------------
63  
64      /***
65       * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
66       */
67      public void testConstructorMain() {
68  
69          // ------------- Construct with localized pattern ------------
70          converter = new FloatLocaleConverter(defaultValue,
71                                                    localizedLocale,
72                                                    localizedDecimalPattern,
73                                                    true);
74  
75  
76          convertValueNoPattern(converter, "(A)", localizedDecimalValue, expectedValue);
77          convertValueWithPattern(converter, "(A)", localizedDecimalValue, localizedDecimalPattern, expectedValue);
78          convertInvalid(converter, "(A)", defaultValue);
79          convertNull(converter, "(A)", defaultValue);
80  
81  
82          // **************************************************************************
83          // Convert value in the wrong format - maybe you would expect it to throw an
84          // exception and return the default - it doesn't, DecimalFormat parses it
85          // quite happily turning "1,234.56" into "1.234"
86          // I guess this is one of the limitations of DecimalFormat
87          // **************************************************************************
88          convertValueNoPattern(converter, "(B)", defaultDecimalValue, new Float("1.234"));
89  
90  
91          // **************************************************************************
92          // Convert with non-localized pattern - this causes an exception in parse()
93          // but it gets swallowed in convert() method and returns default.
94          //  **** IS THIS THE EXPECTED BEHAVIOUR? ****
95          // Maybe if the pattern is no good, we should use a default pattern rather
96          // than just returning the default value.
97          // **************************************************************************
98          convertValueWithPattern(converter, "(B)", localizedDecimalValue, defaultDecimalPattern, defaultValue);
99  
100 
101         // **************************************************************************
102         // Convert with specified type
103         // 
104         // BaseLocaleConverter completely ignores the type - so even if we specify
105         // Float.class here it still returns a Float.
106         //  **** SHOULD IMPLEMENT THIS BEHAVIOUR ****
107         // **************************************************************************
108         convertValueToType(converter, "(B)", Integer.class, localizedDecimalValue, localizedDecimalPattern, expectedValue);
109 
110 
111         // ------------- Construct with non-localized pattern ------------
112         converter = new FloatLocaleConverter(defaultValue,
113                                                   localizedLocale,
114                                                   defaultDecimalPattern,
115                                                   false);
116 
117 
118         convertValueNoPattern(converter, "(C)", localizedDecimalValue, expectedValue);
119         convertValueWithPattern(converter, "(C)", localizedDecimalValue, defaultDecimalPattern, expectedValue);
120         convertInvalid(converter, "(C)", defaultValue);
121         convertNull(converter, "(C)", defaultValue);
122 
123     }
124 
125     /***
126      * Test Converter() constructor
127      * 
128      * Uses the default locale, no default value
129      * 
130      */
131     public void testConstructor_2() {
132 
133         // ------------- Construct using default locale ------------
134         converter = new FloatLocaleConverter();
135 
136         // Perform Tests
137         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
138         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
139         convertInvalid(converter, null);
140         convertNull(converter, null);
141 
142     }
143 
144     /***
145      * Test Converter(locPattern) constructor
146      * 
147      * Uses the default locale, no default value
148      * 
149      */
150     public void testConstructor_3() {
151 
152         // ------------- Construct using localized pattern (default locale) --------
153         converter = new FloatLocaleConverter(true);
154 
155         // Perform Tests
156         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
157         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
158         convertInvalid(converter, null);
159         convertNull(converter, null);
160 
161 
162     }
163 
164     /***
165      * Test Converter(Locale) constructor
166      */
167     public void testConstructor_4() {
168 
169         // ------------- Construct using specified Locale --------
170         converter = new FloatLocaleConverter(localizedLocale);
171 
172         // Perform Tests
173         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
174         convertValueWithPattern(converter, localizedDecimalValue, defaultDecimalPattern, expectedValue);
175         convertInvalid(converter, null);
176         convertNull(converter, null);
177 
178 
179     }
180 
181 
182     /***
183      * Test Converter(Locale, locPattern) constructor
184      */
185     public void testConstructor_5() {
186 
187         // ------------- Construct using specified Locale --------
188         converter = new FloatLocaleConverter(localizedLocale, true);
189 
190         // Perform Tests
191         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
192         convertValueWithPattern(converter, localizedDecimalValue, localizedDecimalPattern, expectedValue);
193         convertInvalid(converter, null);
194         convertNull(converter, null);
195 
196 
197     }
198 
199     /***
200      * Test Converter(Locale, pattern) constructor
201      */
202     public void testConstructor_6() {
203 
204         // ------------- Construct using specified Locale --------
205         converter = new FloatLocaleConverter(localizedLocale, defaultDecimalPattern);
206 
207         // Perform Tests
208         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
209         convertValueWithPattern(converter, localizedDecimalValue, defaultDecimalPattern, expectedValue);
210         convertInvalid(converter, null);
211         convertNull(converter, null);
212 
213     }
214 
215     /***
216      * Test Converter(Locale, pattern, locPattern) constructor
217      */
218     public void testConstructor_7() {
219 
220         // ------------- Construct using specified Locale --------
221         converter = new FloatLocaleConverter(localizedLocale, localizedDecimalPattern, true);
222 
223         // Perform Tests
224         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
225         convertValueWithPattern(converter, localizedDecimalValue, localizedDecimalPattern, expectedValue);
226         convertInvalid(converter, null);
227         convertNull(converter, null);
228 
229     }
230 
231     /***
232      * Test Converter(defaultValue) constructor
233      */
234     public void testConstructor_8() {
235 
236         // ------------- Construct using specified Locale --------
237         converter = new FloatLocaleConverter(defaultValue);
238 
239         // Perform Tests
240         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
241         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
242         convertInvalid(converter, defaultValue);
243         convertNull(converter, defaultValue);
244 
245     }
246 
247     /***
248      * Test Converter(defaultValue, locPattern) constructor
249      */
250     public void testConstructor_9() {
251 
252         // ------------- Construct using specified Locale --------
253         converter = new FloatLocaleConverter(defaultValue, true);
254 
255         // Perform Tests
256         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
257         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
258         convertInvalid(converter, defaultValue);
259         convertNull(converter, defaultValue);
260 
261     }
262 
263     /***
264      * Test Float limits
265      */
266     public void testFloatLimits() {
267 
268         converter = new FloatLocaleConverter(defaultLocale, defaultDecimalPattern);
269         DecimalFormat fmt = new DecimalFormat("#.#############################################################");
270 
271         assertEquals(new Float(-0.12), converter.convert("-0.12"));
272         assertEquals("Positive Float.MAX_VALUE", new Float(Float.MAX_VALUE), converter.convert(fmt.format(Float.MAX_VALUE)));
273         assertEquals("Positive Float.MIN_VALUE", new Float(Float.MIN_VALUE), converter.convert(fmt.format(Float.MIN_VALUE)));
274 
275         assertEquals("Negative Float.MAX_VALUE", new Float(Float.MAX_VALUE * -1), converter.convert(fmt.format(Float.MAX_VALUE * -1)));
276         assertEquals("Negative Float.MIN_VALUE", new Float(Float.MIN_VALUE * -1), converter.convert(fmt.format(Float.MIN_VALUE * -1)));
277         
278         
279         try {
280             converter.convert(fmt.format((double)Float.MAX_VALUE * (double)10));
281             fail("Positive Too Large should throw ConversionException");
282         } catch (ConversionException e) {
283             // expected result
284         }
285         try {
286             converter.convert(fmt.format((double)Float.MAX_VALUE * (double)-10));
287             fail("Negative Too Large should throw ConversionException");
288         } catch (ConversionException e) {
289             // expected result
290         }
291         
292         try {
293             converter.convert(fmt.format((double)Float.MIN_VALUE / (double)10));
294             fail("Positive Too Small should throw ConversionException");
295         } catch (ConversionException e) {
296             // expected result
297         }
298         try {
299             converter.convert(fmt.format((double)Float.MIN_VALUE / (double)-10));
300             fail("Negative Too Small should throw ConversionException");
301         } catch (ConversionException e) {
302             // expected result
303         }
304     }
305 
306 
307 
308 }
309