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