123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*******************************************************************************
- * 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 <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- 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())));
- }
- }
- }
|