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