001: /**************************************************************************************
002: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.thistarget;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.definition.Pointcut;
011:
012: /**
013: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
014: */
015: public class TargetReferencedAndRuntimeCheckTest extends TestCase {
016:
017: private static String s_log = "";
018:
019: //--- Target implements 2 interface and a complex rutime check will thus be done
020: public static interface I0Target {
021: public void call();
022: }
023:
024: public static interface I1Target {
025: }
026:
027: public static interface I2Target {
028: }
029:
030: public static class ImplementsTwoTarget implements I0Target,
031: I1Target, I2Target {
032: public void call() {
033: log("ImplementsTwoTarget");
034: }
035: }
036:
037: public static class ImplementsOneTarget implements I0Target,
038: I1Target {
039: public void call() {
040: log("ImplementsOneTarget");
041: }
042: }
043:
044: public static class ImplementsZeroTarget implements I0Target {
045: public void call() {
046: log("ImplementsZeroTarget");
047: }
048: }
049:
050: //--- Aspect
051:
052: public static class Aspect {
053:
054: /**
055: * @Expression target(myTarget) && call(* test.thistarget.*.call()) && within(test.thistarget.TargetReferencedAndRuntimeCheckTest)
056: */
057: Pointcut referenceI1Target(I1Target myTarget) {
058: return null;
059: }
060:
061: /**
062: * @Expression target(myTarget) && call(* test.thistarget.*.call()) && within(test.thistarget.TargetReferencedAndRuntimeCheckTest)
063: */
064: Pointcut referenceI2Target(I2Target myTarget) {
065: return null;
066: }
067:
068: /**
069: * @Before referenceI1Target(t) && referenceI2Target(t)
070: */
071: void before(Object t) {// if we don't use Object here but f.e. I1Target, the validation visitor will complain
072: log("before_I1TargetAndI2Target");
073: ThisTargetAspect.validate(t, I1Target.class);
074: ThisTargetAspect.validate(t, I2Target.class);
075: }
076:
077: /**
078: * @Before referenceI1Target(t)
079: */
080: void before2(I1Target t) {
081: log("before_I1Target");
082: ThisTargetAspect.validate(t, I1Target.class);
083: }
084: }
085:
086: public void testRuntimeChecks() {
087: I0Target i1 = new ImplementsTwoTarget();
088: s_log = "";
089: i1.call();
090: assertEquals(
091: "before_I1TargetAndI2Target before_I1Target ImplementsTwoTarget ",
092: s_log);
093:
094: I0Target i2 = new ImplementsOneTarget();
095: s_log = "";
096: i2.call();
097: assertEquals("before_I1Target ImplementsOneTarget ", s_log);
098:
099: I0Target i3 = new ImplementsZeroTarget();
100: s_log = "";
101: i3.call();
102: assertEquals("ImplementsZeroTarget ", s_log);
103: }
104:
105: //--- JUnit
106:
107: public static void main(String[] args) {
108: junit.textui.TestRunner.run(suite());
109: }
110:
111: public static junit.framework.Test suite() {
112: return new junit.framework.TestSuite(
113: TargetReferencedAndRuntimeCheckTest.class);
114: }
115:
116: static void log(String s) {
117: s_log += s + " ";
118: }
119:
120: }
|