From 3534aad5edf1adc9d3167c5f70c141e1fbeafc94 Mon Sep 17 00:00:00 2001 From: aclement Date: Thu, 17 Feb 2005 09:10:50 +0000 Subject: [PATCH] Testcases for @within() and @withincode() binding. --- .../annotations/binding/WithinBinding1.aj | 44 ++++++++++++++++++ .../annotations/binding/WithinBinding2.aj | 46 +++++++++++++++++++ .../annotations/binding/WithinCodeBinding1.aj | 44 ++++++++++++++++++ .../systemtest/ajc150/AnnotationBinding.java | 20 ++++++++ .../org/aspectj/systemtest/ajc150/ajc150.xml | 17 +++++++ 5 files changed, 171 insertions(+) create mode 100644 tests/java5/annotations/binding/WithinBinding1.aj create mode 100644 tests/java5/annotations/binding/WithinBinding2.aj create mode 100644 tests/java5/annotations/binding/WithinCodeBinding1.aj diff --git a/tests/java5/annotations/binding/WithinBinding1.aj b/tests/java5/annotations/binding/WithinBinding1.aj new file mode 100644 index 000000000..68b617c0d --- /dev/null +++ b/tests/java5/annotations/binding/WithinBinding1.aj @@ -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 index 000000000..8881a26b4 --- /dev/null +++ b/tests/java5/annotations/binding/WithinBinding2.aj @@ -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 index 000000000..2754c15ea --- /dev/null +++ b/tests/java5/annotations/binding/WithinCodeBinding1.aj @@ -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); + } +} diff --git a/tests/src/org/aspectj/systemtest/ajc150/AnnotationBinding.java b/tests/src/org/aspectj/systemtest/ajc150/AnnotationBinding.java index 8c906f63b..84ea3435c 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/AnnotationBinding.java +++ b/tests/src/org/aspectj/systemtest/ajc150/AnnotationBinding.java @@ -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 diff --git a/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml b/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml index 8e100ce36..22be0285e 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml +++ b/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml @@ -253,6 +253,21 @@ + + + + + + + + + + + + + + + @@ -897,5 +912,7 @@ + + -- 2.39.5