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.validator;
013:
014: import java.util.List;
015:
016: import junit.framework.TestCase;
017: import net.sf.oval.ConstraintViolation;
018: import net.sf.oval.Validator;
019: import net.sf.oval.constraint.NotNull;
020:
021: /**
022: * @author Sebastian Thomschke
023: */
024: public class ProfilesTest extends TestCase {
025: private static class Person {
026: @NotNull(profiles={"profile1"},message="NOTNULL1")
027: public String firstName;
028:
029: @NotNull(profiles={"profile2","profile3"},message="NOTNULL2")
030: public String lastName;
031:
032: @NotNull(profiles={"profile3","profile4"},message="NOTNULL3")
033: public String zipCode;
034: }
035:
036: public void testProfilesGloballyDisabled() {
037: final Validator validator = new Validator();
038:
039: // disable all profiles = no constraints
040: validator.disableAllProfiles();
041: assertFalse(validator.isProfileEnabled("profile1"));
042: assertFalse(validator.isProfileEnabled("profile2"));
043: assertFalse(validator.isProfileEnabled("profile3"));
044: {
045: final Person p = new Person();
046: final List<ConstraintViolation> violations = validator
047: .validate(p);
048: assertEquals(0, violations.size());
049: }
050:
051: // enable profile 1
052: validator.enableProfile("profile1");
053: assertTrue(validator.isProfileEnabled("profile1"));
054: {
055: final Person p = new Person();
056: final List<ConstraintViolation> violations = validator
057: .validate(p);
058: assertEquals(1, violations.size());
059: assertEquals("NOTNULL1", violations.get(0).getMessage());
060: }
061:
062: // enable profile 1 + 2
063: validator.enableProfile("profile2");
064: assertTrue(validator.isProfileEnabled("profile2"));
065: {
066: final Person p = new Person();
067: final List<ConstraintViolation> violations = validator
068: .validate(p);
069: assertEquals(violations.size(), 2);
070: }
071:
072: // enable profile 1 + 2 + 3
073: validator.enableProfile("profile3");
074: assertTrue(validator.isProfileEnabled("profile3"));
075: {
076: final Person p = new Person();
077: final List<ConstraintViolation> violations = validator
078: .validate(p);
079: assertEquals(3, violations.size());
080: }
081:
082: // enable profile 1 + 2 + 3 + 4
083: assertFalse(validator.isProfileEnabled("profile4"));
084: validator.enableProfile("profile4");
085: assertTrue(validator.isProfileEnabled("profile4"));
086: {
087: final Person p = new Person();
088: final List<ConstraintViolation> violations = validator
089: .validate(p);
090: assertEquals(violations.size(), 3);
091: }
092: }
093:
094: public void testProfilesGloballyEnabled() {
095: final Validator validator = new Validator();
096:
097: validator.enableAllProfiles();
098: {
099: final Person p = new Person();
100: final List<ConstraintViolation> violations = validator
101: .validate(p);
102: assertEquals(violations.size(), 3);
103: }
104:
105: assertTrue(validator.isProfileEnabled("profile1"));
106: validator.disableProfile("profile1");
107: assertFalse(validator.isProfileEnabled("profile1"));
108: {
109: final Person p = new Person();
110: final List<ConstraintViolation> violations = validator
111: .validate(p);
112: assertEquals(violations.size(), 2);
113: }
114:
115: assertTrue(validator.isProfileEnabled("profile2"));
116: validator.disableProfile("profile2");
117: assertFalse(validator.isProfileEnabled("profile2"));
118: {
119: final Person p = new Person();
120: final List<ConstraintViolation> violations = validator
121: .validate(p);
122: assertEquals(violations.size(), 2);
123: }
124:
125: assertTrue(validator.isProfileEnabled("profile3"));
126: validator.disableProfile("profile3");
127: assertFalse(validator.isProfileEnabled("profile3"));
128: {
129: final Person p = new Person();
130: final List<ConstraintViolation> violations = validator
131: .validate(p);
132: assertEquals(violations.size(), 1);
133: assertEquals(violations.get(0).getMessage(), "NOTNULL3");
134: }
135:
136: assertTrue(validator.isProfileEnabled("profile4"));
137: validator.disableProfile("profile4");
138: assertFalse(validator.isProfileEnabled("profile4"));
139: {
140: final Person p = new Person();
141: final List<ConstraintViolation> violations = validator
142: .validate(p);
143: assertEquals(violations.size(), 0);
144: }
145: }
146: }
|