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 RequiredIfTest extends AbstractCommonTest {
34
35
36
37
38 protected static final String FORM_KEY = "nameForm";
39
40
41
42
43 protected static final String ACTION = "requiredif";
44
45
46
47
48 @BeforeEach
49 protected void setUp() throws IOException, SAXException {
50
51 loadResources("RequiredIfTest-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 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
88
89 assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
90 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
91 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
92 }
93
94
95
96
97 @Test
98 void testRequiredFirstName() throws ValidatorException {
99
100 final NameBean name = new NameBean();
101 name.setFirstName("Test");
102 name.setLastName("Test");
103
104
105
106 final Validator validator = new Validator(resources, FORM_KEY);
107
108
109 validator.setParameter(Validator.BEAN_PARAM, name);
110
111
112 final ValidatorResults results = validator.validate();
113
114 assertNotNull(results, "Results are null.");
115
116 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
117 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
118
119 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
120 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
121 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
122
123 assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
124 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
125 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
126 }
127
128
129
130
131 @Test
132 void testRequiredFirstNameBlank() throws ValidatorException {
133
134 final NameBean name = new NameBean();
135 name.setFirstName("");
136 name.setLastName("Test");
137
138
139
140 final Validator validator = new Validator(resources, FORM_KEY);
141
142
143 validator.setParameter(Validator.BEAN_PARAM, name);
144
145
146 final ValidatorResults results = validator.validate();
147
148 assertNotNull(results, "Results are null.");
149
150 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
151 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
152
153 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
154 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
155 assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
156
157 assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
158 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
159 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
160 }
161
162
163
164
165 @Test
166 void testRequiredLastName() throws ValidatorException {
167
168 final NameBean name = new NameBean();
169 name.setFirstName("Joe");
170 name.setLastName("Smith");
171
172
173
174 final Validator validator = new Validator(resources, FORM_KEY);
175
176
177 validator.setParameter(Validator.BEAN_PARAM, name);
178
179
180 final ValidatorResults results = validator.validate();
181
182 assertNotNull(results, "Results are null.");
183
184 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
185 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
186
187 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
188 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
189 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
190
191 assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
192 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
193 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
194
195 }
196
197
198
199
200 @Test
201 void testRequiredLastNameBlank() throws ValidatorException {
202
203 final NameBean name = new NameBean();
204 name.setFirstName("Joe");
205 name.setLastName("");
206
207
208
209 final Validator validator = new Validator(resources, FORM_KEY);
210
211
212 validator.setParameter(Validator.BEAN_PARAM, name);
213
214
215 final ValidatorResults results = validator.validate();
216
217 assertNotNull(results, "Results are null.");
218
219 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
220 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
221
222 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
223 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
224 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
225
226 assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
227 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
228 assertFalse(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
229 }
230
231 }