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.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.xml.sax.SAXException;
30
31
32
33
34 class MultipleConfigFilesTest {
35
36
37
38
39 private static final String FORM_KEY = "nameForm";
40
41
42
43
44 private static final String ACTION = "required";
45
46
47
48
49 private ValidatorResources resources;
50
51
52
53
54 @BeforeEach
55 protected void setUp() throws IOException, SAXException {
56 final InputStream[] streams = { this.getClass().getResourceAsStream("MultipleConfigFilesTest-1-config.xml"),
57 this.getClass().getResourceAsStream("MultipleConfigFilesTest-2-config.xml") };
58
59 resources = new ValidatorResources(streams);
60
61 for (final InputStream stream : streams) {
62 stream.close();
63 }
64 }
65
66
67
68
69 @Test
70 void testBothBlank() throws ValidatorException {
71
72 final NameBean name = new NameBean();
73
74
75
76 final Validator validator = new Validator(resources, FORM_KEY);
77
78
79 validator.setParameter(Validator.BEAN_PARAM, name);
80
81
82
83
84
85
86 final ValidatorResults results = validator.validate();
87
88 assertNotNull(results, "Results are null.");
89
90 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
91 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
92
93 assertNotNull(firstNameResult);
94 assertTrue(firstNameResult.containsAction(ACTION));
95 assertFalse(firstNameResult.isValid(ACTION));
96
97 assertNotNull(lastNameResult);
98 assertTrue(lastNameResult.containsAction(ACTION));
99 assertFalse(lastNameResult.isValid(ACTION));
100 assertFalse(lastNameResult.containsAction("int"));
101 }
102
103
104
105
106 @Test
107 void testMergedConfig() {
108
109
110
111
112 final Form form1 = resources.getForm("", "", "", "testForm1");
113 assertNotNull(form1, "Form 'testForm1' not found");
114
115
116 final Form form2 = resources.getForm("", "", "", "testForm2");
117 assertNotNull(form2, "Form 'testForm2' not found");
118
119
120 final Field field1 = form1.getField("testProperty1");
121 assertEquals("testConstValue1", field1.getVarValue("var11"), "testProperty1 - const 1");
122 assertEquals("testConstValue2", field1.getVarValue("var12"), "testProperty1 - const 2");
123
124
125 final Field field2 = form2.getField("testProperty2");
126 assertEquals("testConstValue1", field2.getVarValue("var21"), "testProperty2 - const 1");
127 assertEquals("testConstValue2", field2.getVarValue("var22"), "testProperty2 - const 2");
128
129
130
131
132 final Form form1Fr = resources.getForm("fr", "", "", "testForm1_fr");
133 assertNotNull(form1Fr, "Form 'testForm1_fr' not found");
134
135
136 final Form form2Fr = resources.getForm("fr", "", "", "testForm2_fr");
137 assertNotNull(form2Fr, "Form 'testForm2_fr' not found");
138
139
140 final Field field1Fr = form1Fr.getField("testProperty1_fr");
141 assertEquals("testConstValue1_fr", field1Fr.getVarValue("var11_fr"), "testProperty1_fr - const 1");
142 assertEquals("testConstValue2_fr", field1Fr.getVarValue("var12_fr"), "testProperty1_fr - const 2");
143
144
145 final Field field2Fr = form2Fr.getField("testProperty2_fr");
146 assertEquals("testConstValue1_fr", field2Fr.getVarValue("var21_fr"), "testProperty2_fr - const 1");
147 assertEquals("testConstValue2_fr", field2Fr.getVarValue("var22_fr"), "testProperty2_fr - const 2");
148 }
149
150
151
152
153 @Test
154 void testRequiredFirstNameBlankLastNameShort() throws ValidatorException {
155
156 final NameBean name = new NameBean();
157 name.setFirstName("");
158 name.setLastName("Test");
159
160
161
162 final Validator validator = new Validator(resources, FORM_KEY);
163
164
165 validator.setParameter(Validator.BEAN_PARAM, name);
166
167
168 final ValidatorResults results = validator.validate();
169
170 assertNotNull(results, "Results are null.");
171
172 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
173 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
174
175 assertNotNull(firstNameResult);
176 assertTrue(firstNameResult.containsAction(ACTION));
177 assertFalse(firstNameResult.isValid(ACTION));
178
179 assertNotNull(lastNameResult);
180 assertTrue(lastNameResult.containsAction("int"));
181 assertFalse(lastNameResult.isValid("int"));
182 }
183
184
185
186
187 @Test
188 void testRequiredLastNameLong() throws ValidatorException {
189
190 final NameBean name = new NameBean();
191 name.setFirstName("Joe");
192 name.setLastName("12345678");
193
194
195
196 final Validator validator = new Validator(resources, FORM_KEY);
197
198
199 validator.setParameter(Validator.BEAN_PARAM, name);
200
201
202 final ValidatorResults results = validator.validate();
203
204 assertNotNull(results, "Results are null.");
205
206 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
207 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
208
209 assertNotNull(firstNameResult);
210 assertTrue(firstNameResult.containsAction(ACTION));
211 assertTrue(firstNameResult.isValid(ACTION));
212
213 assertNotNull(lastNameResult);
214 assertTrue(lastNameResult.containsAction("int"));
215 assertTrue(lastNameResult.isValid("int"));
216 }
217
218
219
220
221 @Test
222 void testRequiredLastNameShort() throws ValidatorException {
223
224 final NameBean name = new NameBean();
225 name.setFirstName("Test");
226 name.setLastName("Test");
227
228
229
230 final Validator validator = new Validator(resources, FORM_KEY);
231
232
233 validator.setParameter(Validator.BEAN_PARAM, name);
234
235
236 final ValidatorResults results = validator.validate();
237
238 assertNotNull(results, "Results are null.");
239
240 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
241 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
242
243 assertNotNull(firstNameResult);
244 assertTrue(firstNameResult.containsAction(ACTION));
245 assertTrue(firstNameResult.isValid(ACTION));
246
247 assertNotNull(lastNameResult);
248 assertTrue(lastNameResult.containsAction("int"));
249 assertFalse(lastNameResult.isValid("int"));
250 }
251
252 }