001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import java.io.InputStream;
020:
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: /**
026: * <p>Performs tests for extension in form definitions. Performs the same tests
027: * RequiredNameTest does but with an equivalent validation definition with extension
028: * definitions (validator-extension.xml), plus an extra check on overriding rules and
029: * another one checking it mantains correct order when extending.</p>
030: *
031: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
032: */
033: public class ExtensionTest extends TestCase {
034:
035: /**
036: * The key used to retrieve the set of validation
037: * rules from the xml file.
038: */
039: protected static String FORM_KEY = "nameForm";
040:
041: /**
042: * The key used to retrieve the set of validation
043: * rules from the xml file.
044: */
045: protected static String FORM_KEY2 = "nameForm2";
046:
047: /**
048: * The key used to retrieve the set of validation
049: * rules from the xml file.
050: */
051: protected static String CHECK_MSG_KEY = "nameForm.lastname.displayname";
052:
053: /**
054: * The key used to retrieve the validator action.
055: */
056: protected static String ACTION = "required";
057:
058: /**
059: * Resources used for validation tests.
060: */
061: private ValidatorResources resources = null;
062:
063: /**
064: * Constructor de ExtensionTest.
065: * @param arg0
066: */
067: public ExtensionTest(String arg0) {
068: super (arg0);
069: }
070:
071: /**
072: * Start the tests.
073: *
074: * @param theArgs the arguments. Not used
075: */
076: public static void main(String[] theArgs) {
077: junit.awtui.TestRunner
078: .main(new String[] { RequiredNameTest.class.getName() });
079: }
080:
081: /**
082: * @return a test suite (<code>TestSuite</code>) that includes all methods
083: * starting with "test"
084: */
085: public static Test suite() {
086: // All methods starting with "test" will be executed in the test suite.
087: return new TestSuite(ExtensionTest.class);
088: }
089:
090: /**
091: * Load <code>ValidatorResources</code> from
092: * validator-extension.xml.
093: */
094: protected void setUp() throws Exception {
095: // Load resources
096: InputStream in = null;
097:
098: try {
099: in = this .getClass().getResourceAsStream(
100: "ExtensionTest-config.xml");
101: resources = new ValidatorResources(in);
102: } finally {
103: if (in != null) {
104: in.close();
105: }
106: }
107: }
108:
109: protected void tearDown() {
110: }
111:
112: /**
113: * Tests the required validation failure.
114: */
115: public void testRequired() throws ValidatorException {
116: // Create bean to run test on.
117: NameBean name = new NameBean();
118:
119: // Construct validator based on the loaded resources
120: // and the form key
121: Validator validator = new Validator(resources, FORM_KEY);
122: // add the name bean to the validator as a resource
123: // for the validations to be performed on.
124: validator.setParameter(Validator.BEAN_PARAM, name);
125:
126: // Get results of the validation.
127: ValidatorResults results = null;
128:
129: // throws ValidatorException,
130: // but we aren't catching for testing
131: // since no validation methods we use
132: // throw this
133: results = validator.validate();
134:
135: assertNotNull("Results are null.", results);
136:
137: ValidatorResult firstNameResult = results
138: .getValidatorResult("firstName");
139: ValidatorResult lastNameResult = results
140: .getValidatorResult("lastName");
141:
142: assertNotNull("First Name ValidatorResult should not be null.",
143: firstNameResult);
144: assertTrue("First Name ValidatorResult should contain the '"
145: + ACTION + "' action.", firstNameResult
146: .containsAction(ACTION));
147: assertTrue("First Name ValidatorResult for the '" + ACTION
148: + "' action should have failed.", !firstNameResult
149: .isValid(ACTION));
150:
151: assertNotNull("First Name ValidatorResult should not be null.",
152: lastNameResult);
153: assertTrue("Last Name ValidatorResult should contain the '"
154: + ACTION + "' action.", lastNameResult
155: .containsAction(ACTION));
156: assertTrue("Last Name ValidatorResult for the '" + ACTION
157: + "' action should have failed.", !lastNameResult
158: .isValid(ACTION));
159: }
160:
161: /**
162: * Tests the required validation for first name if it is blank.
163: */
164: public void testRequiredFirstNameBlank() throws ValidatorException {
165: // Create bean to run test on.
166: NameBean name = new NameBean();
167: name.setFirstName("");
168:
169: // Construct validator based on the loaded resources
170: // and the form key
171: Validator validator = new Validator(resources, FORM_KEY);
172: // add the name bean to the validator as a resource
173: // for the validations to be performed on.
174: validator.setParameter(Validator.BEAN_PARAM, name);
175:
176: // Get results of the validation.
177: ValidatorResults results = null;
178:
179: results = validator.validate();
180:
181: assertNotNull("Results are null.", results);
182:
183: ValidatorResult firstNameResult = results
184: .getValidatorResult("firstName");
185: ValidatorResult lastNameResult = results
186: .getValidatorResult("lastName");
187:
188: assertNotNull("First Name ValidatorResult should not be null.",
189: firstNameResult);
190: assertTrue("First Name ValidatorResult should contain the '"
191: + ACTION + "' action.", firstNameResult
192: .containsAction(ACTION));
193: assertTrue("First Name ValidatorResult for the '" + ACTION
194: + "' action should have failed.", !firstNameResult
195: .isValid(ACTION));
196:
197: assertNotNull("First Name ValidatorResult should not be null.",
198: lastNameResult);
199: assertTrue("Last Name ValidatorResult should contain the '"
200: + ACTION + "' action.", lastNameResult
201: .containsAction(ACTION));
202: assertTrue("Last Name ValidatorResult for the '" + ACTION
203: + "' action should have failed.", !lastNameResult
204: .isValid(ACTION));
205: }
206:
207: /**
208: * Tests the required validation for first name.
209: */
210: public void testRequiredFirstName() throws ValidatorException {
211: // Create bean to run test on.
212: NameBean name = new NameBean();
213: name.setFirstName("Joe");
214:
215: // Construct validator based on the loaded resources
216: // and the form key
217: Validator validator = new Validator(resources, FORM_KEY);
218: // add the name bean to the validator as a resource
219: // for the validations to be performed on.
220: validator.setParameter(Validator.BEAN_PARAM, name);
221:
222: // Get results of the validation.
223: ValidatorResults results = null;
224:
225: results = validator.validate();
226:
227: assertNotNull("Results are null.", results);
228:
229: ValidatorResult firstNameResult = results
230: .getValidatorResult("firstName");
231: ValidatorResult lastNameResult = results
232: .getValidatorResult("lastName");
233:
234: assertNotNull("First Name ValidatorResult should not be null.",
235: firstNameResult);
236: assertTrue("First Name ValidatorResult should contain the '"
237: + ACTION + "' action.", firstNameResult
238: .containsAction(ACTION));
239: assertTrue("First Name ValidatorResult for the '" + ACTION
240: + "' action should have passed.", firstNameResult
241: .isValid(ACTION));
242:
243: assertNotNull("First Name ValidatorResult should not be null.",
244: lastNameResult);
245: assertTrue("Last Name ValidatorResult should contain the '"
246: + ACTION + "' action.", lastNameResult
247: .containsAction(ACTION));
248: assertTrue("Last Name ValidatorResult for the '" + ACTION
249: + "' action should have failed.", !lastNameResult
250: .isValid(ACTION));
251: }
252:
253: /**
254: * Tests the required validation for last name if it is blank.
255: */
256: public void testRequiredLastNameBlank() throws ValidatorException {
257: // Create bean to run test on.
258: NameBean name = new NameBean();
259: name.setLastName("");
260:
261: // Construct validator based on the loaded resources
262: // and the form key
263: Validator validator = new Validator(resources, FORM_KEY);
264: // add the name bean to the validator as a resource
265: // for the validations to be performed on.
266: validator.setParameter(Validator.BEAN_PARAM, name);
267:
268: // Get results of the validation.
269: ValidatorResults results = null;
270:
271: results = validator.validate();
272:
273: assertNotNull("Results are null.", results);
274:
275: ValidatorResult firstNameResult = results
276: .getValidatorResult("firstName");
277: ValidatorResult lastNameResult = results
278: .getValidatorResult("lastName");
279:
280: assertNotNull("First Name ValidatorResult should not be null.",
281: firstNameResult);
282: assertTrue("First Name ValidatorResult should contain the '"
283: + ACTION + "' action.", firstNameResult
284: .containsAction(ACTION));
285: assertTrue("First Name ValidatorResult for the '" + ACTION
286: + "' action should have failed.", !firstNameResult
287: .isValid(ACTION));
288:
289: assertNotNull("First Name ValidatorResult should not be null.",
290: lastNameResult);
291: assertTrue("Last Name ValidatorResult should contain the '"
292: + ACTION + "' action.", lastNameResult
293: .containsAction(ACTION));
294: assertTrue("Last Name ValidatorResult for the '" + ACTION
295: + "' action should have failed.", !lastNameResult
296: .isValid(ACTION));
297: }
298:
299: /**
300: * Tests the required validation for last name.
301: */
302: public void testRequiredLastName() throws ValidatorException {
303: // Create bean to run test on.
304: NameBean name = new NameBean();
305: name.setLastName("Smith");
306:
307: // Construct validator based on the loaded resources
308: // and the form key
309: Validator validator = new Validator(resources, FORM_KEY);
310: // add the name bean to the validator as a resource
311: // for the validations to be performed on.
312: validator.setParameter(Validator.BEAN_PARAM, name);
313:
314: // Get results of the validation.
315: ValidatorResults results = null;
316:
317: results = validator.validate();
318:
319: assertNotNull("Results are null.", results);
320:
321: ValidatorResult firstNameResult = results
322: .getValidatorResult("firstName");
323: ValidatorResult lastNameResult = results
324: .getValidatorResult("lastName");
325:
326: assertNotNull("First Name ValidatorResult should not be null.",
327: firstNameResult);
328: assertTrue("First Name ValidatorResult should contain the '"
329: + ACTION + "' action.", firstNameResult
330: .containsAction(ACTION));
331: assertTrue("First Name ValidatorResult for the '" + ACTION
332: + "' action should have failed.", !firstNameResult
333: .isValid(ACTION));
334:
335: assertNotNull("First Name ValidatorResult should not be null.",
336: lastNameResult);
337: assertTrue("Last Name ValidatorResult should contain the '"
338: + ACTION + "' action.", lastNameResult
339: .containsAction(ACTION));
340: assertTrue("Last Name ValidatorResult for the '" + ACTION
341: + "' action should have passed.", lastNameResult
342: .isValid(ACTION));
343:
344: }
345:
346: /**
347: * Tests the required validation for first and last name.
348: */
349: public void testRequiredName() throws ValidatorException {
350: // Create bean to run test on.
351: NameBean name = new NameBean();
352: name.setFirstName("Joe");
353: name.setLastName("Smith");
354:
355: // Construct validator based on the loaded resources
356: // and the form key
357: Validator validator = new Validator(resources, FORM_KEY);
358: // add the name bean to the validator as a resource
359: // for the validations to be performed on.
360: validator.setParameter(Validator.BEAN_PARAM, name);
361:
362: // Get results of the validation.
363: ValidatorResults results = null;
364:
365: results = validator.validate();
366:
367: assertNotNull("Results are null.", results);
368:
369: ValidatorResult firstNameResult = results
370: .getValidatorResult("firstName");
371: ValidatorResult lastNameResult = results
372: .getValidatorResult("lastName");
373:
374: assertNotNull("First Name ValidatorResult should not be null.",
375: firstNameResult);
376: assertTrue("First Name ValidatorResult should contain the '"
377: + ACTION + "' action.", firstNameResult
378: .containsAction(ACTION));
379: assertTrue("First Name ValidatorResult for the '" + ACTION
380: + "' action should have passed.", firstNameResult
381: .isValid(ACTION));
382:
383: assertNotNull("Last Name ValidatorResult should not be null.",
384: lastNameResult);
385: assertTrue("Last Name ValidatorResult should contain the '"
386: + ACTION + "' action.", lastNameResult
387: .containsAction(ACTION));
388: assertTrue("Last Name ValidatorResult for the '" + ACTION
389: + "' action should have passed.", lastNameResult
390: .isValid(ACTION));
391: }
392:
393: /**
394: * Tests if we can override a rule. We "can" override a rule if the message shown
395: * when the firstName required test fails and the lastName test is null.
396: */
397: public void testOverrideRule() throws ValidatorException {
398:
399: // Create bean to run test on.
400: NameBean name = new NameBean();
401: name.setLastName("Smith");
402:
403: // Construct validator based on the loaded resources
404: // and the form key
405: Validator validator = new Validator(resources, FORM_KEY2);
406: // add the name bean to the validator as a resource
407: // for the validations to be performed on.
408: validator.setParameter(Validator.BEAN_PARAM, name);
409:
410: // Get results of the validation.
411: ValidatorResults results = null;
412:
413: results = validator.validate();
414:
415: assertNotNull("Results are null.", results);
416:
417: ValidatorResult firstNameResult = results
418: .getValidatorResult("firstName");
419: ValidatorResult lastNameResult = results
420: .getValidatorResult("lastName");
421: assertNotNull("First Name ValidatorResult should not be null.",
422: firstNameResult);
423: assertTrue("First Name ValidatorResult for the '" + ACTION
424: + "' action should have '" + CHECK_MSG_KEY
425: + " as a key.", firstNameResult.field.getArg(0)
426: .getKey().equals(CHECK_MSG_KEY));
427:
428: assertNull("Last Name ValidatorResult should be null.",
429: lastNameResult);
430: }
431:
432: /**
433: * Tests if the order is mantained when extending a form. Parent form fields should
434: * preceed self form fields, except if we override the rules.
435: */
436: public void testOrder() {
437:
438: Form form = resources.getForm(ValidatorResources.defaultLocale,
439: FORM_KEY);
440: Form form2 = resources.getForm(
441: ValidatorResources.defaultLocale, FORM_KEY2);
442:
443: assertNotNull(FORM_KEY + " is null.", form);
444: assertTrue("There should only be 2 fields in " + FORM_KEY, form
445: .getFields().size() == 2);
446:
447: assertNotNull(FORM_KEY2 + " is null.", form2);
448: assertTrue("There should only be 2 fields in " + FORM_KEY2,
449: form2.getFields().size() == 2);
450:
451: //get the first field
452: Field fieldFirstName = (Field) form.getFields().get(0);
453: //get the second field
454: Field fieldLastName = (Field) form.getFields().get(1);
455: assertTrue("firstName in " + FORM_KEY
456: + " should be the first in the list", fieldFirstName
457: .getKey().equals("firstName"));
458: assertTrue("lastName in " + FORM_KEY
459: + " should be the first in the list", fieldLastName
460: .getKey().equals("lastName"));
461:
462: // get the second field
463: fieldLastName = (Field) form2.getFields().get(0);
464: //get the first field
465: fieldFirstName = (Field) form2.getFields().get(1);
466: assertTrue("firstName in " + FORM_KEY2
467: + " should be the first in the list", fieldFirstName
468: .getKey().equals("firstName"));
469: assertTrue("lastName in " + FORM_KEY2
470: + " should be the first in the list", fieldLastName
471: .getKey().equals("lastName"));
472:
473: }
474: }
|