]> source.dussan.org Git - aspectj.git/commitdiff
Testcases for @within() and @withincode() binding.
authoraclement <aclement>
Thu, 17 Feb 2005 09:10:50 +0000 (09:10 +0000)
committeraclement <aclement>
Thu, 17 Feb 2005 09:10:50 +0000 (09:10 +0000)
tests/java5/annotations/binding/WithinBinding1.aj [new file with mode: 0644]
tests/java5/annotations/binding/WithinBinding2.aj [new file with mode: 0644]
tests/java5/annotations/binding/WithinCodeBinding1.aj [new file with mode: 0644]
tests/src/org/aspectj/systemtest/ajc150/AnnotationBinding.java
tests/src/org/aspectj/systemtest/ajc150/ajc150.xml

diff --git a/tests/java5/annotations/binding/WithinBinding1.aj b/tests/java5/annotations/binding/WithinBinding1.aj
new file mode 100644 (file)
index 0000000..68b617c
--- /dev/null
@@ -0,0 +1,44 @@
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@interface Colored { String color();}
+
+@Colored(color="red")
+public class WithinBinding1 {
+
+  void mRed() {System.out.println("red"); }
+
+  void mBlue() {System.out.println("blue");}
+
+  void mGreen() {System.out.println("green");}
+
+  WithinBinding1() {
+       System.out.println("yellow");
+  }
+
+  public static void main(String[]argv) {
+    WithinBinding1 instance = new WithinBinding1();
+    instance.mRed();
+    instance.mBlue();
+    instance.mGreen();
+    X.verifyRun();
+  }
+}
+
+aspect X {
+
+       static int maxruns = 21; // there are 21 join points in a run of WithinBinding1.main()
+    
+    static int i = 0; // Count of advice executions
+    
+    before(Colored c): @within(c) {
+      System.err.println(thisJoinPoint+" color="+c.color());
+       if (!c.color().equals("red")) throw new RuntimeException("not red? "+c.color());
+       i++;
+    }
+    
+    public static void verifyRun() {
+       if (X.i != maxruns)
+               throw new RuntimeException("Expected "+maxruns+" advice runs but did "+X.i);
+    }
+}
diff --git a/tests/java5/annotations/binding/WithinBinding2.aj b/tests/java5/annotations/binding/WithinBinding2.aj
new file mode 100644 (file)
index 0000000..8881a26
--- /dev/null
@@ -0,0 +1,46 @@
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@interface Colored { String color();}
+
+@Colored(color="red") class RedClass {
+  public void m() { System.err.println("RedClass.m() running"); }
+}
+
+@Colored(color="green") class GreenClass {
+  public GreenClass() { System.err.println("GreenClass.ctor() running"); }
+  public void m() { System.err.println("GreenClass.m() running"); }
+}
+
+class NormalClass {
+  public void m() { System.err.println("NormalClass.m() running"); }
+}
+
+
+public class WithinBinding2 {
+  public static void main(String[]argv) {
+    new RedClass().m();
+    new NormalClass().m();
+    new GreenClass().m();
+    X.verifyRun();
+  }
+}
+
+aspect X {
+
+    static int red = 0;
+    static int green=0;
+    
+    before(Colored c): @within(c) {
+        System.err.println(thisJoinPoint+" color="+c.color());
+       if (c.color().startsWith("r")) red++;
+       else if (c.color().startsWith("g")) green++;
+       else throw new RuntimeException("Didn't expect this color: "+c.color());
+    }
+    
+    public static void verifyRun() {
+       System.err.println(red+"  "+green);
+       if (red!=7) throw new RuntimeException("Expected 7 red join points but got "+red);
+       if (green!=9) throw new RuntimeException("Expected 9 green join points but got "+green);
+    }
+}
diff --git a/tests/java5/annotations/binding/WithinCodeBinding1.aj b/tests/java5/annotations/binding/WithinCodeBinding1.aj
new file mode 100644 (file)
index 0000000..2754c15
--- /dev/null
@@ -0,0 +1,44 @@
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@interface Colored { String color();}
+
+public class WithinCodeBinding1 {
+
+  @Colored(color="red") void mRed() {System.out.println("red"); }
+
+  @Colored(color="blue") void mBlue() {System.out.println("blue");}
+
+  @Colored(color="green") void mGreen() {System.out.println("green");}
+
+  @Colored(color="yellow") WithinCodeBinding1() {
+       System.out.println("yellow");
+  }
+
+  public static void main(String[]argv) {
+    WithinCodeBinding1 instance = new WithinCodeBinding1();
+    instance.mRed();
+    instance.mBlue();
+    instance.mGreen();
+    X.verifyRun();
+  }
+}
+
+aspect X {
+
+    // Expected color order
+    static String exp[] = new String[]{"yellow","red","blue","green"};
+    
+    static int i = 0; // Count of advice executions
+    
+    before(Colored c): @withincode(c) && call(* println(..)) {
+      System.err.println(thisJoinPoint+" color="+c.color());
+       if (!c.color().equals(exp[i])) throw new RuntimeException("not "+exp[i]+"? "+c.color());
+       i++;
+    }
+    
+    public static void verifyRun() {
+       if (X.i != exp.length)
+               throw new RuntimeException("Expected "+exp.length+" advice runs but did "+X.i);
+    }
+}
index 8c906f63b82fe05011ff6742821ebf5fb1d981d7..84ea3435c407cca80a080668a595eee0ba061e55 100644 (file)
@@ -221,6 +221,26 @@ public class AnnotationBinding extends XMLBasedAjcTestCase {
        runTest("handler and @annotation");
   }
   
