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.configuration.annotation.IsInvariant;
20: import net.sf.oval.constraint.NotNull;
21:
22: /**
23: * @author Sebastian Thomschke
24: */
25: public class StaticFieldsAndGettersTest extends TestCase {
26: private static class TestEntity {
27: @NotNull
28: public static String staticA;
29:
30: public static String staticB;
31:
32: @NotNull
33: public String nonstaticA;
34:
35: public String nonstaticB;
36:
37: /**
38: * @return the staticB
39: */
40: @IsInvariant
41: @NotNull
42: public static String getStaticB() {
43: return staticB;
44: }
45:
46: /**
47: * @return the nonstaticB
48: */
49: @IsInvariant
50: @NotNull
51: public String getNonstaticB() {
52: return nonstaticB;
53: }
54: }
55:
56: public void testStaticValidation() {
57: final Validator validator = new Validator();
58:
59: TestEntity.staticA = null;
60: TestEntity.staticB = null;
61:
62: // test that only static fields are validated
63: List<ConstraintViolation> violations = validator
64: .validate(TestEntity.class);
65: assertTrue(violations.size() == 2);
66:
67: TestEntity.staticA = "";
68: TestEntity.staticB = "";
69:
70: violations = validator.validate(TestEntity.class);
71: assertTrue(violations.size() == 0);
72: }
73:
74: public void testNonstaticValidation() {
75: final Validator validator = new Validator();
76:
77: TestEntity.staticA = null;
78: TestEntity.staticB = null;
79:
80: // test that only non static fields are validated
81: final TestEntity t = new TestEntity();
82: List<ConstraintViolation> violations = validator.validate(t);
83: assertTrue(violations.size() == 2);
84:
85: t.nonstaticA = "";
86: t.nonstaticB = "";
87:
88: violations = validator.validate(t);
89: assertTrue(violations.size() == 0);
90: }
91: }
|