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