    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 junit.framework.TestCase;
    20
    21  import java.util.Locale;
    22  import java.text.DecimalFormat;
    23  import java.math.BigDecimal;
    24  /***
    25   * Base Number Test Case.
    26   *
    27   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
    28   */
    29  public class BaseNumberValidatorTest extends TestCase {
    30
    31      protected AbstractNumberValidator validator;
    32      protected AbstractNumberValidator strictValidator;
    33
    34      protected Number max;
    35      protected Number maxPlusOne;
    36      protected Number min;
    37      protected Number minMinusOne;
    38      protected String[] invalid;
    39      protected String[] valid;
    40      protected Number[] validCompare;
    41
    42      protected String[] invalidStrict;
    43      protected String[] validStrict;
    44      protected Number[] validStrictCompare;
    45
    46      protected String testPattern;
    47      protected Number testNumber;
    48      protected Number testZero;
    49      protected String testStringUS;
    50      protected String testStringDE;
    51
    52      protected String localeValue;
    53      protected String localePattern;
    54      protected Locale testLocale;
    55      protected Number localeExpected;
    56
    57      /***
    58       * Constructor
    59       * @param name test name
    60       */
    61      public BaseNumberValidatorTest(String name) {
    62          super(name);
    63      }
    64
    65      protected void setUp() throws Exception {
    66          super.setUp();
    67
    68          Locale.setDefault(Locale.US);
    69
    70      }
    71
    72      /***
    73       * Tear down
    74       * @throws Exception
    75       */
    76      protected void tearDown() throws Exception {
    77          super.tearDown();
    78          validator = null;
    79          strictValidator = null;
    80      }
    81
    82      /***
    83       * Test Format Type
    84       */
    85      public void testFormatType() {
    86          assertEquals("Format Type A", 0, validator.getFormatType());
    87          assertEquals("Format Type B", AbstractNumberValidator.STANDARD_FORMAT, validator.getFormatType());
    88      }
    89
    90      /***
    91       * Test Min/Max values allowed
    92       */
    93      public void testValidateMinMax() {
    94          DecimalFormat fmt = new DecimalFormat("#");
    95          if (max != null) {
    96              assertEquals("Test Max",   max, validator.parse(fmt.format(max), "#", null));
    97              assertNull("Test Max + 1",      validator.parse(fmt.format(maxPlusOne), "#", null));
    98              assertEquals("Test Min",   min, validator.parse(fmt.format(min), "#", null));
    99              assertNull("Test min - 1",      validator.parse(fmt.format(minMinusOne), "#", null));
    100         }
    101     }
    102
    103     /***
    104      * Test Invalid, strict=true
    105      */
    106     public void testInvalidStrict() {
    107         for (int i = 0; i < invalidStrict.length; i++) {
    108             String text = "idx=["+i+"] value=[" + invalidStrict[i] + "]";
    109             assertNull("(A) "  + text, strictValidator.parse(invalidStrict[i], null, Locale.US));
    110             assertFalse("(B) " + text, strictValidator.isValid(invalidStrict[i], null, Locale.US));
    111             assertNull("(C) "  + text, strictValidator.parse(invalidStrict[i], testPattern, null));
    112             assertFalse("(D) " + text, strictValidator.isValid(invalidStrict[i], testPattern, null));
    113         }
    114     }
    115
    116     /***
    117      * Test Invalid, strict=false
    118      */
    119     public void testInvalidNotStrict() {
    120         for (int i = 0; i < invalid.length; i++) {
    121             String text = "idx=["+i+"] value=[" + invalid[i] + "]";
    122             assertNull("(A) "  + text, validator.parse(invalid[i], null, Locale.US));
    123             assertFalse("(B) " + text, validator.isValid(invalid[i], null, Locale.US));
    124             assertNull("(C) "  + text, validator.parse(invalid[i], testPattern, null));
    125             assertFalse("(D) " + text, validator.isValid(invalid[i], testPattern, null));
    126         }
    127     }
    128
    129     /***
    130      * Test Valid, strict=true
    131      */
    132     public void testValidStrict() {
    133         for (int i = 0; i < validStrict.length; i++) {
    134             String text = "idx=["+i+"] value=[" + validStrictCompare[i] + "]";
    135             assertEquals("(A) "  + text, validStrictCompare[i], strictValidator.parse(validStrict[i], null, Locale.US));
    136             assertTrue("(B) "    + text,                        strictValidator.isValid(validStrict[i], null, Locale.US));
    137             assertEquals("(C) "  + text, validStrictCompare[i], strictValidator.parse(validStrict[i], testPattern, null));
    138             assertTrue("(D) "    + text,                        strictValidator.isValid(validStrict[i], testPattern, null));
    139         }
    140     }
    141
    142     /***
    143      * Test Valid, strict=false
    144      */
    145     public void testValidNotStrict() {
    146         for (int i = 0; i < valid.length; i++) {
    147             String text = "idx=["+i+"] value=[" + validCompare[i] + "]";
    148             assertEquals("(A) "  + text, validCompare[i], validator.parse(valid[i], null, Locale.US));
    149             assertTrue("(B) "    + text,                  validator.isValid(valid[i], null, Locale.US));
    150             assertEquals("(C) "  + text, validCompare[i], validator.parse(valid[i], testPattern, null));
    151             assertTrue("(D) "    + text,                  validator.isValid(valid[i], testPattern, null));
    152         }
    153     }
    154
    155     /***
    156      * Test different Locale
    157      */
    158     public void testValidateLocale() {
    159
    160         assertEquals("US Locale, US Format", testNumber, strictValidator.parse(testStringUS, null, Locale.US));
    161         assertNull("US Locale, DE Format", strictValidator.parse(testStringDE, null, Locale.US));
    162
    163         // Default German Locale
    164         assertEquals("DE Locale, DE Format", testNumber, strictValidator.parse(testStringDE, null, Locale.GERMAN));
    165         assertNull("DE Locale, US Format", strictValidator.parse(testStringUS, null, Locale.GERMAN));
    166
    167         // Default Locale has been set to Locale.US in setup()
    168         assertEquals("Default Locale, US Format", testNumber, strictValidator.parse(testStringUS, null, null));
    169         assertNull("Default Locale, DE Format", strictValidator.parse(testStringDE, null, null));
    170     }
    171
    172     /***
    173      * Test format() methods
    174      */
    175     public void testFormat() {
    176         Number number = new BigDecimal("1234.5");
    177         assertEquals("US Locale, US Format", "1,234.5", strictValidator.format(number, Locale.US));
    178         assertEquals("DE Locale, DE Format", "1.234,5", strictValidator.format(number, Locale.GERMAN));
    179         assertEquals("Pattern #,#0.00", "12,34.50",  strictValidator.format(number, "#,#0.00"));
    180     }
    181
    182     /***
    183      * Test Range/Min/Max
    184      */
    185     public void testRangeMinMax() {
    186         Number number9 = new Integer(9);
    187         Number number10 = new Integer(10);
    188         Number number11 = new Integer(11);
    189         Number number19 = new Integer(19);
    190         Number number20 = new Integer(20);
    191         Number number21 = new Integer(21);
    192
    193         // Test isInRange()
    194         assertFalse("isInRange() < min",   strictValidator.isInRange(number9 ,  number10, number20));
    195         assertTrue("isInRange() = min",    strictValidator.isInRange(number10 , number10, number20));
    196         assertTrue("isInRange() in range", strictValidator.isInRange(number11 , number10, number20));
    197         assertTrue("isInRange() = max",    strictValidator.isInRange(number20 , number10, number20));
    198         assertFalse("isInRange() > max",   strictValidator.isInRange(number21 , number10, number20));
    199
    200         // Test minValue()
    201         assertFalse("minValue() < min",    strictValidator.minValue(number9 ,  number10));
    202         assertTrue("minValue() = min",     strictValidator.minValue(number10 , number10));
    203         assertTrue("minValue() > min",     strictValidator.minValue(number11 , number10));
    204
    205         // Test minValue()
    206         assertTrue("maxValue() < max",     strictValidator.maxValue(number19 , number20));
    207         assertTrue("maxValue() = max",     strictValidator.maxValue(number20 , number20));
    208         assertFalse("maxValue() > max",    strictValidator.maxValue(number21 , number20));
    209     }
    210
    211 }