001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import java.io.IOException;
020:
021: import org.xml.sax.SAXException;
022:
023: /**
024: * Performs Validation Test for exception handling.
025: *
026: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
027: */
028: public class ExceptionTest extends TestCommon {
029:
030: /**
031: * The key used to retrieve the set of validation
032: * rules from the xml file.
033: */
034: protected static String FORM_KEY = "exceptionForm";
035:
036: /**
037: * The key used to retrieve the validator action.
038: */
039: protected static String ACTION = "raiseException";
040:
041: public ExceptionTest(String name) {
042: super (name);
043: }
044:
045: /**
046: * Load <code>ValidatorResources</code> from
047: * validator-exception.xml.
048: */
049: protected void setUp() throws IOException, SAXException {
050: loadResources("ExceptionTest-config.xml");
051: }
052:
053: /**
054: * Tests handling of checked exceptions - should become
055: * ValidatorExceptions.
056: */
057: public void testValidatorException() {
058: // Create bean to run test on.
059: ValueBean info = new ValueBean();
060: info.setValue("VALIDATOR");
061:
062: // Construct validator based on the loaded resources
063: // and the form key
064: Validator validator = new Validator(resources, FORM_KEY);
065: // add the name bean to the validator as a resource
066: // for the validations to be performed on.
067: validator.setParameter(Validator.BEAN_PARAM, info);
068:
069: // Get results of the validation which can throw ValidatorException
070: try {
071: validator.validate();
072: fail("ValidatorException should occur here!");
073: } catch (ValidatorException expected) {
074: assertTrue("VALIDATOR-EXCEPTION".equals(expected
075: .getMessage()));
076: }
077: }
078:
079: /**
080: * Tests handling of runtime exceptions.
081: *
082: * N.B. This test has been removed (renamed) as it currently
083: * serves no purpose. If/When exception handling
084: * is changed in Validator 2.0 it can be reconsidered
085: * then.
086: */
087: public void XtestRuntimeException() throws ValidatorException {
088: // Create bean to run test on.
089: ValueBean info = new ValueBean();
090: info.setValue("RUNTIME");
091:
092: // Construct validator based on the loaded resources
093: // and the form key
094: Validator validator = new Validator(resources, FORM_KEY);
095: // add the name bean to the validator as a resource
096: // for the validations to be performed on.
097: validator.setParameter(Validator.BEAN_PARAM, info);
098:
099: // Get results of the validation which can throw ValidatorException
100: try {
101: validator.validate();
102: //fail("RuntimeException should occur here!");
103: } catch (RuntimeException expected) {
104: fail("RuntimeExceptions should be treated as validation failures in Validator 1.x.");
105: // This will be true in Validator 2.0
106: //assertTrue("RUNTIME-EXCEPTION".equals(expected.getMessage()));
107: }
108: }
109:
110: /**
111: * Tests handling of checked exceptions - should become
112: * ValidatorExceptions.
113: *
114: * N.B. This test has been removed (renamed) as it currently
115: * serves no purpose. If/When exception handling
116: * is changed in Validator 2.0 it can be reconsidered
117: * then.
118: */
119: public void XtestCheckedException() {
120: // Create bean to run test on.
121: ValueBean info = new ValueBean();
122: info.setValue("CHECKED");
123:
124: // Construct validator based on the loaded resources
125: // and the form key
126: Validator validator = new Validator(resources, FORM_KEY);
127: // add the name bean to the validator as a resource
128: // for the validations to be performed on.
129: validator.setParameter(Validator.BEAN_PARAM, info);
130:
131: // Get results of the validation which can throw ValidatorException
132:
133: // Tests Validator 1.x exception handling
134: try {
135: validator.validate();
136: } catch (ValidatorException expected) {
137: fail("Checked exceptions are not wrapped in ValidatorException in Validator 1.x.");
138: } catch (Exception e) {
139: assertTrue("CHECKED-EXCEPTION".equals(e.getMessage()));
140: }
141:
142: // This will be true in Validator 2.0
143: // try {
144: // validator.validate();
145: // fail("ValidatorException should occur here!");
146: // } catch (ValidatorException expected) {
147: // assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));
148: // }
149: }
150: }
|