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 java.lang.reflect.Method;
015:
016: import junit.framework.TestCase;
017: import net.sf.oval.constraint.AssertFieldConstraints;
018: import net.sf.oval.constraint.AssertFieldConstraintsCheck;
019: import net.sf.oval.constraint.AssertTrue;
020: import net.sf.oval.constraint.Length;
021: import net.sf.oval.constraint.MatchPattern;
022: import net.sf.oval.constraint.NotEmpty;
023: import net.sf.oval.constraint.NotNull;
024: import net.sf.oval.guard.ConstraintsViolatedAdapter;
025: import net.sf.oval.guard.Guard;
026: import net.sf.oval.guard.Guarded;
027:
028: /**
029: * @author Sebastian Thomschke
030: */
031: public class ApplyFieldConstraintsToParametersTest extends TestCase {
032: @Guarded
033: private class Person {
034: @NotNull(message="NOT_NULL")
035: private String firstName = "";
036:
037: @AssertTrue(message="ASSERT_TRUE")
038: private boolean isValid = true;
039:
040: @NotNull(message="NOT_NULL")
041: private String lastName = "";
042:
043: @NotNull(message="NOT_NULL")
044: @Length(max=6,message="LENGTH")
045: @NotEmpty(message="NOT_EMPTY")
046: @MatchPattern(pattern="^[0-9]*$",message="REG_EX")
047: private String zipCode = "1";
048:
049: public String getFirstName() {
050: return firstName;
051: }
052:
053: public String getLastName() {
054: return lastName;
055: }
056:
057: public String getZipCode() {
058: return zipCode;
059: }
060:
061: public boolean isValid() {
062: return isValid;
063: }
064:
065: public void setDummyFirstName(
066: @AssertFieldConstraints(value="firstName")
067: final String dummyFirstName) {
068: // doing interesting stuff here
069: }
070:
071: public void setFirstName(@AssertFieldConstraints
072: final String firstName) {
073: this .firstName = firstName;
074: }
075:
076: public void setLastName(@AssertFieldConstraints
077: final String lastName) {
078: this .lastName = lastName;
079: }
080:
081: public void setValid(@AssertFieldConstraints
082: final boolean isValid) {
083: this .isValid = isValid;
084: }
085:
086: public void setZipCode(@AssertFieldConstraints
087: final String zipCode) {
088: this .zipCode = zipCode;
089: }
090:
091: public void setZipCode2(final String zipCode) {
092: this .zipCode = zipCode;
093: }
094: }
095:
096: /**
097: * by default constraints specified for a field are also used for validating
098: * method parameters of the corresponding setter methods
099: */
100: public void testSetterValidation() throws Exception {
101: final Person p = new Person();
102:
103: final Guard guard = new Guard();
104: TestGuardAspect.aspectOf().setGuard(guard);
105:
106: guard.setInProbeMode(p, true);
107:
108: final ConstraintsViolatedAdapter va = new ConstraintsViolatedAdapter();
109: guard.addListener(va, p);
110:
111: // test @Length(max=)
112: p.setFirstName("Mike");
113: p.setLastName("Mahoney");
114: p.setZipCode("1234567");
115: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
116: assertTrue(va.getConstraintViolations().size() == 1);
117: assertTrue(va.getConstraintViolations().get(0).getMessage()
118: .equals("LENGTH"));
119: va.clear();
120:
121: // test @NotEmpty
122: p.setZipCode("");
123: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
124: assertTrue(va.getConstraintViolations().size() == 1);
125: assertTrue(va.getConstraintViolations().get(0).getMessage()
126: .equals("NOT_EMPTY"));
127: va.clear();
128:
129: // test @RegEx
130: p.setZipCode("dffd34");
131: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
132: assertTrue(va.getConstraintViolations().size() == 1);
133: assertTrue(va.getConstraintViolations().get(0).getMessage()
134: .equals("REG_EX"));
135: va.clear();
136:
137: // test @AssertTrue
138: p.setValid(false);
139: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
140: assertTrue(va.getConstraintViolations().size() == 1);
141: assertTrue(va.getConstraintViolations().get(0).getMessage()
142: .equals("ASSERT_TRUE"));
143: va.clear();
144:
145: // test @FieldConstraint("fieldname")
146: p.setDummyFirstName(null);
147: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
148: assertTrue(va.getConstraintViolations().size() == 1);
149: assertTrue(va.getConstraintViolations().get(0).getMessage()
150: .equals("NOT_NULL"));
151: va.clear();
152:
153: // test dynamic introduction of FieldConstraintsCheck
154: {
155: p.setZipCode2("dffd34");
156: assertTrue(va.getConstraintsViolatedExceptions().size() == 0);
157: }
158: {
159: final Method setter = p.getClass().getMethod("setZipCode2",
160: new Class<?>[] { String.class });
161: final AssertFieldConstraintsCheck check = new AssertFieldConstraintsCheck();
162: guard.addChecks(setter, 0, check);
163: p.setZipCode2("dffd34");
164: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
165: assertTrue(va.getConstraintViolations().size() == 1);
166: assertTrue(va.getConstraintViolations().get(0).getMessage()
167: .equals("REG_EX"));
168: va.clear();
169: guard.removeChecks(setter, 0, check);
170: }
171: {
172: final Method setter = p.getClass().getMethod("setZipCode2",
173: new Class<?>[] { String.class });
174: final AssertFieldConstraintsCheck check = new AssertFieldConstraintsCheck();
175: check.setFieldName("firstName");
176: guard.addChecks(setter, 0, check);
177: p.setZipCode2("dffd34");
178: assertTrue(va.getConstraintsViolatedExceptions().size() == 0);
179: p.setZipCode2(null);
180: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
181: assertTrue(va.getConstraintViolations().size() == 1);
182: assertTrue(va.getConstraintViolations().get(0).getMessage()
183: .equals("NOT_NULL"));
184: va.clear();
185: guard.removeChecks(setter, 0, check);
186: }
187: {
188: p.setZipCode2("dffd34");
189: assertTrue(va.getConstraintsViolatedExceptions().size() == 0);
190: }
191: }
192: }
|