1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
59 ValueBean info = new ValueBean();
60 info.setValue("VALIDATOR");
61
62
63
64 Validator validator = new Validator(resources, FORM_KEY);
65
66
67 validator.setParameter(Validator.BEAN_PARAM, info);
68
69
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
88 ValueBean info = new ValueBean();
89 info.setValue("RUNTIME");
90
91
92
93 Validator validator = new Validator(resources, FORM_KEY);
94
95
96 validator.setParameter(Validator.BEAN_PARAM, info);
97
98
99 try {
100 validator.validate();
101
102 } catch (RuntimeException expected) {
103 fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
104
105
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
120 ValueBean info = new ValueBean();
121 info.setValue("CHECKED");
122
123
124
125 Validator validator = new Validator(resources, FORM_KEY);
126
127
128 validator.setParameter(Validator.BEAN_PARAM, info);
129
130
131
132
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
142
143
144
145
146
147
148 }
149 }