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 * Abstracts number unit tests methods.
30 */
31 abstract public class TestNumber extends TestCommon {
32
33 /***
34 * The key used to retrieve the set of validation
35 * rules from the xml file.
36 */
37 protected String FORM_KEY;
38
39 /***
40 * The key used to retrieve the validator action.
41 */
42 protected String ACTION;
43
44
45 public TestNumber(String name) {
46 super(name);
47 }
48
49 /***
50 * Load <code>ValidatorResources</code> from
51 * validator-numeric.xml.
52 */
53 protected void setUp() throws IOException, SAXException {
54
55 loadResources("validator-numeric.xml");
56 }
57
58 protected void tearDown() {
59 }
60
61 /***
62 * Tests the number validation.
63 */
64 public void testNumber() throws ValidatorException {
65
66 ValueBean info = new ValueBean();
67 info.setValue("0");
68 if (log.isDebugEnabled()) {
69 log.debug("testNumberFailure Action=" + ACTION + ", FORM_KEY=" + FORM_KEY);
70 }
71 valueTest(info, true);
72 }
73
74 /***
75 * Tests the float validation failure.
76 */
77 public void testNumberFailure() throws ValidatorException {
78
79 ValueBean info = new ValueBean();
80 if (log.isDebugEnabled()) {
81 log.debug("testNumberFailure Action=" + ACTION + ", FORM_KEY=" + FORM_KEY);
82 }
83 valueTest(info, false);
84 }
85
86 /***
87 * Utlity class to run a test on a value.
88 *
89 * @param info Value to run test on.
90 * @param passed Whether or not the test is expected to pass.
91 */
92 protected void valueTest(Object info, boolean passed) throws ValidatorException {
93
94
95 Validator validator = new Validator(resources, FORM_KEY);
96
97
98 validator.setParameter(Validator.BEAN_PARAM, info);
99
100
101 ValidatorResults results = null;
102
103
104
105
106
107 results = validator.validate();
108
109 assertNotNull("Results are null.", results);
110
111 ValidatorResult result = results.getValidatorResult("value");
112
113 assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
114 assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
115 assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
116 }
117
118
119 }