001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.test.guard;
013:
014: import junit.framework.TestCase;
015: import net.sf.oval.ConstraintViolation;
016: import net.sf.oval.constraint.NotNull;
017: import net.sf.oval.context.FieldContext;
018: import net.sf.oval.context.MethodParameterContext;
019: import net.sf.oval.exception.ConstraintsViolatedException;
020: import net.sf.oval.guard.ConstraintsViolatedAdapter;
021: import net.sf.oval.guard.Guarded;
022: import net.sf.oval.guard.PostValidateThis;
023: import net.sf.oval.guard.PreValidateThis;
024:
025: /**
026: * @author Sebastian Thomschke
027: */
028: public class PrePostValidateThisTest extends TestCase {
029: @Guarded(applyFieldConstraintsToSetters=true,checkInvariants=false)
030: public static class TestEntity {
031: @NotNull(message="NOT_NULL")
032: public String name;
033:
034: public TestEntity() {
035: // do nothing
036: }
037:
038: @PostValidateThis
039: public TestEntity(String name) {
040: this .name = name;
041: }
042:
043: @PreValidateThis
044: public String getName() {
045: return name;
046: }
047:
048: public void setName(String name) {
049: this .name = name;
050: }
051:
052: @PostValidateThis
053: public void setNamePost(String name) {
054: this .name = name;
055: }
056: }
057:
058: public void testConstructorValidation() {
059: try {
060: new TestEntity(null);
061:
062: fail();
063: } catch (ConstraintsViolatedException e) {
064: ConstraintViolation[] violations = e
065: .getConstraintViolations();
066: assertTrue(violations != null && violations.length == 1);
067: assertTrue(violations[0].getMessage().equals("NOT_NULL"));
068: assertTrue(violations[0].getContext() instanceof FieldContext);
069: }
070:
071: new TestEntity("test");
072: }
073:
074: public void testMethodValidationInProbeMode() {
075: final TestEntity t = new TestEntity();
076:
077: TestGuardAspect.aspectOf().getGuard().setInProbeMode(t, true);
078:
079: final ConstraintsViolatedAdapter va = new ConstraintsViolatedAdapter();
080: TestGuardAspect.aspectOf().getGuard().addListener(va, t);
081:
082: // test non-getter precondition failed
083: t.getName();
084: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
085: assertTrue(va.getConstraintViolations().size() == 1);
086: assertTrue(va.getConstraintViolations().get(0).getMessage()
087: .equals("NOT_NULL"));
088: va.clear();
089:
090: t.setName(null);
091: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
092: assertTrue(va.getConstraintViolations().size() == 1);
093: assertTrue(va.getConstraintViolations().get(0).getMessage()
094: .equals("NOT_NULL"));
095: va.clear();
096:
097: // test post-condition ignored even if pre-conditions satisfied
098: t.setNamePost(null);
099: assertTrue(va.getConstraintsViolatedExceptions().size() == 0);
100:
101: // test setter
102: t.setName("the name");
103: assertTrue(va.getConstraintsViolatedExceptions().size() == 0);
104: assertTrue(va.getConstraintViolations().size() == 0);
105:
106: // test getter returns null because we are in probe mode
107: t.name = "the name";
108: assertNull(t.getName());
109: assertTrue(va.getConstraintsViolatedExceptions().size() == 0);
110: assertTrue(va.getConstraintViolations().size() == 0);
111: }
112:
113: public void testMethodValidation() {
114: final TestEntity t = new TestEntity();
115:
116: try {
117: t.getName();
118: fail();
119: } catch (ConstraintsViolatedException e) {
120: ConstraintViolation[] violations = e
121: .getConstraintViolations();
122: assertTrue(violations != null && violations.length > 0);
123: assertTrue(violations[0].getMessage().equals("NOT_NULL"));
124: assertTrue(violations[0].getContext() instanceof FieldContext);
125: }
126:
127: t.setName("test");
128:
129: try {
130: t.setName(null);
131: fail();
132: } catch (ConstraintsViolatedException e) {
133: ConstraintViolation[] violations = e
134: .getConstraintViolations();
135: assertTrue(violations != null && violations.length > 0);
136: assertTrue(violations[0].getMessage().equals("NOT_NULL"));
137: assertTrue(violations[0].getContext() instanceof MethodParameterContext);
138: }
139:
140: t.setName("the name");
141: assertNotNull(t.getName());
142: }
143: }
|