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 junit.framework.TestCase;
015: import net.sf.oval.constraint.NotNull;
016: import net.sf.oval.exception.ConstraintsViolatedException;
017: import net.sf.oval.guard.Guard;
018: import net.sf.oval.guard.Guarded;
019:
020: /**
021: * @author Sebastian Thomschke
022: */
023: public class InnerClassTest extends TestCase {
024:
025: @Guarded
026: protected static class TestEntity {
027: protected static class InnerClassNotGuarded {
028: @NotNull
029: protected String name;
030:
031: /**
032: * the @PostValidateObject annotation should lead to a warning by the ApiUsageAuditor
033: */
034: private InnerClassNotGuarded(String name) {
035: this .name = name;
036: }
037:
038: /**
039: * @param name the name to set
040: */
041: public void setName(String name) {
042: this .name = name;
043: }
044: }
045:
046: @Guarded(applyFieldConstraintsToSetters=true)
047: protected static class InnerClassGuarded {
048: @NotNull
049: protected String name;
050:
051: private InnerClassGuarded(String name) {
052: this .name = name;
053: }
054:
055: /**
056: * @param name the name to set
057: */
058: public void setName(String name) {
059: this .name = name;
060: }
061: }
062: }
063:
064: /**
065: * test that specified constraints for inner classes not marked with @Constrained
066: * are ignored
067: */
068: public void testInnerClassNotGuarded() {
069: final Guard guard = new Guard();
070: TestGuardAspect.aspectOf().setGuard(guard);
071:
072: TestEntity.InnerClassNotGuarded instance = new TestEntity.InnerClassNotGuarded(
073: null);
074: instance.setName(null);
075: }
076:
077: public void testInnerClassGuarded() {
078: final Guard guard = new Guard();
079: TestGuardAspect.aspectOf().setGuard(guard);
080: guard.setInvariantsEnabled(true);
081:
082: try {
083: new TestEntity.InnerClassGuarded(null);
084: fail();
085: } catch (ConstraintsViolatedException ex) {
086: // expected
087: }
088:
089: TestEntity.InnerClassGuarded instance = null;
090:
091: instance = new TestEntity.InnerClassGuarded("");
092:
093: try {
094: instance.setName(null);
095: fail();
096: } catch (ConstraintsViolatedException ex) {
097: // expected
098: }
099: }
100: }
|