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