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