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