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 public void testRuntimeException() throws ValidatorException {
85
86 ValueBean info = new ValueBean();
87 info.setValue("RUNTIME");
88
89
90
91 Validator validator = new Validator(resources, FORM_KEY);
92
93
94 validator.setParameter(Validator.BEAN_PARAM, info);
95
96
97 try {
98 validator.validate();
99
100 } catch (RuntimeException expected) {
101 fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
102
103
104 }
105 }
106
107 /***
108 * Tests handling of checked exceptions - should become
109 * ValidatorExceptions.
110 */
111 public void testCheckedException() {
112
113 ValueBean info = new ValueBean();
114 info.setValue("CHECKED");
115
116
117
118 Validator validator = new Validator(resources, FORM_KEY);
119
120
121 validator.setParameter(Validator.BEAN_PARAM, info);
122
123
124
125
126 try {
127 validator.validate();
128 } catch (ValidatorException expected) {
129 fail("Checked exceptions are not wrapped in ValidatorException in Validator 1.x.");
130 } catch (Exception e) {
131 assertTrue("CHECKED-EXCEPTION".equals(e.getMessage()));
132 }
133
134
135
136
137
138
139
140
141 }
142 }