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.List;
015:
016: import junit.framework.TestCase;
017: import net.sf.oval.ConstraintViolation;
018: import net.sf.oval.constraint.Length;
019: import net.sf.oval.constraint.NotNull;
020: import net.sf.oval.context.ConstructorParameterContext;
021: import net.sf.oval.context.FieldContext;
022: import net.sf.oval.exception.ConstraintsViolatedException;
023: import net.sf.oval.guard.ConstraintsViolatedAdapter;
024: import net.sf.oval.guard.Guard;
025: import net.sf.oval.guard.Guarded;
026:
027: /**
028: * @author Sebastian Thomschke
029: */
030: public class ParameterConstraintsTest extends TestCase {
031: @Guarded
032: public static class TestEntity {
033: @SuppressWarnings("unused")
034: @NotNull(message="NOT_NULL")
035: private String name = "";
036:
037: /**
038: * Constructor 1
039: *
040: * @param name
041: */
042: public TestEntity(@NotNull(message="NOT_NULL")
043: final String name) {
044: this .name = name;
045: }
046:
047: /**
048: * Constructor 2
049: *
050: * @param name
051: * @param bla
052: */
053: public TestEntity(final String name, final int bla) {
054: this .name = name;
055: }
056:
057: public void setName(@NotNull(message="NOT_NULL")
058: @Length(max=4,message="LENGTH")
059: final String name) {
060: this .name = name;
061: }
062: }
063:
064: public void testConstructorParameterConstraints() {
065: final Guard guard = new Guard();
066: TestGuardAspect.aspectOf().setGuard(guard);
067:
068: /*
069: * Testing Constructor 1
070: * parameter constraint
071: */
072: try {
073: new TestEntity(null);
074: fail();
075: } catch (final ConstraintsViolatedException e) {
076: final ConstraintViolation[] violations = e
077: .getConstraintViolations();
078: assertTrue(violations != null && violations.length == 1);
079: assertTrue(violations[0].getMessage().equals("NOT_NULL"));
080: assertTrue(violations[0].getContext() instanceof ConstructorParameterContext);
081: }
082:
083: new TestEntity("test");
084:
085: /*
086: * Testing Constructor 2
087: * invariant constraint
088: */
089: try {
090: new TestEntity(null, 100);
091: fail();
092: } catch (final ConstraintsViolatedException e) {
093: final ConstraintViolation[] violations = e
094: .getConstraintViolations();
095: assertTrue(violations != null && violations.length == 1);
096: assertTrue(violations[0].getMessage().equals("NOT_NULL"));
097: assertTrue(violations[0].getContext() instanceof FieldContext);
098: }
099: }
100:
101: public void testMethodParameters() {
102: final Guard guard = new Guard();
103: TestGuardAspect.aspectOf().setGuard(guard);
104:
105: try {
106: final TestEntity t1 = new TestEntity("");
107: t1.setName(null);
108: fail();
109: } catch (final ConstraintsViolatedException e) {
110: final ConstraintViolation[] violations = e
111: .getConstraintViolations();
112: assertTrue(violations != null && violations.length > 0);
113: assertTrue(violations[0].getMessage().equals("NOT_NULL"));
114: }
115:
116: try {
117: final TestEntity t1 = new TestEntity("");
118: t1.setName("12345678");
119: fail();
120: } catch (final ConstraintsViolatedException e) {
121: final ConstraintViolation[] violations = e
122: .getConstraintViolations();
123: assertTrue(violations != null && violations.length > 0);
124: assertTrue(violations[0].getMessage().equals("LENGTH"));
125: }
126: }
127:
128: public void testMethodParametersInProbeMode() {
129: final Guard guard = new Guard();
130: TestGuardAspect.aspectOf().setGuard(guard);
131:
132: final TestEntity entity = new TestEntity("");
133:
134: guard.setInProbeMode(entity, true);
135:
136: final ConstraintsViolatedAdapter va = new ConstraintsViolatedAdapter();
137: guard.addListener(va, entity);
138:
139: entity.setName(null);
140: entity.setName("12345678");
141: final List<ConstraintViolation> violations = va
142: .getConstraintViolations();
143: assertTrue(violations.size() == 2);
144: assertTrue(violations.get(0).getMessage().equals("NOT_NULL"));
145: assertTrue(violations.get(1).getMessage().equals("LENGTH"));
146:
147: guard.removeListener(va, entity);
148: }
149: }
|