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 import java.util.Iterator;
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import org.xml.sax.SAXException;
25
26 /***
27 * Test ValidatorResults.
28 *
29 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
30 */
31 public class ValidatorResultsTest extends TestCommon {
32
33 private static final String FORM_KEY = "nameForm";
34 private static final String firstNameField = "firstName";
35 private static final String middleNameField = "middleName";
36 private static final String lastNameField = "lastName";
37
38 private String firstName;
39 private String middleName;
40 private String lastName;
41
42 /***
43 * Constructor.
44 */
45 public ValidatorResultsTest(String name) {
46 super(name);
47 }
48
49 /***
50 * Start the tests.
51 *
52 * @param theArgs the arguments. Not used
53 */
54 public static void main(String[] theArgs) {
55 junit.awtui.TestRunner.main(new String[] {ValidatorResultsTest.class.getName()});
56 }
57
58 /***
59 * @return a test suite (<code>TestSuite</code>) that includes all methods
60 * starting with "test"
61 */
62 public static Test suite() {
63
64 return new TestSuite(ValidatorResultsTest.class);
65 }
66
67 /***
68 * Load <code>ValidatorResources</code> from
69 * ValidatorResultsTest-config.xml.
70 */
71 protected void setUp() throws IOException, SAXException {
72
73 loadResources("ValidatorResultsTest-config.xml");
74
75
76 firstName = "foo";
77 middleName = "123";
78 lastName = "456";
79
80 }
81
82 protected void tearDown() {
83 }
84
85 /***
86 * Test all validations ran and passed.
87 */
88 public void testAllValid() throws ValidatorException {
89
90
91 NameBean bean = createNameBean();
92
93
94 ValidatorResults results = validate(bean);
95
96
97 checkValidatorResult(results, firstNameField, "required", true);
98 checkValidatorResult(results, middleNameField, "required", true);
99 checkValidatorResult(results, middleNameField, "int", true);
100 checkValidatorResult(results, middleNameField, "positive", true);
101 checkValidatorResult(results, lastNameField, "required", true);
102 checkValidatorResult(results, lastNameField, "int", true);
103
104 }
105
106 /***
107 * Test some validations failed and some didn't run.
108 */
109 public void testErrors() throws ValidatorException {
110
111 middleName = "XXX";
112 lastName = null;
113
114
115 NameBean bean = createNameBean();
116
117
118 ValidatorResults results = validate(bean);
119
120
121 checkValidatorResult(results, firstNameField, "required", true);
122 checkValidatorResult(results, middleNameField, "required", true);
123 checkValidatorResult(results, middleNameField, "int", false);
124 checkNotRun(results, middleNameField, "positive");
125 checkValidatorResult(results, lastNameField, "required", false);
126 checkNotRun(results, lastNameField, "int");
127
128 }
129
130 /***
131 * Check a validator has not been run for a field and the result.
132 */
133 private void checkNotRun(ValidatorResults results, String field, String action) {
134 ValidatorResult result = results.getValidatorResult(field);
135 assertNotNull(field + " result", result);
136 assertFalse(field + "[" + action + "] run", result.containsAction(action));
137
138 }
139
140 /***
141 * Check a validator has run for a field and the result.
142 */
143 private void checkValidatorResult(ValidatorResults results, String field, String action, boolean expected) {
144 ValidatorResult result = results.getValidatorResult(field);
145
146 assertNotNull(field + " result", result);
147 assertTrue(field + "[" + action + "] not run", result.containsAction(action));
148 assertEquals(field + "[" + action + "] result", expected, result.isValid(action));
149 }
150
151 /***
152 * Create a NameBean.
153 */
154 private NameBean createNameBean() {
155 NameBean name = new NameBean();
156 name.setFirstName(firstName);
157 name.setMiddleName(middleName);
158 name.setLastName(lastName);
159 return name;
160 }
161
162 /***
163 * Validate results.
164 */
165 private ValidatorResults validate(Object bean) throws ValidatorException {
166
167
168
169 Validator validator = new Validator(resources, FORM_KEY);
170
171
172
173 validator.setParameter(Validator.BEAN_PARAM, bean);
174
175
176 ValidatorResults results = validator.validate();
177
178 return results;
179
180 }
181
182 }