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.lang.reflect.Field;
15:
16: import junit.framework.TestCase;
17: import net.sf.oval.ConstraintViolation;
18: import net.sf.oval.Validator;
19: import net.sf.oval.constraint.NotNull;
20: import net.sf.oval.exception.ConstraintsViolatedException;
21:
22: /**
23: * @author Sebastian Thomschke
24: */
25: public class ValidatorAssertTest extends TestCase {
26: private class TestEntity {
27: @NotNull(message="NOT_NULL")
28: public String name;
29:
30: @NotNull(message="NOT_NULL")
31: public Integer value;
32:
33: }
34:
35: public void testValidatorAssert() throws Exception {
36: final TestEntity e = new TestEntity();
37: final Validator v = new Validator();
38: try {
39: v.assertValid(e);
40: fail();
41: } catch (final ConstraintsViolatedException ex) {
42: final ConstraintViolation[] violations = ex
43: .getConstraintViolations();
44: assertEquals(2, violations.length);
45: assertEquals("NOT_NULL", violations[0].getMessage());
46: assertEquals("NOT_NULL", violations[1].getMessage());
47: }
48:
49: e.name = "asdads";
50: e.value = 5;
51: v.assertValid(e);
52: }
53:
54: public void testValidatorAssertField() throws Exception {
55: final Field f = TestEntity.class.getField("name");
56:
57: final TestEntity e = new TestEntity();
58: final Validator v = new Validator();
59: try {
60: v.assertValidFieldValue(e, f, null);
61: fail();
62: } catch (final ConstraintsViolatedException ex) {
63: final ConstraintViolation[] violations = ex
64: .getConstraintViolations();
65: assertEquals(1, violations.length);
66: assertEquals("NOT_NULL", violations[0].getMessage());
67: }
68:
69: v.assertValidFieldValue(e, f, "test");
70: }
71: }
|