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: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
27   */
28  public class ExceptionTest extends TestCommon {
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      protected void setUp() throws IOException, SAXException {
50          loadResources("ExceptionTest-config.xml");
51      }
52  
53      /***
54       * Tests handling of checked exceptions - should become 
55       * ValidatorExceptions.
56       */
57      public void testValidatorException() {
58          // Create bean to run test on.
59          ValueBean info = new ValueBean();
60          info.setValue("VALIDATOR");
61  
62          // Construct validator based on the loaded resources 
63          // and the form key
64          Validator validator = new Validator(resources, FORM_KEY);
65          // add the name bean to the validator as a resource 
66          // for the validations to be performed on.
67          validator.setParameter(Validator.BEAN_PARAM, info);
68  
69          // Get results of the validation which can throw ValidatorException
70          try {
71              validator.validate();
72              fail("ValidatorException should occur here!");
73          } catch (ValidatorException expected) {
74              assertTrue("VALIDATOR-EXCEPTION".equals(expected.getMessage()));
75          }
76      }
77  
78      /***
79       * Tests handling of runtime exceptions.
80       *
81       * N.B. This test has been removed (renamed) as it currently
82       *      serves no purpose. If/When exception handling
83       *      is changed in Validator 2.0 it can be reconsidered
84       *      then.
85       */
86      public void XtestRuntimeException() throws ValidatorException {
87          // Create bean to run test on.
88          ValueBean info = new ValueBean();
89          info.setValue("RUNTIME");
90  
91          // Construct validator based on the loaded resources 
92          // and the form key
93          Validator validator = new Validator(resources, FORM_KEY);
94          // add the name bean to the validator as a resource 
95          // for the validations to be performed on.
96          validator.setParameter(Validator.BEAN_PARAM, info);
97  
98          // Get results of the validation which can throw ValidatorException
99          try {
100             validator.validate();
101             //fail("RuntimeException should occur here!");
102         } catch (RuntimeException expected) {
103             fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
104             // This will be true in Validator 2.0
105             //assertTrue("RUNTIME-EXCEPTION".equals(expected.getMessage()));
106         }
107     }
108 
109     /***
110      * Tests handling of checked exceptions - should become 
111      * ValidatorExceptions.
112      *
113      * N.B. This test has been removed (renamed) as it currently
114      *      serves no purpose. If/When exception handling
115      *      is changed in Validator 2.0 it can be reconsidered
116      *      then.
117      */
118     public void XtestCheckedException() {
119         // Create bean to run test on.
120         ValueBean info = new ValueBean();
121         info.setValue("CHECKED");
122 
123         // Construct validator based on the loaded resources 
124         // and the form key
125         Validator validator = new Validator(resources, FORM_KEY);
126         // add the name bean to the validator as a resource 
127         // for the validations to be performed on.
128         validator.setParameter(Validator.BEAN_PARAM, info);
129 
130         // Get results of the validation which can throw ValidatorException
131         
132         // Tests Validator 1.x exception handling
133         try {
134             validator.validate();
135         } catch (ValidatorException expected) {
136             fail("Checked exceptions are not wrapped in ValidatorException in Validator 1.x.");
137         } catch (Exception e) {
138             assertTrue("CHECKED-EXCEPTION".equals(e.getMessage()));
139         }
140         
141         // This will be true in Validator 2.0
142 //        try {
143 //            validator.validate();
144 //            fail("ValidatorException should occur here!");
145 //        } catch (ValidatorException expected) {
146 //            assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));
147 //        }
148     }
149 }