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