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.ValidateWithMethod;
20:
21: /**
22: * @author Sebastian Thomschke
23: */
24: public class ValidateWithMethodConstraintTest extends TestCase {
25: private static class TestEntity {
26: @ValidateWithMethod(methodName="isNameValid",parameterType=String.class,ignoreIfNull=false)
27: public String name;
28:
29: protected boolean isNameValid(String name) {
30: if (name == null)
31: return false;
32: if (name.length() == 0)
33: return false;
34: if (name.length() > 4)
35: return false;
36: return true;
37: }
38: }
39:
40: public void testCheckByMethod() {
41: final Validator validator = new Validator();
42:
43: final TestEntity t = new TestEntity();
44:
45: List<ConstraintViolation> violations;
46:
47: violations = validator.validate(t);
48: assertTrue(violations.size() == 1);
49:
50: t.name = "";
51: violations = validator.validate(t);
52: assertTrue(violations.size() == 1);
53:
54: t.name = "12345";
55: violations = validator.validate(t);
56: assertTrue(violations.size() == 1);
57:
58: t.name = "1234";
59: violations = validator.validate(t);
60: assertTrue(violations.size() == 0);
61: }
62: }
|