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