    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.converters;
    19
    20
    21  /***
    22   * Test Case for the IntegerLocaleConverter class.
    23   *
    24   * @author Niall Pemberton
    25   * @version $Revision: 555645 $ $Date: 2007-07-12 15:40:37 +0100 (Thu, 12 Jul 2007) $
    26   */
    27
    28  public class IntegerLocaleConverterTestCase extends BaseLocaleConverterTestCase {
    29
    30
    31
    32      // ---------------------------------------------------------- Constructors
    33
    34      public IntegerLocaleConverterTestCase(String name) {
    35          super(name);
    36      }
    37
    38      // -------------------------------------------------- Overall Test Methods
    39
    40      /***
    41       * Set up instance variables required by this test case.
    42       */
    43      public void setUp() throws Exception {
    44
    45          super.setUp();
    46
    47          defaultValue  = new Integer("999");
    48          expectedValue = new Integer(expectedIntegerValue);
    49
    50      }
    51
    52      /***
    53       * Tear down instance variables required by this test case.
    54       */
    55      public void tearDown() {
    56          super.tearDown();
    57      }
    58
    59
    60      // ------------------------------------------------------------------------
    61
    62      /***
    63       * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
    64       */
    65      public void testConstructorMain() {
    66
    67          // ------------- Construct with localized pattern ------------
    68          converter = new IntegerLocaleConverter(defaultValue,
    69                                                    localizedLocale,
    70                                                    localizedIntegerPattern,
    71                                                    true);
    72
    73
    74          convertValueNoPattern(converter, "(A)", localizedIntegerValue, expectedValue);
    75          convertValueWithPattern(converter, "(A)", localizedIntegerValue, localizedIntegerPattern, expectedValue);
    76          convertInvalid(converter, "(A)", defaultValue);
    77          convertNull(converter, "(A)", defaultValue);
    78
    79
    80          // **************************************************************************
    81          // Convert value in the wrong format - maybe you would expect it to throw an
    82          // exception and return the default - it doesn't, DecimalFormat parses it
    83          // quite happily turning "1,234" into "1"
    84          // I guess this is one of the limitations of DecimalFormat
    85          // **************************************************************************
    86          convertValueNoPattern(converter, "(B)", defaultIntegerValue, new Integer("1"));
    87
    88
    89          // **************************************************************************
    90          // Convert with non-localized pattern - unlike the equivalent BigDecimal Test Case
    91          // it doesn't causes an exception in parse() - DecimalFormat parses it
    92          // quite happily turning "1,234" into "1"
    93          // Again this is one of the limitations of DecimalFormat
    94          // **************************************************************************
    95          convertValueWithPattern(converter, "(B)", localizedIntegerValue, defaultIntegerPattern, new Integer("1"));
    96
    97
    98          // **************************************************************************
    99          // Convert with specified type
    100         //
    101         // BaseLocaleConverter completely ignores the type - so even if we specify
    102         // Double.class here it still returns a Integer.
    103         //  **** SHOULD IMPLEMENT THIS BEHAVIOUR ****
    104         // **************************************************************************
    105         convertValueToType(converter, "(B)", Double.class, localizedIntegerValue, localizedIntegerPattern, expectedValue);
    106
    107
    108         // ------------- Construct with non-localized pattern ------------
    109         converter = new IntegerLocaleConverter(defaultValue,
    110                                                   localizedLocale,
    111                                                   defaultIntegerPattern,
    112                                                   false);
    113
    114
    115         convertValueNoPattern(converter, "(C)", localizedIntegerValue, expectedValue);
    116         convertValueWithPattern(converter, "(C)", localizedIntegerValue, defaultIntegerPattern, expectedValue);
    117         convertInvalid(converter, "(C)", defaultValue);
    118         convertNull(converter, "(C)", defaultValue);
    119
    120     }
    121
    122     /***
    123      * Test Converter() constructor
    124      *
    125      * Uses the default locale, no default value
    126      *
    127      */
    128     public void testConstructor_2() {
    129
    130         // ------------- Construct using default locale ------------
    131         converter = new IntegerLocaleConverter();
    132
    133         // Perform Tests
    134         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
    135         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
    136         convertInvalid(converter, null);
    137         convertNull(converter, null);
    138
    139     }
    140
    141     /***
    142      * Test Converter(locPattern) constructor
    143      *
    144      * Uses the default locale, no default value
    145      *
    146      */
    147     public void testConstructor_3() {
    148
    149         // ------------- Construct using localized pattern (default locale) --------
    150         converter = new IntegerLocaleConverter(true);
    151
    152         // Perform Tests
    153         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
    154         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
    155         convertInvalid(converter, null);
    156         convertNull(converter, null);
    157
    158
    159     }
    160
    161     /***
    162      * Test Converter(Locale) constructor
    163      */
    164     public void testConstructor_4() {
    165
    166         // ------------- Construct using specified Locale --------
    167         converter = new IntegerLocaleConverter(localizedLocale);
    168
    169         // Perform Tests
    170         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
    171         convertValueWithPattern(converter, localizedIntegerValue, defaultIntegerPattern, expectedValue);
    172         convertInvalid(converter, null);
    173         convertNull(converter, null);
    174
    175
    176     }
    177
    178
    179     /***
    180      * Test Converter(Locale, locPattern) constructor
    181      */
    182     public void testConstructor_5() {
    183
    184         // ------------- Construct using specified Locale --------
    185         converter = new IntegerLocaleConverter(localizedLocale, true);
    186
    187         // Perform Tests
    188         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
    189         convertValueWithPattern(converter, localizedIntegerValue, localizedIntegerPattern, expectedValue);
    190         convertInvalid(converter, null);
    191         convertNull(converter, null);
    192
    193
    194     }
    195
    196     /***
    197      * Test Converter(Locale, pattern) constructor
    198      */
    199     public void testConstructor_6() {
    200
    201         // ------------- Construct using specified Locale --------
    202         converter = new IntegerLocaleConverter(localizedLocale, defaultIntegerPattern);
    203
    204         // Perform Tests
    205         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
    206         convertValueWithPattern(converter, localizedIntegerValue, defaultIntegerPattern, expectedValue);
    207         convertInvalid(converter, null);
    208         convertNull(converter, null);
    209
    210     }
    211
    212     /***
    213      * Test Converter(Locale, pattern, locPattern) constructor
    214      */
    215     public void testConstructor_7() {
    216
    217         // ------------- Construct using specified Locale --------
    218         converter = new IntegerLocaleConverter(localizedLocale, localizedIntegerPattern, true);
    219
    220         // Perform Tests
    221         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
    222         convertValueWithPattern(converter, localizedIntegerValue, localizedIntegerPattern, expectedValue);
    223         convertInvalid(converter, null);
    224         convertNull(converter, null);
    225
    226     }
    227
    228     /***
    229      * Test Converter(defaultValue) constructor
    230      */
    231     public void testConstructor_8() {
    232
    233         // ------------- Construct using specified Locale --------
    234         converter = new IntegerLocaleConverter(defaultValue);
    235
    236         // Perform Tests
    237         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
    238         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
    239         convertInvalid(converter, defaultValue);
    240         convertNull(converter, defaultValue);
    241
    242     }
    243
    244     /***
    245      * Test Converter(defaultValue, locPattern) constructor
    246      */
    247     public void testConstructor_9() {
    248
    249         // ------------- Construct using specified Locale --------
    250         converter = new IntegerLocaleConverter(defaultValue, true);
    251
    252         // Perform Tests
    253         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
    254         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
    255         convertInvalid(converter, defaultValue);
    256         convertNull(converter, defaultValue);
    257
    258     }
    259
    260
    261     /***
    262      * Test Converting a number
    263      */
    264     public void testNumber() {
    265
    266         // ------------- Construct using specified Locale --------
    267         converter = new IntegerLocaleConverter(localizedLocale);
    268
    269         Integer value = new Integer(1234);
    270         assertEquals("Convert Integer", value, converter.convert(value));
    271         assertEquals("Convert Long",    value, converter.convert(new Long(value.intValue())));
    272     }
    273
    274
    275 }
    276