    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    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      // ---------------------------------------------------- Instance Variables
    47
    48      private char m_decimalSeparator;
    49
    50      // ---------------------------------------------------------- Constructors
    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      // -------------------------------------------------- Overall Test Methods
    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          // could be commas instead of stops in Europe.
    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          // No action required
    96      }
    97
    98
    99      // ------------------------------------------------ Individual Test Methods
    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         /*  fixme Boolean converters not implemented at this point
    139         value = LocaleConvertUtils.convert("foo", Boolean.TYPE);
    140         ...
    141
    142         value = LocaleConvertUtils.convert("foo", Boolean.class);
    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             // Expected result
    152         }
    153
    154         try {
    155             value = LocaleConvertUtils.convert("foo", Byte.class);
    156             fail("Should have thrown conversion exception (2)");
    157         } catch (ConversionException e) {
    158             // Expected result
    159         }
    160
    161         /* fixme - not implemented
    162          try {
    163              value = LocaleConvertUtils.convert("org.apache.commons.beanutils.Undefined", Class.class);
    164              fail("Should have thrown conversion exception");
    165          } catch (ConversionException e) {
    166              ; // Expected result
    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             // Expected result
    175         }
    176
    177         try {
    178             value = LocaleConvertUtils.convert("foo", Double.class);
    179             fail("Should have thrown conversion exception (4)");
    180         } catch (ConversionException e) {
    181             // Expected result
    182         }
    183
    184         try {
    185             value = LocaleConvertUtils.convert("foo", Float.TYPE);
    186             fail("Should have thrown conversion exception (5)");
    187         } catch (ConversionException e) {
    188             // Expected result
    189         }
    190
    191         try {
    192             value = LocaleConvertUtils.convert("foo", Float.class);
    193             fail("Should have thrown conversion exception (6)");
    194         } catch (ConversionException e) {
    195             // Expected result
    196         }
    197
    198         try {
    199             value = LocaleConvertUtils.convert("foo", Integer.TYPE);
    200             fail("Should have thrown conversion exception (7)");
    201         } catch (ConversionException e) {
    202             // Expected result
    203         }
    204
    205         try {
    206             value = LocaleConvertUtils.convert("foo", Integer.class);
    207             fail("Should have thrown conversion exception (8)");
    208         } catch (ConversionException e) {
    209             // Expected result
    210         }
    211
    212         try {
    213             value = LocaleConvertUtils.convert("foo", Byte.TYPE);
    214             fail("Should have thrown conversion exception (9)");
    215         } catch (ConversionException e) {
    216             // Expected result
    217         }
    218
    219         try {
    220             value = LocaleConvertUtils.convert("foo", Long.class);
    221             fail("Should have thrown conversion exception (10)");
    222         } catch (ConversionException e) {
    223             // Expected result
    224         }
    225
    226         try {
    227             value = LocaleConvertUtils.convert("foo", Short.TYPE);
    228             fail("Should have thrown conversion exception (11)");
    229         } catch (ConversionException e) {
    230             // Expected result
    231         }
    232
    233         try {
    234             value = LocaleConvertUtils.convert("foo", Short.class);
    235             fail("Should have thrown conversion exception (12)");
    236         } catch (ConversionException e) {
    237             // Expected result
    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         /* fixme Boolean converters not implemented
    389          value = LocaleConvertUtils.convert("true", Boolean.TYPE);
    390          assertTrue(value instanceof Boolean);
    391          assertEquals(((Boolean) value).booleanValue(), true);
    392
    393          value = LocaleConvertUtils.convert("true", Boolean.class);
    394          assertTrue(value instanceof Boolean);
    395          assertEquals(((Boolean) value).booleanValue(), true);
    396
    397          value = LocaleConvertUtils.convert("yes", Boolean.TYPE);
    398          assertTrue(value instanceof Boolean);
    399          assertEquals(((Boolean) value).booleanValue(), true);
    400
    401          value = LocaleConvertUtils.convert("yes", Boolean.class);
    402          assertTrue(value instanceof Boolean);
    403          assertEquals(((Boolean) value).booleanValue(), true);
    404
    405          value = LocaleConvertUtils.convert("y", Boolean.TYPE);
    406          assertTrue(value instanceof Boolean);
    407          assertEquals(((Boolean) value).booleanValue(), true);
    408
    409          value = LocaleConvertUtils.convert("y", Boolean.class);
    410          assertTrue(value instanceof Boolean);
    411          assertEquals(((Boolean) value).booleanValue(), true);
    412
    413          value = LocaleConvertUtils.convert("on", Boolean.TYPE);
    414          assertTrue(value instanceof Boolean);
    415          assertEquals(((Boolean) value).booleanValue(), true);
    416
    417          value = LocaleConvertUtils.convert("on", Boolean.class);
    418          assertTrue(value instanceof Boolean);
    419          assertEquals(((Boolean) value).booleanValue(), true);
    420
    421          value = LocaleConvertUtils.convert("false", Boolean.TYPE);
    422          assertTrue(value instanceof Boolean);
    423          assertEquals(((Boolean) value).booleanValue(), false);
    424
    425          value = LocaleConvertUtils.convert("false", Boolean.class);
    426          assertTrue(value instanceof Boolean);
    427          assertEquals(((Boolean) value).booleanValue(), false);
    428
    429          value = LocaleConvertUtils.convert("no", Boolean.TYPE);
    430          assertTrue(value instanceof Boolean);
    431          assertEquals(((Boolean) value).booleanValue(), false);
    432
    433          value = LocaleConvertUtils.convert("no", Boolean.class);
    434          assertTrue(value instanceof Boolean);
    435          assertEquals(((Boolean) value).booleanValue(), false);
    436
    437          value = LocaleConvertUtils.convert("n", Boolean.TYPE);
    438          assertTrue(value instanceof Boolean);
    439          assertEquals(((Boolean) value).booleanValue(), false);
    440
    441          value = LocaleConvertUtils.convert("n", Boolean.class);
    442          assertTrue(value instanceof Boolean);
    443          assertEquals(((Boolean) value).booleanValue(), false);
    444
    445          value = LocaleConvertUtils.convert("off", Boolean.TYPE);
    446          assertTrue(value instanceof Boolean);
    447          assertEquals(((Boolean) value).booleanValue(), false);
    448
    449          value = LocaleConvertUtils.convert("off", Boolean.class);
    450          assertTrue(value instanceof Boolean);
    451          assertEquals(((Boolean) value).booleanValue(), false);
    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         /*fixme Character conversion not implemented yet
    463         value = LocaleConvertUtils.convert("a", Character.TYPE);
    464         assertTrue(value instanceof Character);
    465         assertEquals(((Character) value).charValue(), 'a');
    466
    467         value = LocaleConvertUtils.convert("a", Character.class);
    468         assertTrue(value instanceof Character);
    469         assertEquals(((Character) value).charValue(), 'a');
    470         */
    471         /* fixme - this is a discrepancy with standard converters ( probably not major issue )
    472         value = LocaleConvertUtils.convert("java.lang.String", Class.class);
    473         assertTrue(value instanceof Class);
    474         assertEquals(String.class, (Class) value);
    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         /* fixme - Short conversion not implemented at this point
    512         value = LocaleConvertUtils.convert("123", Short.TYPE);
    513         assertTrue(value instanceof Short);
    514         assertEquals(((Short) value).shortValue(), (short) 123);
    515
    516         value = LocaleConvertUtils.convert("123", Short.class);
    517         assertTrue(value instanceof Short);
    518         assertEquals(((Short) value).shortValue(), (short) 123);
    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     // -------------------------------------------------------- Private Methods
    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