1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
62 ValueBean info = new ValueBean();
63 info.setValue("VALIDATOR");
64
65
66
67 Validator validator = new Validator(resources, FORM_KEY);
68
69
70 validator.setParameter(Validator.BEAN_PARAM, info);
71
72
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 * N.B. This test has been removed (renamed) as it currently
85 * serves no purpose. If/When exception handling
86 * is changed in Validator 2.0 it can be reconsidered
87 * then.
88 */
89 public void XtestRuntimeException() throws ValidatorException {
90
91 ValueBean info = new ValueBean();
92 info.setValue("RUNTIME");
93
94
95
96 Validator validator = new Validator(resources, FORM_KEY);
97
98
99 validator.setParameter(Validator.BEAN_PARAM, info);
100
101
102 try {
103 validator.validate();
104
105 } catch (RuntimeException expected) {
106 fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
107
108
109 }
110 }
111
112 /***
113 * Tests handling of checked exceptions - should become
114 * ValidatorExceptions.
115 *
116 * N.B. This test has been removed (renamed) as it currently
117 * serves no purpose. If/When exception handling
118 * is changed in Validator 2.0 it can be reconsidered
119 * then.
120 */
121 public void XtestCheckedException() {
122
123 ValueBean info = new ValueBean();
124 info.setValue("CHECKED");
125
126
127
128 Validator validator = new Validator(resources, FORM_KEY);
129
130
131 validator.setParameter(Validator.BEAN_PARAM, info);
132
133
134
135
136 try {
137 validator.validate();
138 } catch (ValidatorException expected) {
139 fail("Checked exceptions are not wrapped in ValidatorException in Validator 1.x.");
140 } catch (Exception e) {
141 assertTrue("CHECKED-EXCEPTION".equals(e.getMessage()));
142 }
143
144
145
146
147
148
149
150
151 }
152 }