From cf9f154462313dc5cd54047c880551fc8439a68d Mon Sep 17 00:00:00 2001 From: aclement Date: Tue, 19 Apr 2005 10:41:40 +0000 Subject: [PATCH] From branch: @AJ test material --- tests/java5/ataspectj/SimpleAfter.java | 30 ++++ tests/java5/ataspectj/SimpleBefore.java | 29 ++++ .../java5/ataspectj/ataspectj/AfterXTest.java | 119 +++++++++++++++ .../ataspectj/ataspectj/BindingTest.java | 79 ++++++++++ .../java5/ataspectj/ataspectj/CflowTest.java | 75 ++++++++++ .../ataspectj/ataspectj/IfPointcutTest.java | 65 +++++++++ .../ataspectj/ataspectj/PerClauseTest.java | 122 ++++++++++++++++ .../ataspectj/PointcutReferenceTest.java | 98 +++++++++++++ .../ataspectj/ataspectj/PrecedenceTest.java | 75 ++++++++++ .../SingletonAspectBindingsTest.java | 135 ++++++++++++++++++ .../java5/ataspectj/ataspectj/TestHelper.java | 46 ++++++ .../ataspectj/ataspectj/XXJoinPointTest.java | 107 ++++++++++++++ 12 files changed, 980 insertions(+) create mode 100644 tests/java5/ataspectj/SimpleAfter.java create mode 100644 tests/java5/ataspectj/SimpleBefore.java create mode 100644 tests/java5/ataspectj/ataspectj/AfterXTest.java create mode 100644 tests/java5/ataspectj/ataspectj/BindingTest.java create mode 100644 tests/java5/ataspectj/ataspectj/CflowTest.java create mode 100644 tests/java5/ataspectj/ataspectj/IfPointcutTest.java create mode 100644 tests/java5/ataspectj/ataspectj/PerClauseTest.java create mode 100644 tests/java5/ataspectj/ataspectj/PointcutReferenceTest.java create mode 100644 tests/java5/ataspectj/ataspectj/PrecedenceTest.java create mode 100644 tests/java5/ataspectj/ataspectj/SingletonAspectBindingsTest.java create mode 100644 tests/java5/ataspectj/ataspectj/TestHelper.java create mode 100644 tests/java5/ataspectj/ataspectj/XXJoinPointTest.java diff --git a/tests/java5/ataspectj/SimpleAfter.java b/tests/java5/ataspectj/SimpleAfter.java new file mode 100644 index 000000000..6484c1909 --- /dev/null +++ b/tests/java5/ataspectj/SimpleAfter.java @@ -0,0 +1,30 @@ +import org.aspectj.lang.annotation.*; + +public class SimpleAfter { + + public static void main(String []argv) { + SimpleAfter instance = new SimpleAfter(); + X.s.append("1"); + instance.m(); + if (!X.s.toString().equals("12a")) + throw new RuntimeException("Either advice not run or ordering wrong, expected 12a: "+X.s); + } + + public void m() { + X.s.append("2"); + } + + + @Aspect("issingleton") + public static class X { + + public static StringBuffer s = new StringBuffer(""); + + @After("execution(* SimpleAfter.m())") + public void before() { + s.append("a"); + } + } + +} + diff --git a/tests/java5/ataspectj/SimpleBefore.java b/tests/java5/ataspectj/SimpleBefore.java new file mode 100644 index 000000000..2327881b4 --- /dev/null +++ b/tests/java5/ataspectj/SimpleBefore.java @@ -0,0 +1,29 @@ +import org.aspectj.lang.annotation.*; + +public class SimpleBefore { + + public static void main(String []argv) { + SimpleBefore instance = new SimpleBefore(); + X.s.append("1"); + instance.m(); + if (!X.s.toString().equals("1b2")) + throw new RuntimeException("Either advice not run or ordering wrong, expected 1b2: "+X.s); + } + + public void m() { + X.s.append("2"); + } + + @Aspect("issingleton") + public static class X { + + public static StringBuffer s = new StringBuffer(""); + + @Before("execution(* SimpleBefore.m())") + public void before() { + s.append("b"); + } + } + +} + diff --git a/tests/java5/ataspectj/ataspectj/AfterXTest.java b/tests/java5/ataspectj/ataspectj/AfterXTest.java new file mode 100644 index 000000000..dbfefd480 --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/AfterXTest.java @@ -0,0 +1,119 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.JoinPoint; +import junit.framework.TestCase; + +/** + * AfterXXX tests + * + * @author Alexandre Vasseur + */ +public class AfterXTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(AfterXTest.class); + } + + public int dupPos(int i) throws NoSuchMethodException { + if (i > 0) + return i*2; + else + throw new NoSuchMethodException("arg is negative"); + } + + public void testAfterReturningAndThrowing() { + AfterXTest me = new AfterXTest(); + + try { + s_log = new StringBuffer(); + int dup2 = me.dupPos(2); + // see after advice precedence here.. + //TODO really weird after finally comes first + // see BcelWeaver fix on sorting of shadowMungerList + //assertEquals("after after2- 2 4 afterRet- 2 ", s_log.toString()); + assertEquals("afterRet- 2 after2- 2 4 after ", s_log.toString()); + } catch (Exception e) { + fail("Should not fail " + e.toString()); + } + + s_log = new StringBuffer(); + try { + int dupm2 = me.dupPos(-2); + fail("should not be reached"); + } catch (NoSuchMethodException e) { + //TODO really weird after finally comes first + // see BcelWeaver fix in sorting of shadowMungerList + assertEquals("after afterThrowing afterThrowing3- [arg is negative] ", s_log.toString()); + } + } + + + + @Aspect + public static class TestAspect { + + @AfterReturning("execution(int ataspectj.AfterXTest.dupPos(..)) && args(arg)") + public void afterRet(int arg) { + log("afterRet-"); + log(""+arg); + } + + @AfterReturning(returning="ret", pointcut="execution(int ataspectj.AfterXTest.dupPos(..)) && args(arg)") + public void after2(int arg, int ret) {//CORRECT + //public void after2(int ret, int arg) {//INCORRECT + log("after2-"); + log(""+arg); + log(""+ret); + } + + @After("execution(int ataspectj.AfterXTest.dupPos(..))") + public void after() { + log("after"); + } + + @AfterThrowing("execution(int ataspectj.AfterXTest.dupPos(..))") + public void afterThrowing(JoinPoint jp) { + log("afterThrowing"); + } + + // formal binding is mandatory in AJ + //@AfterThrowing(throwned="java.lang.RuntimeException", pointcut="execution(int alex.test.ReturnAndThrowHelloWorld.dupPos(..))") + //public void afterThrowing2() { + @AfterThrowing(throwing= "e", pointcut="execution(int ataspectj.AfterXTest.dupPos(..))") + public void afterThrowing2(RuntimeException e) { + fail("should not be bounded"); + } + + @AfterThrowing(throwing= "e", value="execution(int ataspectj.AfterXTest.dupPos(..))") + public void afterThrowing3(NoSuchMethodException e) { + log("afterThrowing3-"); + log("["+e.getMessage()+"]"); + } + } + +} diff --git a/tests/java5/ataspectj/ataspectj/BindingTest.java b/tests/java5/ataspectj/ataspectj/BindingTest.java new file mode 100644 index 000000000..639389732 --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/BindingTest.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import junit.framework.TestCase; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; + +/** + * @author Alexandre Vasseur + */ +public class BindingTest extends TestCase { + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(BindingTest.class); + } + + public int substract(int i, int j) { + int res = i - j; + return res+1;//see +1 here + } + + public static int dup(int i) { + return i*3;//see x3 here + } + + public void testAroundArgs() { + int res = substract(3, 2);// should be 2 without around advice + assertEquals(1, res); + } + + public void testProceedArgs() { + int res = dup((3+1));// advice will change arg with arg-1 + assertEquals(6, res); + callWithinStatic(); + } + + private static void callWithinStatic() { + int res = dup((3+1)); + assertEquals(6, res); + } + + @Aspect + public static class TestAspect_1 { + + @Pointcut("call(int substract(int, int)) && within(ataspectj.BindingTest) && args(arg1, arg2)") + void pc(int arg2, int arg1) {}// see rather fancy ordering here.. + + @Around("pc(argAdvice2, argAdvice1) && target(t)")//see here ordering remade consistent + public int aaround(ProceedingJoinPoint jp, BindingTest t, int argAdvice1, int argAdvice2) throws Throwable { + int res = ((Integer)jp.proceed()).intValue(); + return res-1; + } + + @Pointcut("call(int dup(int)) && within(ataspectj.BindingTest) && args(arg1)") + void pc2(int arg1) {} + + @Around("pc2(argAdvice1)") + public Object aaround2(int argAdvice1, ProceedingJoinPoint jp) throws Throwable { + int res = ((Integer)jp.proceed(new Object[]{new Integer(argAdvice1-1)})).intValue(); + return new Integer(res/3*2); + } + } +} diff --git a/tests/java5/ataspectj/ataspectj/CflowTest.java b/tests/java5/ataspectj/ataspectj/CflowTest.java new file mode 100644 index 000000000..1b48a915b --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/CflowTest.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.runtime.internal.CFlowCounter; +import junit.framework.TestCase; + +/** + * Test cflow (LTW of the aspect to add cflow fields to it) + * + * @author Alexandre Vasseur + */ +public class CflowTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(CflowTest.class); + } + + public void hello() { + log("hello"); + } + + public void startCflow() { + hello(); + } + + public void testCflow() { + CflowTest me = new CflowTest(); + s_log = new StringBuffer(); + me.hello(); + assertEquals("hello ", s_log.toString()); + + s_log = new StringBuffer(); + me.startCflow(); + assertEquals("before hello ", s_log.toString()); + } + + @Aspect + public static class TestAspect { + + //LTW will add: + //public static final CFlowCounter ajc$cflowCounter$0 = new CFlowCounter(); + + @Before("execution(* ataspectj.CflowTest.hello(..)) && this(t) && cflow(execution(* ataspectj.CflowTest.startCflow(..)))") + public void before(Object t, JoinPoint jp) { + assertEquals(CflowTest.class.getName(), t.getClass().getName()); + try { + //jp.proceed(); + } catch (Throwable throwable) { + fail("proceed called in before Advice must be a NO-OP:" + throwable.toString()); + } + log("before"); + } + } + +} diff --git a/tests/java5/ataspectj/ataspectj/IfPointcutTest.java b/tests/java5/ataspectj/ataspectj/IfPointcutTest.java new file mode 100644 index 000000000..66693ec4a --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/IfPointcutTest.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import junit.framework.TestCase; +import org.aspectj.lang.annotation.Aspect; + +/** + * @author Alexandre Vasseur + */ +public class IfPointcutTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public void hello(int i) { + System.out.println("IfPointcutTest.hello " + i); + } + + public void testIf() { + IfPointcutTest me = new IfPointcutTest(); + me.hello(1); + me.hello(-1); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(IfPointcutTest.class); + } + + + + @Aspect + public static class TestAspect { + + public boolean positive(int i) { + return (i>=0); + } + + //FIXME av if pcd support + //@Pointcut("args(i) && if(i>0)") + void ifPc(int i) {} + + //FIXME av if pcd support + //@Before("execution(* ataspectj.IfPointcutTest.hello(int)) && ifPc(i)") + void before(int i) { + System.out.println("IfPointcutTest$TestAspect.before"); + } + } +} diff --git a/tests/java5/ataspectj/ataspectj/PerClauseTest.java b/tests/java5/ataspectj/ataspectj/PerClauseTest.java new file mode 100644 index 000000000..47e95289b --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/PerClauseTest.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.Aspects; +import org.aspectj.lang.NoAspectBoundException; +import junit.framework.TestCase; + +/** + * @author Alexandre Vasseur + */ +public class PerClauseTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(AfterXTest.class); + } + + public void perTarget() { + log("perTarget"); + } + + public void testPerTarget() { + s_log = new StringBuffer(); + perTarget(); + assertEquals("AOP.perTarget perTarget ", s_log.toString()); + + // singleton + try { + Aspects.aspectOf(TestAspectPerTarget.class); + fail("should fail with NOABE"); + } catch (NoAspectBoundException e) { + ;//ok + } + + // this per + try { + TestAspectPerTarget aspect = (TestAspectPerTarget) Aspects.aspectOf(TestAspectPerTarget.class, this); + assertNotNull(aspect); + } catch (NoAspectBoundException e) { + fail(e.toString()); + } + + // another per + PerClauseTest me = new PerClauseTest(); + try { + Aspects.aspectOf(TestAspectPerTarget.class, me); + fail("should fail"); + } catch (NoAspectBoundException e) { + ;//ok + } + me.perTarget(); + try { + TestAspectPerTarget aspect = (TestAspectPerTarget) Aspects.aspectOf(TestAspectPerTarget.class, me); + assertNotNull(aspect); + } catch (NoAspectBoundException e) { + fail(e.toString()); + } + } + + @Aspect("pertarget(execution(* ataspectj.PerClauseTest.perTarget()))") + public static class TestAspectPerTarget { + + public TestAspectPerTarget() { + ; + } + + @Before("execution(* ataspectj.PerClauseTest.perTarget()) && target(t)") + public void before(JoinPoint jp, Object t) { + log("AOP."+jp.getSignature().getName()); + assertTrue("perX match", this.equals(Aspects.aspectOf(getClass(), t))); + } + } + + public void perCflowEntry() { + perCflow(); + } + + public void perCflow() { + log("perCflow"); + } + + public void testPerCflow() { + s_log = new StringBuffer(); + perCflow(); + assertEquals("perCflow ", s_log.toString()); + + s_log = new StringBuffer(); + perCflowEntry(); + assertEquals("AOP.perCflow perCflow ", s_log.toString()); + } + + @Aspect("percflow(execution(* ataspectj.PerClauseTest.perCflowEntry()))") + public static class TestAspectPerCflow { + + public TestAspectPerCflow() { + ; + } + + @Before("execution(* ataspectj.PerClauseTest.perCflow())") + public void before(JoinPoint jp) { + log("AOP."+jp.getSignature().getName()); + assertTrue("perX match", this.equals(Aspects.aspectOf(getClass(), Thread.currentThread()))); + } + } +} diff --git a/tests/java5/ataspectj/ataspectj/PointcutReferenceTest.java b/tests/java5/ataspectj/ataspectj/PointcutReferenceTest.java new file mode 100644 index 000000000..4bef14b8c --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/PointcutReferenceTest.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.JoinPoint; +import org.aspectj.runtime.internal.CFlowCounter; +import junit.framework.TestCase; + +/** + * Test pointcut reference without binding + * + * @author Alexandre Vasseur + */ +public class PointcutReferenceTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(PointcutReferenceTest.class); + } + + public void hello() { + log("hello"); + } + + public void helloWithRef() { + log("helloWithRef"); + } + + public void testPointcutReferenceNoBinding() { + PointcutReferenceTest me = new PointcutReferenceTest(); + s_log = new StringBuffer(); + me.hello(); + assertEquals("before hello ", s_log.toString()); + } + + public void testPointcutReferenceBinding() { + PointcutReferenceTest me = new PointcutReferenceTest(); + s_log = new StringBuffer(); + me.helloWithRef(); + assertEquals("beforeWithRef helloWithRef ", s_log.toString()); + } + + + @Aspect + public static class TestAspect { + + // order of pointcut in source code does not matter for pc refs + @Pointcut("execution(* ataspectj.PointcutReferenceTest.hello(..))") + void pcRef() {} + + @Pointcut("pcRef()") + void pcRef2() {} + + @Before("pcRef2()") + public void before(JoinPoint jp) { + log("before"); + } + + + // see here outer aspect reference + @Pointcut("execution(* ataspectj.PointcutReferenceTest.helloWithRef(..))" + + " && ataspectj.PointcutReferenceTest.RefAspect.pcRefObjectBinding(t)") + void pcRefBinding(Object t) {} + + @Before("pcRefBinding(ttt)") + public void before(Object ttt, JoinPoint jp) { + log("beforeWithRef"); + } + } + + @Aspect + public static class RefAspect { + @Pointcut("this(obj)") + public void pcRefObjectBinding(PointcutReferenceTest obj) {} + + } + +} diff --git a/tests/java5/ataspectj/ataspectj/PrecedenceTest.java b/tests/java5/ataspectj/ataspectj/PrecedenceTest.java new file mode 100644 index 000000000..8f4573e37 --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/PrecedenceTest.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import junit.framework.TestCase; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.DeclarePrecedence; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; + +/** + * @author Alexandre Vasseur + */ +public class PrecedenceTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(PrecedenceTest.class); + } + + public void hello() { + log("hello"); + } + + public void testPrecedence() { + s_log = new StringBuffer(); + hello(); + assertEquals("TestAspect_3 TestAspect_2 TestAspect_1 hello ", s_log.toString()); + } + + + @Aspect() + public static class TestAspect_1 { + @Before("execution(* ataspectj.PrecedenceTest.hello())") + void before() { + log("TestAspect_1"); + } + } + + @Aspect() + @DeclarePrecedence("ataspectj.PrecedenceTest.TestAspect_3, ataspectj.PrecedenceTest.TestAspect_1") + public static class TestAspect_2 { + @Before("execution(* ataspectj.PrecedenceTest.hello())") + void before() { + log("TestAspect_2"); + } + } + + @Aspect() + public static class TestAspect_3 { + @Before("execution(* ataspectj.PrecedenceTest.hello())") + void before() { + log("TestAspect_3"); + } + } + + @Aspect() + @DeclarePrecedence("ataspectj.PrecedenceTest.TestAspect_3, ataspectj.PrecedenceTest.TestAspect_2, ataspectj.PrecedenceTest.TestAspect_1, *") + public static class TestAspect_Order { + } +} diff --git a/tests/java5/ataspectj/ataspectj/SingletonAspectBindingsTest.java b/tests/java5/ataspectj/ataspectj/SingletonAspectBindingsTest.java new file mode 100644 index 000000000..037c67571 --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/SingletonAspectBindingsTest.java @@ -0,0 +1,135 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.DeclarePrecedence; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.runtime.internal.AroundClosure; +import junit.framework.TestCase; + +/** + * Test various advice and JoinPoint + binding, without pc ref + * + * @author Alexandre Vasseur + */ +public class SingletonAspectBindingsTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(SingletonAspectBindingsTest.class); + } + + public void hello() { + log("hello"); + } + + public void hello(String s) { + log("hello-"); + log(s); + } + + public void testExecutionWithThisBinding() { + s_log = new StringBuffer(); + SingletonAspectBindingsTest me = new SingletonAspectBindingsTest(); + me.hello(); + // see here advice precedence as in source code order + //TODO check around relative order + // see fix in BcelWeaver sorting shadowMungerList + //assertEquals("around2_ around_ before hello after _around _around2 ", s_log.toString()); + assertEquals("around_ around2_ before hello _around2 _around after ", s_log.toString()); + } + + public void testExecutionWithArgBinding() { + s_log = new StringBuffer(); + SingletonAspectBindingsTest me = new SingletonAspectBindingsTest(); + me.hello("x"); + assertEquals("before- x hello- x ", s_log.toString()); + } + + + @Aspect + public static class TestAspect { + + static int s = 0; + + static { + s++; + } + + public TestAspect() { + // assert clinit has run when singleton aspectOf reaches that + assertTrue(s>0); + } + + //public static TestAspect aspectOf() {return null;} + + @Around("execution(* ataspectj.SingletonAspectBindingsTest.hello())") + public void aaround(ProceedingJoinPoint jp) { + log("around_"); + try { + jp.proceed(); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + log("_around"); + } + + @Around("execution(* ataspectj.SingletonAspectBindingsTest.hello()) && this(t)") + public void around2(ProceedingJoinPoint jp, Object t) { + log("around2_"); + assertEquals(SingletonAspectBindingsTest.class.getName(), t.getClass().getName()); + try { + jp.proceed(); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + log("_around2"); + } + + @Before("execution(* ataspectj.SingletonAspectBindingsTest.hello())") + public void before(JoinPoint.StaticPart sjp) { + log("before"); + assertEquals("hello", sjp.getSignature().getName()); + } + + @After("execution(* ataspectj.SingletonAspectBindingsTest.hello())") + public void after(JoinPoint.StaticPart sjp) { + log("after"); + assertEquals("execution(public void ataspectj.SingletonAspectBindingsTest.hello())", sjp.toLongString()); + } + + //TODO see String alias, see before advice name clash - all that works + // 1/ String is in java.lang.* - see SimpleScope.javalangPrefix array + // 2/ the advice is register thru its Bcel Method mirror + @Before("execution(* ataspectj.SingletonAspectBindingsTest.hello(String)) && args(s)") + public void before(String s, JoinPoint.StaticPart sjp) { + log("before-"); + log(s); + assertEquals("hello", sjp.getSignature().getName()); + } + + } +} diff --git a/tests/java5/ataspectj/ataspectj/TestHelper.java b/tests/java5/ataspectj/ataspectj/TestHelper.java new file mode 100644 index 000000000..77fbb0993 --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/TestHelper.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import junit.textui.TestRunner; +import junit.framework.TestResult; + +import java.util.Enumeration; + +/** + * Helper to run a test as a main class, but still throw exception and not just print on stderr + * upon test failure. + *

+ * This is required for Ajc test case that are also designed to work with LTW. + * + * @author Alexandre Vasseur + */ +public class TestHelper { + + public static void runAndThrowOnFailure(junit.framework.Test test) { + TestRunner r = new TestRunner(); + TestResult rr = r.doRun(test); + if (!rr.wasSuccessful()) { + StringBuffer sb = new StringBuffer("\n"); + Enumeration e = rr.failures(); + while (e.hasMoreElements()) { + sb.append("Failure: "); + sb.append(e.nextElement()); + sb.append("\n"); + } + e = rr.errors(); + while (e.hasMoreElements()) { + sb.append("Error: "); + sb.append(e.nextElement()); + sb.append("\n"); + } + throw new RuntimeException(sb.toString()); + } + } + +} diff --git a/tests/java5/ataspectj/ataspectj/XXJoinPointTest.java b/tests/java5/ataspectj/ataspectj/XXJoinPointTest.java new file mode 100644 index 000000000..de5aeb70d --- /dev/null +++ b/tests/java5/ataspectj/ataspectj/XXJoinPointTest.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) Jonas Bonér, Alexandre Vasseur + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + *******************************************************************************/ +package ataspectj; + +import junit.framework.TestCase; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.JoinPoint; + +/** + * @author Alexandre Vasseur + */ +public class XXJoinPointTest extends TestCase { + + static StringBuffer s_log = new StringBuffer(); + static void log(String s) { + s_log.append(s).append(" "); + } + + public static void main(String[] args) { + TestHelper.runAndThrowOnFailure(suite()); + } + + public static junit.framework.Test suite() { + return new junit.framework.TestSuite(XXJoinPointTest.class); + } + + public void hello() { + log("hello"); + } + + public void testJoinPointsInAdviceSignature() { + s_log = new StringBuffer(); + XXJoinPointTest me = new XXJoinPointTest(); + me.hello(); + assertEquals("jp sjp esjp jp-sjp sjp-esjp sjp-jp-esjp esjp-jp-sjp hello ", s_log.toString()); + } + + @Aspect + public static class TestAspect { + + @Pointcut("call(* ataspectj.XXJoinPointTest.hello()) && within(ataspectj.XXJoinPointTest)") + void pc() {} + + @Before("pc()") + void before(JoinPoint jp) { + assertEquals("hello", jp.getSignature().getName()); + log("jp"); + } + + @Before("pc()") + void before(JoinPoint.StaticPart sjp) { + assertEquals("hello", sjp.getSignature().getName()); + log("sjp"); + } + + @Before("pc()") + void beforeEnclosing(JoinPoint.EnclosingStaticPart esjp) { + assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName()); + log("esjp"); + } + + //weird order + @Before("pc()") + void beforeWEIRD1(JoinPoint jp, JoinPoint.StaticPart sjp) { + assertEquals("hello", jp.getSignature().getName()); + assertEquals("hello", sjp.getSignature().getName()); + log("jp-sjp"); + } + + @Before("pc()") + void before(JoinPoint.StaticPart sjp, JoinPoint.EnclosingStaticPart esjp) { + assertEquals("hello", sjp.getSignature().getName()); + assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName()); + log("sjp-esjp"); + } + + // conventional order + @Before("pc()") + void before(JoinPoint.StaticPart sjp, JoinPoint jp, JoinPoint.EnclosingStaticPart esjp) { + assertEquals("hello", sjp.getSignature().getName()); + assertEquals("hello", jp.getSignature().getName()); + assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName()); + log("sjp-jp-esjp"); + } + + // weird order + @Before("pc()") + void beforeWEIRD2(JoinPoint.EnclosingStaticPart esjp, JoinPoint jp, JoinPoint.StaticPart sjp) { + assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName()); + assertEquals("hello", jp.getSignature().getName()); + assertEquals("hello", sjp.getSignature().getName()); + log("esjp-jp-sjp"); + } + } + +} -- 2.39.5