+  ///////////////////////////////////// @WITHIN
+
+  // '@within()'
+  public void testWithinBinding1() {
+       runTest("@within");
+  }
+  
+  //'@within()' but multiple types around (some annotated)
+  public void testWithinBinding2() {
+       runTest("@within - multiple types");
+       System.err.println(getLastRunResult().getStdErr());
+  }
+  
+  ///////////////////////////////////// @WITHINCODE
+  
+  // '@withincode() && call(* println(..))'
+  public void testWithinCodeBinding1() {
+       runTest("@withincode() and call(* println(..))");
+  }
+  
 
   ///////////////////////////////////// @ANNOTATION complex tests
   
index 8e100ce36473d52b44f6b81a7ccd87321e1f5812..22be0285e38288f32af4d9f7f76d7731482fa879 100644 (file)
         <compile files="HandlerBinding.aj" options="-1.5"/>
         <run class="HandlerBinding"/>    
     </ajc-test>
+    
+    <ajc-test dir="java5/annotations/binding" vm="1.5" title="@withincode() and call(* println(..))">
+       <compile files="WithinCodeBinding1.aj" options="-1.5"/>
+       <run class="WithinCodeBinding1"/>
+    </ajc-test>
+    
+    <ajc-test dir="java5/annotations/binding" vm="1.5" title="@within">
+       <compile files="WithinBinding1.aj" options="-1.5"/>
+       <run class="WithinBinding1"/>
+    </ajc-test>
+    
+    <ajc-test dir="java5/annotations/binding" vm="1.5" title="@within - multiple types">
+       <compile files="WithinBinding2.aj" options="-1.5"/>
+       <run class="WithinBinding2"/>
+    </ajc-test>
  
     <ajc-test dir="java5/annotations/binding/complexExample" vm="1.5" title="packages and no binding">
         <compile files="A.java,B.java,Color.java,X.java" options="-1.5"/>
            <message kind="weave" text="Type 'SimpleVarargs' (SimpleVarargs.java:27) advised by before advice from 'VarargsAspect06' (VarargsAspect06.aj:3)"/>
        </compile>
    </ajc-test>
+   
+
 </suite>