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 PrePostJavascriptTest 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: // Rhino's LiveConnect only supports access to public properties and methods
25: public BigDecimal value;
26:
27: @Pre(expr="_this.value!=null && value2add!=null && _args[0]!=null",lang="javascript",message="PRE")
28: public void increase1(
29: @Assert(expr="_value!=null",lang="javascript",message="ASSERT")
30: final BigDecimal value2add) {
31: value = value.add(value2add);
32: }
33:
34: @Post(expr="_this.value>0",lang="groovy",message="POST")
35: public void increase2(@NotNull
36: final BigDecimal value2add) {
37: value = value.add(value2add);
38: }
39: }
40:
41: public void testPostJavascript() {
42: final Guard guard = new Guard();
43: TestGuardAspect.aspectOf().setGuard(guard);
44:
45: final TestTransaction t = new TestTransaction();
46:
47: try {
48: t.value = new BigDecimal(-2);
49: t.increase2(new BigDecimal(1));
50: fail();
51: } catch (final ConstraintsViolatedException ex) {
52: assertEquals(ex.getConstraintViolations()[0].getMessage(),
53: "POST");
54: }
55:
56: t.value = new BigDecimal(0);
57: t.increase2(new BigDecimal(1));
58: }
59:
60: public void testPreJavascript() {
61: final Guard guard = new Guard();
62: TestGuardAspect.aspectOf().setGuard(guard);
63:
64: final TestTransaction t = new TestTransaction();
65:
66: try {
67: t.increase1(new BigDecimal(1));
68: fail();
69: } catch (final ConstraintsViolatedException ex) {
70: assertEquals(ex.getConstraintViolations()[0].getMessage(),
71: "PRE");
72: }
73:
74: try {
75: t.value = new BigDecimal(2);
76: t.increase1(null);
77: fail();
78: } catch (final ConstraintsViolatedException ex) {
79: assertEquals(ex.getConstraintViolations()[0].getMessage(),
80: "ASSERT");
81: }
82: try {
83: t.increase1(new BigDecimal(1));
84: } catch (final ConstraintsViolatedException ex) {
85: System.out.println(ex.getConstraintViolations()[0]
86: .getMessage());
87: }
88: }
89: }
|