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 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
35
36 public FloatLocaleConverterTestCase(String name) {
37 super(name);
38 }
39
40
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
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
84
85
86
87
88 convertValueNoPattern(converter, "(B)", defaultDecimalValue, new Float("1.234"));
89
90
91
92
93
94
95
96
97
98 convertValueWithPattern(converter, "(B)", localizedDecimalValue, defaultDecimalPattern, defaultValue);
99
100
101
102
103
104
105
106
107
108 convertValueToType(converter, "(B)", Integer.class, localizedDecimalValue, localizedDecimalPattern, expectedValue);
109
110
111
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
134 converter = new FloatLocaleConverter();
135
136
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
153 converter = new FloatLocaleConverter(true);
154
155
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
170 converter = new FloatLocaleConverter(localizedLocale);
171
172
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
188 converter = new FloatLocaleConverter(localizedLocale, true);
189
190
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
205 converter = new FloatLocaleConverter(localizedLocale, defaultDecimalPattern);
206
207
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
221 converter = new FloatLocaleConverter(localizedLocale, localizedDecimalPattern, true);
222
223
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
237 converter = new FloatLocaleConverter(defaultValue);
238
239
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
253 converter = new FloatLocaleConverter(defaultValue, true);
254
255
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
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
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
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
303 }
304 }
305
306
307
308 }
309