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