1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.sql.Date;
22 import java.sql.Time;
23 import java.sql.Timestamp;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Locale;
27 import org.apache.commons.beanutils.converters.DateConverter;
28 import junit.framework.TestCase;
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32
33
34
35
36
37
38
39
40
41
42 public class ConvertUtilsTestCase extends TestCase {
43
44
45
46
47
48
49
50
51
52
53
54
55 public ConvertUtilsTestCase(String name) {
56 super(name);
57 }
58
59
60
61
62
63
64
65
66 public void setUp() {
67
68 ConvertUtils.deregister();
69
70 }
71
72
73
74
75
76 public static Test suite() {
77 return (new TestSuite(ConvertUtilsTestCase.class));
78 }
79
80
81
82
83
84 public void tearDown() {
85
86 }
87
88
89
90
91
92
93
94
95 public void testNegativeIntegerArray() {
96
97 Object value = null;
98 int intArray[] = new int[0];
99
100 value = ConvertUtils.convert((String) null, intArray.getClass());
101 checkIntegerArray(value, intArray);
102 value = ConvertUtils.convert("a", intArray.getClass());
103 checkIntegerArray(value, intArray);
104 value = ConvertUtils.convert("{ a }", intArray.getClass());
105 checkIntegerArray(value, intArray);
106 value = ConvertUtils.convert("1a3", intArray.getClass());
107 checkIntegerArray(value, intArray);
108 value = ConvertUtils.convert("{ 1a3 }", intArray.getClass());
109 checkIntegerArray(value, intArray);
110 value = ConvertUtils.convert("0,1a3", intArray.getClass());
111 checkIntegerArray(value, intArray);
112 value = ConvertUtils.convert("{ 0, 1a3 }", intArray.getClass());
113 checkIntegerArray(value, intArray);
114
115
116 }
117
118
119
120
121
122
123 public void testNegativeScalar() {
124
125 Object value = null;
126
127 value = ConvertUtils.convert("foo", Boolean.TYPE);
128 assertTrue(value instanceof Boolean);
129 assertEquals(((Boolean) value).booleanValue(), false);
130
131 value = ConvertUtils.convert("foo", Boolean.class);
132 assertTrue(value instanceof Boolean);
133 assertEquals(((Boolean) value).booleanValue(), false);
134
135 value = ConvertUtils.convert("foo", Byte.TYPE);
136 assertTrue(value instanceof Byte);
137 assertEquals(((Byte) value).byteValue(), (byte) 0);
138
139 value = ConvertUtils.convert("foo", Byte.class);
140 assertTrue(value instanceof Byte);
141 assertEquals(((Byte) value).byteValue(), (byte) 0);
142
143 try {
144 value = ConvertUtils.convert
145 ("org.apache.commons.beanutils.Undefined", Class.class);
146 fail("Should have thrown conversion exception");
147 } catch (ConversionException e) {
148
149 }
150
151 value = ConvertUtils.convert("foo", Double.TYPE);
152 assertTrue(value instanceof Double);
153 assertEquals(((Double) value).doubleValue(), 0.0,
154 0.005);
155
156 value = ConvertUtils.convert("foo", Double.class);
157 assertTrue(value instanceof Double);
158 assertEquals(((Double) value).doubleValue(), 0.0, 0.005);
159
160 value = ConvertUtils.convert("foo", Float.TYPE);
161 assertTrue(value instanceof Float);
162 assertEquals(((Float) value).floatValue(), (float) 0.0,
163 (float) 0.005);
164
165 value = ConvertUtils.convert("foo", Float.class);
166 assertTrue(value instanceof Float);
167 assertEquals(((Float) value).floatValue(), (float) 0.0,
168 (float) 0.005);
169
170 value = ConvertUtils.convert("foo", Integer.TYPE);
171 assertTrue(value instanceof Integer);
172 assertEquals(((Integer) value).intValue(), 0);
173
174 value = ConvertUtils.convert("foo", Integer.class);
175 assertTrue(value instanceof Integer);
176 assertEquals(((Integer) value).intValue(), 0);
177
178 value = ConvertUtils.convert("foo", Byte.TYPE);
179 assertTrue(value instanceof Byte);
180 assertEquals(((Byte) value).byteValue(), (byte) 0);
181
182 value = ConvertUtils.convert("foo", Long.class);
183 assertTrue(value instanceof Long);
184 assertEquals(((Long) value).longValue(), 0);
185
186 value = ConvertUtils.convert("foo", Short.TYPE);
187 assertTrue(value instanceof Short);
188 assertEquals(((Short) value).shortValue(), (short) 0);
189
190 value = ConvertUtils.convert("foo", Short.class);
191 assertTrue(value instanceof Short);
192 assertEquals(((Short) value).shortValue(), (short) 0);
193
194 }
195
196
197
198
199
200 public void testNegativeStringArray() {
201
202 Object value = null;
203 String stringArray[] = new String[0];
204
205 value = ConvertUtils.convert((String) null, stringArray.getClass());
206 checkStringArray(value, stringArray);
207
208 }
209
210
211
212
213
214 public void testObjectToStringArray() {
215
216 int intArray0[] = new int[0];
217 int intArray1[] = { 123 };
218 int intArray2[] = { 123, 456 };
219 String stringArray0[] = new String[0];
220 String stringArray1[] = { "abc" };
221 String stringArray2[] = { "abc", "def" };
222
223 assertEquals("intArray0", null,
224 ConvertUtils.convert(intArray0));
225 assertEquals("intArray1", "123",
226 ConvertUtils.convert(intArray1));
227 assertEquals("intArray2", "123",
228 ConvertUtils.convert(intArray2));
229
230 assertEquals("stringArray0", null,
231 ConvertUtils.convert(stringArray0));
232 assertEquals("stringArray1", "abc",
233 ConvertUtils.convert(stringArray1));
234 assertEquals("stringArray2", "abc",
235 ConvertUtils.convert(stringArray2));
236
237 }
238
239
240
241
242
243 public void testObjectToStringScalar() {
244
245 assertEquals("Boolean->String", "false",
246 ConvertUtils.convert(Boolean.FALSE));
247 assertEquals("Boolean->String", "true",
248 ConvertUtils.convert(Boolean.TRUE));
249 assertEquals("Byte->String", "123",
250 ConvertUtils.convert(new Byte((byte) 123)));
251 assertEquals("Character->String", "a",
252 ConvertUtils.convert(new Character('a')));
253 assertEquals("Double->String", "123.0",
254 ConvertUtils.convert(new Double(123.0)));
255 assertEquals("Float->String", "123.0",
256 ConvertUtils.convert(new Float((float) 123.0)));
257 assertEquals("Integer->String", "123",
258 ConvertUtils.convert(new Integer(123)));
259 assertEquals("Long->String", "123",
260 ConvertUtils.convert(new Long(123)));
261 assertEquals("Short->String", "123",
262 ConvertUtils.convert(new Short((short) 123)));
263 assertEquals("String->String", "abc",
264 ConvertUtils.convert("abc"));
265 assertEquals("String->String null", null,
266 ConvertUtils.convert(null));
267
268 }
269
270
271
272
273
274 public void testPositiveArray() {
275
276 String values1[] = { "10", "20", "30" };
277 Object value = ConvertUtils.convert(values1, Integer.TYPE);
278 int shape[] = new int[0];
279 assertEquals(shape.getClass(), value.getClass());
280 int results1[] = (int[]) value;
281 assertEquals(results1[0], 10);
282 assertEquals(results1[1], 20);
283 assertEquals(results1[2], 30);
284
285 String values2[] = { "100", "200", "300" };
286 value = ConvertUtils.convert(values2, shape.getClass());
287 assertEquals(shape.getClass(), value.getClass());
288 int results2[] = (int[]) value;
289 assertEquals(results2[0], 100);
290 assertEquals(results2[1], 200);
291 assertEquals(results2[2], 300);
292
293 }
294
295
296
297
298
299 public void testPositiveIntegerArray() {
300
301 Object value = null;
302 int intArray[] = new int[0];
303 int intArray1[] = new int[] { 0 };
304 int intArray2[] = new int[] { 0, 10 };
305
306 value = ConvertUtils.convert("{ }", intArray.getClass());
307 checkIntegerArray(value, intArray);
308
309 value = ConvertUtils.convert("0", intArray.getClass());
310 checkIntegerArray(value, intArray1);
311 value = ConvertUtils.convert(" 0 ", intArray.getClass());
312 checkIntegerArray(value, intArray1);
313 value = ConvertUtils.convert("{ 0 }", intArray.getClass());
314 checkIntegerArray(value, intArray1);
315
316 value = ConvertUtils.convert("0,10", intArray.getClass());
317 checkIntegerArray(value, intArray2);
318 value = ConvertUtils.convert("0 10", intArray.getClass());
319 checkIntegerArray(value, intArray2);
320 value = ConvertUtils.convert("{0,10}", intArray.getClass());
321 checkIntegerArray(value, intArray2);
322 value = ConvertUtils.convert("{0 10}", intArray.getClass());
323 checkIntegerArray(value, intArray2);
324 value = ConvertUtils.convert("{ 0, 10 }", intArray.getClass());
325 checkIntegerArray(value, intArray2);
326 value = ConvertUtils.convert("{ 0 10 }", intArray.getClass());
327 checkIntegerArray(value, intArray2);
328
329 }
330
331
332
333
334
335 public void testPositiveScalar() {
336
337 Object value = null;
338
339 value = ConvertUtils.convert("true", Boolean.TYPE);
340 assertTrue(value instanceof Boolean);
341 assertEquals(((Boolean) value).booleanValue(), true);
342
343 value = ConvertUtils.convert("true", Boolean.class);
344 assertTrue(value instanceof Boolean);
345 assertEquals(((Boolean) value).booleanValue(), true);
346
347 value = ConvertUtils.convert("yes", Boolean.TYPE);
348 assertTrue(value instanceof Boolean);
349 assertEquals(((Boolean) value).booleanValue(), true);
350
351 value = ConvertUtils.convert("yes", Boolean.class);
352 assertTrue(value instanceof Boolean);
353 assertEquals(((Boolean) value).booleanValue(), true);
354
355 value = ConvertUtils.convert("y", Boolean.TYPE);
356 assertTrue(value instanceof Boolean);
357 assertEquals(((Boolean) value).booleanValue(), true);
358
359 value = ConvertUtils.convert("y", Boolean.class);
360 assertTrue(value instanceof Boolean);
361 assertEquals(((Boolean) value).booleanValue(), true);
362
363 value = ConvertUtils.convert("on", Boolean.TYPE);
364 assertTrue(value instanceof Boolean);
365 assertEquals(((Boolean) value).booleanValue(), true);
366
367 value = ConvertUtils.convert("on", Boolean.class);
368 assertTrue(value instanceof Boolean);
369 assertEquals(((Boolean) value).booleanValue(), true);
370
371 value = ConvertUtils.convert("false", Boolean.TYPE);
372 assertTrue(value instanceof Boolean);
373 assertEquals(((Boolean) value).booleanValue(), false);
374
375 value = ConvertUtils.convert("false", Boolean.class);
376 assertTrue(value instanceof Boolean);
377 assertEquals(((Boolean) value).booleanValue(), false);
378
379 value = ConvertUtils.convert("no", Boolean.TYPE);
380 assertTrue(value instanceof Boolean);
381 assertEquals(((Boolean) value).booleanValue(), false);
382
383 value = ConvertUtils.convert("no", Boolean.class);
384 assertTrue(value instanceof Boolean);
385 assertEquals(((Boolean) value).booleanValue(), false);
386
387 value = ConvertUtils.convert("n", Boolean.TYPE);
388 assertTrue(value instanceof Boolean);
389 assertEquals(((Boolean) value).booleanValue(), false);
390
391 value = ConvertUtils.convert("n", Boolean.class);
392 assertTrue(value instanceof Boolean);
393 assertEquals(((Boolean) value).booleanValue(), false);
394
395 value = ConvertUtils.convert("off", Boolean.TYPE);
396 assertTrue(value instanceof Boolean);
397 assertEquals(((Boolean) value).booleanValue(), false);
398
399 value = ConvertUtils.convert("off", Boolean.class);
400 assertTrue(value instanceof Boolean);
401 assertEquals(((Boolean) value).booleanValue(), false);
402
403 value = ConvertUtils.convert("123", Byte.TYPE);
404 assertTrue(value instanceof Byte);
405 assertEquals(((Byte) value).byteValue(), (byte) 123);
406
407 value = ConvertUtils.convert("123", Byte.class);
408 assertTrue(value instanceof Byte);
409 assertEquals(((Byte) value).byteValue(), (byte) 123);
410
411 value = ConvertUtils.convert("a", Character.TYPE);
412 assertTrue(value instanceof Character);
413 assertEquals(((Character) value).charValue(), 'a');
414
415 value = ConvertUtils.convert("a", Character.class);
416 assertTrue(value instanceof Character);
417 assertEquals(((Character) value).charValue(), 'a');
418
419 value = ConvertUtils.convert("java.lang.String", Class.class);
420 assertTrue(value instanceof Class);
421 assertEquals(String.class, value);
422
423 value = ConvertUtils.convert("123.456", Double.TYPE);
424 assertTrue(value instanceof Double);
425 assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
426
427 value = ConvertUtils.convert("123.456", Double.class);
428 assertTrue(value instanceof Double);
429 assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
430
431 value = ConvertUtils.convert("123.456", Float.TYPE);
432 assertTrue(value instanceof Float);
433 assertEquals(((Float) value).floatValue(), (float) 123.456,
434 (float) 0.005);
435
436 value = ConvertUtils.convert("123.456", Float.class);
437 assertTrue(value instanceof Float);
438 assertEquals(((Float) value).floatValue(), (float) 123.456,
439 (float) 0.005);
440
441 value = ConvertUtils.convert("123", Integer.TYPE);
442 assertTrue(value instanceof Integer);
443 assertEquals(((Integer) value).intValue(), 123);
444
445 value = ConvertUtils.convert("123", Integer.class);
446 assertTrue(value instanceof Integer);
447 assertEquals(((Integer) value).intValue(), 123);
448
449 value = ConvertUtils.convert("123", Long.TYPE);
450 assertTrue(value instanceof Long);
451 assertEquals(((Long) value).longValue(), 123);
452
453 value = ConvertUtils.convert("123", Long.class);
454 assertTrue(value instanceof Long);
455 assertEquals(((Long) value).longValue(), 123);
456
457 value = ConvertUtils.convert("123", Short.TYPE);
458 assertTrue(value instanceof Short);
459 assertEquals(((Short) value).shortValue(), (short) 123);
460
461 value = ConvertUtils.convert("123", Short.class);
462 assertTrue(value instanceof Short);
463 assertEquals(((Short) value).shortValue(), (short) 123);
464
465 String input = null;
466
467 input = "2002-03-17";
468 value = ConvertUtils.convert(input, Date.class);
469 assertTrue(value instanceof Date);
470 assertEquals(input, value.toString());
471
472 input = "20:30:40";
473 value = ConvertUtils.convert(input, Time.class);
474 assertTrue(value instanceof Time);
475 assertEquals(input, value.toString());
476
477 input = "2002-03-17 20:30:40.0";
478 value = ConvertUtils.convert(input, Timestamp.class);
479 assertTrue(value instanceof Timestamp);
480 assertEquals(input, value.toString());
481
482 }
483
484
485
486
487
488 public void testPositiveStringArray() {
489
490 Object value = null;
491 String stringArray[] = new String[0];
492 String stringArray1[] = new String[]
493 { "abc" };
494 String stringArray2[] = new String[]
495 { "abc", "de,f" };
496
497 value = ConvertUtils.convert("", stringArray.getClass());
498 checkStringArray(value, stringArray);
499 value = ConvertUtils.convert(" ", stringArray.getClass());
500 checkStringArray(value, stringArray);
501 value = ConvertUtils.convert("{}", stringArray.getClass());
502 checkStringArray(value, stringArray);
503 value = ConvertUtils.convert("{ }", stringArray.getClass());
504 checkStringArray(value, stringArray);
505
506 value = ConvertUtils.convert("abc", stringArray.getClass());
507 checkStringArray(value, stringArray1);
508 value = ConvertUtils.convert("{abc}", stringArray.getClass());
509 checkStringArray(value, stringArray1);
510 value = ConvertUtils.convert("\"abc\"", stringArray.getClass());
511 checkStringArray(value, stringArray1);
512 value = ConvertUtils.convert("{\"abc\"}", stringArray.getClass());
513 checkStringArray(value, stringArray1);
514 value = ConvertUtils.convert("'abc'", stringArray.getClass());
515 checkStringArray(value, stringArray1);
516 value = ConvertUtils.convert("{'abc'}", stringArray.getClass());
517 checkStringArray(value, stringArray1);
518
519 value = ConvertUtils.convert("abc 'de,f'",
520 stringArray.getClass());
521 checkStringArray(value, stringArray2);
522 value = ConvertUtils.convert("{abc, 'de,f'}",
523 stringArray.getClass());
524 checkStringArray(value, stringArray2);
525 value = ConvertUtils.convert("\"abc\",\"de,f\"",
526 stringArray.getClass());
527 checkStringArray(value, stringArray2);
528 value = ConvertUtils.convert("{\"abc\" 'de,f'}",
529 stringArray.getClass());
530 checkStringArray(value, stringArray2);
531 value = ConvertUtils.convert("'abc' 'de,f'",
532 stringArray.getClass());
533 checkStringArray(value, stringArray2);
534 value = ConvertUtils.convert("{'abc', \"de,f\"}",
535 stringArray.getClass());
536 checkStringArray(value, stringArray2);
537
538
539 }
540
541 public void testSeparateConvertInstances() throws Exception {
542 ConvertUtilsBean utilsOne = new ConvertUtilsBean();
543 ConvertUtilsBean utilsTwo = new ConvertUtilsBean();
544
545
546 Object
547 value = utilsOne.convert("true", Boolean.TYPE);
548 assertTrue(value instanceof Boolean);
549 assertEquals(
550 "Standard conversion failed (1)",
551 ((Boolean) value).booleanValue(),
552 true);
553
554 value = utilsTwo.convert("true", Boolean.TYPE);
555 assertTrue(value instanceof Boolean);
556 assertEquals(
557 "Standard conversion failed (2)",
558 ((Boolean) value).booleanValue(),
559 true);
560
561
562
563 utilsOne.register(new ThrowExceptionConverter(), Boolean.TYPE);
564 try {
565
566 utilsOne.convert("true", Boolean.TYPE);
567 fail("Register converter failed.");
568
569 } catch (PassTestException e) {
570
571 try {
572
573 value = utilsTwo.convert("true", Boolean.TYPE);
574 assertTrue(value instanceof Boolean);
575 assertEquals(
576 "Standard conversion failed (3)",
577 ((Boolean) value).booleanValue(),
578 true);
579
580 } catch (PassTestException e) {
581
582
583 fail("Registering a converter for an instance should not effect another instance.");
584 }
585
586
587 utilsOne.deregister();
588 value = utilsOne.convert("true", Boolean.TYPE);
589 assertTrue(value instanceof Boolean);
590 assertEquals("Instance deregister failed.", ((Boolean) value).booleanValue(), true);
591
592 value = utilsTwo.convert("true", Boolean.TYPE);
593 assertTrue(value instanceof Boolean);
594 assertEquals(
595 "Standard conversion failed (4)",
596 ((Boolean) value).booleanValue(),
597 true);
598 }
599
600 public void testDeregisteringSingleConverter() throws Exception {
601
602 Object
603 value = ConvertUtils.convert("true", Boolean.TYPE);
604 assertTrue(value instanceof Boolean);
605 assertEquals(
606 "Standard conversion failed (1)",
607 ((Boolean) value).booleanValue(),
608 true);
609
610
611 ConvertUtils.deregister(Boolean.TYPE);
612 assertNull("Converter should be null",ConvertUtils.lookup(Boolean.TYPE));
613
614 }
615
616 public void testConvertToString() throws Exception {
617 Converter dummyConverter = new Converter() {
618 public Object convert(Class type, Object value) {
619 return value;
620 }
621 };
622
623 Converter fooConverter = new Converter() {
624 public Object convert(Class type, Object value) {
625 return "Foo-Converter";
626 }
627 };
628
629 DateConverter dateConverter = new DateConverter();
630 dateConverter.setLocale(Locale.US);
631
632 ConvertUtilsBean utils = new ConvertUtilsBean();
633 utils.register(dateConverter, java.util.Date.class);
634 utils.register(fooConverter, String.class);
635
636
637 java.util.Date today = new java.util.Date();
638 DateFormat fmt = new SimpleDateFormat("M/d/yy");
639 String expected = fmt.format(today);
640 assertEquals("DateConverter M/d/yy", expected, utils.convert(today, String.class));
641
642
643 utils.register(dummyConverter, java.util.Date.class);
644 assertEquals("Date Converter doesn't do String conversion", "Foo-Converter", utils.convert(today, String.class));
645
646
647 utils.deregister(java.util.Date.class);
648 assertEquals("No registered Date converter", "Foo-Converter", utils.convert(today, String.class));
649
650
651 utils.register(dummyConverter, String.class);
652 assertEquals("String Converter doesn't do Strings!!!", today.toString(), utils.convert(today, String.class));
653
654
655 utils.deregister(String.class);
656 assertEquals("Object's toString()", today.toString(), utils.convert(today, String.class));
657
658 }
659
660
661
662
663 private void checkIntegerArray(Object value, int intArray[]) {
664
665 assertNotNull("Returned value is not null", value);
666 assertEquals("Returned value is int[]",
667 intArray.getClass(), value.getClass());
668 int results[] = (int[]) value;
669 assertEquals("Returned array length", intArray.length, results.length);
670 for (int i = 0; i < intArray.length; i++) {
671 assertEquals("Returned array value " + i,
672 intArray[i], results[i]);
673 }
674
675 }
676
677
678 private void checkStringArray(Object value, String stringArray[]) {
679
680 assertNotNull("Returned value is not null", value);
681 assertEquals("Returned value is String[]",
682 stringArray.getClass(), value.getClass());
683 String results[] = (String[]) value;
684 assertEquals("Returned array length",
685 stringArray.length, results.length);
686 for (int i = 0; i < stringArray.length; i++) {
687 assertEquals("Returned array value " + i,
688 stringArray[i], results[i]);
689 }
690
691 }
692
693
694 }
695