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.util.ArrayList;
015: import java.util.List;
016:
017: import junit.framework.TestCase;
018: import net.sf.oval.Check;
019: import net.sf.oval.ConstraintSet;
020: import net.sf.oval.constraint.AssertConstraintSet;
021: import net.sf.oval.constraint.LengthCheck;
022: import net.sf.oval.constraint.MatchPatternCheck;
023: import net.sf.oval.constraint.NotEmptyCheck;
024: import net.sf.oval.constraint.NotNullCheck;
025: import net.sf.oval.guard.ConstraintsViolatedAdapter;
026: import net.sf.oval.guard.Guard;
027: import net.sf.oval.guard.Guarded;
028:
029: /**
030: * @author Sebastian Thomschke
031: */
032: public class ConstraintSetTest extends TestCase {
033: @Guarded
034: private class Person {
035: private String zipCode;
036:
037: public String getZipCode() {
038: return zipCode;
039: }
040:
041: public void setZipCode(@AssertConstraintSet(id="zipCode")
042: String zipCode) {
043: this .zipCode = zipCode;
044: }
045: }
046:
047: public void testConstraintSetValidation() {
048: ConstraintSet constraintSet = new ConstraintSet("zipCode");
049: List<Check> checks = new ArrayList<Check>();
050: constraintSet.setChecks(checks);
051:
052: NotNullCheck notNull = new NotNullCheck();
053: notNull.setMessage("NOT_NULL");
054: checks.add(notNull);
055:
056: LengthCheck length = new LengthCheck();
057: length.setMessage("LENGTH");
058: length.setMax(6);
059: checks.add(length);
060:
061: NotEmptyCheck notEmpty = new NotEmptyCheck();
062: notEmpty.setMessage("NOT_EMPTY");
063: checks.add(notEmpty);
064:
065: MatchPatternCheck matchPattern = new MatchPatternCheck();
066: matchPattern.setMessage("MATCH_PATTERN");
067: matchPattern.setPattern("^[0-9]*$", 0);
068: checks.add(matchPattern);
069:
070: final Guard guard = new Guard();
071: TestGuardAspect.aspectOf().setGuard(guard);
072:
073: guard.addConstraintSet(constraintSet, false);
074:
075: {
076: final Person p = new Person();
077:
078: TestGuardAspect.aspectOf().getGuard().setInProbeMode(p,
079: true);
080:
081: final ConstraintsViolatedAdapter va = new ConstraintsViolatedAdapter();
082: TestGuardAspect.aspectOf().getGuard().addListener(va, p);
083:
084: // test @Length(max=)
085: p.setZipCode("1234567");
086: assertEquals(va.getConstraintsViolatedExceptions().size(),
087: 1);
088: assertEquals(va.getConstraintViolations().size(), 1);
089: assertEquals(va.getConstraintViolations().get(0)
090: .getMessage(), "LENGTH");
091: va.clear();
092:
093: // test @NotEmpty
094: p.setZipCode("");
095: assertEquals(va.getConstraintsViolatedExceptions().size(),
096: 1);
097: assertEquals(va.getConstraintViolations().size(), 1);
098: assertEquals(va.getConstraintViolations().get(0)
099: .getMessage(), "NOT_EMPTY");
100: va.clear();
101:
102: // test @MatchPattern
103: p.setZipCode("dffd34");
104: assertEquals(va.getConstraintsViolatedExceptions().size(),
105: 1);
106: assertEquals(va.getConstraintViolations().size(), 1);
107: assertEquals(va.getConstraintViolations().get(0)
108: .getMessage(), "MATCH_PATTERN");
109: va.clear();
110: }
111:
112: }
113: }
|