    1   /*
    2    * $Id: BigDecimalValidatorTest.java 371174 2006-01-22 03:24:40Z niallp $
    3    * $Revision: 371174 $
    4    * $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
    5    *
    6    * ====================================================================
    7    * Copyright 2006 The Apache Software Foundation
    8    *
    9    * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *     http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   */
    21  package org.apache.commons.validator.routines;
    22
    23  import java.math.BigDecimal;
    24  import java.util.Locale;
    25
    26  /***
    27   * Test Case for BigDecimalValidator.
    28   *
    29   * @version $Revision: 371174 $ $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
    30   */
    31  public class BigDecimalValidatorTest extends BaseNumberValidatorTest {
    32
    33      /***
    34       * Main
    35       * @param args arguments
    36       */
    37      public static void main(String[] args) {
    38          junit.textui.TestRunner.run(BigDecimalValidatorTest.class);
    39      }
    40
    41      /***
    42       * Constructor
    43       * @param name test name
    44       */
    45      public BigDecimalValidatorTest(String name) {
    46          super(name);
    47      }
    48
    49      protected void setUp() throws Exception {
    50          super.setUp();
    51
    52          validator       = new BigDecimalValidator(false);
    53          strictValidator = new BigDecimalValidator();
    54
    55          testPattern = "#,###.###";
    56
    57          // testValidateMinMax()
    58          max = null;
    59          maxPlusOne = null;
    60          min = null;
    61          minMinusOne = null;
    62
    63          // testInvalidStrict()
    64          invalidStrict = new String[] {null, "", "X", "X12", "12X", "1X2", "1.234X"};
    65
    66          // testInvalidNotStrict()
    67          invalid       = new String[] {null, "", "X", "X12"};
    68
    69          // testValid()
    70          testNumber    = new BigDecimal("1234.5");
    71          Number testNumber2 = new BigDecimal(".1");
    72          Number testNumber3 = new BigDecimal("12345.67899");
    73          testZero      = new BigDecimal("0");
    74          validStrict          = new String[] {"0", "1234.5", "1,234.5", ".1", "12345.678990"};
    75          validStrictCompare   = new Number[] {testZero, testNumber, testNumber, testNumber2, testNumber3};
    76          valid                = new String[] {"0", "1234.5", "1,234.5", "1,234.5", "1234.5X"};
    77          validCompare         = new Number[] {testZero, testNumber, testNumber, testNumber, testNumber};
    78
    79          testStringUS = "1,234.5";
    80          testStringDE = "1.234,5";
    81
    82          // Localized Pattern test
    83          localeValue = testStringDE;
    84          localePattern = "#.###,#";
    85          testLocale    = Locale.GERMANY;
    86          localeExpected = testNumber;
    87
    88      }
    89
    90      /***
    91       * Test BigDecimalValidator validate Methods
    92       */
    93      public void testBigDecimalValidatorMethods() {
    94          Locale locale           = Locale.GERMAN;
    95          String pattern          = "0,00,00";
    96          String patternVal       = "1,23,45";
    97          String germanPatternVal = "1.23.45";
    98          String localeVal        = "12.345";
    99          String defaultVal       = "12,345";
    100         String XXXX             = "XXXX";
    101         BigDecimal expected = new BigDecimal(12345);
    102         assertEquals("validate(A) default", expected, BigDecimalValidator.getInstance().validate(defaultVal));
    103         assertEquals("validate(A) locale ", expected, BigDecimalValidator.getInstance().validate(localeVal, locale));
    104         assertEquals("validate(A) pattern", expected, BigDecimalValidator.getInstance().validate(patternVal, pattern));
    105         assertEquals("validate(A) both",    expected, BigDecimalValidator.getInstance().validate(germanPatternVal, pattern, Locale.GERMAN));
    106
    107         assertTrue("isValid(A) default", BigDecimalValidator.getInstance().isValid(defaultVal));
    108         assertTrue("isValid(A) locale ", BigDecimalValidator.getInstance().isValid(localeVal, locale));
    109         assertTrue("isValid(A) pattern", BigDecimalValidator.getInstance().isValid(patternVal, pattern));
    110         assertTrue("isValid(A) both",    BigDecimalValidator.getInstance().isValid(germanPatternVal, pattern, Locale.GERMAN));
    111
    112         assertNull("validate(B) default", BigDecimalValidator.getInstance().validate(XXXX));
    113         assertNull("validate(B) locale ", BigDecimalValidator.getInstance().validate(XXXX, locale));
    114         assertNull("validate(B) pattern", BigDecimalValidator.getInstance().validate(XXXX, pattern));
    115         assertNull("validate(B) both",    BigDecimalValidator.getInstance().validate(patternVal, pattern, Locale.GERMAN));
    116
    117         assertFalse("isValid(B) default", BigDecimalValidator.getInstance().isValid(XXXX));
    118         assertFalse("isValid(B) locale ", BigDecimalValidator.getInstance().isValid(XXXX, locale));
    119         assertFalse("isValid(B) pattern", BigDecimalValidator.getInstance().isValid(XXXX, pattern));
    120         assertFalse("isValid(B) both",    BigDecimalValidator.getInstance().isValid(patternVal, pattern, Locale.GERMAN));
    121     }
    122
    123     /***
    124      * Test BigDecimal Range/Min/Max
    125      */
    126     public void testBigDecimalRangeMinMax() {
    127         BigDecimalValidator validator = new BigDecimalValidator(true, AbstractNumberValidator.STANDARD_FORMAT, true);
    128         BigDecimal number9  = new BigDecimal("9");
    129         BigDecimal number10 = new BigDecimal("10");
    130         BigDecimal number11 = new BigDecimal("11");
    131         BigDecimal number19 = new BigDecimal("19");
    132         BigDecimal number20 = new BigDecimal("20");
    133         BigDecimal number21 = new BigDecimal("21");
    134
    135         float min = (float)10;
    136         float max = (float)20;
    137
    138         // Test isInRange()
    139         assertFalse("isInRange(A) < min",   validator.isInRange(number9,  min, max));
    140         assertTrue("isInRange(A) = min",    validator.isInRange(number10, min, max));
    141         assertTrue("isInRange(A) in range", validator.isInRange(number11, min, max));
    142         assertTrue("isInRange(A) = max",    validator.isInRange(number20, min, max));
    143         assertFalse("isInRange(A) > max",   validator.isInRange(number21, min, max));
    144
    145         // Test minValue()
    146         assertFalse("minValue(A) < min",    validator.minValue(number9,  min));
    147         assertTrue("minValue(A) = min",     validator.minValue(number10, min));
    148         assertTrue("minValue(A) > min",     validator.minValue(number11, min));
    149
    150         // Test minValue()
    151         assertTrue("maxValue(A) < max",     validator.maxValue(number19, max));
    152         assertTrue("maxValue(A) = max",     validator.maxValue(number20, max));
    153         assertFalse("maxValue(A) > max",    validator.maxValue(number21, max));
    154     }
    155 }