001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestValidation.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import com.uwyn.rife.tools.ExceptionUtils;
011: import java.text.SimpleDateFormat;
012: import java.util.Date;
013: import java.util.Iterator;
014: import java.util.LinkedHashSet;
015: import java.util.List;
016: import java.util.Set;
017: import junit.framework.TestCase;
018:
019: public class TestValidation extends TestCase {
020: public TestValidation(String name) {
021: super (name);
022: }
023:
024: public void testInstantiation() {
025: Bean bean = new Bean("value");
026: assertNotNull(bean);
027: assertEquals(0, bean.countValidationErrors());
028: assertEquals(0, bean.getValidationErrors().size());
029: assertEquals(0, bean.getConstrainedProperties().size());
030: assertEquals(0, bean.getRules().size());
031: assertEquals(0, bean.getGroups().size());
032: assertNull(bean.getConstrainedBean());
033: assertTrue(bean.isSubjectValid("property"));
034: assertTrue(bean.validate());
035: }
036:
037: public void testValidRule() {
038: Bean bean = new Bean("value");
039: bean.addRule(null);
040: ValidationRule rule1 = new ValidationRuleNotEmpty("property")
041: .setBean(bean);
042: ValidationRule rule2 = new ValidationRuleNotNull("property");
043: bean.addRule(rule1);
044: bean.addRule(rule2);
045:
046: List<ValidationRule> rules = bean.getRules();
047: assertEquals(2, rules.size());
048: assertSame(rule1, rules.get(0));
049: assertSame(rule2, rules.get(1));
050:
051: assertTrue(bean.validate());
052: assertEquals(0, bean.countValidationErrors());
053: assertEquals(0, bean.getValidationErrors().size());
054: assertTrue(bean.isSubjectValid("property"));
055: }
056:
057: public void testInvalidRule() {
058: Bean bean = new Bean(null);
059: bean.addRule(null);
060: ValidationRule rule1 = new ValidationRuleNotEmpty("property");
061: ValidationRule rule2 = new ValidationRuleNotNull("property");
062: bean.addRule(rule1);
063: bean.addRule(rule2);
064: assertFalse(bean.validate());
065: assertEquals(1, bean.countValidationErrors());
066: assertEquals(1, bean.getValidationErrors().size());
067: assertFalse(bean.isSubjectValid("property"));
068: assertTrue(bean.isSubjectValid("property_unknown"));
069: Iterator<ValidationError> it = bean.getValidationErrors()
070: .iterator();
071: assertTrue(it.hasNext());
072: ValidationError error = it.next();
073: assertFalse(it.hasNext());
074: assertEquals(ValidationError.IDENTIFIER_MANDATORY, error
075: .getIdentifier());
076: assertEquals("property", error.getSubject());
077: }
078:
079: public void testGetGroupIllegalArgument() {
080: Bean bean = new Bean("value");
081: assertNull(bean.getGroup(null));
082: assertNull(bean.getGroup(""));
083: assertNull(bean.getGroup("group2"));
084: }
085:
086: public void testAddGroup() {
087: Bean bean = new Bean("value");
088: ValidationGroup group1 = bean.addGroup("group1").addRule(
089: new ValidationRuleNotEmpty("property")).addRule(
090: new ValidationRuleNotNull("property"));
091: ValidationGroup group2 = bean.addGroup("group2").addRule(
092: new ValidationRuleNotNull("theDate"));
093:
094: assertEquals(2, bean.getGroups().size());
095: assertSame(group1, bean.getGroup("group1"));
096: assertSame(group2, bean.getGroup("group2"));
097: }
098:
099: public void testAddValidationError() {
100: Bean bean = new Bean(null);
101: bean.addValidationError(null);
102: assertEquals(0, bean.countValidationErrors());
103: assertEquals(0, bean.getValidationErrors().size());
104: bean
105: .addValidationError(new ValidationError.MANDATORY(
106: "subject"));
107: assertEquals(1, bean.countValidationErrors());
108: assertEquals(1, bean.getValidationErrors().size());
109: }
110:
111: public void testReplaceValidationError() {
112: Bean bean = new Bean(null);
113:
114: Set<ValidationError> errors = new LinkedHashSet<ValidationError>();
115: bean.replaceValidationErrors(errors);
116: assertSame(errors, bean.getValidationErrors());
117: }
118:
119: public void testValidateWithoutReset() {
120: Bean bean = new Bean("");
121: ValidationRule rule = new ValidationRuleNotEmpty("property");
122: bean.addRule(rule);
123: assertFalse(bean.validate());
124: assertEquals(1, bean.countValidationErrors());
125: bean.setProperty("value");
126: assertFalse(bean.validate());
127: assertEquals(1, bean.countValidationErrors());
128: }
129:
130: public void testResetValidation() {
131: Bean bean = new Bean("");
132: ValidationRule rule = new ValidationRuleNotEmpty("property");
133: bean.addRule(rule);
134: assertFalse(bean.validate());
135: bean.resetValidation();
136: assertEquals(0, bean.countValidationErrors());
137: assertEquals(0, bean.getValidationErrors().size());
138: assertTrue(bean.isSubjectValid("property"));
139: }
140:
141: public void testFocusGroupIllegalArguments() {
142: Bean bean = new Bean("");
143: bean.focusGroup(null);
144: assertEquals(0, bean.countValidationErrors());
145:
146: bean.addGroup("group1").addRule(
147: new ValidationRuleLimitedLength("property", 1, -1))
148: .addRule(new ValidationRuleNotEmpty("property"));
149: bean.validate();
150: assertEquals(1, bean.countValidationErrors());
151: bean.focusGroup("unknown");
152: assertEquals(1, bean.countValidationErrors());
153: }
154:
155: public void testFocusGroup() {
156: Bean bean = new Bean("");
157: bean.addGroup("group1").addRule(
158: new ValidationRuleLimitedLength("property", 1, -1))
159: .addRule(new ValidationRuleNotEmpty("property"));
160: bean.addGroup("group2").addRule(
161: new ValidationRuleNotNull("theDate"));
162:
163: assertFalse(bean.validate());
164: assertEquals(2, bean.countValidationErrors());
165: assertFalse(bean.isSubjectValid("property"));
166: assertFalse(bean.isSubjectValid("theDate"));
167:
168: bean.focusGroup("group1");
169: assertEquals(1, bean.countValidationErrors());
170: Set<ValidationError> set = bean.getValidationErrors();
171: assertFalse(bean.isSubjectValid("property"));
172: assertTrue(bean.isSubjectValid("theDate"));
173: Iterator<ValidationError> set_it = set.iterator();
174: ValidationError error = set_it.next();
175: assertEquals(ValidationError.IDENTIFIER_WRONGLENGTH, error
176: .getIdentifier());
177: assertEquals("property", error.getSubject());
178:
179: bean.resetValidation();
180:
181: assertFalse(bean.validate());
182: assertEquals(2, bean.countValidationErrors());
183: assertFalse(bean.isSubjectValid("property"));
184: assertFalse(bean.isSubjectValid("theDate"));
185:
186: bean.focusGroup("group2");
187: assertEquals(1, bean.countValidationErrors());
188: set = bean.getValidationErrors();
189: assertTrue(bean.isSubjectValid("property"));
190: assertFalse(bean.isSubjectValid("theDate"));
191: set_it = set.iterator();
192: error = set_it.next();
193: assertEquals(ValidationError.IDENTIFIER_MANDATORY, error
194: .getIdentifier());
195: assertEquals("theDate", error.getSubject());
196: }
197:
198: public void testResetGroupIllegalArguments() {
199: Bean bean = new Bean("");
200: bean.resetGroup(null);
201: assertEquals(0, bean.countValidationErrors());
202:
203: bean.addGroup("group1").addRule(
204: new ValidationRuleLimitedLength("property", 1, -1))
205: .addRule(new ValidationRuleNotEmpty("property"));
206: bean.validate();
207: assertEquals(1, bean.countValidationErrors());
208: bean.resetGroup("unknown");
209: assertEquals(1, bean.countValidationErrors());
210: }
211:
212: public void testResetGroup() {
213: Bean bean = new Bean("");
214: bean.addGroup("group1").addRule(
215: new ValidationRuleLimitedLength("property", 1, -1))
216: .addRule(new ValidationRuleNotEmpty("property"));
217: bean.addGroup("group2").addRule(
218: new ValidationRuleNotNull("theDate"));
219:
220: assertFalse(bean.validate());
221: assertEquals(2, bean.countValidationErrors());
222: assertFalse(bean.isSubjectValid("property"));
223: assertFalse(bean.isSubjectValid("theDate"));
224:
225: bean.resetGroup("group1");
226: assertEquals(1, bean.countValidationErrors());
227: Set<ValidationError> set = bean.getValidationErrors();
228: assertTrue(bean.isSubjectValid("property"));
229: assertFalse(bean.isSubjectValid("theDate"));
230: Iterator<ValidationError> set_it = set.iterator();
231: ValidationError error = set_it.next();
232: assertEquals(ValidationError.IDENTIFIER_MANDATORY, error
233: .getIdentifier());
234: assertEquals("theDate", error.getSubject());
235:
236: bean.resetValidation();
237:
238: assertFalse(bean.validate());
239: assertEquals(2, bean.countValidationErrors());
240: assertFalse(bean.isSubjectValid("property"));
241: assertFalse(bean.isSubjectValid("theDate"));
242:
243: bean.resetGroup("group2");
244: assertEquals(1, bean.countValidationErrors());
245: set = bean.getValidationErrors();
246: assertFalse(bean.isSubjectValid("property"));
247: assertTrue(bean.isSubjectValid("theDate"));
248: set_it = set.iterator();
249: error = set_it.next();
250: assertEquals(ValidationError.IDENTIFIER_WRONGLENGTH, error
251: .getIdentifier());
252: assertEquals("property", error.getSubject());
253: }
254:
255: public void testValidateGroupIllegalArguments() {
256: Bean bean = new Bean("value");
257: bean.addGroup("group1").addRule(
258: new ValidationRuleNotEmpty("property")).addRule(
259: new ValidationRuleNotNull("property"));
260:
261: assertTrue(bean.validateGroup(null));
262: assertTrue(bean.validateGroup("unknown"));
263: }
264:
265: public void testValidateGroup() {
266: Bean bean = new Bean("");
267: bean.setProperty("12");
268: bean.addGroup("group1").addRule(
269: new ValidationRuleLimitedLength("property", 3, -1));
270: bean.addGroup("group2").addRule(
271: new ValidationRuleNotNull("theDate"));
272:
273: assertFalse(bean.validate());
274: assertEquals(2, bean.countValidationErrors());
275: assertFalse(bean.isSubjectValid("property"));
276: assertFalse(bean.isSubjectValid("theDate"));
277:
278: bean.resetValidation();
279:
280: assertFalse(bean.validateGroup("group1"));
281: assertEquals(1, bean.countValidationErrors());
282: assertFalse(bean.isSubjectValid("property"));
283: assertTrue(bean.isSubjectValid("theDate"));
284: Set<ValidationError> set = bean.getValidationErrors();
285: Iterator<ValidationError> set_it = set.iterator();
286: ValidationError error = set_it.next();
287: assertEquals(ValidationError.IDENTIFIER_WRONGLENGTH, error
288: .getIdentifier());
289: assertEquals("property", error.getSubject());
290: assertFalse(set_it.hasNext());
291:
292: bean.getGroup("group1").addRule(
293: new ValidationRuleNotEqual("property", "12"));
294: assertFalse(bean.validateGroup("group1"));
295: assertEquals(2, bean.countValidationErrors());
296: set = bean.getValidationErrors();
297: set_it = set.iterator();
298: error = set_it.next();
299: assertEquals(ValidationError.IDENTIFIER_WRONGLENGTH, error
300: .getIdentifier());
301: assertEquals("property", error.getSubject());
302: error = set_it.next();
303: assertEquals(ValidationError.IDENTIFIER_INVALID, error
304: .getIdentifier());
305: assertEquals("property", error.getSubject());
306: assertFalse(set_it.hasNext());
307:
308: bean.resetValidation();
309:
310: assertFalse(bean.validateGroup("group2"));
311: assertEquals(1, bean.countValidationErrors());
312: set = bean.getValidationErrors();
313: set_it = set.iterator();
314: error = set_it.next();
315: assertTrue(bean.isSubjectValid("property"));
316: assertFalse(bean.isSubjectValid("theDate"));
317: assertEquals(ValidationError.IDENTIFIER_MANDATORY, error
318: .getIdentifier());
319: assertEquals("theDate", error.getSubject());
320:
321: assertFalse(bean.validateGroup("group2"));
322: assertEquals(1, bean.countValidationErrors());
323: }
324:
325: public void testUniqueSubjectErrors() {
326: Bean bean = new Bean("");
327: bean.setProperty("://wrong");
328: bean.addRule(new ValidationRuleEmail("property"));
329: bean.addRule(new ValidationRuleUrl("property"));
330: assertFalse(bean.validate());
331: assertEquals(1, bean.countValidationErrors());
332: assertEquals(1, bean.getValidationErrors().size());
333: }
334:
335: public void testLimitSubjectErrors() {
336: Bean bean = new Bean("");
337: bean.setProperty("://wrong");
338: bean.limitSubjectErrors(null);
339: bean.addRule(new ValidationRuleEmail("property"));
340: bean.addRule(new ValidationRuleLimitedLength("property", 2, 4));
341: assertFalse(bean.validate());
342: assertEquals(2, bean.countValidationErrors());
343: assertEquals(2, bean.getValidationErrors().size());
344: bean.limitSubjectErrors("property");
345: bean.limitSubjectErrors("property");
346: assertEquals(2, bean.countValidationErrors());
347: assertEquals(2, bean.getValidationErrors().size());
348: bean.resetValidation();
349: assertFalse(bean.validate());
350: assertEquals(1, bean.countValidationErrors());
351: assertEquals(1, bean.getValidationErrors().size());
352: bean.unlimitSubjectErrors("property");
353: assertEquals(1, bean.countValidationErrors());
354: assertEquals(1, bean.getValidationErrors().size());
355: }
356:
357: public void testUnlimitSubjectErrors() {
358: Bean bean = new Bean("");
359: bean.setProperty("://wrong");
360: bean.unlimitSubjectErrors(null);
361: bean.unlimitSubjectErrors("property");
362: bean.limitSubjectErrors("property");
363: bean.addRule(new ValidationRuleEmail("property"));
364: bean.addRule(new ValidationRuleLimitedLength("property", 2, 4));
365: assertFalse(bean.validate());
366: assertEquals(1, bean.countValidationErrors());
367: assertEquals(1, bean.getValidationErrors().size());
368: bean.unlimitSubjectErrors("property");
369: assertEquals(1, bean.countValidationErrors());
370: assertEquals(1, bean.getValidationErrors().size());
371: bean.resetValidation();
372: assertFalse(bean.validate());
373: assertEquals(2, bean.countValidationErrors());
374: assertEquals(2, bean.getValidationErrors().size());
375: }
376:
377: public void testMakeSubjectValid() {
378: Bean bean = new Bean("");
379: bean.makeSubjectValid(null);
380: assertTrue(bean.isSubjectValid(null));
381: bean.makeSubjectValid("blurp");
382: assertTrue(bean.isSubjectValid("blurp"));
383: ValidationRule rule = new ValidationRuleNotEmpty("property");
384: bean.addRule(rule);
385: assertFalse(bean.validate());
386: bean.makeSubjectValid("property_blah");
387: assertEquals(1, bean.countValidationErrors());
388: assertEquals(1, bean.getValidationErrors().size());
389: assertFalse(bean.isSubjectValid("property"));
390: bean.makeSubjectValid("property");
391: assertEquals(0, bean.countValidationErrors());
392: assertEquals(0, bean.getValidationErrors().size());
393: assertTrue(bean.isSubjectValid("property"));
394: }
395:
396: public void testMakeErrorValid() {
397: Bean bean = new Bean("");
398: bean.makeErrorValid(null, null);
399: assertTrue(bean.isSubjectValid(null));
400: bean.makeErrorValid(null, "blurp");
401: assertTrue(bean.isSubjectValid("blurp"));
402: bean.makeErrorValid("INVALID", "blurp");
403: assertTrue(bean.isSubjectValid("blurp"));
404: ValidationRule rule1 = new ValidationRuleNotEqual("property",
405: "");
406: ValidationRule rule2 = new ValidationRuleLimitedLength(
407: "property", 2, 4);
408: bean.addRule(rule1);
409: bean.addRule(rule2);
410: assertFalse(bean.validate());
411:
412: bean.makeErrorValid("INVALID", "property_blah");
413: assertEquals(2, bean.countValidationErrors());
414: assertEquals(2, bean.getValidationErrors().size());
415: assertFalse(bean.isSubjectValid("property"));
416:
417: bean.makeErrorValid(null, "property");
418: assertEquals(2, bean.countValidationErrors());
419: assertEquals(2, bean.getValidationErrors().size());
420: assertFalse(bean.isSubjectValid("property"));
421:
422: bean.makeErrorValid("INVALID", "property");
423: assertEquals(1, bean.countValidationErrors());
424: assertEquals(1, bean.getValidationErrors().size());
425: assertFalse(bean.isSubjectValid("property"));
426:
427: ValidationError error = (ValidationError) bean
428: .getValidationErrors().iterator().next();
429: assertEquals("property", error.getSubject());
430: assertEquals("WRONGLENGTH", error.getIdentifier());
431:
432: bean.makeErrorValid("WRONGLENGTH", "property");
433: assertEquals(0, bean.countValidationErrors());
434: assertEquals(0, bean.getValidationErrors().size());
435: assertTrue(bean.isSubjectValid("property"));
436: }
437:
438: public void testGetErrorIndication() {
439: Bean bean = new Bean("test");
440: ValidationRule rule = new ValidationRuleNotEmpty("property");
441: bean.addRule(rule);
442: assertTrue(bean.validate());
443: assertEquals("valid", Validation.getErrorIndication(bean,
444: "property", "valid", "error"));
445: bean.setProperty("");
446: assertFalse(bean.validate());
447: assertEquals("error", Validation.getErrorIndication(bean,
448: "property", "valid", "error"));
449: }
450:
451: public void testConstrainedBean() {
452: Bean bean = null;
453:
454: ConstrainedBean constraint = new ConstrainedBean().unique(
455: "property", "other");
456:
457: bean = new Bean("value");
458: bean.addConstraint(constraint);
459: assertSame(constraint, bean.getConstrainedBean());
460: }
461:
462: public void testSeveralConstrainedBean() {
463: Bean bean = null;
464:
465: ConstrainedBean constraint1 = new ConstrainedBean().unique(
466: "property", "other");
467: ConstrainedBean constraint2 = new ConstrainedBean().unique(
468: "property3", "other2", "other3")
469: .defaultOrder("ordered");
470:
471: bean = new Bean("value");
472: bean.addConstraint(constraint1);
473: assertEquals(1, bean.getConstrainedBean().getConstraints()
474: .size());
475: bean.addConstraint((ConstrainedBean) null);
476: bean.addConstraint(constraint2);
477: assertSame(constraint2, bean.getConstrainedBean());
478: assertEquals(2, bean.getConstrainedBean().getConstraints()
479: .size());
480: assertEquals(1, bean.getConstrainedBean().getUniques().size());
481: assertEquals("property3", ((String[]) bean.getConstrainedBean()
482: .getUniques().get(0))[0]);
483: assertEquals("other2", ((String[]) bean.getConstrainedBean()
484: .getUniques().get(0))[1]);
485: assertEquals("other3", ((String[]) bean.getConstrainedBean()
486: .getUniques().get(0))[2]);
487: assertEquals("ordered", ((ConstrainedBean.Order) bean
488: .getConstrainedBean().getDefaultOrdering().get(0))
489: .getPropertyName());
490: assertSame(ConstrainedBean.ASC, ((ConstrainedBean.Order) bean
491: .getConstrainedBean().getDefaultOrdering().get(0))
492: .getDirection());
493: }
494:
495: public void testConstraintSubject() {
496: Bean bean = null;
497:
498: bean = new Bean("value");
499: bean.addConstraint(new ConstrainedProperty("property")
500: .notNull(true));
501: bean.setProperty(null);
502: assertFalse(bean.validate());
503: Set<ValidationError> set = bean.getValidationErrors();
504: assertEquals("property", set.iterator().next().getSubject());
505:
506: bean = new Bean("value");
507: bean.addConstraint(new ConstrainedProperty("property")
508: .subjectName("subject").notNull(true));
509: bean.setProperty(null);
510: assertFalse(bean.validate());
511: set = bean.getValidationErrors();
512: assertEquals("subject", set.iterator().next().getSubject());
513: }
514:
515: public void testConstraintNotNull() {
516: Bean bean = new Bean("value");
517: bean.addConstraint(new ConstrainedProperty("property")
518: .notNull(true));
519: Iterator<ConstrainedProperty> it = bean
520: .getConstrainedProperties().iterator();
521: assertTrue(it.hasNext());
522: assertTrue(it.next().isNotNull());
523: assertFalse(it.hasNext());
524:
525: assertTrue(bean.validate());
526:
527: bean.setProperty(null);
528: assertFalse(bean.validate());
529: }
530:
531: public void testConstraintNotEmpty() {
532: Bean bean = new Bean("value");
533: bean.addConstraint(new ConstrainedProperty("property")
534: .notEmpty(true));
535: Iterator<ConstrainedProperty> it = bean
536: .getConstrainedProperties().iterator();
537: assertTrue(it.hasNext());
538: assertTrue(it.next().isNotEmpty());
539: assertFalse(it.hasNext());
540:
541: assertTrue(bean.validate());
542:
543: bean.setProperty("");
544: assertFalse(bean.validate());
545: }
546:
547: public void testConstraintNotEqual() {
548: Bean bean = new Bean("other");
549: bean.addConstraint(new ConstrainedProperty("property")
550: .notEqual("value"));
551: Iterator<ConstrainedProperty> it = bean
552: .getConstrainedProperties().iterator();
553: assertTrue(it.hasNext());
554: assertTrue(it.next().isNotEqual());
555: assertFalse(it.hasNext());
556:
557: assertTrue(bean.validate());
558:
559: bean.setProperty("value");
560: assertFalse(bean.validate());
561: }
562:
563: public void testConstraintLimitedLength() {
564: Bean bean = new Bean("value");
565: bean.addConstraint(new ConstrainedProperty("property")
566: .minLength(3));
567: Iterator<ConstrainedProperty> it = bean
568: .getConstrainedProperties().iterator();
569: assertTrue(it.hasNext());
570: assertTrue(it.next().hasLimitedLength());
571: assertFalse(it.hasNext());
572:
573: assertTrue(bean.validate());
574:
575: bean.setProperty("df");
576: assertFalse(bean.validate());
577: }
578:
579: public void testConstraintEmail() {
580: Bean bean = new Bean("test@domain.com");
581: bean.addConstraint(new ConstrainedProperty("property")
582: .email(true));
583: Iterator<ConstrainedProperty> it = bean
584: .getConstrainedProperties().iterator();
585: assertTrue(it.hasNext());
586: assertTrue(it.next().isEmail());
587: assertFalse(it.hasNext());
588:
589: assertTrue(bean.validate());
590:
591: bean.setProperty("dfdf@");
592: assertFalse(bean.validate());
593: }
594:
595: public void testConstraintUrl() {
596: Bean bean = new Bean("http://test.some.com");
597: bean.addConstraint(new ConstrainedProperty("property")
598: .url(true));
599: Iterator<ConstrainedProperty> it = bean
600: .getConstrainedProperties().iterator();
601: assertTrue(it.hasNext());
602: assertTrue(it.next().isUrl());
603: assertFalse(it.hasNext());
604:
605: assertTrue(bean.validate());
606:
607: bean.setProperty("htt:www/.test.com");
608: assertFalse(bean.validate());
609: }
610:
611: public void testConstraintRegexp() {
612: Bean bean = new Bean("two words");
613: bean.addConstraint(new ConstrainedProperty("property")
614: .regexp("\\w+ \\w+"));
615: Iterator<ConstrainedProperty> it = bean
616: .getConstrainedProperties().iterator();
617: assertTrue(it.hasNext());
618: assertTrue(it.next().matchesRegexp());
619: assertFalse(it.hasNext());
620:
621: assertTrue(bean.validate());
622:
623: bean.setProperty("aword");
624: assertFalse(bean.validate());
625: }
626:
627: public void testConstraintFormat() {
628: Bean bean = new Bean("12032003");
629: bean.addConstraint(new ConstrainedProperty("property")
630: .format(new SimpleDateFormat("ddmmyyyy")));
631: Iterator<ConstrainedProperty> it = bean
632: .getConstrainedProperties().iterator();
633: assertTrue(it.hasNext());
634: assertTrue(it.next().isFormatted());
635: assertFalse(it.hasNext());
636:
637: assertTrue(bean.validate());
638:
639: bean.setProperty("12");
640: assertFalse(bean.validate());
641: }
642:
643: public void testConstraintLimitedDate() {
644: Bean bean = new Bean(null);
645: bean.setTheDate(new Date(2003, 12, 11));
646: bean.addConstraint(new ConstrainedProperty("theDate")
647: .maxDate(new Date(2004, 3, 1)));
648: Iterator<ConstrainedProperty> it = bean
649: .getConstrainedProperties().iterator();
650: assertTrue(it.hasNext());
651: assertTrue(it.next().isLimitedDate());
652: assertFalse(it.hasNext());
653:
654: assertTrue(bean.validate());
655:
656: bean.setTheDate(new Date(2004, 12, 11));
657: assertFalse(bean.validate());
658: }
659:
660: public void testConstraintInList() {
661: Bean bean = new Bean("entry");
662: bean
663: .addConstraint(new ConstrainedProperty("property")
664: .inList(new String[] { "one", "two", "entry",
665: "three" }));
666: Iterator<ConstrainedProperty> it = bean
667: .getConstrainedProperties().iterator();
668: assertTrue(it.hasNext());
669: assertTrue(it.next().isInList());
670: assertFalse(it.hasNext());
671:
672: assertTrue(bean.validate());
673:
674: bean.setProperty("notinlist");
675: assertFalse(bean.validate());
676: }
677:
678: public void testConstraintRange() {
679: Bean bean = new Bean("bbbb");
680: bean.addConstraint(new ConstrainedProperty("property")
681: .rangeBegin("aaab").rangeEnd("ccca"));
682: Iterator<ConstrainedProperty> it = bean
683: .getConstrainedProperties().iterator();
684: assertTrue(it.hasNext());
685: assertTrue(it.next().isRange());
686: assertFalse(it.hasNext());
687:
688: assertTrue(bean.validate());
689:
690: bean.setProperty("dddd");
691: assertFalse(bean.validate());
692: }
693:
694: public void testConstraintSameAs() {
695: Bean bean = new Bean("first value");
696: bean.setOther("first value");
697: bean.addConstraint(new ConstrainedProperty("other")
698: .sameAs("property"));
699: Iterator<ConstrainedProperty> it = bean
700: .getConstrainedProperties().iterator();
701: assertTrue(it.hasNext());
702: assertTrue(it.next().isSameAs());
703: assertFalse(it.hasNext());
704:
705: assertTrue(bean.validate());
706:
707: bean.setOther("second value");
708: assertFalse(bean.validate());
709: }
710:
711: public void testSeveralConstraints() {
712: Bean bean = new Bean("value");
713: bean.setTheDate(new Date());
714:
715: assertNull(bean.getConstrainedProperty(null));
716: assertNull(bean.getConstrainedProperty(""));
717: assertNull(bean.getConstrainedProperty("property"));
718:
719: bean.addConstraint((ConstrainedProperty) null);
720: bean.addConstraint(new ConstrainedProperty("theDate")
721: .notNull(true));
722: bean.addConstraint(new ConstrainedProperty("property").notNull(
723: true).notEmpty(true));
724:
725: assertNull(bean.getConstrainedProperty("property_unknown"));
726: assertTrue(bean.getConstrainedProperty("theDate").isNotNull());
727: assertTrue(bean.getConstrainedProperty("property").isNotNull());
728: assertTrue(bean.getConstrainedProperty("property").isNotEmpty());
729:
730: Iterator<ConstrainedProperty> it = bean
731: .getConstrainedProperties().iterator();
732: assertTrue(it.hasNext());
733: assertTrue(it.next().isNotNull());
734: assertTrue(it.hasNext());
735: ConstrainedProperty property = it.next();
736: assertTrue(property.isNotNull());
737: assertTrue(property.isNotEmpty());
738: assertFalse(it.hasNext());
739:
740: assertTrue(bean.validate());
741:
742: bean.setTheDate(null);
743: assertFalse(bean.validate());
744:
745: bean.setTheDate(new Date());
746: bean.resetValidation();
747: assertTrue(bean.validate());
748:
749: bean.setProperty("");
750: bean.resetValidation();
751: assertFalse(bean.validate());
752:
753: bean.setProperty(null);
754: bean.resetValidation();
755: assertFalse(bean.validate());
756: }
757:
758: public void testSameConstraintProperties() {
759: Bean bean = new Bean("value");
760: bean.setTheDate(new Date());
761:
762: bean.addConstraint(new ConstrainedProperty("theDate")
763: .notNull(true));
764: bean.addConstraint(new ConstrainedProperty("property").notNull(
765: true).notEmpty(false));
766: bean.addConstraint(new ConstrainedProperty("property")
767: .notEmpty(true));
768:
769: assertNull(bean.getConstrainedProperty("property_unknown"));
770: assertTrue(bean.getConstrainedProperty("theDate").isNotNull());
771: assertTrue(bean.getConstrainedProperty("property").isNotNull());
772: assertTrue(bean.getConstrainedProperty("property").isNotEmpty());
773:
774: Iterator<ConstrainedProperty> it = bean
775: .getConstrainedProperties().iterator();
776: assertTrue(it.hasNext());
777: assertTrue(it.next().isNotNull());
778: assertTrue(it.hasNext());
779: ConstrainedProperty property = it.next();
780: assertTrue(property.isNotNull());
781: assertTrue(property.isNotEmpty());
782: assertFalse(it.hasNext());
783:
784: assertTrue(bean.validate());
785:
786: bean.setTheDate(null);
787: assertFalse(bean.validate());
788:
789: bean.setTheDate(new Date());
790: bean.resetValidation();
791: assertTrue(bean.validate());
792:
793: bean.setProperty("");
794: bean.resetValidation();
795: assertFalse(bean.validate());
796:
797: bean.setProperty(null);
798: bean.resetValidation();
799: assertFalse(bean.validate());
800: }
801:
802: public void testCloneEmpty() {
803: Bean bean = new Bean("value");
804: Bean other = null;
805: try {
806: other = (Bean) bean.clone();
807: } catch (CloneNotSupportedException e) {
808: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
809: }
810: assertNotNull(other);
811: assertEquals(0, other.getRules().size());
812: assertEquals(0, other.countValidationErrors());
813: assertEquals(0, other.getValidationErrors().size());
814: assertEquals(0, other.getConstrainedProperties().size());
815: assertEquals(0, other.getGroups().size());
816: assertNull(other.getConstrainedBean());
817: }
818:
819: public void testCloneFilled() {
820: Iterator<ValidationGroup> it_groups = null;
821: Iterator<String> it_subjects = null;
822: Iterator<ConstrainedProperty> it_constraints = null;
823: Iterator<ValidationError> it_errors = null;
824: ValidationError error = null;
825:
826: Bean bean = new Bean("value");
827:
828: bean.addGroup("group1");
829: it_groups = bean.getGroups().iterator();
830: assertTrue(it_groups.hasNext());
831: assertEquals(it_groups.next().getName(), "group1");
832: assertSame(bean, bean.getGroup("group1").getValidation());
833: assertFalse(it_groups.hasNext());
834:
835: bean.addConstraint(new ConstrainedProperty("property")
836: .notEqual("value"));
837: it_constraints = bean.getConstrainedProperties().iterator();
838: assertTrue(it_constraints.hasNext());
839: assertTrue(it_constraints.next().isNotEqual());
840: assertFalse(it_constraints.hasNext());
841:
842: it_subjects = bean.getValidatedSubjects().iterator();
843: assertTrue(it_subjects.hasNext());
844: assertEquals("property", it_subjects.next());
845: assertFalse(it_subjects.hasNext());
846:
847: bean.limitSubjectErrors("property");
848:
849: assertFalse(bean.validate());
850: assertEquals(1, bean.countValidationErrors());
851: assertEquals(1, bean.getValidationErrors().size());
852: assertFalse(bean.validate());
853: assertEquals(1, bean.countValidationErrors());
854: assertEquals(1, bean.getValidationErrors().size());
855:
856: it_errors = bean.getValidationErrors().iterator();
857: assertTrue(it_errors.hasNext());
858: error = it_errors.next();
859: assertEquals(ValidationError.IDENTIFIER_INVALID, error
860: .getIdentifier());
861: assertEquals("property", error.getSubject());
862: assertFalse(it_errors.hasNext());
863:
864: bean.setProperty("test");
865: bean.resetValidation();
866: assertTrue(bean.validate());
867: bean.setProperty("value");
868: bean.resetValidation();
869: assertFalse(bean.validate());
870:
871: Bean other = null;
872: try {
873: other = (Bean) bean.clone();
874: } catch (CloneNotSupportedException e) {
875: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
876: }
877: it_groups = other.getGroups().iterator();
878: assertTrue(it_groups.hasNext());
879: assertEquals(it_groups.next().getName(), "group1");
880: assertSame(other, other.getGroup("group1").getValidation());
881: assertFalse(it_groups.hasNext());
882:
883: it_constraints = other.getConstrainedProperties().iterator();
884: assertTrue(it_constraints.hasNext());
885: assertTrue(it_constraints.next().isNotEqual());
886: assertFalse(it_constraints.hasNext());
887: assertEquals(1, other.countValidationErrors());
888: assertEquals(1, other.getValidationErrors().size());
889: assertFalse(bean.validate());
890: assertEquals(1, other.countValidationErrors());
891: assertEquals(1, other.getValidationErrors().size());
892:
893: it_subjects = other.getValidatedSubjects().iterator();
894: assertTrue(it_subjects.hasNext());
895: assertEquals("property", it_subjects.next());
896: assertFalse(it_subjects.hasNext());
897:
898: it_errors = other.getValidationErrors().iterator();
899: assertTrue(it_errors.hasNext());
900: error = it_errors.next();
901: assertEquals(ValidationError.IDENTIFIER_INVALID, error
902: .getIdentifier());
903: assertEquals("property", error.getSubject());
904: assertFalse(it_errors.hasNext());
905:
906: bean.setProperty("test");
907: bean.resetValidation();
908: assertTrue(bean.validate());
909: other.resetValidation();
910: assertFalse(other.validate());
911:
912: other.setProperty("test2");
913: other.resetValidation();
914: assertTrue(other.validate());
915: other.resetValidation();
916: assertTrue(other.validate());
917:
918: bean.setProperty("value");
919: bean.resetValidation();
920: assertFalse(bean.validate());
921: other.resetValidation();
922: assertTrue(other.validate());
923: }
924:
925: public class Bean extends Validation {
926: private String mProperty = null;
927: private String mOther = null;
928: private Date mTheDate = null;
929:
930: public Bean(String property) {
931: mProperty = property;
932: }
933:
934: public void setProperty(String property) {
935: mProperty = property;
936: }
937:
938: public String getProperty() {
939: return mProperty;
940: }
941:
942: public void setOther(String other) {
943: mOther = other;
944: }
945:
946: public String getOther() {
947: return mOther;
948: }
949:
950: public void setTheDate(Date date) {
951: mTheDate = date;
952: }
953:
954: public Date getTheDate() {
955: return mTheDate;
956: }
957: }
958: }
|