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 static org.junit.jupiter.api.Assertions.assertFalse;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.IOException;
24
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.xml.sax.SAXException;
29
30
31
32
33 class RequiredNameTest extends AbstractCommonTest {
34
35
36
37
38 protected static final String FORM_KEY = "nameForm";
39
40
41
42
43 protected static final String ACTION = "required";
44
45
46
47
48 @BeforeEach
49 protected void setUp() throws IOException, SAXException {
50
51 loadResources("RequiredNameTest-config.xml");
52 }
53
54 @AfterEach
55 protected void tearDown() {
56 }
57
58
59
60
61 @Test
62 void testRequired() throws ValidatorException {
63
64 final NameBean name = new NameBean();
65
66
67
68 final Validator validator = new Validator(resources, FORM_KEY);
69
70
71 validator.setParameter(Validator.BEAN_PARAM, name);
72
73
74
75
76
77
78 final ValidatorResults results = validator.validate();
79
80 assertNotNull(results, "Results are null.");
81
82 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
83 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
84
85 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
86 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
87 assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
88
89 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
90 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
91 assertFalse(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
92 }
93
94
95
96
97 @Test
98 void testRequiredFirstName() throws ValidatorException {
99
100 final NameBean name = new NameBean();
101 name.setFirstName("Joe");
102
103
104
105 final Validator validator = new Validator(resources, FORM_KEY);
106
107
108 validator.setParameter(Validator.BEAN_PARAM, name);
109
110
111 final ValidatorResults results = validator.validate();
112
113 assertNotNull(results, "Results are null.");
114
115 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
116 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
117
118 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
119 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
120 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
121
122 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
123 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
124 assertFalse(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
125 }
126
127
128
129
130 @Test
131 void testRequiredFirstNameBlank() throws ValidatorException {
132
133 final NameBean name = new NameBean();
134 name.setFirstName("");
135
136
137
138 final Validator validator = new Validator(resources, FORM_KEY);
139
140
141 validator.setParameter(Validator.BEAN_PARAM, name);
142
143
144 final ValidatorResults results = validator.validate();
145
146 assertNotNull(results, "Results are null.");
147
148 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
149 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
150
151 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
152 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
153 assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
154
155 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
156 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
157 assertFalse(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
158 }
159
160
161
162
163 @Test
164 void testRequiredLastName() throws ValidatorException {
165
166 final NameBean name = new NameBean();
167 name.setLastName("Smith");
168
169
170
171 final Validator validator = new Validator(resources, FORM_KEY);
172
173
174 validator.setParameter(Validator.BEAN_PARAM, name);
175
176
177 final ValidatorResults results = validator.validate();
178
179 assertNotNull(results, "Results are null.");
180
181 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
182 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
183
184 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
185 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
186 assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
187
188 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
189 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
190 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
191
192 }
193
194
195
196
197 @Test
198 void testRequiredLastNameBlank() throws ValidatorException {
199
200 final NameBean name = new NameBean();
201 name.setLastName("");
202
203
204
205 final Validator validator = new Validator(resources, FORM_KEY);
206
207
208 validator.setParameter(Validator.BEAN_PARAM, name);
209
210
211 final ValidatorResults results = validator.validate();
212
213 assertNotNull(results, "Results are null.");
214
215 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
216 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
217
218 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
219 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
220 assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
221
222 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
223 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
224 assertFalse(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
225 }
226
227
228
229
230 @Test
231 void testRequiredName() throws ValidatorException {
232
233 final NameBean name = new NameBean();
234 name.setFirstName("Joe");
235 name.setLastName("Smith");
236
237
238
239 final Validator validator = new Validator(resources, FORM_KEY);
240
241
242 validator.setParameter(Validator.BEAN_PARAM, name);
243
244
245 final ValidatorResults results = validator.validate();
246
247 assertNotNull(results, "Results are null.");
248
249 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
250 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
251
252 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
253 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
254 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
255
256 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
257 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
258 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
259 }
260
261 }