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.ArrayList;
015: import java.util.Arrays;
016: import java.util.HashMap;
017: import java.util.HashSet;
018: import java.util.List;
019: import java.util.Map;
020: import java.util.Set;
021:
022: import junit.framework.TestCase;
023: import net.sf.oval.ConstraintViolation;
024: import net.sf.oval.Validator;
025: import net.sf.oval.constraint.AssertValid;
026: import net.sf.oval.constraint.Length;
027: import net.sf.oval.constraint.MatchPattern;
028: import net.sf.oval.constraint.NotEmpty;
029: import net.sf.oval.constraint.NotNull;
030:
031: /**
032: * @author Sebastian Thomschke
033: */
034: public class AssertValidTest extends TestCase {
035: private static class Address {
036: @NotNull
037: public String street;
038:
039: @NotNull
040: public String city;
041:
042: @NotNull
043: @Length(max=6)
044: @NotEmpty
045: @MatchPattern(pattern="^[0-9]*$")
046: public String zipCode;
047:
048: @AssertValid(message="ASSERT_VALID")
049: public Person contact;
050: }
051:
052: private static class Person {
053: @NotNull
054: public String firstName;
055:
056: @NotNull
057: public String lastName;
058:
059: @AssertValid(message="ASSERT_VALID")
060: public Address homeAddress;
061:
062: @AssertValid(message="ASSERT_VALID")
063: public List<Address> otherAddresses1;
064:
065: @AssertValid(message="ASSERT_VALID",requireValidElements=false)
066: public Set<Address> otherAddresses2;
067:
068: @AssertValid(message="ASSERT_VALID",requireValidElements=true)
069: public Set<Address> otherAddresses3;
070:
071: }
072:
073: private static class Registry {
074: @AssertValid
075: public List<Address[]> addressClusters;
076:
077: @AssertValid
078: public Map<String, List<Person>> personsByCity;
079:
080: @AssertValid
081: public Map<String, Map<String, Address[]>> addressesByCityAndStreet;
082: }
083:
084: public void testCollectionValues() {
085: final Validator validator = new Validator();
086:
087: final Person p = new Person();
088: p.firstName = "John";
089: p.lastName = "Doe";
090: p.otherAddresses1 = new ArrayList<Address>();
091: p.otherAddresses2 = new HashSet<Address>();
092: p.otherAddresses3 = new HashSet<Address>();
093:
094: final Address a = new Address();
095: a.street = "The Street";
096: a.city = "The City";
097: a.zipCode = null;
098: assertEquals(1, validator.validate(a).size());
099:
100: p.otherAddresses1.add(a);
101: assertEquals(1, validator.validate(p).size());
102:
103: p.otherAddresses1.remove(a);
104: p.otherAddresses2.add(a);
105: assertEquals(0, validator.validate(p).size());
106:
107: p.otherAddresses3.add(a);
108: assertEquals(1, validator.validate(p).size());
109: }
110:
111: public void testRecursion() {
112: final Validator validator = new Validator();
113:
114: final Registry registry = new Registry();
115:
116: // nulled collections and maps are valid
117: assertTrue(validator.validate(registry).size() == 0);
118:
119: registry.addressesByCityAndStreet = new HashMap<String, Map<String, Address[]>>();
120: registry.addressClusters = new ArrayList<Address[]>();
121: registry.personsByCity = new HashMap<String, List<Person>>();
122:
123: // empty collections and maps are valid
124: assertEquals(0, validator.validate(registry).size());
125:
126: final Person invalidPerson1 = new Person();
127: final Person invalidPerson2 = new Person();
128:
129: // map with an empty list is valid
130: registry.personsByCity.put("city1", new ArrayList<Person>());
131: assertEquals(0, validator.validate(registry).size());
132:
133: registry.personsByCity.put("city1", Arrays
134: .asList(new Person[] { invalidPerson1 }));
135: assertEquals(1, validator.validate(registry).size());
136:
137: registry.personsByCity.put("city2", Arrays
138: .asList(new Person[] { invalidPerson2 }));
139: assertEquals(2, validator.validate(registry).size());
140:
141: registry.personsByCity.clear();
142: registry.personsByCity.put("city1", Arrays.asList(new Person[] {
143: invalidPerson1, invalidPerson1, invalidPerson2,
144: invalidPerson2 }));
145: assertEquals(4, validator.validate(registry).size());
146:
147: registry.personsByCity.clear();
148:
149: // list with an array with empty elements is valid
150: registry.addressClusters.add(new Address[10]);
151: assertEquals(0, validator.validate(registry).size());
152:
153: final Address invalidAddress1 = new Address();
154: final Address invalidAddress2 = new Address();
155:
156: registry.addressClusters.add(new Address[10]);
157: assertEquals(0, validator.validate(registry).size());
158:
159: registry.addressClusters.add(new Address[] { invalidAddress1,
160: invalidAddress2, invalidAddress1, invalidAddress2 });
161: assertEquals(4, validator.validate(registry).size());
162:
163: registry.addressClusters.clear();
164:
165: // map with an entry with an empty map is valid
166: registry.addressesByCityAndStreet.put("city1",
167: new HashMap<String, Address[]>());
168: assertEquals(0, validator.validate(registry).size());
169:
170: // map with an entry with an map with an element with an empty array is valid
171: registry.addressesByCityAndStreet.get("city1").put("street1",
172: new Address[0]);
173: assertEquals(0, validator.validate(registry).size());
174:
175: registry.addressesByCityAndStreet.get("city1").put(
176: "street1",
177: new Address[] { invalidAddress1, invalidAddress1,
178: invalidAddress2, invalidAddress2 });
179: assertEquals(4, validator.validate(registry).size());
180: }
181:
182: public void testScalarValues() {
183: final Validator validator = new Validator();
184:
185: final Person p = new Person();
186: p.firstName = "John";
187: p.lastName = "Doe";
188: assertEquals(0, validator.validate(p).size());
189:
190: final Address a = new Address();
191: a.street = "The Street";
192: a.city = "The City";
193: a.zipCode = "12345";
194: assertEquals(0, validator.validate(a).size());
195:
196: // make the address invalid
197: a.zipCode = null;
198: assertEquals(1, validator.validate(a).size());
199:
200: // associate the invalid address with the person check the person for validity
201: p.homeAddress = a;
202: List<ConstraintViolation> violations = validator.validate(p);
203: assertEquals(1, violations.size());
204: assertEquals("ASSERT_VALID", violations.get(0).getMessage());
205:
206: // test circular dependencies
207: a.contact = p;
208: violations = validator.validate(p);
209: assertEquals(1, violations.size());
210: assertEquals("ASSERT_VALID", violations.get(0).getMessage());
211: }
212: }
|