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.guard;
13:
14: import junit.framework.TestCase;
15: import net.sf.oval.constraint.AssertFieldConstraints;
16: import net.sf.oval.constraint.NotNull;
17: import net.sf.oval.exception.ConstraintsViolatedException;
18: import net.sf.oval.guard.Guard;
19: import net.sf.oval.guard.Guarded;
20:
21: /**
22: * @author Sebastian Thomschke
23: */
24: public class StaticMethodsTest extends TestCase {
25: @Guarded
26: private static class TestEntity {
27: @NotNull(message="NULL")
28: public static String value;
29:
30: public static void setValue(@AssertFieldConstraints
31: String value) {
32: TestEntity.value = value;
33: }
34:
35: public static void doSomethingPre() {
36: //
37: }
38:
39: public static void doSomethingPost() {
40: //
41: }
42: }
43:
44: public void testPreValidateThis() throws Exception {
45: final Guard guard = new Guard();
46: TestGuardAspect.aspectOf().setGuard(guard);
47:
48: TestEntity.value = null;
49:
50: try {
51: TestEntity.doSomethingPre();
52: fail();
53: } catch (ConstraintsViolatedException ex) {
54: assertTrue(ex.getConstraintViolations().length == 1);
55: assertTrue(ex.getConstraintViolations()[0].getMessage()
56: .equals("NULL"));
57: }
58:
59: TestEntity.value = "";
60: TestEntity.doSomethingPre();
61: }
62:
63: public void testPostValidateThis() throws Exception {
64: final Guard guard = new Guard();
65: TestGuardAspect.aspectOf().setGuard(guard);
66:
67: TestEntity.value = null;
68:
69: try {
70: TestEntity.doSomethingPost();
71: fail();
72: } catch (ConstraintsViolatedException ex) {
73: assertTrue(ex.getConstraintViolations().length == 1);
74: assertTrue(ex.getConstraintViolations()[0].getMessage()
75: .equals("NULL"));
76: }
77:
78: TestEntity.value = "";
79: TestEntity.doSomethingPost();
80: }
81:
82: public void testSetterValidation() throws Exception {
83: final Guard guard = new Guard();
84: TestGuardAspect.aspectOf().setGuard(guard);
85:
86: try {
87: TestEntity.setValue(null);
88: fail();
89: } catch (ConstraintsViolatedException ex) {
90: assertTrue(ex.getConstraintViolations().length == 1);
91: assertTrue(ex.getConstraintViolations()[0].getMessage()
92: .equals("NULL"));
93: }
94: }
95: }
|