You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PointcutReferenceTest.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*******************************************************************************
  2. * Copyright (c) Jonas Bonér, Alexandre Vasseur
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Common Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/cpl-v10.html
  7. *******************************************************************************/
  8. package ataspectj;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Before;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.aspectj.lang.annotation.Aspect;
  13. import org.aspectj.lang.annotation.Before;
  14. import org.aspectj.lang.annotation.Before;
  15. import org.aspectj.lang.annotation.Pointcut;
  16. import org.aspectj.lang.JoinPoint;
  17. import org.aspectj.runtime.internal.CFlowCounter;
  18. import junit.framework.TestCase;
  19. /**
  20. * Test pointcut reference without binding
  21. *
  22. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  23. */
  24. public class PointcutReferenceTest extends TestCase {
  25. static StringBuffer s_log = new StringBuffer();
  26. static void log(String s) {
  27. s_log.append(s).append(" ");
  28. }
  29. public static void main(String[] args) {
  30. TestHelper.runAndThrowOnFailure(suite());
  31. }
  32. public static junit.framework.Test suite() {
  33. return new junit.framework.TestSuite(PointcutReferenceTest.class);
  34. }
  35. public void hello() {
  36. log("hello");
  37. }
  38. public void helloWithRef() {
  39. log("helloWithRef");
  40. }
  41. public void testPointcutReferenceNoBinding() {
  42. PointcutReferenceTest me = new PointcutReferenceTest();
  43. s_log = new StringBuffer();
  44. me.hello();
  45. assertEquals("before hello ", s_log.toString());
  46. }
  47. public void testPointcutReferenceBinding() {
  48. PointcutReferenceTest me = new PointcutReferenceTest();
  49. s_log = new StringBuffer();
  50. me.helloWithRef();
  51. assertEquals("beforeWithRef helloWithRef ", s_log.toString());
  52. }
  53. @Aspect
  54. public static class TestAspect {
  55. // order of pointcut in source code does not matter for pc refs
  56. @Pointcut("execution(* ataspectj.PointcutReferenceTest.hello(..))")
  57. void pcRef() {}
  58. @Pointcut("pcRef()")
  59. void pcRef2() {}
  60. @Before("pcRef2()")
  61. public void before(JoinPoint jp) {
  62. log("before");
  63. }
  64. // see here outer aspect reference
  65. @Pointcut("execution(* ataspectj.PointcutReferenceTest.helloWithRef(..))" +
  66. " && ataspectj.PointcutReferenceTest.RefAspect.pcRefObjectBinding(t)")
  67. void pcRefBinding(Object t) {}
  68. @Before("pcRefBinding(ttt)")
  69. public void before(Object ttt, JoinPoint jp) {
  70. log("beforeWithRef");
  71. }
  72. }
  73. @Aspect
  74. public static class RefAspect {
  75. @Pointcut("this(obj)")
  76. public void pcRefObjectBinding(PointcutReferenceTest obj) {}
  77. }
  78. }