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 PrePostRubyTest 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!=nil && value2add!=nil && _args[0]!=nil",lang="ruby",message="PRE")
27: public void increase1(
28: @Assert(expr="_value!=nil",lang="ruby",message="ASSERT")
29: BigDecimal value2add) {
30: value = value.add(value2add);
31: }
32:
33: @Post(expr="_this.value>_old['value']",old="{ 'value' => _this.value }",lang="ruby",message="POST")
34: public void increase2(@NotNull
35: BigDecimal value2add) {
36: value = value.add(value2add);
37: }
38:
39: @Post(expr="_this.value>_old['value']",old="{ 'value' => _this.value }",lang="ruby",message="POST")
40: public void increase2buggy(@NotNull
41: BigDecimal value2add) {
42: value = value.subtract(value2add);
43: }
44:
45: public BigDecimal getValue() {
46: return value;
47: }
48: }
49:
50: public void testPreRuby() {
51: final Guard guard = new Guard();
52: TestGuardAspect.aspectOf().setGuard(guard);
53:
54: TestTransaction t = new TestTransaction();
55:
56: try {
57: t.increase1(new BigDecimal(1));
58: fail();
59: } catch (ConstraintsViolatedException ex) {
60: assertEquals(ex.getConstraintViolations()[0].getMessage(),
61: "PRE");
62: }
63:
64: try {
65: t.value = new BigDecimal(2);
66: t.increase1(null);
67: fail();
68: } catch (ConstraintsViolatedException ex) {
69: assertEquals(ex.getConstraintViolations()[0].getMessage(),
70: "ASSERT");
71: }
72: try {
73: t.increase1(new BigDecimal(1));
74: } catch (ConstraintsViolatedException ex) {
75: System.out.println(ex.getConstraintViolations()[0]
76: .getMessage());
77: }
78: }
79:
80: public void testPostRuby() {
81: final Guard guard = new Guard();
82: TestGuardAspect.aspectOf().setGuard(guard);
83:
84: TestTransaction t = new TestTransaction();
85:
86: try {
87: t.value = new BigDecimal(-2);
88: t.increase2buggy(new BigDecimal(1));
89: fail();
90: } catch (ConstraintsViolatedException ex) {
91: assertEquals(ex.getConstraintViolations()[0].getMessage(),
92: "POST");
93: }
94:
95: t.increase2(new BigDecimal(1));
96: }
97: }
|