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