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