    1   /*
    2    * $Id: FloatValidatorTest.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.text.DecimalFormat;
    24  import java.util.Locale;
    25
    26  /***
    27   * Test Case for FloatValidator.
    28   *
    29   * @version $Revision: 371174 $ $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
    30   */
    31  public class FloatValidatorTest extends BaseNumberValidatorTest {
    32
    33      /***
    34       * Main
    35       * @param args arguments
    36       */
    37      public static void main(String[] args) {
    38          junit.textui.TestRunner.run(FloatValidatorTest.class);
    39      }
    40
    41      /***
    42       * Constructor
    43       * @param name test name
    44       */
    45      public FloatValidatorTest(String name) {
    46          super(name);
    47      }
    48
    49      protected void setUp() throws Exception {
    50          super.setUp();
    51
    52          validator       = new FloatValidator(false, 0);
    53          strictValidator = new FloatValidator();
    54
    55          testPattern = "#,###.#";
    56
    57          // testValidateMinMax()
    58          max = new Float(Float.MAX_VALUE);
    59          maxPlusOne = new Double(max.doubleValue() * 10);
    60          min = new Float(Float.MAX_VALUE * -1);
    61          minMinusOne = new Double(min.doubleValue() * 10);
    62
    63          // testInvalidStrict()
    64          invalidStrict = new String[] {null, "", "X", "X12", "12X", "1X2"};
    65
    66          // testInvalidNotStrict()
    67          invalid       = new String[] {null, "", "X", "X12"};
    68
    69          // testValid()
    70          testNumber    = new Float(1234.5);
    71          testZero      = new Float(0);
    72          validStrict          = new String[] {"0", "1234.5", "1,234.5"};
    73          validStrictCompare   = new Number[] {testZero, testNumber, testNumber};
    74          valid                = new String[] {"0", "1234.5", "1,234.5", "1,234.5", "1234.5X"};
    75          validCompare         = new Number[] {testZero, testNumber, testNumber, testNumber, testNumber};
    76
    77          testStringUS = "1,234.5";
    78          testStringDE = "1.234,5";
    79
    80          // Localized Pattern test
    81          localeValue = testStringDE;
    82          localePattern = "#.###,#";
    83          testLocale    = Locale.GERMANY;
    84          localeExpected = testNumber;
    85
    86      }
    87
    88      /***
    89       * Test FloatValidator validate Methods
    90       */
    91      public void testFloatValidatorMethods() {
    92          Locale locale     = Locale.GERMAN;
    93          String pattern    = "0,00,00";
    94          String patternVal = "1,23,45";
    95          String localeVal  = "12.345";
    96          String germanPatternVal = "1.23.45";
    97          String defaultVal = "12,345";
    98          String XXXX    = "XXXX";
    99          Float expected = new Float(12345);
    100         assertEquals("validate(A) default", expected, FloatValidator.getInstance().validate(defaultVal));
    101         assertEquals("validate(A) locale ", expected, FloatValidator.getInstance().validate(localeVal, locale));
    102         assertEquals("validate(A) pattern", expected, FloatValidator.getInstance().validate(patternVal, pattern));
    103         assertEquals("validate(A) both",    expected, FloatValidator.getInstance().validate(germanPatternVal, pattern, Locale.GERMAN));
    104
    105         assertTrue("isValid(A) default", FloatValidator.getInstance().isValid(defaultVal));
    106         assertTrue("isValid(A) locale ", FloatValidator.getInstance().isValid(localeVal, locale));
    107         assertTrue("isValid(A) pattern", FloatValidator.getInstance().isValid(patternVal, pattern));
    108         assertTrue("isValid(A) both",    FloatValidator.getInstance().isValid(germanPatternVal, pattern, Locale.GERMAN));
    109
    110         assertNull("validate(B) default", FloatValidator.getInstance().validate(XXXX));
    111         assertNull("validate(B) locale ", FloatValidator.getInstance().validate(XXXX, locale));
    112         assertNull("validate(B) pattern", FloatValidator.getInstance().validate(XXXX, pattern));
    113         assertNull("validate(B) both",    FloatValidator.getInstance().validate(patternVal, pattern, Locale.GERMAN));
    114
    115         assertFalse("isValid(B) default", FloatValidator.getInstance().isValid(XXXX));
    116         assertFalse("isValid(B) locale ", FloatValidator.getInstance().isValid(XXXX, locale));
    117         assertFalse("isValid(B) pattern", FloatValidator.getInstance().isValid(XXXX, pattern));
    118         assertFalse("isValid(B) both",    FloatValidator.getInstance().isValid(patternVal, pattern, Locale.GERMAN));
    119     }
    120
    121     /***
    122      * Test Float validation for values too small to handle.
    123      * (slightly different from max/min which are the largest +ve/-ve
    124      */
    125     public void testFloatSmallestValues() {
    126         String pattern = "#.#################################################################";
    127         DecimalFormat fmt = new DecimalFormat(pattern);
    128
    129         // Validate Smallest +ve value
    130         Float smallestPositive  = new Float(Float.MIN_VALUE);
    131         String strSmallestPositive = fmt.format(smallestPositive);
    132         assertEquals("Smallest +ve", smallestPositive, FloatValidator.getInstance().validate(strSmallestPositive, pattern));
    133
    134         // Validate Smallest -ve value
    135         Float smallestNegative  = new Float(Float.MIN_VALUE * -1);
    136         String strSmallestNegative = fmt.format(smallestNegative);
    137         assertEquals("Smallest -ve", smallestNegative, FloatValidator.getInstance().validate(strSmallestNegative, pattern));
    138
    139         // Validate Too Small +ve
    140         Double tooSmallPositive = new Double(((double)Float.MIN_VALUE / (double)10));
    141         String strTooSmallPositive = fmt.format(tooSmallPositive);
    142         assertFalse("Too small +ve", FloatValidator.getInstance().isValid(strTooSmallPositive, pattern));
    143
    144         // Validate Too Small -ve
    145         Double tooSmallNegative = new Double(tooSmallPositive.doubleValue() * (double)-1);
    146         String strTooSmallNegative = fmt.format(tooSmallNegative);
    147         assertFalse("Too small -ve", FloatValidator.getInstance().isValid(strTooSmallNegative, pattern));
    148     }
    149
    150     /***
    151      * Test Float Range/Min/Max
    152      */
    153     public void testFloatRangeMinMax() {
    154         FloatValidator validator = (FloatValidator)strictValidator;
    155         Float number9  = validator.validate("9", "#");
    156         Float number10 = validator.validate("10", "#");
    157         Float number11 = validator.validate("11", "#");
    158         Float number19 = validator.validate("19", "#");
    159         Float number20 = validator.validate("20", "#");
    160         Float number21 = validator.validate("21", "#");
    161
    162         // Test isInRange()
    163         assertFalse("isInRange() < min",   validator.isInRange(number9,  10, 20));
    164         assertTrue("isInRange() = min",    validator.isInRange(number10, 10, 20));
    165         assertTrue("isInRange() in range", validator.isInRange(number11, 10, 20));
    166         assertTrue("isInRange() = max",    validator.isInRange(number20, 10, 20));
    167         assertFalse("isInRange() > max",   validator.isInRange(number21, 10, 20));
    168
    169         // Test minValue()
    170         assertFalse("minValue() < min",    validator.minValue(number9,  10));
    171         assertTrue("minValue() = min",     validator.minValue(number10, 10));
    172         assertTrue("minValue() > min",     validator.minValue(number11, 10));
    173
    174         // Test minValue()
    175         assertTrue("maxValue() < max",     validator.maxValue(number19, 20));
    176         assertTrue("maxValue() = max",     validator.maxValue(number20, 20));
    177         assertFalse("maxValue() > max",    validator.maxValue(number21, 20));
    178     }
    179 }