01: /**
02: *
03: */package net.sf.oval.test.integration.spring;
04:
05: import junit.framework.TestCase;
06: import net.sf.oval.Validator;
07: import net.sf.oval.constraint.NotNegative;
08: import net.sf.oval.constraint.NotNull;
09: import net.sf.oval.integration.spring.SpringValidator;
10:
11: import org.springframework.validation.BindException;
12:
13: /**
14: * @author Administrator
15: *
16: */
17: public class SpringValidatorTest extends TestCase {
18: public static class Entity {
19: @NotNull(errorCode="E1",message="M1")
20: private String name;
21:
22: @NotNegative(errorCode="E2",message="M2")
23: private int age;
24:
25: public int getAge() {
26: return age;
27: }
28:
29: public String getName() {
30: return name;
31: }
32: }
33:
34: public void testSpringValidator() {
35: final SpringValidator v = new SpringValidator(new Validator());
36: final Entity e = new Entity();
37: {
38: e.name = null;
39: e.age = -1;
40: final BindException errors = new BindException(e, e
41: .getClass().getName());
42: v.validate(e, errors);
43: assertEquals(2, errors.getErrorCount());
44: assertEquals(2, errors.getFieldErrorCount());
45: assertEquals(null, errors.getFieldError("name")
46: .getRejectedValue());
47: assertTrue(errors.getFieldError("name").getCodes()[0]
48: .startsWith("E1"));
49: assertEquals(-1, errors.getFieldError("age")
50: .getRejectedValue());
51: assertTrue(errors.getFieldError("age").getCodes()[0]
52: .startsWith("E2"));
53: }
54: {
55: final BindException errors = new BindException(e, e
56: .getClass().getName());
57: e.name = "";
58: e.age = -1;
59: v.validate(e, errors);
60: assertEquals(1, errors.getErrorCount());
61: assertEquals(1, errors.getFieldErrorCount());
62: assertEquals(-1, errors.getFieldError("age")
63: .getRejectedValue());
64: assertTrue(errors.getFieldError("age").getCodes()[0]
65: .startsWith("E2"));
66: }
67: {
68: final BindException errors = new BindException(e, e
69: .getClass().getName());
70: e.name = null;
71: e.age = 0;
72: v.validate(e, errors);
73: assertEquals(1, errors.getErrorCount());
74: assertEquals(1, errors.getFieldErrorCount());
75: assertEquals(null, errors.getFieldError("name")
76: .getRejectedValue());
77: assertTrue(errors.getFieldError("name").getCodes()[0]
78: .startsWith("E1"));
79: }
80: {
81: final BindException errors = new BindException(e, e
82: .getClass().getName());
83: e.name = "";
84: e.age = 0;
85: v.validate(e, errors);
86: assertEquals(0, errors.getErrorCount());
87: assertEquals(0, errors.getFieldErrorCount());
88: }
89: }
90: }
|