    1   /*
    2    * $Id: TestNumber.java 329871 2005-10-31 17:50:55Z niallp $
    3    * $Rev: 329871 $
    4    * $Date: 2005-10-31 17:50:55 +0000 (Mon, 31 Oct 2005) $
    5    *
    6    * ====================================================================
    7    * Copyright 2001-2005 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
    22  package org.apache.commons.validator;
    23
    24  import java.io.IOException;
    25
    26  import org.xml.sax.SAXException;
    27
    28  /***
    29   * Abstracts number unit tests methods.
    30   */
    31  abstract public class TestNumber extends TestCommon {
    32
    33      /***
    34       * The key used to retrieve the set of validation
    35       * rules from the xml file.
    36       */
    37      protected String FORM_KEY;
    38
    39      /***
    40       * The key used to retrieve the validator action.
    41       */
    42      protected String ACTION;
    43
    44
    45      public TestNumber(String name) {
    46          super(name);
    47      }
    48
    49      /***
    50       * Load <code>ValidatorResources</code> from
    51       * validator-numeric.xml.
    52       */
    53      protected void setUp() throws IOException, SAXException {
    54          // Load resources
    55          loadResources("TestNumber-config.xml");
    56      }
    57
    58      protected void tearDown() {
    59      }
    60
    61      /***
    62       * Tests the number validation.
    63       */
    64      public void testNumber() throws ValidatorException {
    65          // Create bean to run test on.
    66          ValueBean info = new ValueBean();
    67          info.setValue("0");
    68          valueTest(info, true);
    69      }
    70
    71      /***
    72       * Tests the float validation failure.
    73       */
    74      public void testNumberFailure() throws ValidatorException {
    75          // Create bean to run test on.
    76          ValueBean info = new ValueBean();
    77          valueTest(info, false);
    78      }
    79
    80      /***
    81       * Utlity class to run a test on a value.
    82       *
    83       * @param	info	Value to run test on.
    84       * @param	passed	Whether or not the test is expected to pass.
    85       */
    86      protected void valueTest(Object info, boolean passed) throws ValidatorException {
    87          // Construct validator based on the loaded resources
    88          // and the form key
    89          Validator validator = new Validator(resources, FORM_KEY);
    90          // add the name bean to the validator as a resource
    91          // for the validations to be performed on.
    92          validator.setParameter(Validator.BEAN_PARAM, info);
    93
    94          // Get results of the validation.
    95          ValidatorResults results = null;
    96
    97          // throws ValidatorException,
    98          // but we aren't catching for testing
    99          // since no validation methods we use
    100         // throw this
    101         results = validator.validate();
    102
    103         assertNotNull("Results are null.", results);
    104
    105         ValidatorResult result = results.getValidatorResult("value");
    106
    107         assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
    108         assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
    109         assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
    110     }
    111
    112
    113 }