    1   /*
    2    * $Id: PercentValidatorTest.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 junit.framework.TestCase;
    24
    25  import java.util.Locale;
    26  import java.math.BigDecimal;
    27  /***
    28   * Test Case for PercentValidator.
    29   *
    30   * @version $Revision: 371174 $ $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
    31   */
    32  public class PercentValidatorTest extends TestCase {
    33
    34      protected PercentValidator validator;
    35
    36      /***
    37       * Main
    38       * @param args arguments
    39       */
    40      public static void main(String[] args) {
    41          junit.textui.TestRunner.run(PercentValidatorTest.class);
    42      }
    43
    44      /***
    45       * Constructor
    46       * @param name test name
    47       */
    48      public PercentValidatorTest(String name) {
    49          super(name);
    50      }
    51
    52      protected void setUp() throws Exception {
    53          super.setUp();
    54          validator = new PercentValidator();
    55      }
    56
    57      /***
    58       * Tear down
    59       * @throws Exception
    60       */
    61      protected void tearDown() throws Exception {
    62          super.tearDown();
    63          validator = null;
    64      }
    65
    66      /***
    67       * Test Format Type
    68       */
    69      public void testFormatType() {
    70          assertEquals("Format Type A", 2, PercentValidator.getInstance().getFormatType());
    71          assertEquals("Format Type B", PercentValidator.PERCENT_FORMAT, PercentValidator.getInstance().getFormatType());
    72      }
    73
    74      /***
    75       * Test Valid percentage values
    76       */
    77      public void testValid() {
    78          // Set the default Locale
    79          Locale origDefault = Locale.getDefault();
    80          Locale.setDefault(Locale.UK);
    81
    82          BigDecimalValidator validator = PercentValidator.getInstance();
    83          BigDecimal expected = new BigDecimal("0.12");
    84          BigDecimal negative = new BigDecimal("-0.12");
    85          BigDecimal hundred  = new BigDecimal("1.00");
    86
    87          assertEquals("Default locale", expected, validator.validate("12%"));
    88          assertEquals("Default negtve", negative, validator.validate("-12%"));
    89
    90          // Invalid UK
    91          assertEquals("UK locale",      expected, validator.validate("12%",   Locale.UK));
    92          assertEquals("UK negative",    negative, validator.validate("-12%",  Locale.UK));
    93          assertEquals("UK No symbol",   expected, validator.validate("12",    Locale.UK));
    94
    95          // Invalid US - can't find a Locale with different symbols!
    96          assertEquals("US locale",      expected, validator.validate("12%",   Locale.US));
    97          assertEquals("US negative",    negative, validator.validate("-12%",  Locale.US));
    98          assertEquals("US No symbol",   expected, validator.validate("12",    Locale.US));
    99
    100         assertEquals("100%",           hundred, validator.validate("100%"));
    101
    102         // Restore the original default
    103         Locale.setDefault(origDefault);
    104     }
    105
    106     /***
    107      * Test Invalid percentage values
    108      */
    109     public void testInvalid() {
    110         BigDecimalValidator validator = PercentValidator.getInstance();
    111
    112         // Invalid Missing
    113         assertFalse("isValid() Null Value",    validator.isValid(null));
    114         assertFalse("isValid() Empty Value",   validator.isValid(""));
    115         assertNull("validate() Null Value",    validator.validate(null));
    116         assertNull("validate() Empty Value",   validator.validate(""));
    117
    118         // Invalid UK
    119         assertFalse("UK wrong symbol",    validator.isValid("12@",   Locale.UK)); // ???
    120         assertFalse("UK wrong negative",  validator.isValid("(12%)", Locale.UK));
    121
    122         // Invalid US - can't find a Locale with different symbols!
    123         assertFalse("US wrong symbol",    validator.isValid("12@",   Locale.US)); // ???
    124         assertFalse("US wrong negative",  validator.isValid("(12%)", Locale.US));
    125     }
    126
    127 }