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.

XXJoinPointTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 junit.framework.TestCase;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.aspectj.lang.annotation.Before;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.Before;
  15. import org.aspectj.lang.annotation.Before;
  16. import org.aspectj.lang.annotation.Pointcut;
  17. import org.aspectj.lang.JoinPoint;
  18. /**
  19. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  20. */
  21. public class XXJoinPointTest extends TestCase {
  22. static StringBuffer s_log = new StringBuffer();
  23. static void log(String s) {
  24. s_log.append(s).append(" ");
  25. }
  26. public static void main(String[] args) {
  27. TestHelper.runAndThrowOnFailure(suite());
  28. }
  29. public static junit.framework.Test suite() {
  30. return new junit.framework.TestSuite(XXJoinPointTest.class);
  31. }
  32. public void hello() {
  33. log("hello");
  34. }
  35. public void testJoinPointsInAdviceSignature() {
  36. s_log = new StringBuffer();
  37. XXJoinPointTest me = new XXJoinPointTest();
  38. me.hello();
  39. assertEquals("jp sjp esjp jp-sjp sjp-esjp sjp-jp-esjp esjp-jp-sjp hello ", s_log.toString());
  40. }
  41. @Aspect
  42. public static class TestAspect {
  43. @Pointcut("call(* ataspectj.XXJoinPointTest.hello()) && within(ataspectj.XXJoinPointTest)")
  44. void pc() {}
  45. @Before("pc()")
  46. void before(JoinPoint jp) {
  47. assertEquals("hello", jp.getSignature().getName());
  48. log("jp");
  49. }
  50. @Before("pc()")
  51. void before(JoinPoint.StaticPart sjp) {
  52. assertEquals("hello", sjp.getSignature().getName());
  53. log("sjp");
  54. }
  55. @Before("pc()")
  56. void beforeEnclosing(JoinPoint.EnclosingStaticPart esjp) {
  57. assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName());
  58. log("esjp");
  59. }
  60. //weird order
  61. @Before("pc()")
  62. void beforeWEIRD1(JoinPoint jp, JoinPoint.StaticPart sjp) {
  63. assertEquals("hello", jp.getSignature().getName());
  64. assertEquals("hello", sjp.getSignature().getName());
  65. log("jp-sjp");
  66. }
  67. @Before("pc()")
  68. void before(JoinPoint.StaticPart sjp, JoinPoint.EnclosingStaticPart esjp) {
  69. assertEquals("hello", sjp.getSignature().getName());
  70. assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName());
  71. log("sjp-esjp");
  72. }
  73. // conventional order
  74. @Before("pc()")
  75. void before(JoinPoint.StaticPart sjp, JoinPoint jp, JoinPoint.EnclosingStaticPart esjp) {
  76. assertEquals("hello", sjp.getSignature().getName());
  77. assertEquals("hello", jp.getSignature().getName());
  78. assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName());
  79. log("sjp-jp-esjp");
  80. }
  81. // weird order
  82. @Before("pc()")
  83. void beforeWEIRD2(JoinPoint.EnclosingStaticPart esjp, JoinPoint jp, JoinPoint.StaticPart sjp) {
  84. assertEquals("testJoinPointsInAdviceSignature", esjp.getSignature().getName());
  85. assertEquals("hello", jp.getSignature().getName());
  86. assertEquals("hello", sjp.getSignature().getName());
  87. log("esjp-jp-sjp");
  88. }
  89. }
  90. }