1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import java.io.IOException;
20
21 import org.xml.sax.SAXException;
22
23 /***
24 * Abstracts number unit tests methods.
25 *
26 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
27 */
28 abstract public class TestNumber extends TestCommon {
29
30 /***
31 * The key used to retrieve the set of validation
32 * rules from the xml file.
33 */
34 protected String FORM_KEY;
35
36 /***
37 * The key used to retrieve the validator action.
38 */
39 protected String ACTION;
40
41
42 public TestNumber(String name) {
43 super(name);
44 }
45
46 /***
47 * Load <code>ValidatorResources</code> from
48 * validator-numeric.xml.
49 */
50 protected void setUp() throws IOException, SAXException {
51
52 loadResources("TestNumber-config.xml");
53 }
54
55 protected void tearDown() {
56 }
57
58 /***
59 * Tests the number validation.
60 */
61 public void testNumber() throws ValidatorException {
62
63 ValueBean info = new ValueBean();
64 info.setValue("0");
65 valueTest(info, true);
66 }
67
68 /***
69 * Tests the float validation failure.
70 */
71 public void testNumberFailure() throws ValidatorException {
72
73 ValueBean info = new ValueBean();
74 valueTest(info, false);
75 }
76
77 /***
78 * Utlity class to run a test on a value.
79 *
80 * @param info Value to run test on.
81 * @param passed Whether or not the test is expected to pass.
82 */
83 protected void valueTest(Object info, boolean passed) throws ValidatorException {
84
85
86 Validator validator = new Validator(resources, FORM_KEY);
87
88
89 validator.setParameter(Validator.BEAN_PARAM, info);
90
91
92 ValidatorResults results = null;
93
94
95
96
97
98 results = validator.validate();
99
100 assertNotNull("Results are null.", results);
101
102 ValidatorResult result = results.getValidatorResult("value");
103
104 assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
105 assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
106 assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
107 }
108
109
110 }