    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  package org.apache.commons.validator.routines;
    18
    19  import java.math.BigDecimal;
    20  import java.util.Locale;
    21
    22  /***
    23   * Test Case for BigDecimalValidator.
    24   *
    25   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
    26   */
    27  public class BigDecimalValidatorTest extends BaseNumberValidatorTest {
    28
    29      /***
    30       * Main
    31       * @param args arguments
    32       */
    33      public static void main(String[] args) {
    34          junit.textui.TestRunner.run(BigDecimalValidatorTest.class);
    35      }
    36
    37      /***
    38       * Constructor
    39       * @param name test name
    40       */
    41      public BigDecimalValidatorTest(String name) {
    42          super(name);
    43      }
    44
    45      protected void setUp() throws Exception {
    46          super.setUp();
    47
    48          validator       = new BigDecimalValidator(false);
    49          strictValidator = new BigDecimalValidator();
    50
    51          testPattern = "#,###.###";
    52
    53          // testValidateMinMax()
    54          max = null;
    55          maxPlusOne = null;
    56          min = null;
    57          minMinusOne = null;
    58
    59          // testInvalidStrict()
    60          invalidStrict = new String[] {null, "", "X", "X12", "12X", "1X2", "1.234X"};
    61
    62          // testInvalidNotStrict()
    63          invalid       = new String[] {null, "", "X", "X12"};
    64
    65          // testValid()
    66          testNumber    = new BigDecimal("1234.5");
    67          Number testNumber2 = new BigDecimal(".1");
    68          Number testNumber3 = new BigDecimal("12345.67899");
    69          testZero      = new BigDecimal("0");
    70          validStrict          = new String[] {"0", "1234.5", "1,234.5", ".1", "12345.678990"};
    71          validStrictCompare   = new Number[] {testZero, testNumber, testNumber, testNumber2, testNumber3};
    72          valid                = new String[] {"0", "1234.5", "1,234.5", "1,234.5", "1234.5X"};
    73          validCompare         = new Number[] {testZero, testNumber, testNumber, testNumber, testNumber};
    74
    75          testStringUS = "1,234.5";
    76          testStringDE = "1.234,5";
    77
    78          // Localized Pattern test
    79          localeValue = testStringDE;
    80          localePattern = "#.###,#";
    81          testLocale    = Locale.GERMANY;
    82          localeExpected = testNumber;
    83
    84      }
    85
    86      /***
    87       * Test BigDecimalValidator validate Methods
    88       */
    89      public void testBigDecimalValidatorMethods() {
    90          Locale locale           = Locale.GERMAN;
    91          String pattern          = "0,00,00";
    92          String patternVal       = "1,23,45";
    93          String germanPatternVal = "1.23.45";
    94          String localeVal        = "12.345";
    95          String defaultVal       = "12,345";
    96          String XXXX             = "XXXX";
    97          BigDecimal expected = new BigDecimal(12345);
    98          assertEquals("validate(A) default", expected, BigDecimalValidator.getInstance().validate(defaultVal));
    99          assertEquals("validate(A) locale ", expected, BigDecimalValidator.getInstance().validate(localeVal, locale));
    100         assertEquals("validate(A) pattern", expected, BigDecimalValidator.getInstance().validate(patternVal, pattern));
    101         assertEquals("validate(A) both",    expected, BigDecimalValidator.getInstance().validate(germanPatternVal, pattern, Locale.GERMAN));
    102
    103         assertTrue("isValid(A) default", BigDecimalValidator.getInstance().isValid(defaultVal));
    104         assertTrue("isValid(A) locale ", BigDecimalValidator.getInstance().isValid(localeVal, locale));
    105         assertTrue("isValid(A) pattern", BigDecimalValidator.getInstance().isValid(patternVal, pattern));
    106         assertTrue("isValid(A) both",    BigDecimalValidator.getInstance().isValid(germanPatternVal, pattern, Locale.GERMAN));
    107
    108         assertNull("validate(B) default", BigDecimalValidator.getInstance().validate(XXXX));
    109         assertNull("validate(B) locale ", BigDecimalValidator.getInstance().validate(XXXX, locale));
    110         assertNull("validate(B) pattern", BigDecimalValidator.getInstance().validate(XXXX, pattern));
    111         assertNull("validate(B) both",    BigDecimalValidator.getInstance().validate(patternVal, pattern, Locale.GERMAN));
    112
    113         assertFalse("isValid(B) default", BigDecimalValidator.getInstance().isValid(XXXX));
    114         assertFalse("isValid(B) locale ", BigDecimalValidator.getInstance().isValid(XXXX, locale));
    115         assertFalse("isValid(B) pattern", BigDecimalValidator.getInstance().isValid(XXXX, pattern));
    116         assertFalse("isValid(B) both",    BigDecimalValidator.getInstance().isValid(patternVal, pattern, Locale.GERMAN));
    117     }
    118
    119     /***
    120      * Test BigDecimal Range/Min/Max
    121      */
    122     public void testBigDecimalRangeMinMax() {
    123         BigDecimalValidator validator = new BigDecimalValidator(true, AbstractNumberValidator.STANDARD_FORMAT, true);
    124         BigDecimal number9  = new BigDecimal("9");
    125         BigDecimal number10 = new BigDecimal("10");
    126         BigDecimal number11 = new BigDecimal("11");
    127         BigDecimal number19 = new BigDecimal("19");
    128         BigDecimal number20 = new BigDecimal("20");
    129         BigDecimal number21 = new BigDecimal("21");
    130
    131         float min = (float)10;
    132         float max = (float)20;
    133
    134         // Test isInRange()
    135         assertFalse("isInRange(A) < min",   validator.isInRange(number9,  min, max));
    136         assertTrue("isInRange(A) = min",    validator.isInRange(number10, min, max));
    137         assertTrue("isInRange(A) in range", validator.isInRange(number11, min, max));
    138         assertTrue("isInRange(A) = max",    validator.isInRange(number20, min, max));
    139         assertFalse("isInRange(A) > max",   validator.isInRange(number21, min, max));
    140
    141         // Test minValue()
    142         assertFalse("minValue(A) < min",    validator.minValue(number9,  min));
    143         assertTrue("minValue(A) = min",     validator.minValue(number10, min));
    144         assertTrue("minValue(A) > min",     validator.minValue(number11, min));
    145
    146         // Test minValue()
    147         assertTrue("maxValue(A) < max",     validator.maxValue(number19, max));
    148         assertTrue("maxValue(A) = max",     validator.maxValue(number20, max));
    149         assertFalse("maxValue(A) > max",    validator.maxValue(number21, max));
    150     }
    151 }