1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.commons.validator;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import junit.framework.TestCase;
28
29 import org.xml.sax.SAXException;
30
31 /***
32 * Tests that validator rules split between 2 different XML files get
33 * merged properly.
34 */
35 public class MultipleConfigFilesTest extends TestCase {
36
37 /***
38 * Resources used for validation tests.
39 */
40 private ValidatorResources resources = null;
41
42 /***
43 * The key used to retrieve the set of validation
44 * rules from the xml file.
45 */
46 private static final String FORM_KEY = "nameForm";
47
48 /***
49 * The key used to retrieve the validator action.
50 */
51 private static final String ACTION = "required";
52
53 /***
54 * Constructor for MultipleConfigFilesTest.
55 * @param name
56 */
57 public MultipleConfigFilesTest(String name) {
58 super(name);
59 }
60
61 /***
62 * Load <code>ValidatorResources</code> from multiple xml files.
63 */
64 protected void setUp() throws IOException, SAXException {
65 InputStream[] streams =
66 new InputStream[] {
67 this.getClass().getResourceAsStream(
68 "MultipleConfigFilesTest-1-config.xml"),
69 this.getClass().getResourceAsStream(
70 "MultipleConfigFilesTest-2-config.xml")};
71
72 this.resources = new ValidatorResources(streams);
73
74 for (int i = 0; i < streams.length; i++) {
75 streams[i].close();
76 }
77 }
78
79 /***
80 * Check the forms and constants from different config files have
81 * been merged into the same FormSet.
82 */
83 public void testMergedConfig() throws ValidatorException {
84
85
86
87
88 Form form1 = resources.getForm("", "", "", "testForm1");
89 assertNotNull("Form 'testForm1' not found", form1);
90
91
92 Form form2 = resources.getForm("", "", "", "testForm2");
93 assertNotNull("Form 'testForm2' not found", form2);
94
95
96 Field field1 = form1.getField("testProperty1");
97 assertEquals("testProperty1 - const 1", "testConstValue1", field1.getVarValue("var11"));
98 assertEquals("testProperty1 - const 2", "testConstValue2", field1.getVarValue("var12"));
99
100
101 Field field2 = form2.getField("testProperty2");
102 assertEquals("testProperty2 - const 1", "testConstValue1", field2.getVarValue("var21"));
103 assertEquals("testProperty2 - const 2", "testConstValue2", field2.getVarValue("var22"));
104
105
106
107
108 Form form1_fr = resources.getForm("fr", "", "", "testForm1_fr");
109 assertNotNull("Form 'testForm1_fr' not found", form1_fr);
110
111
112 Form form2_fr = resources.getForm("fr", "", "", "testForm2_fr");
113 assertNotNull("Form 'testForm2_fr' not found", form2_fr);
114
115
116 Field field1_fr = form1_fr.getField("testProperty1_fr");
117 assertEquals("testProperty1_fr - const 1", "testConstValue1_fr", field1_fr.getVarValue("var11_fr"));
118 assertEquals("testProperty1_fr - const 2", "testConstValue2_fr", field1_fr.getVarValue("var12_fr"));
119
120
121 Field field2_fr = form2_fr.getField("testProperty2_fr");
122 assertEquals("testProperty2_fr - const 1", "testConstValue1_fr", field2_fr.getVarValue("var21_fr"));
123 assertEquals("testProperty2_fr - const 2", "testConstValue2_fr", field2_fr.getVarValue("var22_fr"));
124 }
125
126 /***
127 * With nothing provided, we should fail both because both are required.
128 */
129 public void testBothBlank() throws ValidatorException {
130
131 NameBean name = new NameBean();
132
133
134
135 Validator validator = new Validator(resources, FORM_KEY);
136
137
138 validator.setParameter(Validator.BEAN_PARAM, name);
139
140
141 ValidatorResults results = null;
142
143
144
145
146
147 results = validator.validate();
148
149 assertNotNull("Results are null.", results);
150
151 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
152 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
153
154 assertNotNull(firstNameResult);
155 assertTrue(firstNameResult.containsAction(ACTION));
156 assertTrue(!firstNameResult.isValid(ACTION));
157
158 assertNotNull(lastNameResult);
159 assertTrue(lastNameResult.containsAction(ACTION));
160 assertTrue(!lastNameResult.isValid(ACTION));
161 assertTrue(!lastNameResult.containsAction("int"));
162 }
163
164 /***
165 * If the first name fails required, and the second test fails int, we should get two errors.
166 */
167 public void testRequiredFirstNameBlankLastNameShort()
168 throws ValidatorException {
169
170 NameBean name = new NameBean();
171 name.setFirstName("");
172 name.setLastName("Test");
173
174
175
176 Validator validator = new Validator(resources, FORM_KEY);
177
178
179 validator.setParameter(Validator.BEAN_PARAM, name);
180
181
182 ValidatorResults results = null;
183
184 results = validator.validate();
185
186 assertNotNull("Results are null.", results);
187
188 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
189 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
190
191 assertNotNull(firstNameResult);
192 assertTrue(firstNameResult.containsAction(ACTION));
193 assertTrue(!firstNameResult.isValid(ACTION));
194
195 assertNotNull(lastNameResult);
196 assertTrue(lastNameResult.containsAction("int"));
197 assertTrue(!lastNameResult.isValid("int"));
198 }
199
200 /***
201 * If the first name is there, and the last name fails int, we should get one error.
202 */
203 public void testRequiredLastNameShort() throws ValidatorException {
204
205 NameBean name = new NameBean();
206 name.setFirstName("Test");
207 name.setLastName("Test");
208
209
210
211 Validator validator = new Validator(resources, FORM_KEY);
212
213
214 validator.setParameter(Validator.BEAN_PARAM, name);
215
216
217 ValidatorResults results = null;
218
219 results = validator.validate();
220
221 assertNotNull("Results are null.", results);
222
223 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
224 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
225
226 assertNotNull(firstNameResult);
227 assertTrue(firstNameResult.containsAction(ACTION));
228 assertTrue(firstNameResult.isValid(ACTION));
229
230 assertNotNull(lastNameResult);
231 assertTrue(lastNameResult.containsAction("int"));
232 assertTrue(!lastNameResult.isValid("int"));
233 }
234
235 /***
236 * If first name is ok and last name is ok and is an int, no errors.
237 */
238 public void testRequiredLastNameLong() throws ValidatorException {
239
240 NameBean name = new NameBean();
241 name.setFirstName("Joe");
242 name.setLastName("12345678");
243
244
245
246 Validator validator = new Validator(resources, FORM_KEY);
247
248
249 validator.setParameter(Validator.BEAN_PARAM, name);
250
251
252 ValidatorResults results = null;
253
254 results = validator.validate();
255
256 assertNotNull("Results are null.", results);
257
258 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
259 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
260
261 assertNotNull(firstNameResult);
262 assertTrue(firstNameResult.containsAction(ACTION));
263 assertTrue(firstNameResult.isValid(ACTION));
264
265 assertNotNull(lastNameResult);
266 assertTrue(lastNameResult.containsAction("int"));
267 assertTrue(lastNameResult.isValid("int"));
268 }
269
270 }