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