1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils.locale;
19
20
21 import java.sql.Date;
22 import java.sql.Time;
23 import java.sql.Timestamp;
24 import java.text.DecimalFormat;
25 import java.text.NumberFormat;
26
27 import junit.framework.TestCase;
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30 import org.apache.commons.beanutils.ConversionException;
31
32
33 /***
34 * <p>
35 * Test Case for the LocaleConvertUtils class.
36 * See unimplemented functionality of the convert utils in the method begining with fixme
37 * </p>
38 *
39 * @author Michael Szlapa
40 * @author Paul Hamamnt & Rune Johannesen (pairing) - patches.
41 * @version $Revision: 469743 $ $Date: 2006-11-01 01:27:40 +0000 (Wed, 01 Nov 2006) $
42 */
43
44 public class LocaleConvertUtilsTestCase extends TestCase {
45
46
47
48 private char m_decimalSeparator;
49
50
51
52
53 /***
54 * Construct a new instance of this test case.
55 *
56 * @param name Name of the test case
57 */
58 public LocaleConvertUtilsTestCase(String name) {
59 super(name);
60 }
61
62
63
64
65
66 /***
67 * Set up instance variables required by this test case.
68 */
69 public void setUp() {
70
71 LocaleConvertUtils.deregister();
72
73 NumberFormat nf = DecimalFormat.getNumberInstance();
74 String result = nf.format(1.1);
75
76
77 m_decimalSeparator = result.charAt(1);
78
79
80 }
81
82
83 /***
84 * Return the tests included in this test suite.
85 */
86 public static Test suite() {
87 return (new TestSuite(LocaleConvertUtilsTestCase.class));
88 }
89
90
91 /***
92 * Tear down instance variables required by this test case.
93 */
94 public void tearDown() {
95
96 }
97
98
99
100
101
102 /***
103 * Negative String to primitive integer array tests.
104 */
105 public void fixmetestNegativeIntegerArray() {
106
107 fail("Array conversions not implemented yet.");
108
109 Object value = null;
110 int intArray[] = new int[0];
111
112 value = LocaleConvertUtils.convert((String) null, intArray.getClass());
113 checkIntegerArray(value, intArray);
114 value = LocaleConvertUtils.convert("a", intArray.getClass());
115 checkIntegerArray(value, intArray);
116 value = LocaleConvertUtils.convert("{ a }", intArray.getClass());
117 checkIntegerArray(value, intArray);
118 value = LocaleConvertUtils.convert("1a3", intArray.getClass());
119 checkIntegerArray(value, intArray);
120 value = LocaleConvertUtils.convert("{ 1a3 }", intArray.getClass());
121 checkIntegerArray(value, intArray);
122 value = LocaleConvertUtils.convert("0,1a3", intArray.getClass());
123 checkIntegerArray(value, intArray);
124 value = LocaleConvertUtils.convert("{ 0, 1a3 }", intArray.getClass());
125 checkIntegerArray(value, intArray);
126
127 }
128
129
130 /***
131 * Negative scalar conversion tests. These rely on the standard
132 * default value conversions in LocaleConvertUtils.
133 */
134 public void testNegativeScalar() {
135
136 Object value = null;
137
138
139
140
141
142
143
144
145
146
147 try {
148 value = LocaleConvertUtils.convert("foo", Byte.TYPE);
149 fail("Should have thrown conversion exception (1)");
150 } catch (ConversionException e) {
151
152 }
153
154 try {
155 value = LocaleConvertUtils.convert("foo", Byte.class);
156 fail("Should have thrown conversion exception (2)");
157 } catch (ConversionException e) {
158
159 }
160
161
162
163
164
165
166
167
168
169
170 try {
171 value = LocaleConvertUtils.convert("foo", Double.TYPE);
172 fail("Should have thrown conversion exception (3)");
173 } catch (ConversionException e) {
174
175 }
176
177 try {
178 value = LocaleConvertUtils.convert("foo", Double.class);
179 fail("Should have thrown conversion exception (4)");
180 } catch (ConversionException e) {
181
182 }
183
184 try {
185 value = LocaleConvertUtils.convert("foo", Float.TYPE);
186 fail("Should have thrown conversion exception (5)");
187 } catch (ConversionException e) {
188
189 }
190
191 try {
192 value = LocaleConvertUtils.convert("foo", Float.class);
193 fail("Should have thrown conversion exception (6)");
194 } catch (ConversionException e) {
195
196 }
197
198 try {
199 value = LocaleConvertUtils.convert("foo", Integer.TYPE);
200 fail("Should have thrown conversion exception (7)");
201 } catch (ConversionException e) {
202
203 }
204
205 try {
206 value = LocaleConvertUtils.convert("foo", Integer.class);
207 fail("Should have thrown conversion exception (8)");
208 } catch (ConversionException e) {
209
210 }
211
212 try {
213 value = LocaleConvertUtils.convert("foo", Byte.TYPE);
214 fail("Should have thrown conversion exception (9)");
215 } catch (ConversionException e) {
216
217 }
218
219 try {
220 value = LocaleConvertUtils.convert("foo", Long.class);
221 fail("Should have thrown conversion exception (10)");
222 } catch (ConversionException e) {
223
224 }
225
226 try {
227 value = LocaleConvertUtils.convert("foo", Short.TYPE);
228 fail("Should have thrown conversion exception (11)");
229 } catch (ConversionException e) {
230
231 }
232
233 try {
234 value = LocaleConvertUtils.convert("foo", Short.class);
235 fail("Should have thrown conversion exception (12)");
236 } catch (ConversionException e) {
237
238 }
239
240 }
241
242
243 /***
244 * Negative String to String array tests.
245 */
246 public void fixmetestNegativeStringArray() {
247
248 fail("Array conversions not implemented yet.");
249
250 Object value = null;
251 String stringArray[] = new String[0];
252
253 value = LocaleConvertUtils.convert((String) null, stringArray.getClass());
254 checkStringArray(value, stringArray);
255 }
256
257
258 /***
259 * Test conversion of object to string for arrays - .
260 */
261 public void fixmetestObjectToStringArray() {
262
263 fail("Array conversions not implemented yet.");
264 int intArray0[] = new int[0];
265 int intArray1[] = {123};
266 int intArray2[] = {123, 456};
267 String stringArray0[] = new String[0];
268 String stringArray1[] = {"abc"};
269 String stringArray2[] = {"abc", "def"};
270
271 assertEquals("intArray0", null,
272 LocaleConvertUtils.convert(intArray0));
273 assertEquals("intArray1", "123",
274 LocaleConvertUtils.convert(intArray1));
275 assertEquals("intArray2", "123",
276 LocaleConvertUtils.convert(intArray2));
277
278 assertEquals("stringArray0", null,
279 LocaleConvertUtils.convert(stringArray0));
280 assertEquals("stringArray1", "abc",
281 LocaleConvertUtils.convert(stringArray1));
282 assertEquals("stringArray2", "abc",
283 LocaleConvertUtils.convert(stringArray2));
284
285 }
286
287
288 /***
289 * Test conversion of object to string for scalars.
290 */
291 public void testObjectToStringScalar() {
292
293 assertEquals("Boolean->String", "false",
294 LocaleConvertUtils.convert(Boolean.FALSE));
295 assertEquals("Boolean->String", "true",
296 LocaleConvertUtils.convert(Boolean.TRUE));
297 assertEquals("Byte->String", "123",
298 LocaleConvertUtils.convert(new Byte((byte) 123)));
299 assertEquals("Character->String", "a",
300 LocaleConvertUtils.convert(new Character('a')));
301 assertEquals("Double->String", "123" + m_decimalSeparator + "4",
302 LocaleConvertUtils.convert(new Double(123.4)));
303 assertEquals("Float->String", "123" + m_decimalSeparator + "4",
304 LocaleConvertUtils.convert(new Float((float) 123.4)));
305 assertEquals("Integer->String", "123",
306 LocaleConvertUtils.convert(new Integer(123)));
307 assertEquals("Long->String", "123",
308 LocaleConvertUtils.convert(new Long(123)));
309 assertEquals("Short->String", "123",
310 LocaleConvertUtils.convert(new Short((short) 123)));
311 assertEquals("String->String", "abc",
312 LocaleConvertUtils.convert("abc"));
313 assertEquals("String->String null", null,
314 LocaleConvertUtils.convert(null));
315
316 }
317
318
319 /***
320 * Positive array conversion tests.
321 */
322 public void fixmetestPositiveArray() {
323
324 fail("Array conversions not implemented yet.");
325
326 String values1[] = {"10", "20", "30"};
327 Object value = LocaleConvertUtils.convert(values1, Integer.TYPE);
328 int shape[] = new int[0];
329 assertEquals(shape.getClass(), value.getClass());
330 int results1[] = (int[]) value;
331 assertEquals(results1[0], 10);
332 assertEquals(results1[1], 20);
333 assertEquals(results1[2], 30);
334
335 String values2[] = {"100", "200", "300"};
336 value = LocaleConvertUtils.convert(values2, shape.getClass());
337 assertEquals(shape.getClass(), value.getClass());
338 int results2[] = (int[]) value;
339 assertEquals(results2[0], 100);
340 assertEquals(results2[1], 200);
341 assertEquals(results2[2], 300);
342 }
343
344
345 /***
346 * Positive String to primitive integer array tests.
347 */
348 public void fixmetestPositiveIntegerArray() {
349
350 fail("Array conversions not implemented yet.");
351
352 Object value = null;
353 int intArray[] = new int[0];
354 int intArray1[] = new int[]{0};
355 int intArray2[] = new int[]{0, 10};
356
357 value = LocaleConvertUtils.convert("{ }", intArray.getClass());
358 checkIntegerArray(value, intArray);
359
360 value = LocaleConvertUtils.convert("0", intArray.getClass());
361 checkIntegerArray(value, intArray1);
362 value = LocaleConvertUtils.convert(" 0 ", intArray.getClass());
363 checkIntegerArray(value, intArray1);
364 value = LocaleConvertUtils.convert("{ 0 }", intArray.getClass());
365 checkIntegerArray(value, intArray1);
366
367 value = LocaleConvertUtils.convert("0,10", intArray.getClass());
368 checkIntegerArray(value, intArray2);
369 value = LocaleConvertUtils.convert("0 10", intArray.getClass());
370 checkIntegerArray(value, intArray2);
371 value = LocaleConvertUtils.convert("{0,10}", intArray.getClass());
372 checkIntegerArray(value, intArray2);
373 value = LocaleConvertUtils.convert("{0 10}", intArray.getClass());
374 checkIntegerArray(value, intArray2);
375 value = LocaleConvertUtils.convert("{ 0, 10 }", intArray.getClass());
376 checkIntegerArray(value, intArray2);
377 value = LocaleConvertUtils.convert("{ 0 10 }", intArray.getClass());
378 checkIntegerArray(value, intArray2);
379 }
380
381
382 /***
383 * Positive scalar conversion tests.
384 */
385 public void testPositiveScalar() {
386 Object value = null;
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 value = LocaleConvertUtils.convert("123", Byte.TYPE);
455 assertTrue(value instanceof Byte);
456 assertEquals(((Byte) value).byteValue(), (byte) 123);
457
458 value = LocaleConvertUtils.convert("123", Byte.class);
459 assertTrue(value instanceof Byte);
460 assertEquals(((Byte) value).byteValue(), (byte) 123);
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Double.TYPE);
478 assertTrue(value instanceof Double);
479 assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
480
481 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Double.class);
482 assertTrue(value instanceof Double);
483 assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
484
485 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Float.TYPE);
486 assertTrue(value instanceof Float);
487 assertEquals(((Float) value).floatValue(), (float) 123.456,
488 (float) 0.005);
489
490 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Float.class);
491 assertTrue(value instanceof Float);
492 assertEquals(((Float) value).floatValue(), (float) 123.456,
493 (float) 0.005);
494
495 value = LocaleConvertUtils.convert("123", Integer.TYPE);
496 assertTrue(value instanceof Integer);
497 assertEquals(((Integer) value).intValue(), 123);
498
499 value = LocaleConvertUtils.convert("123", Integer.class);
500 assertTrue(value instanceof Integer);
501 assertEquals(((Integer) value).intValue(), 123);
502
503 value = LocaleConvertUtils.convert("123", Long.TYPE);
504 assertTrue(value instanceof Long);
505 assertEquals(((Long) value).longValue(), 123);
506
507 value = LocaleConvertUtils.convert("123456", Long.class);
508 assertTrue(value instanceof Long);
509 assertEquals(((Long) value).longValue(), 123456);
510
511
512
513
514
515
516
517
518
519
520
521 String input = null;
522
523 input = "2002-03-17";
524 value = LocaleConvertUtils.convert(input, Date.class);
525 assertTrue(value instanceof Date);
526 assertEquals(input, value.toString());
527
528 input = "20:30:40";
529 value = LocaleConvertUtils.convert(input, Time.class);
530 assertTrue(value instanceof Time);
531 assertEquals(input, value.toString());
532
533 input = "2002-03-17 20:30:40.0";
534 value = LocaleConvertUtils.convert(input, Timestamp.class);
535 assertTrue(value instanceof Timestamp);
536 assertEquals(input, value.toString());
537
538 }
539
540
541 /***
542 * Positive String to String array tests.
543 */
544 public void fixmetestPositiveStringArray() {
545
546 fail("Array conversions not implemented yet.");
547
548 Object value = null;
549 String stringArray[] = new String[0];
550 String stringArray1[] = new String[]
551 {"abc"};
552 String stringArray2[] = new String[]
553 {"abc", "de,f"};
554
555 value = LocaleConvertUtils.convert("", stringArray.getClass());
556 checkStringArray(value, stringArray);
557 value = LocaleConvertUtils.convert(" ", stringArray.getClass());
558 checkStringArray(value, stringArray);
559 value = LocaleConvertUtils.convert("{}", stringArray.getClass());
560 checkStringArray(value, stringArray);
561 value = LocaleConvertUtils.convert("{ }", stringArray.getClass());
562 checkStringArray(value, stringArray);
563
564 value = LocaleConvertUtils.convert("abc", stringArray.getClass());
565 checkStringArray(value, stringArray1);
566 value = LocaleConvertUtils.convert("{abc}", stringArray.getClass());
567 checkStringArray(value, stringArray1);
568 value = LocaleConvertUtils.convert("\"abc\"", stringArray.getClass());
569 checkStringArray(value, stringArray1);
570 value = LocaleConvertUtils.convert("{\"abc\"}", stringArray.getClass());
571 checkStringArray(value, stringArray1);
572 value = LocaleConvertUtils.convert("'abc'", stringArray.getClass());
573 checkStringArray(value, stringArray1);
574 value = LocaleConvertUtils.convert("{'abc'}", stringArray.getClass());
575 checkStringArray(value, stringArray1);
576
577 value = LocaleConvertUtils.convert("abc 'de,f'",
578 stringArray.getClass());
579 checkStringArray(value, stringArray2);
580 value = LocaleConvertUtils.convert("{abc, 'de,f'}",
581 stringArray.getClass());
582 checkStringArray(value, stringArray2);
583 value = LocaleConvertUtils.convert("\"abc\",\"de,f\"",
584 stringArray.getClass());
585 checkStringArray(value, stringArray2);
586 value = LocaleConvertUtils.convert("{\"abc\" 'de,f'}",
587 stringArray.getClass());
588 checkStringArray(value, stringArray2);
589 value = LocaleConvertUtils.convert("'abc' 'de,f'",
590 stringArray.getClass());
591 checkStringArray(value, stringArray2);
592 value = LocaleConvertUtils.convert("{'abc', \"de,f\"}",
593 stringArray.getClass());
594 checkStringArray(value, stringArray2);
595
596 }
597
598
599
600
601
602 private void checkIntegerArray(Object value, int intArray[]) {
603
604 assertNotNull("Returned value is not null", value);
605 assertEquals("Returned value is int[]",
606 intArray.getClass(), value.getClass());
607 int results[] = (int[]) value;
608 assertEquals("Returned array length", intArray.length, results.length);
609 for (int i = 0; i < intArray.length; i++) {
610 assertEquals("Returned array value " + i,
611 intArray[i], results[i]);
612 }
613
614 }
615
616
617 private void checkStringArray(Object value, String stringArray[]) {
618
619 assertNotNull("Returned value is not null", value);
620 assertEquals("Returned value is String[]",
621 stringArray.getClass(), value.getClass());
622 String results[] = (String[]) value;
623 assertEquals("Returned array length",
624 stringArray.length, results.length);
625 for (int i = 0; i < stringArray.length; i++) {
626 assertEquals("Returned array value " + i,
627 stringArray[i], results[i]);
628 }
629
630 }
631
632
633 }
634