1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.commons.validator;
24
25 import java.io.IOException;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.xml.sax.SAXException;
31
32 /***
33 * Performs Validation Test.
34 */
35 public class RequiredIfTest extends TestCommon {
36
37 /***
38 * The key used to retrieve the set of validation
39 * rules from the xml file.
40 */
41 protected static String FORM_KEY = "nameForm";
42
43 /***
44 * The key used to retrieve the validator action.
45 */
46 protected static String ACTION = "requiredif";
47
48 public RequiredIfTest(String name) {
49 super(name);
50 }
51
52 /***
53 * Start the tests.
54 *
55 * @param theArgs the arguments. Not used
56 */
57 public static void main(String[] theArgs) {
58 junit.awtui.TestRunner.main(new String[] {RequiredIfTest.class.getName()});
59 }
60
61 /***
62 * @return a test suite (<code>TestSuite</code>) that includes all methods
63 * starting with "test"
64 */
65 public static Test suite() {
66
67 return new TestSuite(RequiredIfTest.class);
68 }
69
70 /***
71 * Load <code>ValidatorResources</code> from
72 * validator-requiredif.xml.
73 */
74 protected void setUp() throws IOException, SAXException {
75
76 loadResources("validator-requiredif.xml");
77 }
78
79 protected void tearDown() {
80 }
81
82 /***
83 * With nothing provided, we should pass since the fields only fail on
84 * null if the other field is non-blank.
85 */
86 public void testRequired() throws ValidatorException {
87
88 NameBean name = new NameBean();
89
90
91
92 Validator validator = new Validator(resources, FORM_KEY);
93
94
95 validator.setParameter(Validator.BEAN_PARAM, name);
96
97
98 ValidatorResults results = null;
99
100
101
102
103
104 results = validator.validate();
105
106 assertNotNull("Results are null.", results);
107
108 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
109 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
110
111 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
112 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
113 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
114
115 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
116 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
117 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
118 }
119
120 /***
121 * Tests the required validation for first name if it is blank.
122 */
123 public void testRequiredFirstNameBlank() throws ValidatorException {
124
125 NameBean name = new NameBean();
126 name.setFirstName("");
127 name.setLastName("Test");
128
129
130
131 Validator validator = new Validator(resources, FORM_KEY);
132
133
134 validator.setParameter(Validator.BEAN_PARAM, name);
135
136
137 ValidatorResults results = null;
138
139 results = validator.validate();
140
141 assertNotNull("Results are null.", results);
142
143 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
144 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
145
146 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
147 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
148 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
149
150 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
151 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
152 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
153 }
154
155 /***
156 * Tests the required validation for last name.
157 */
158 public void testRequiredFirstName() throws ValidatorException {
159
160 NameBean name = new NameBean();
161 name.setFirstName("Test");
162 name.setLastName("Test");
163
164
165
166 Validator validator = new Validator(resources, FORM_KEY);
167
168
169 validator.setParameter(Validator.BEAN_PARAM, name);
170
171
172 ValidatorResults results = null;
173
174 results = validator.validate();
175
176 assertNotNull("Results are null.", results);
177
178 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
179 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
180
181 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
182 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
183 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
184
185 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
186 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
187 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
188 }
189
190 /***
191 * Tests the required validation for last name if it is blank.
192 */
193 public void testRequiredLastNameBlank() throws ValidatorException {
194
195 NameBean name = new NameBean();
196 name.setFirstName("Joe");
197 name.setLastName("");
198
199
200
201 Validator validator = new Validator(resources, FORM_KEY);
202
203
204 validator.setParameter(Validator.BEAN_PARAM, name);
205
206
207 ValidatorResults results = null;
208
209 results = validator.validate();
210
211 assertNotNull("Results are null.", results);
212
213 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
214 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
215
216 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
217 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
218 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
219
220 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
221 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
222 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
223 }
224
225 /***
226 * Tests the required validation for last name.
227 */
228 public void testRequiredLastName() throws ValidatorException {
229
230 NameBean name = new NameBean();
231 name.setFirstName("Joe");
232 name.setLastName("Smith");
233
234
235
236 Validator validator = new Validator(resources, FORM_KEY);
237
238
239 validator.setParameter(Validator.BEAN_PARAM, name);
240
241
242 ValidatorResults results = null;
243
244 results = validator.validate();
245
246 assertNotNull("Results are null.", results);
247
248 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
249 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
250
251 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
252 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
253 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
254
255 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
256 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
257 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
258
259 }
260
261 }