    1   /*
    2    * $Id: ExceptionTest.java 387116 2006-03-20 02:32:31Z niallp $
    3    * $Rev: 387116 $
    4    * $Date: 2006-03-20 02:32:31 +0000 (Mon, 20 Mar 2006) $
    5    *
    6    * ====================================================================
    7    * Copyright  2004-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   * Performs Validation Test for exception handling.
    30   */
    31  public class ExceptionTest extends TestCommon {
    32
    33      /***
    34       * The key used to retrieve the set of validation
    35       * rules from the xml file.
    36       */
    37      protected static String FORM_KEY = "exceptionForm";
    38
    39      /***
    40       * The key used to retrieve the validator action.
    41       */
    42      protected static String ACTION = "raiseException";
    43
    44      public ExceptionTest(String name) {
    45          super(name);
    46      }
    47
    48      /***
    49       * Load <code>ValidatorResources</code> from
    50       * validator-exception.xml.
    51       */
    52      protected void setUp() throws IOException, SAXException {
    53          loadResources("ExceptionTest-config.xml");
    54      }
    55
    56      /***
    57       * Tests handling of checked exceptions - should become
    58       * ValidatorExceptions.
    59       */
    60      public void testValidatorException() {
    61          // Create bean to run test on.
    62          ValueBean info = new ValueBean();
    63          info.setValue("VALIDATOR");
    64
    65          // Construct validator based on the loaded resources
    66          // and the form key
    67          Validator validator = new Validator(resources, FORM_KEY);
    68          // add the name bean to the validator as a resource
    69          // for the validations to be performed on.
    70          validator.setParameter(Validator.BEAN_PARAM, info);
    71
    72          // Get results of the validation which can throw ValidatorException
    73          try {
    74              validator.validate();
    75              fail("ValidatorException should occur here!");
    76          } catch (ValidatorException expected) {
    77              assertTrue("VALIDATOR-EXCEPTION".equals(expected.getMessage()));
    78          }
    79      }
    80
    81      /***
    82       * Tests handling of runtime exceptions.
    83       *
    84       * N.B. This test has been removed (renamed) as it currently
    85       *      serves no purpose. If/When exception handling
    86       *      is changed in Validator 2.0 it can be reconsidered
    87       *      then.
    88       */
    89      public void XtestRuntimeException() throws ValidatorException {
    90          // Create bean to run test on.
    91          ValueBean info = new ValueBean();
    92          info.setValue("RUNTIME");
    93
    94          // Construct validator based on the loaded resources
    95          // and the form key
    96          Validator validator = new Validator(resources, FORM_KEY);
    97          // add the name bean to the validator as a resource
    98          // for the validations to be performed on.
    99          validator.setParameter(Validator.BEAN_PARAM, info);
    100
    101         // Get results of the validation which can throw ValidatorException
    102         try {
    103             validator.validate();
    104             //fail("RuntimeException should occur here!");
    105         } catch (RuntimeException expected) {
    106             fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
    107             // This will be true in Validator 2.0
    108             //assertTrue("RUNTIME-EXCEPTION".equals(expected.getMessage()));
    109         }
    110     }
    111
    112     /***
    113      * Tests handling of checked exceptions - should become
    114      * ValidatorExceptions.
    115      *
    116      * N.B. This test has been removed (renamed) as it currently
    117      *      serves no purpose. If/When exception handling
    118      *      is changed in Validator 2.0 it can be reconsidered
    119      *      then.
    120      */
    121     public void XtestCheckedException() {
    122         // Create bean to run test on.
    123         ValueBean info = new ValueBean();
    124         info.setValue("CHECKED");
    125
    126         // Construct validator based on the loaded resources
    127         // and the form key
    128         Validator validator = new Validator(resources, FORM_KEY);
    129         // add the name bean to the validator as a resource
    130         // for the validations to be performed on.
    131         validator.setParameter(Validator.BEAN_PARAM, info);
    132
    133         // Get results of the validation which can throw ValidatorException
    134
    135         // Tests Validator 1.x exception handling
    136         try {
    137             validator.validate();
    138         } catch (ValidatorException expected) {
    139             fail("Checked exceptions are not wrapped in ValidatorException in Validator 1.x.");
    140         } catch (Exception e) {
    141             assertTrue("CHECKED-EXCEPTION".equals(e.getMessage()));
    142         }
    143
    144         // This will be true in Validator 2.0
    145 //        try {
    146 //            validator.validate();
    147 //            fail("ValidatorException should occur here!");
    148 //        } catch (ValidatorException expected) {
    149 //            assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));
    150 //        }
    151     }
    152 }