2
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ExceptionTest.java,v 1.2 2004/02/21 17:10:30 rleland Exp $
4
* $Date: 2004/02/21 17:10:30 $
6
* ====================================================================
7
* Copyright 2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
package org.apache.commons.validator;
24
import java.io.IOException;
26
import org.xml.sax.SAXException;
29
* Performs Validation Test for exception handling.
31
public class ExceptionTest extends TestCommon {
34
* The key used to retrieve the set of validation
35
* rules from the xml file.
37
protected static String FORM_KEY = "exceptionForm";
40
* The key used to retrieve the validator action.
42
protected static String ACTION = "raiseException";
44
public ExceptionTest(String name) {
49
* Load <code>ValidatorResources</code> from
50
* validator-exception.xml.
52
protected void setUp() throws IOException, SAXException {
53
loadResources("validator-exception.xml");
57
* Tests handling of checked exceptions - should become
58
* ValidatorExceptions.
60
public void testValidatorException() {
61
// Create bean to run test on.
62
ValueBean info = new ValueBean();
63
info.setValue("VALIDATOR");
65
// Construct validator based on the loaded resources
67
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);
72
// Get results of the validation which can throw ValidatorException
75
fail("ValidatorException should occur here!");
76
} catch (ValidatorException expected) {
77
assertTrue("VALIDATOR-EXCEPTION".equals(expected.getMessage()));
82
* Tests handling of runtime exceptions.
84
public void testRuntimeException() throws ValidatorException {
85
// Create bean to run test on.
86
ValueBean info = new ValueBean();
87
info.setValue("RUNTIME");
89
// Construct validator based on the loaded resources
91
Validator validator = new Validator(resources, FORM_KEY);
92
// add the name bean to the validator as a resource
93
// for the validations to be performed on.
94
validator.setParameter(Validator.BEAN_PARAM, info);
96
// Get results of the validation which can throw ValidatorException
99
//fail("RuntimeException should occur here!");
100
} catch (RuntimeException expected) {
101
fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
102
// This will be true in Validator 2.0
103
//assertTrue("RUNTIME-EXCEPTION".equals(expected.getMessage()));
108
* Tests handling of checked exceptions - should become
109
* ValidatorExceptions.
111
public void testCheckedException() {
112
// Create bean to run test on.
113
ValueBean info = new ValueBean();
114
info.setValue("CHECKED");
116
// Construct validator based on the loaded resources
118
Validator validator = new Validator(resources, FORM_KEY);
119
// add the name bean to the validator as a resource
120
// for the validations to be performed on.
121
validator.setParameter(Validator.BEAN_PARAM, info);
123
// Get results of the validation which can throw ValidatorException
125
// Tests Validator 1.x exception handling
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()));
134
// This will be true in Validator 2.0
136
// validator.validate();
137
// fail("ValidatorException should occur here!");
138
// } catch (ValidatorException expected) {
139
// assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));
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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.validator;
19
import java.io.IOException;
21
import org.xml.sax.SAXException;
24
* Performs Validation Test for exception handling.
26
* @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
28
public class ExceptionTest extends TestCommon {
31
* The key used to retrieve the set of validation
32
* rules from the xml file.
34
protected static String FORM_KEY = "exceptionForm";
37
* The key used to retrieve the validator action.
39
protected static String ACTION = "raiseException";
41
public ExceptionTest(String name) {
46
* Load <code>ValidatorResources</code> from
47
* validator-exception.xml.
49
protected void setUp() throws IOException, SAXException {
50
loadResources("ExceptionTest-config.xml");
54
* Tests handling of checked exceptions - should become
55
* ValidatorExceptions.
57
public void testValidatorException() {
58
// Create bean to run test on.
59
ValueBean info = new ValueBean();
60
info.setValue("VALIDATOR");
62
// Construct validator based on the loaded resources
64
Validator validator = new Validator(resources, FORM_KEY);
65
// add the name bean to the validator as a resource
66
// for the validations to be performed on.
67
validator.setParameter(Validator.BEAN_PARAM, info);
69
// Get results of the validation which can throw ValidatorException
72
fail("ValidatorException should occur here!");
73
} catch (ValidatorException expected) {
74
assertTrue("VALIDATOR-EXCEPTION".equals(expected.getMessage()));
79
* Tests handling of runtime exceptions.
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
86
public void XtestRuntimeException() throws ValidatorException {
87
// Create bean to run test on.
88
ValueBean info = new ValueBean();
89
info.setValue("RUNTIME");
91
// Construct validator based on the loaded resources
93
Validator validator = new Validator(resources, FORM_KEY);
94
// add the name bean to the validator as a resource
95
// for the validations to be performed on.
96
validator.setParameter(Validator.BEAN_PARAM, info);
98
// Get results of the validation which can throw ValidatorException
100
validator.validate();
101
//fail("RuntimeException should occur here!");
102
} catch (RuntimeException expected) {
103
fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
104
// This will be true in Validator 2.0
105
//assertTrue("RUNTIME-EXCEPTION".equals(expected.getMessage()));
110
* Tests handling of checked exceptions - should become
111
* ValidatorExceptions.
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
118
public void XtestCheckedException() {
119
// Create bean to run test on.
120
ValueBean info = new ValueBean();
121
info.setValue("CHECKED");
123
// Construct validator based on the loaded resources
125
Validator validator = new Validator(resources, FORM_KEY);
126
// add the name bean to the validator as a resource
127
// for the validations to be performed on.
128
validator.setParameter(Validator.BEAN_PARAM, info);
130
// Get results of the validation which can throw ValidatorException
132
// Tests Validator 1.x exception handling
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()));
141
// This will be true in Validator 2.0
143
// validator.validate();
144
// fail("ValidatorException should occur here!");
145
// } catch (ValidatorException expected) {
146
// assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));