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