01: package org.conform.validator;
02:
03: import junit.framework.TestCase;
04: import org.conform.*;
05: import org.conform.format.DefaultFormatFactory;
06: import org.conform.modifier.AnnotationModifier;
07:
08: import java.util.Iterator;
09: import java.sql.Date;
10: import java.sql.Time;
11:
12: public class ValidatorTest extends TestCase {
13:
14: public void testDateRange() throws Exception {
15: DateRangeValidator validator = new DateRangeValidator();
16:
17: validator.setFrom(new java.sql.Date(100, 0, 1));
18: assertEquals("validation.dateFrom", getMessage(validator,
19: new Date(100, 0, 1)));
20: validator.setFromInclusive(true);
21: assertNull(getMessage(validator, new Date(100, 0, 1)));
22: assertEquals("validation.dateFromInclusive", getMessage(
23: validator, new Date(99, 0, 1)));
24:
25: validator.setUntil(new java.sql.Date(100, 0, 31));
26: assertEquals("validation.dateUntil", getMessage(validator,
27: new Date(100, 0, 31)));
28: validator.setUntilInclusive(true);
29: assertNull(getMessage(validator, new Date(100, 0, 31)));
30: assertEquals("validation.dateUntilInclusive", getMessage(
31: validator, new Date(100, 1, 1)));
32: }
33:
34: public void testTimeRange() throws Exception {
35: TimeRangeValidator validator = new TimeRangeValidator();
36:
37: validator.setFrom(new java.sql.Time(12, 0, 1));
38: assertEquals("validation.timeFrom", getMessage(validator,
39: new Time(12, 0, 1)));
40: validator.setFromInclusive(true);
41: assertNull(getMessage(validator, new Time(12, 0, 1)));
42: assertEquals("validation.timeFromInclusive", getMessage(
43: validator, new Time(11, 0, 1)));
44:
45: validator.setUntil(new java.sql.Time(12, 0, 59));
46: assertEquals("validation.timeUntil", getMessage(validator,
47: new Time(12, 0, 59)));
48: validator.setUntilInclusive(true);
49: assertNull(getMessage(validator, new Time(12, 0, 59)));
50: assertEquals("validation.timeUntilInclusive", getMessage(
51: validator, new Time(12, 1, 1)));
52: }
53:
54: private String getMessage(Validator validator, Object value) {
55: String code = null;
56: try {
57: validator.validate(value);
58: } catch (ValidationException e) {
59: Iterator<ValidationException.Message> iterator = e
60: .getMessages().iterator();
61: code = iterator.hasNext() ? iterator.next().getCode()
62: : null;
63: }
64: return code;
65: }
66:
67: public void testAnnotations() throws Exception {
68: new DefaultFormatFactory();
69: BeanMeta beanMeta = new BeanMeta(SomeClass.class);
70: new AnnotationModifier(new Beans()).modify(beanMeta);
71: PropertyMeta somePropertyMeta = beanMeta
72: .getProperty("someProperty");
73:
74: assertEquals(5, somePropertyMeta.getValidators().size());
75: }
76:
77: public static class SomeClass {
78: @DateRange(from=DateRangeType.TODAY_INCLUSIVE)
79: @TimeRange(until=TimeRangeType.NOW)
80: @NumberRange(from="1.0",until="7")
81: @RegularExpression(type=RegularExpressionType.email)
82: @URLString(protocols={"http","https"})
83: String someProperty;
84:
85: public String getSomeProperty() {
86: return someProperty;
87: }
88:
89: public void setSomeProperty(String someProperty) {
90: this.someProperty = someProperty;
91: }
92: }
93: }
|