~ubuntu-branches/ubuntu/oneiric/libcommons-validator-java/oneiric

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/validator/ExceptionTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath, Kumar Appaiah, Varun Hiremath
  • Date: 2007-09-16 00:57:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070916005746-r5xwnjm12ng9fbdf
Tags: 1:1.3.1-1
[ Kumar Appaiah ]
* New upstream release.
* New uploaders: Varun Hiremath and Kumar Appaiah, removed Wolfgang Baer
* Use upstream's conf directory for configuration and DTDs.
* Use a custom ant.properties to avoid downloads.
* Use DEB_UPSTREAM_VERSION in rules instead of versions in rules for symlinking.

[ Varun Hiremath ]
* debian/control:
  + Add rhino to Build-Depends-Indep and Depends.
  + Add XS-Vcs-{Svn, Browser} headers.
  + Depend on java-gcj-compat instead of kaffe.
* debian/compat: switch to 5
* Remove debian/patches and remove RELEASE-NOTES.txt in debian/rules.
* Add debian/orig-tar.sh to remove CRLF line terminators from upstream files.
* debian/rules: implement get-orig-source
* debian/watch: switch to version 3 and call debian/orig-tar.sh

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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 $
3
 
 * $Revision: 1.2 $
4
 
 * $Date: 2004/02/21 17:10:30 $
5
 
 *
6
 
 * ====================================================================
7
 
 * Copyright  2004 The Apache Software Foundation
8
 
 *
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
12
 
 *
13
 
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 
 *
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.
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("validator-exception.xml");
54
 
    }
55
 
 
56
 
    /**
57
 
     * Tests handling of checked exceptions - should become 
58
 
     * ValidatorExceptions.
59
 
     */
60
 
    public void testValidatorException() {
61
 
        // Create bean to run test on.
62
 
        ValueBean info = new ValueBean();
63
 
        info.setValue("VALIDATOR");
64
 
 
65
 
        // Construct validator based on the loaded resources 
66
 
        // and the form key
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);
71
 
 
72
 
        // Get results of the validation which can throw ValidatorException
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
 
        // Create bean to run test on.
86
 
        ValueBean info = new ValueBean();
87
 
        info.setValue("RUNTIME");
88
 
 
89
 
        // Construct validator based on the loaded resources 
90
 
        // and the form key
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);
95
 
 
96
 
        // Get results of the validation which can throw ValidatorException
97
 
        try {
98
 
            validator.validate();
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()));
104
 
        }
105
 
    }
106
 
 
107
 
    /**
108
 
     * Tests handling of checked exceptions - should become 
109
 
     * ValidatorExceptions.
110
 
     */
111
 
    public void testCheckedException() {
112
 
        // Create bean to run test on.
113
 
        ValueBean info = new ValueBean();
114
 
        info.setValue("CHECKED");
115
 
 
116
 
        // Construct validator based on the loaded resources 
117
 
        // and the form key
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);
122
 
 
123
 
        // Get results of the validation which can throw ValidatorException
124
 
        
125
 
        // Tests Validator 1.x exception handling
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
 
        // This will be true in Validator 2.0
135
 
//        try {
136
 
//            validator.validate();
137
 
//            fail("ValidatorException should occur here!");
138
 
//        } catch (ValidatorException expected) {
139
 
//            assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));
140
 
//        }
141
 
    }
142
 
}
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
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
        // Create bean to run test on.
 
59
        ValueBean info = new ValueBean();
 
60
        info.setValue("VALIDATOR");
 
61
 
 
62
        // Construct validator based on the loaded resources 
 
63
        // and the form key
 
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);
 
68
 
 
69
        // Get results of the validation which can throw ValidatorException
 
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
        // Create bean to run test on.
 
88
        ValueBean info = new ValueBean();
 
89
        info.setValue("RUNTIME");
 
90
 
 
91
        // Construct validator based on the loaded resources 
 
92
        // and the form key
 
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);
 
97
 
 
98
        // Get results of the validation which can throw ValidatorException
 
99
        try {
 
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()));
 
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
        // Create bean to run test on.
 
120
        ValueBean info = new ValueBean();
 
121
        info.setValue("CHECKED");
 
122
 
 
123
        // Construct validator based on the loaded resources 
 
124
        // and the form key
 
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);
 
129
 
 
130
        // Get results of the validation which can throw ValidatorException
 
131
        
 
132
        // Tests Validator 1.x exception handling
 
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
        // This will be true in Validator 2.0
 
142
//        try {
 
143
//            validator.validate();
 
144
//            fail("ValidatorException should occur here!");
 
145
//        } catch (ValidatorException expected) {
 
146
//            assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage()));
 
147
//        }
 
148
    }
 
149
}