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.NotNull;
16: import net.sf.oval.exception.ConstraintsViolatedException;
17: import net.sf.oval.exception.ExceptionTranslatorJDKExceptionsImpl;
18: import net.sf.oval.guard.Guard;
19: import net.sf.oval.guard.Guarded;
20:
21: /**
22: *
23: * @author Sebastian Thomschke
24: */
25: public class ExceptionTranslatorTest extends TestCase {
26: @Guarded
27: public final static class TestEntity {
28: public void setName(@NotNull(message="NULL")
29: final String name) {
30: // ...
31: }
32: }
33:
34: public void testExceptionTranslator() {
35: final Guard guard = new Guard();
36: TestGuardAspect.aspectOf().setGuard(guard);
37:
38: assertNull(guard.getExceptionTranslator());
39:
40: try {
41: final TestEntity t = new TestEntity();
42: t.setName(null);
43: } catch (final ConstraintsViolatedException ex) {
44: assertEquals(ex.getMessage(), "NULL");
45: }
46:
47: try {
48: guard
49: .setExceptionTranslator(new ExceptionTranslatorJDKExceptionsImpl());
50: try {
51: final TestEntity t = new TestEntity();
52: t.setName(null);
53: fail();
54: } catch (final IllegalArgumentException ex) {
55: assertEquals(ex.getMessage(), "NULL");
56: }
57:
58: } finally {
59: guard.setExceptionTranslator(null);
60: }
61: }
62: }
|