001: package net.sf.oval.test.guard;
002:
003: import java.math.BigDecimal;
004: import java.util.Date;
005:
006: import junit.framework.TestCase;
007: import net.sf.oval.constraint.Assert;
008: import net.sf.oval.constraint.NotNull;
009: import net.sf.oval.exception.ConstraintsViolatedException;
010: import net.sf.oval.guard.Guard;
011: import net.sf.oval.guard.Guarded;
012: import net.sf.oval.guard.Post;
013: import net.sf.oval.guard.Pre;
014:
015: public class PrePostMVELTest extends TestCase {
016: @Guarded
017: public static class TestTransaction {
018: @SuppressWarnings("unused")
019: private Date date;
020:
021: @SuppressWarnings("unused")
022: private String description;
023:
024: private BigDecimal value;
025:
026: /**
027: * @return the value
028: */
029: public BigDecimal getValue() {
030: return value;
031: }
032:
033: @Pre(expr="_this.value!=null && value2add!=null && _args[0]!=null",lang="mvel",message="PRE")
034: public void increase1(
035: @Assert(expr="_value!=null",lang="mvel",message="ASSERT")
036: final BigDecimal value2add) {
037: value = value.add(value2add);
038: }
039:
040: @Post(expr="_this.value>_old.value",old="[\"value\":_this.value]",lang="mvel",message="POST")
041: public void increase2(@NotNull
042: final BigDecimal value2add) {
043: value = value.add(value2add);
044: }
045:
046: @Post(expr="_this.value>_old.value",old="[\"value\":_this.value]",lang="mvel",message="POST")
047: public void increase2buggy(@NotNull
048: final BigDecimal value2add) {
049: value = value.subtract(value2add);
050: }
051: }
052:
053: public void testPostMVEL() {
054: final Guard guard = new Guard();
055: TestGuardAspect.aspectOf().setGuard(guard);
056:
057: final TestTransaction t = new TestTransaction();
058:
059: try {
060: t.value = new BigDecimal(-2);
061: t.increase2buggy(new BigDecimal(1));
062: fail();
063: } catch (final ConstraintsViolatedException ex) {
064: assertEquals(ex.getConstraintViolations()[0].getMessage(),
065: "POST");
066: }
067:
068: t.increase2(new BigDecimal(1));
069: }
070:
071: public void testPreMVEL() {
072: final Guard guard = new Guard();
073: TestGuardAspect.aspectOf().setGuard(guard);
074:
075: final TestTransaction t = new TestTransaction();
076:
077: try {
078: t.increase1(new BigDecimal(1));
079: fail();
080: } catch (final ConstraintsViolatedException ex) {
081: assertEquals(ex.getConstraintViolations()[0].getMessage(),
082: "PRE");
083: }
084:
085: try {
086: t.value = new BigDecimal(2);
087: t.increase1(null);
088: fail();
089: } catch (final ConstraintsViolatedException ex) {
090: assertEquals(ex.getConstraintViolations()[0].getMessage(),
091: "ASSERT");
092: }
093: try {
094: t.increase1(new BigDecimal(1));
095: } catch (final ConstraintsViolatedException ex) {
096: System.out.println(ex.getConstraintViolations()[0]
097: .getMessage());
098: }
099: }
100: }
|