1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
31
32 public class FloatLocaleConverterTestCase extends BaseLocaleConverterTestCase {
33
34
35
36
37
38 public FloatLocaleConverterTestCase(final String name) {
39 super(name);
40 }
41
42
43
44
45
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
59
60 @Override
61 public void tearDown() {
62 super.tearDown();
63 }
64
65
66
67
68
69
70
71 public void testConstructorMain() {
72
73
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
88
89
90
91
92 convertValueNoPattern(converter, "(B)", defaultDecimalValue, new Float("1.234"));
93
94
95
96
97
98
99
100
101
102 convertValueWithPattern(converter, "(B)", localizedDecimalValue, defaultDecimalPattern, defaultValue);
103
104
105
106
107
108
109
110
111
112
113
114
115
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
131
132
133
134
135 public void testConstructor_2() {
136
137
138 converter = new FloatLocaleConverter();
139
140
141 convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
142 convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
143 convertInvalid(converter, null);
144 convertNull(converter, null);
145
146 }
147
148
149
150
151
152
153
154 public void testConstructor_3() {
155
156
157 converter = new FloatLocaleConverter(true);
158
159
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
170
171 public void testConstructor_4() {
172
173
174 converter = new FloatLocaleConverter(localizedLocale);
175
176
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
188
189 public void testConstructor_5() {
190
191
192 converter = new FloatLocaleConverter(localizedLocale, true);
193
194
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
205
206 public void testConstructor_6() {
207
208
209 converter = new FloatLocaleConverter(localizedLocale, defaultDecimalPattern);
210
211
212 convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
213 convertValueWithPattern(converter, localizedDecimalValue, defaultDecimalPattern, expectedValue);
214 convertInvalid(converter, null);
215 convertNull(converter, null);
216
217 }
218
219
220
221
222 public void testConstructor_7() {
223
224
225 converter = new FloatLocaleConverter(localizedLocale, localizedDecimalPattern, true);
226
227
228 convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
229 convertValueWithPattern(converter, localizedDecimalValue, localizedDecimalPattern, expectedValue);
230 convertInvalid(converter, null);
231 convertNull(converter, null);
232
233 }
234
235
236
237
238 public void testConstructor_8() {
239
240
241 converter = new FloatLocaleConverter(defaultValue);
242
243
244 convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
245 convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
246 convertInvalid(converter, defaultValue);
247 convertNull(converter, defaultValue);
248
249 }
250
251
252
253
254 public void testConstructor_9() {
255
256
257 converter = new FloatLocaleConverter(defaultValue, true);
258
259
260 convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
261 convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
262 convertInvalid(converter, defaultValue);
263 convertNull(converter, defaultValue);
264
265 }
266
267
268
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
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
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
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
307 }
308 }
309
310
311
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