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.HashSet;
016: import java.util.Set;
017: import java.util.regex.Pattern;
018:
019: import junit.framework.TestCase;
020: import net.sf.oval.Check;
021: import net.sf.oval.ConstraintViolation;
022: import net.sf.oval.configuration.pojo.elements.ClassConfiguration;
023: import net.sf.oval.configuration.pojo.elements.ConstraintSetConfiguration;
024: import net.sf.oval.configuration.pojo.elements.ConstructorConfiguration;
025: import net.sf.oval.configuration.pojo.elements.FieldConfiguration;
026: import net.sf.oval.configuration.pojo.elements.MethodConfiguration;
027: import net.sf.oval.configuration.pojo.elements.MethodReturnValueConfiguration;
028: import net.sf.oval.configuration.pojo.elements.ParameterConfiguration;
029: import net.sf.oval.configuration.xml.XMLConfigurer;
030: import net.sf.oval.constraint.AssertConstraintSetCheck;
031: import net.sf.oval.constraint.Length;
032: import net.sf.oval.constraint.LengthCheck;
033: import net.sf.oval.constraint.MatchPatternCheck;
034: import net.sf.oval.constraint.NotNullCheck;
035: import net.sf.oval.exception.ConstraintsViolatedException;
036: import net.sf.oval.exception.ValidationFailedException;
037: import net.sf.oval.guard.ConstraintsViolatedAdapter;
038: import net.sf.oval.guard.Guard;
039: import net.sf.oval.guard.Guarded;
040:
041: /**
042: * @author Sebastian Thomschke
043: */
044: public class XMLConfigurationTest extends TestCase {
045: @Guarded
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: public User() {
058: // do nothing
059: }
060:
061: public User(String userId, String managerId, int somethingElse) {
062: this .userId = userId;
063: this .managerId = managerId;
064: }
065:
066: /**
067: * @return the managerId
068: */
069: public String getManagerId() {
070: return managerId;
071: }
072:
073: /**
074: * @param managerId the managerId to set
075: */
076: public void setManagerId(String managerId) {
077: this .managerId = managerId;
078: }
079: }
080:
081: public void testImportedFile() {
082: try {
083: XMLConfigurer x = new XMLConfigurer();
084: x.fromXML(XMLConfigurationTest.class
085: .getResourceAsStream("XMLConfigurationTest.xml"));
086:
087: Guard guard = new Guard(x);
088: guard.setInvariantsEnabled(false);
089: TestGuardAspect.aspectOf().setGuard(guard);
090:
091: validateUser();
092: } catch (ValidationFailedException ex) {
093: ex.getCause().printStackTrace();
094: throw ex;
095: }
096: }
097:
098: public void testSerializedObjectConfiguration() {
099: XMLConfigurer x = new XMLConfigurer();
100:
101: /*
102: * define a configuration
103: */
104: final Set<ConstraintSetConfiguration> constraintSetsConfig = new HashSet<ConstraintSetConfiguration>();
105: {
106: ConstraintSetConfiguration csf = new ConstraintSetConfiguration();
107: constraintSetsConfig.add(csf);
108:
109: csf.id = "user.userid";
110: csf.checks = new ArrayList<Check>();
111: NotNullCheck nnc = new NotNullCheck();
112: nnc.setMessage("{context} is null");
113: csf.checks.add(nnc);
114: MatchPatternCheck rec = new MatchPatternCheck();
115: rec.setPattern(Pattern.compile("^[a-z0-9]{8}$", 0));
116: rec
117: .setMessage("{context} does not match the pattern {pattern}");
118: csf.checks.add(rec);
119: }
120:
121: final Set<ClassConfiguration> classConfigs = new HashSet<ClassConfiguration>();
122: {
123: ClassConfiguration cf = new ClassConfiguration();
124: classConfigs.add(cf);
125: cf.type = User.class;
126:
127: cf.fieldConfigurations = new HashSet<FieldConfiguration>();
128: {
129: FieldConfiguration fc = new FieldConfiguration();
130: cf.fieldConfigurations.add(fc);
131:
132: fc.name = "firstName";
133: fc.checks = new ArrayList<Check>();
134: LengthCheck lc = new LengthCheck();
135: lc
136: .setMessage("{context} is not between {min} and {max} characters long");
137: lc.setMax(3);
138: fc.checks.add(lc);
139: }
140: {
141: FieldConfiguration fc = new FieldConfiguration();
142: cf.fieldConfigurations.add(fc);
143:
144: fc.name = "lastName";
145: fc.checks = new ArrayList<Check>();
146: LengthCheck lc = new LengthCheck();
147: lc
148: .setMessage("{context} is not between {min} and {max} characters long");
149: lc.setMax(5);
150: fc.checks.add(lc);
151: }
152: {
153: FieldConfiguration fc = new FieldConfiguration();
154: fc.overwrite = Boolean.TRUE;
155: cf.fieldConfigurations.add(fc);
156:
157: fc.name = "userId";
158: fc.checks = new ArrayList<Check>();
159: AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
160: acsc.setId("user.userid");
161: fc.checks.add(acsc);
162: }
163:
164: cf.constructorConfigurations = new HashSet<ConstructorConfiguration>();
165: {
166: ConstructorConfiguration cc = new ConstructorConfiguration();
167: cf.constructorConfigurations.add(cc);
168: cc.parameterConfigurations = new ArrayList<ParameterConfiguration>();
169:
170: AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
171: acsc.setId("user.userid");
172:
173: ParameterConfiguration pc1 = new ParameterConfiguration();
174: pc1.type = String.class;
175: pc1.checks = new ArrayList<Check>();
176: pc1.checks.add(acsc);
177: cc.parameterConfigurations.add(pc1);
178: ParameterConfiguration pc2 = new ParameterConfiguration();
179: pc2.type = String.class;
180: pc2.checks = new ArrayList<Check>();
181: pc2.checks.add(acsc);
182: cc.parameterConfigurations.add(pc2);
183: ParameterConfiguration pc3 = new ParameterConfiguration();
184: pc3.type = int.class;
185: cc.parameterConfigurations.add(pc3);
186: }
187:
188: cf.methodConfigurations = new HashSet<MethodConfiguration>();
189: {
190: AssertConstraintSetCheck acsc = new AssertConstraintSetCheck();
191: acsc.setId("user.userid");
192:
193: MethodConfiguration mc = new MethodConfiguration();
194: cf.methodConfigurations.add(mc);
195: mc.name = "getManagerId";
196: mc.returnValueConfiguration = new MethodReturnValueConfiguration();
197: mc.returnValueConfiguration.checks = new ArrayList<Check>();
198: mc.returnValueConfiguration.checks.add(acsc);
199:
200: mc = new MethodConfiguration();
201: cf.methodConfigurations.add(mc);
202: mc.name = "setManagerId";
203: mc.parameterConfigurations = new ArrayList<ParameterConfiguration>();
204: ParameterConfiguration pc1 = new ParameterConfiguration();
205: pc1.type = String.class;
206: pc1.checks = new ArrayList<Check>();
207: pc1.checks.add(acsc);
208: mc.parameterConfigurations.add(pc1);
209: }
210: }
211:
212: x.getPojoConfigurer().setClassConfigurations(classConfigs);
213: x.getPojoConfigurer().setConstraintSetConfigurations(
214: constraintSetsConfig);
215:
216: /*
217: * serialize the configuration to XML
218: */
219: String xmlConfig = x.toXML();
220:
221: /*
222: * deserialize the configuration from XML
223: */
224: x.fromXML(xmlConfig);
225:
226: Guard guard = new Guard(x);
227: guard.setInvariantsEnabled(false);
228: TestGuardAspect.aspectOf().setGuard(guard);
229:
230: validateUser();
231: }
232:
233: private void validateUser() {
234: ConstraintsViolatedAdapter listener = new ConstraintsViolatedAdapter();
235: TestGuardAspect.aspectOf().getGuard().addListener(listener,
236: User.class);
237:
238: listener.clear();
239: try {
240: new User(null, null, 1);
241: fail("ConstraintViolationException expected");
242: } catch (ConstraintsViolatedException ex) {
243: ConstraintViolation[] violations = ex
244: .getConstraintViolations();
245: assertEquals(2, violations.length);
246: assertEquals(
247: User.class.getName()
248: + "(class java.lang.String,class java.lang.String,int) Parameter 0 (userId) is null",
249: violations[0].getMessage());
250: assertEquals(
251: User.class.getName()
252: + "(class java.lang.String,class java.lang.String,int) Parameter 1 (managerId) is null",
253: violations[1].getMessage());
254: }
255:
256: listener.clear();
257: try {
258: User user = new User("12345678", "12345678", 1);
259: user.setManagerId(null);
260: fail("ConstraintViolationException expected");
261: } catch (ConstraintsViolatedException ex) {
262: ConstraintViolation[] violations = ex
263: .getConstraintViolations();
264: assertEquals(1, violations.length);
265: assertEquals(
266: User.class.getName()
267: + ".setManagerId(class java.lang.String) Parameter 0 (managerId) is null",
268: violations[0].getMessage());
269: }
270:
271: listener.clear();
272: try {
273: User user = new User();
274: user.getManagerId();
275: fail("ConstraintViolationException expected");
276: } catch (ConstraintsViolatedException ex) {
277: ConstraintViolation[] violations = ex
278: .getConstraintViolations();
279: assertEquals(1, violations.length);
280: assertEquals(User.class.getName()
281: + ".getManagerId() is null", violations[0]
282: .getMessage());
283: }
284: }
285: }
|