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