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