01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.test.validator;
13:
14: import java.util.List;
15:
16: import junit.framework.TestCase;
17: import net.sf.oval.ConstraintViolation;
18: import net.sf.oval.Validator;
19:
20: /**
21: * @author Sebastian Thomschke
22: */
23: public class AssertGroovyTest extends TestCase {
24: @net.sf.oval.constraint.Assert(expr="_this.firstName!=null && _this.lastName!=null && (_this.firstName.length() + _this.lastName.length() > 9)",lang="groovy",errorCode="C0")
25: private static class Person {
26: @net.sf.oval.constraint.Assert(expr="_value!=null",lang="groovy",errorCode="C1")
27: public String firstName;
28:
29: @net.sf.oval.constraint.Assert(expr="_value!=null",lang="groovy",errorCode="C2")
30: public String lastName;
31:
32: @net.sf.oval.constraint.Assert(expr="_value!=null && _value.length()>0 && _value.length()<7",lang="groovy",errorCode="C3")
33: public String zipCode;
34: }
35:
36: public void testGroovyExpression() {
37: final Validator validator = new Validator();
38:
39: // test not null
40: final Person p = new Person();
41: List<ConstraintViolation> violations = validator.validate(p);
42: assertTrue(violations.size() == 4);
43:
44: // test max length
45: p.firstName = "Mike";
46: p.lastName = "Mahoney";
47: p.zipCode = "1234567";
48: violations = validator.validate(p);
49: assertTrue(violations.size() == 1);
50: assertTrue(violations.get(0).getErrorCode().equals("C3"));
51:
52: // test not empty
53: p.zipCode = "";
54: violations = validator.validate(p);
55: assertTrue(violations.size() == 1);
56: assertTrue(violations.get(0).getErrorCode().equals("C3"));
57:
58: // test ok
59: p.zipCode = "wqeew";
60: violations = validator.validate(p);
61: assertTrue(violations.size() == 0);
62:
63: // test object-level constraint
64: p.firstName = "12345";
65: p.lastName = "1234";
66: violations = validator.validate(p);
67: assertTrue(violations.size() == 1);
68: assertTrue(violations.get(0).getErrorCode().equals("C0"));
69: }
70: }
|