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.constraint.AssertTrue;
016: import net.sf.oval.constraint.Length;
017: import net.sf.oval.constraint.MatchPattern;
018: import net.sf.oval.constraint.NotEmpty;
019: import net.sf.oval.constraint.NotNull;
020: import net.sf.oval.guard.ConstraintsViolatedAdapter;
021: import net.sf.oval.guard.Guard;
022: import net.sf.oval.guard.Guarded;
023:
024: /**
025: * @author Sebastian Thomschke
026: */
027: public class ApplyFieldConstraintsToSettersTest extends TestCase {
028: @Guarded(applyFieldConstraintsToSetters=true)
029: private class Person {
030: @AssertTrue(message="ASSERT_TRUE")
031: private boolean isValid = true;
032:
033: @NotNull(message="NOT_NULL")
034: private String firstName = "";
035:
036: @NotNull(message="NOT_NULL")
037: private String lastName = "";
038:
039: @NotNull(message="NOT_NULL")
040: @Length(max=6,message="LENGTH")
041: @NotEmpty(message="NOT_EMPTY")
042: @MatchPattern(pattern="^[0-9]*$",message="REG_EX")
043: private String zipCode = "1";
044:
045: public String getFirstName() {
046: return firstName;
047: }
048:
049: public String getLastName() {
050: return lastName;
051: }
052:
053: public String getZipCode() {
054: return zipCode;
055: }
056:
057: public boolean isValid() {
058: return isValid;
059: }
060:
061: public void setFirstName(final String firstName) {
062: this .firstName = firstName;
063: }
064:
065: public void setLastName(final String lastName) {
066: this .lastName = lastName;
067: }
068:
069: public void setValid(final boolean isValid) {
070: this .isValid = isValid;
071: }
072:
073: public void setZipCode(final String zipCode) {
074: this .zipCode = zipCode;
075: }
076: }
077:
078: /**
079: * by default constraints specified for a field are also used for validating
080: * method parameters of the corresponding setter methods
081: */
082: public void testSetterValidation() {
083: final Person p = new Person();
084:
085: final Guard guard = new Guard();
086: TestGuardAspect.aspectOf().setGuard(guard);
087:
088: guard.setInProbeMode(p, true);
089:
090: final ConstraintsViolatedAdapter va = new ConstraintsViolatedAdapter();
091: guard.addListener(va, p);
092:
093: // test @Length(max=)
094: p.setFirstName("Mike");
095: p.setLastName("Mahoney");
096: p.setZipCode("1234567");
097: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
098: assertTrue(va.getConstraintViolations().size() == 1);
099: assertTrue(va.getConstraintViolations().get(0).getMessage()
100: .equals("LENGTH"));
101: va.clear();
102:
103: // test @NotEmpty
104: p.setZipCode("");
105: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
106: assertTrue(va.getConstraintViolations().size() == 1);
107: assertTrue(va.getConstraintViolations().get(0).getMessage()
108: .equals("NOT_EMPTY"));
109: va.clear();
110:
111: // test @RegEx
112: p.setZipCode("dffd34");
113: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
114: assertTrue(va.getConstraintViolations().size() == 1);
115: assertTrue(va.getConstraintViolations().get(0).getMessage()
116: .equals("REG_EX"));
117: va.clear();
118:
119: // test @AssertTrue
120: p.setValid(false);
121: assertTrue(va.getConstraintsViolatedExceptions().size() == 1);
122: assertTrue(va.getConstraintViolations().size() == 1);
123: assertTrue(va.getConstraintViolations().get(0).getMessage()
124: .equals("ASSERT_TRUE"));
125: va.clear();
126: }
127: }
|