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