01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.test.guard;
13:
14: import junit.framework.TestCase;
15: import net.sf.oval.constraint.AssertTrue;
16: import net.sf.oval.constraint.Length;
17: import net.sf.oval.constraint.MatchPattern;
18: import net.sf.oval.constraint.NotEmpty;
19: import net.sf.oval.constraint.NotNull;
20: import net.sf.oval.exception.ConstraintsViolatedException;
21: import net.sf.oval.guard.Guarded;
22:
23: /**
24: * @author Sebastian Thomschke
25: */
26: public class ApplyFieldConstraintsToConstructorsTest extends TestCase {
27: @Guarded(applyFieldConstraintsToConstructors=true,checkInvariants=false)
28: private class Person {
29: @SuppressWarnings("unused")
30: @AssertTrue(message="ASSERT_TRUE")
31: private boolean isValid = true;
32:
33: @SuppressWarnings("unused")
34: @NotNull(message="NOT_NULL")
35: private String firstName = "";
36:
37: @SuppressWarnings("unused")
38: @NotNull(message="NOT_NULL")
39: private String lastName = "";
40:
41: @SuppressWarnings("unused")
42: @NotNull(message="NOT_NULL")
43: @Length(max=6,message="LENGTH")
44: @NotEmpty(message="NOT_EMPTY")
45: @MatchPattern(pattern="^[0-9]*$",message="REG_EX")
46: private String zipCode = "1";
47:
48: public Person(final boolean isValid, final String firstName,
49: final String lastName, final String zipCode) {
50: super ();
51: this .isValid = isValid;
52: this .firstName = firstName;
53: this .lastName = lastName;
54: this .zipCode = zipCode;
55: }
56:
57: public Person(final String theFirstName,
58: final String theLastName, final String theZipCode) {
59: super ();
60: firstName = theFirstName;
61: lastName = theLastName;
62: zipCode = theZipCode;
63: }
64: }
65:
66: /**
67: * by default constraints specified for a field are also used for validating
68: * method parameters of the corresponding setter methods
69: */
70: public void testConstrucorParameterValidation() {
71: try {
72: new Person(false, null, null, null);
73: } catch (final ConstraintsViolatedException ex) {
74: assertEquals(ex.getConstraintViolations().length, 4);
75: }
76:
77: try {
78: new Person(true, "", "", "12345");
79: } catch (final ConstraintsViolatedException ex) {
80: fail();
81: }
82:
83: try {
84: new Person(null, null, null);
85: } catch (final ConstraintsViolatedException ex) {
86: fail();
87: }
88:
89: }
90: }
|