    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.BigDecimal;
    21
    22  /***
    23   * Test Case for the BigDecimalLocaleConverter 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 BigDecimalLocaleConverterTestCase extends BaseLocaleConverterTestCase {
    30
    31
    32
    33      // ---------------------------------------------------------- Constructors
    34
    35      public BigDecimalLocaleConverterTestCase(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 BigDecimal("9.99");
    49          expectedValue = new BigDecimal(expectedDecimalValue);
    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 BigDecimalLocaleConverter(defaultValue,
    70                                                    localizedLocale,
    71                                                    localizedDecimalPattern,
    72                                                    true);
    73
    74
    75          convertValueNoPattern(converter, "(A)", localizedDecimalValue, expectedValue);
    76          convertValueWithPattern(converter, "(A)", localizedDecimalValue, localizedDecimalPattern, 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.56" into "1.234"
    85          // I guess this is one of the limitations of DecimalFormat
    86          // **************************************************************************
    87          convertValueNoPattern(converter, "(B)", defaultDecimalValue, new BigDecimal("1.234"));
    88
    89
    90          // **************************************************************************
    91          // Convert with non-localized pattern - this causes an exception in parse()
    92          // but it gets swallowed in convert() method and returns default.
    93          //  **** IS THIS THE EXPECTED BEHAVIOUR? ****
    94          // Maybe if the pattern is no good, we should use a default pattern rather
    95          // than just returning the default value.
    96          // **************************************************************************
    97          convertValueWithPattern(converter, "(B)", localizedDecimalValue, defaultDecimalPattern, defaultValue);
    98
    99
    100         // **************************************************************************
    101         // Convert with specified type
    102         //
    103         // BaseLocaleConverter completely ignores the type - so even if we specify
    104         // Double.class here it still returns a BigDecimal.
    105         //  **** SHOULD IMPLEMENT THIS BEHAVIOUR ****
    106         // **************************************************************************
    107         convertValueToType(converter, "(B)", Double.class, localizedDecimalValue, localizedDecimalPattern, expectedValue);
    108
    109
    110         // ------------- Construct with non-localized pattern ------------
    111         converter = new BigDecimalLocaleConverter(defaultValue,
    112                                                   localizedLocale,
    113                                                   defaultDecimalPattern,
    114                                                   false);
    115
    116
    117         convertValueNoPattern(converter, "(C)", localizedDecimalValue, expectedValue);
    118         convertValueWithPattern(converter, "(C)", localizedDecimalValue, defaultDecimalPattern, expectedValue);
    119         convertInvalid(converter, "(C)", defaultValue);
    120         convertNull(converter, "(C)", defaultValue);
    121
    122     }
    123
    124     /***
    125      * Test Converter() constructor
    126      *
    127      * Uses the default locale, no default value
    128      *
    129      */
    130     public void testConstructor_2() {
    131
    132         // ------------- Construct using default locale ------------
    133         converter = new BigDecimalLocaleConverter();
    134
    135         // Perform Tests
    136         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
    137         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
    138         convertInvalid(converter, null);
    139         convertNull(converter, null);
    140
    141     }
    142
    143     /***
    144      * Test Converter(locPattern) constructor
    145      *
    146      * Uses the default locale, no default value
    147      *
    148      */
    149     public void testConstructor_3() {
    150
    151         // ------------- Construct using localized pattern (default locale) --------
    152         converter = new BigDecimalLocaleConverter(true);
    153
    154         // Perform Tests
    155         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
    156         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
    157         convertInvalid(converter, null);
    158         convertNull(converter, null);
    159
    160
    161     }
    162
    163     /***
    164      * Test Converter(Locale) constructor
    165      */
    166     public void testConstructor_4() {
    167
    168         // ------------- Construct using specified Locale --------
    169         converter = new BigDecimalLocaleConverter(localizedLocale);
    170
    171         // Perform Tests
    172         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
    173         convertValueWithPattern(converter, localizedDecimalValue, defaultDecimalPattern, expectedValue);
    174         convertInvalid(converter, null);
    175         convertNull(converter, null);
    176
    177
    178     }
    179
    180
    181     /***
    182      * Test Converter(Locale, locPattern) constructor
    183      */
    184     public void testConstructor_5() {
    185
    186         // ------------- Construct using specified Locale --------
    187         converter = new BigDecimalLocaleConverter(localizedLocale, true);
    188
    189         // Perform Tests
    190         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
    191         convertValueWithPattern(converter, localizedDecimalValue, localizedDecimalPattern, expectedValue);
    192         convertInvalid(converter, null);
    193         convertNull(converter, null);
    194
    195
    196     }
    197
    198     /***
    199      * Test Converter(Locale, pattern) constructor
    200      */
    201     public void testConstructor_6() {
    202
    203         // ------------- Construct using specified Locale --------
    204         converter = new BigDecimalLocaleConverter(localizedLocale, defaultDecimalPattern);
    205
    206         // Perform Tests
    207         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
    208         convertValueWithPattern(converter, localizedDecimalValue, defaultDecimalPattern, expectedValue);
    209         convertInvalid(converter, null);
    210         convertNull(converter, null);
    211
    212     }
    213
    214     /***
    215      * Test Converter(Locale, pattern, locPattern) constructor
    216      */
    217     public void testConstructor_7() {
    218
    219         // ------------- Construct using specified Locale --------
    220         converter = new BigDecimalLocaleConverter(localizedLocale, localizedDecimalPattern, true);
    221
    222         // Perform Tests
    223         convertValueNoPattern(converter, localizedDecimalValue, expectedValue);
    224         convertValueWithPattern(converter, localizedDecimalValue, localizedDecimalPattern, expectedValue);
    225         convertInvalid(converter, null);
    226         convertNull(converter, null);
    227
    228     }
    229
    230     /***
    231      * Test Converter(defaultValue) constructor
    232      */
    233     public void testConstructor_8() {
    234
    235         // ------------- Construct using specified Locale --------
    236         converter = new BigDecimalLocaleConverter(defaultValue);
    237
    238         // Perform Tests
    239         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
    240         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
    241         convertInvalid(converter, defaultValue);
    242         convertNull(converter, defaultValue);
    243
    244     }
    245
    246     /***
    247      * Test Converter(defaultValue, locPattern) constructor
    248      */
    249     public void testConstructor_9() {
    250
    251         // ------------- Construct using specified Locale --------
    252         converter = new BigDecimalLocaleConverter(defaultValue, true);
    253
    254         // Perform Tests
    255         convertValueNoPattern(converter, defaultDecimalValue, expectedValue);
    256         convertValueWithPattern(converter, defaultDecimalValue, defaultDecimalPattern, expectedValue);
    257         convertInvalid(converter, defaultValue);
    258         convertNull(converter, defaultValue);
    259
    260     }
    261
    262
    263
    264 }
    265