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.io.ByteArrayInputStream;
015: import java.io.ByteArrayOutputStream;
016: import java.io.ObjectInputStream;
017: import java.io.ObjectOutputStream;
018: import java.util.ArrayList;
019: import java.util.HashSet;
020: import java.util.List;
021: import java.util.Set;
022: import java.util.regex.Pattern;
023:
024: import junit.framework.TestCase;
025: import net.sf.oval.Check;
026: import net.sf.oval.ConstraintViolation;
027: import net.sf.oval.Validator;
028: import net.sf.oval.configuration.pojo.POJOConfigurer;
029: import net.sf.oval.configuration.pojo.elements.ClassConfiguration;
030: import net.sf.oval.configuration.pojo.elements.ConstraintSetConfiguration;
031: import net.sf.oval.configuration.pojo.elements.FieldConfiguration;
032: import net.sf.oval.configuration.pojo.elements.MethodConfiguration;
033: import net.sf.oval.configuration.pojo.elements.MethodReturnValueConfiguration;
034: import net.sf.oval.configuration.xml.XMLConfigurer;
035: import net.sf.oval.constraint.AssertCheck;
036: import net.sf.oval.constraint.AssertConstraintSetCheck;
037: import net.sf.oval.constraint.Length;
038: import net.sf.oval.constraint.LengthCheck;
039: import net.sf.oval.constraint.MatchPatternCheck;
040: import net.sf.oval.constraint.NotNullCheck;
041:
042: /**
043: * @author Sebastian Thomschke
044: */
045: public class XMLConfigurationTest extends TestCase {
046: public static class User {
047: // added @Length to test if overwrite=true works
048: @Length(min=10,max=10)
049: protected String userId;
050:
051: protected String managerId;
052:
053: protected String firstName;
054:
055: protected String lastName;
056:
057: /**
058: * @return the managerId
059: */
060: public String getManagerId() {
061: return managerId;
062: }
063: }
064:
065: public void testImportedFile() {
066: final XMLConfigurer x = new XMLConfigurer();
067: x.fromXML(XMLConfigurationTest.class
068: .getResourceAsStream("XMLConfigurationTest.xml"));
069: validateUser(new Validator(x));
070: }
071:
072: public void testSerializedObjectConfiguration() throws Exception {
073: final XMLConfigurer x = new XMLConfigurer();
074:
075: /*
076: * define a configuration
077: */
078: final Set<ConstraintSetConfiguration> constraintSetsConfig = new HashSet<ConstraintSetConfiguration>();
079: {
080: final ConstraintSetConfiguration csf = new ConstraintSetConfiguration();
081: constraintSetsConfig.add(csf);
082:
083: csf.id = "user.userid";
084: csf.checks = new ArrayList<Check>();
085: final NotNullCheck nnc = new NotNullCheck();
086: nnc.setMessage("{context} is null");
087: csf.checks.add(nnc);
088: final MatchPatternCheck rec = new MatchPatternCheck();
089: rec.setPattern(Pattern.compile("^[a-z0-9]{8}$", 0));
090: rec
091: .setMessage("{context} does not match the pattern {pattern}");
092: rec.setProfiles("a", "b");
093: csf.checks.add(rec);
094: }
095:
096: final Set<ClassConfiguration> classConfigs = new HashSet<ClassConfiguration>();
097: {
098: final ClassConfiguration cf = new ClassConfiguration();
099: classConfigs.add(cf);
100: cf.type = User.class;
101:
102: cf.fieldConfigurations = new HashSet<FieldConfiguration>();
103: {
104: final FieldConfiguration fc = new FieldConfiguration();
105: cf.fieldConfigurations.add(fc);
106:
107: fc.name = "firstName";
108: fc.checks = new ArrayList<Check>();
109: final AssertCheck ac = new AssertCheck();
110: ac
111: .setExpression("_value != null && _value.length() < 3");
112: ac
113: .setMessage("{context} cannot be longer than 3 characters");
114: ac.setLang("groovy");
115: fc.checks.add(ac);
116: }
117: {
118: final FieldConfiguration fc = new FieldConfiguration();
119: cf.fieldConfigurations.add(fc);
120:
121: fc.name = "lastName";
122: fc.checks = new ArrayList<Check>();
123: final LengthCheck lc = new LengthCheck();
124: lc
125: .setMessage("{context} is not between {min} and {max} characters long");
126: lc.setMin(1);
127: lc.setMax(5);
128: fc.checks.add(lc);
129: }
130: {
131: final FieldConfiguration fc = new FieldConfiguration();
132: fc.overwrite = Boolean.TRUE;
133: cf.fieldConfigurations.add(fc);
134:
135: fc.name = "userId";
136: fc.checks = new ArrayList<Check>();
137: final AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
138: acsc.setId("user.userid");
139: fc.checks.add(acsc);
140: }
141:
142: cf.methodConfigurations = new HashSet<MethodConfiguration>();
143: {
144: final MethodConfiguration mc = new MethodConfiguration();
145: cf.methodConfigurations.add(mc);
146: mc.name = "getManagerId";
147: mc.isInvariant = true;
148: mc.returnValueConfiguration = new MethodReturnValueConfiguration();
149: mc.returnValueConfiguration.checks = new ArrayList<Check>();
150: final AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
151: acsc.setId("user.userid");
152: mc.returnValueConfiguration.checks.add(acsc);
153: }
154: }
155:
156: x.getPojoConfigurer().setClassConfigurations(classConfigs);
157: x.getPojoConfigurer().setConstraintSetConfigurations(
158: constraintSetsConfig);
159:
160: /*
161: * test POJO Configurer object serialization
162: */
163: final ByteArrayOutputStream bos = new ByteArrayOutputStream();
164: final ObjectOutputStream oos = new ObjectOutputStream(bos);
165: oos.writeObject(x.getPojoConfigurer());
166: oos.flush();
167: oos.close();
168:
169: final ByteArrayInputStream bin = new ByteArrayInputStream(bos
170: .toByteArray());
171: final ObjectInputStream ois = new ObjectInputStream(bin);
172: x.setPojoConfigurer((POJOConfigurer) ois.readObject());
173: ois.close();
174:
175: /*
176: * test XML de/serialization
177: */
178: final String xmlConfig = x.toXML();
179: // System.out.println(xmlConfig);
180: x.fromXML(xmlConfig);
181: validateUser(new Validator(x));
182: }
183:
184: private void validateUser(final Validator validator) {
185: final User usr = new User();
186:
187: usr.lastName = "1";
188: usr.userId = "12345678";
189: usr.managerId = "12345678";
190:
191: /*
192: * check constraints for firstName
193: */
194: usr.firstName = "123456";
195: List<ConstraintViolation> violations = validator.validate(usr);
196: assertEquals(1, violations.size());
197: assertEquals(User.class.getName()
198: + ".firstName cannot be longer than 3 characters",
199: violations.get(0).getMessage());
200:
201: usr.firstName = "";
202:
203: /*
204: * check constraints for lastName
205: */
206: usr.lastName = "123456";
207: violations = validator.validate(usr);
208: assertEquals(1, violations.size());
209: assertEquals(User.class.getName()
210: + ".lastName is not between 1 and 5 characters long",
211: violations.get(0).getMessage());
212:
213: usr.lastName = "1";
214:
215: /*
216: * check constraints for userId
217: */
218: usr.userId = null;
219: violations = validator.validate(usr);
220: assertEquals(1, violations.size());
221: assertEquals(User.class.getName() + ".userId is null",
222: violations.get(0).getMessage());
223:
224: usr.userId = "%$$e3";
225: violations = validator.validate(usr);
226: assertEquals(1, violations.size());
227: assertEquals(User.class.getName()
228: + ".userId does not match the pattern ^[a-z0-9]{8}$",
229: violations.get(0).getMessage());
230: usr.userId = "12345678";
231:
232: /*
233: * check constraints for managerId
234: */
235: usr.managerId = null;
236: violations = validator.validate(usr);
237: assertEquals(1, violations.size());
238: assertEquals(User.class.getName() + ".getManagerId() is null",
239: violations.get(0).getMessage());
240:
241: usr.managerId = "%$$e3";
242: violations = validator.validate(usr);
243: assertEquals(1, violations.size());
244: assertEquals(
245: User.class.getName()
246: + ".getManagerId() does not match the pattern ^[a-z0-9]{8}$",
247: violations.get(0).getMessage());
248: }
249: }
|