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.Length;
20: import net.sf.oval.constraint.MatchPattern;
21: import net.sf.oval.constraint.NotEmpty;
22: import net.sf.oval.constraint.NotNull;
23:
24: /**
25: * @author Sebastian Thomschke
26: */
27: public class AssertFieldConstraintsValidationTest extends TestCase {
28: private static class Person {
29: @NotNull
30: public String firstName;
31:
32: @NotNull
33: public String lastName;
34:
35: @NotNull
36: @Length(max=6,message="LENGTH")
37: @NotEmpty(message="NOT_EMPTY")
38: @MatchPattern(pattern=PATTERN_ZIP_CODE,message="MATCH_PATTERN")
39: public String zipCode;
40: }
41:
42: private final static String PATTERN_ZIP_CODE = "^[0-9]*$";
43:
44: public void testFieldValidation() {
45: final Validator validator = new Validator();
46:
47: // test @NotNull
48: final Person p = new Person();
49: List<ConstraintViolation> violations = validator.validate(p);
50: assertTrue(violations.size() == 3);
51:
52: // test @Length(max=)
53: p.firstName = "Mike";
54: p.lastName = "Mahoney";
55: p.zipCode = "1234567";
56: violations = validator.validate(p);
57: assertTrue(violations.size() == 1);
58: assertTrue(violations.get(0).getMessage().equals("LENGTH"));
59:
60: // test @NotEmpty
61: p.zipCode = "";
62: violations = validator.validate(p);
63: assertTrue(violations.size() == 1);
64: assertTrue(violations.get(0).getMessage().equals("NOT_EMPTY"));
65:
66: // test @RegEx
67: p.zipCode = "dffd34";
68: violations = validator.validate(p);
69: assertTrue(violations.size() == 1);
70: assertTrue(violations.get(0).getMessage().equals(
71: "MATCH_PATTERN"));
72: }
73: }
|