1   /*
2    * $Id: ExceptionTest.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  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      public void testRuntimeException() throws ValidatorException {
85          // Create bean to run test on.
86          ValueBean info = new ValueBean();
87          info.setValue("RUNTIME");
88  
89          // Construct validator based on the loaded resources 
90          // and the form key
91          Validator validator = new Validator(resources, FORM_KEY);
92          // add the name bean to the validator as a resource 
93          // for the validations to be performed on.
94          validator.setParameter(Validator.BEAN_PARAM, info);
95  
96          // Get results of the validation which can throw ValidatorException
97          try {
98              validator.validate();
99              //fail("RuntimeException should occur here!");
100         } catch (RuntimeException expected) {
101             fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
102             // This will be true in Validator 2.0
103             //assertTrue("RUNTIME-EXCEPTION".equals(expected.getMessage()));
104         }
105     }
106 
107     /***
108      * Tests handling of checked exceptions - should become 
109      * ValidatorExceptions.
110      */
111     public void testCheckedException() {
112         // Create bean to run test on.
113         ValueBean info = new ValueBean();
114         info.setValue("CHECKED");
115 
116         // Construct validator based on the loaded resources 
117         // and the form key
118         Validator validator = new Validator(resources, FORM_KEY);
119         // add the name bean to the validator as a resource 
120         // for the validations to be performed on.
121         validator.setParameter(Validator.BEAN_PARAM, info);
122 
123         // Get results of the validation which can throw ValidatorException
124         
125         // Tests Validator 1.x exception handling
126         try {
127             validator.validate();
128         } catch (ValidatorException expected) {
129             fail("Checked exceptions are not wrapped in ValidatorException in Validator 1.x.");
130         } catch (Exception e) {
131             assertTrue("CHECKED-EXCEPTION".equals(e.getMessage()));
132         }
133         
134         // This will be true in Validator 2.0
135 //        try {
136 //            validator.validate();
137 //            fail("ValidatorException should occur here!");
138 //        } catch (ValidatorException expected) {
139 //            assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));
140 //        }
141     }
142 }