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: import net.sf.oval.constraint.Assert;
20:
21: /**
22: * @author Sebastian Thomschke
23: */
24: public class AssertRubyTest extends TestCase {
25: @net.sf.oval.constraint.Assert(expr="_this.firstName!=nil && _this.lastName && (_this.firstName + _this.lastName).length > 9",lang="ruby",message="C0")
26: public static class Person {
27: @Assert(expr="_value != nil",lang="ruby",message="C1")
28: public String firstName;
29:
30: @Assert(expr="_value != nil",lang="ruby",message="C2")
31: public String lastName;
32:
33: @Assert(expr="_value != nil && _value.length>0 && _value.length<7",lang="ruby",message="C3")
34: public String zipCode;
35: }
36:
37: public void testRubyExpression() {
38: final Validator validator = new Validator();
39:
40: // test not null
41: final Person p = new Person();
42: List<ConstraintViolation> violations = validator.validate(p);
43: assertTrue(violations.size() == 4);
44:
45: // test max length
46: p.firstName = "Mike";
47: p.lastName = "Mahoney";
48: p.zipCode = "1234567";
49: violations = validator.validate(p);
50: assertTrue(violations.size() == 1);
51: assertTrue(violations.get(0).getMessage().equals("C3"));
52:
53: // test not empty
54: p.zipCode = "";
55: violations = validator.validate(p);
56: assertTrue(violations.size() == 1);
57: assertTrue(violations.get(0).getMessage().equals("C3"));
58:
59: // test ok
60: p.zipCode = "wqeew";
61: violations = validator.validate(p);
62: assertTrue(violations.size() == 0);
63:
64: // test object-level constraint
65: p.firstName = "12345";
66: p.lastName = "1234";
67: violations = validator.validate(p);
68: assertTrue(violations.size() == 1);
69: assertTrue(violations.get(0).getMessage().equals("C0"));
70: }
71: }
|