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