01: package net.sf.oval.test.guard;
02:
03: import java.math.BigDecimal;
04: import java.util.Date;
05:
06: import junit.framework.TestCase;
07: import net.sf.oval.constraint.Assert;
08: import net.sf.oval.constraint.NotNull;
09: import net.sf.oval.exception.ConstraintsViolatedException;
10: import net.sf.oval.guard.Guard;
11: import net.sf.oval.guard.Guarded;
12: import net.sf.oval.guard.Post;
13: import net.sf.oval.guard.Pre;
14:
15: public class PrePostBeanShellTest extends TestCase {
16: @Guarded
17: public static class TestTransaction {
18: @SuppressWarnings("unused")
19: private Date date;
20:
21: @SuppressWarnings("unused")
22: private String description;
23:
24: private BigDecimal value;
25:
26: @Pre(expr="_this.value!=null && value2add!=null && _args[0]!=null",lang="bsh",message="PRE")
27: public void increase1(
28: @Assert(expr="_value!=null",lang="bsh",message="ASSERT")
29: final BigDecimal value2add) {
30: value = value.add(value2add);
31: }
32:
33: @Post(expr="_this.value.longValue()>0",lang="beanshell",message="POST")
34: public void increase2(@NotNull
35: final BigDecimal value2add) {
36: value = value.add(value2add);
37: }
38: }
39:
40: public void testPostBeanShell() {
41: final Guard guard = new Guard();
42: TestGuardAspect.aspectOf().setGuard(guard);
43:
44: final TestTransaction t = new TestTransaction();
45:
46: try {
47: t.value = new BigDecimal(-2);
48: t.increase2(new BigDecimal(1));
49: fail();
50: } catch (final ConstraintsViolatedException ex) {
51: assertEquals(ex.getConstraintViolations()[0].getMessage(),
52: "POST");
53: }
54:
55: t.value = new BigDecimal(0);
56: t.increase2(new BigDecimal(1));
57: }
58:
59: public void testPreBeanShell() {
60: final Guard guard = new Guard();
61: TestGuardAspect.aspectOf().setGuard(guard);
62:
63: final TestTransaction t = new TestTransaction();
64:
65: try {
66: t.increase1(new BigDecimal(1));
67: fail();
68: } catch (final ConstraintsViolatedException ex) {
69: assertEquals(ex.getConstraintViolations()[0].getMessage(),
70: "PRE");
71: }
72:
73: try {
74: t.value = new BigDecimal(2);
75: t.increase1(null);
76: fail();
77: } catch (final ConstraintsViolatedException ex) {
78: assertEquals(ex.getConstraintViolations()[0].getMessage(),
79: "ASSERT");
80: }
81: try {
82: t.increase1(new BigDecimal(1));
83: } catch (final ConstraintsViolatedException ex) {
84: System.out.println(ex.getConstraintViolations()[0]
85: .getMessage());
86: }
87: }
88: }
|