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.Length;
16: import net.sf.oval.constraint.NotNull;
17: import net.sf.oval.exception.ConstraintsViolatedException;
18: import net.sf.oval.guard.Guard;
19: import net.sf.oval.guard.Guarded;
20:
21: /**
22: * @author Sebastian Thomschke
23: */
24: public class MethodReturnValueConstraintsValidationTest extends
25: TestCase {
26: @Guarded
27: public static class TestEntity {
28: public String name = "";
29:
30: @NotNull(message="NOT_NULL")
31: @Length(max=4,message="LENGTH")
32: public String getName() {
33: return name;
34: }
35: }
36:
37: public void testMethodReturnValueConstraintValidation() {
38: final Guard guard = new Guard();
39:
40: TestGuardAspect.aspectOf().setGuard(guard);
41:
42: final TestEntity t = new TestEntity();
43:
44: try {
45: t.name = null;
46: t.getName();
47: fail();
48: } catch (final ConstraintsViolatedException e) {
49: assertTrue(e.getConstraintViolations().length == 1);
50: assertTrue(e.getConstraintViolations()[0].getMessage()
51: .equals("NOT_NULL"));
52: }
53:
54: t.name = "testtest";
55:
56: try {
57: t.getName();
58: fail();
59: } catch (final ConstraintsViolatedException e) {
60: assertTrue(e.getConstraintViolations().length == 1);
61: assertTrue(e.getConstraintViolations()[0].getMessage()
62: .equals("LENGTH"));
63: }
64: }
65:
66: }
|