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