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.

AtAspectJTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  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. * Contributors:
  9. * Alexandre Vasseur initial implementation
  10. *******************************************************************************/
  11. package test.loadtime5;
  12. import org.aspectj.lang.annotation.Before;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.After;
  15. import org.aspectj.lang.annotation.Around;
  16. import org.aspectj.lang.JoinPoint;
  17. import org.aspectj.lang.ProceedingJoinPoint;
  18. import junit.framework.TestCase;
  19. import junit.textui.TestRunner;
  20. /**
  21. * Test various advice and JoinPoint + binding, without pc ref
  22. *
  23. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  24. */
  25. public class AtAspectJTest extends TestCase {
  26. static StringBuffer s_log = new StringBuffer();
  27. static void log(String s) {
  28. s_log.append(s).append(" ");
  29. }
  30. public static void main(String[] args) {
  31. TestRunner.run(AtAspectJTest.class);
  32. }
  33. public static junit.framework.Test suite() {
  34. return new junit.framework.TestSuite(AtAspectJTest.class);
  35. }
  36. public void hello() {
  37. log("hello");
  38. }
  39. public void hello(String s) {
  40. log("hello-");
  41. log(s);
  42. }
  43. public void testExecutionWithThisBinding() {
  44. s_log = new StringBuffer();
  45. AtAspectJTest me = new AtAspectJTest();
  46. me.hello();
  47. // see here advice precedence as in source code order
  48. //TODO check around relative order
  49. // see fix in BcelWeaver sorting shadowMungerList
  50. //assertEquals("around2_ around_ before hello after _around _around2 ", s_log.toString());
  51. assertEquals("around_ around2_ before hello _around2 _around after ", s_log.toString());
  52. }
  53. public void testExecutionWithArgBinding() {
  54. s_log = new StringBuffer();
  55. AtAspectJTest me = new AtAspectJTest();
  56. me.hello("x");
  57. assertEquals("before- x hello- x ", s_log.toString());
  58. }
  59. @Aspect
  60. public static class TestAspect {
  61. static int s = 0;
  62. static {
  63. s++;
  64. }
  65. public TestAspect() {
  66. // assert clinit has run when singleton aspectOf reaches that
  67. assertTrue(s>0);
  68. }
  69. //public static TestAspect aspectOf() {return null;}
  70. @Around("execution(* test.loadtime5.AtAspectJTest.hello())")
  71. public void aaround(ProceedingJoinPoint jp) {
  72. log("around_");
  73. try {
  74. jp.proceed();
  75. } catch (Throwable throwable) {
  76. throwable.printStackTrace();
  77. }
  78. log("_around");
  79. }
  80. @Around("execution(* test.loadtime5.AtAspectJTest.hello()) && this(t)")
  81. public void around2(ProceedingJoinPoint jp, Object t) {
  82. log("around2_");
  83. assertEquals(AtAspectJTest.class.getName(), t.getClass().getName());
  84. try {
  85. jp.proceed();
  86. } catch (Throwable throwable) {
  87. throwable.printStackTrace();
  88. }
  89. log("_around2");
  90. }
  91. @Before("execution(* test.loadtime5.AtAspectJTest.hello())")
  92. public void before(JoinPoint.StaticPart sjp) {
  93. log("before");
  94. assertEquals("hello", sjp.getSignature().getName());
  95. }
  96. @After("execution(* test.loadtime5.AtAspectJTest.hello())")
  97. public void after(JoinPoint.StaticPart sjp) {
  98. log("after");
  99. assertEquals("execution(public void test.loadtime5.AtAspectJTest.hello())", sjp.toLongString());
  100. }
  101. //TODO see String alias, see before advice name clash - all that works
  102. // 1/ String is in java.lang.* - see SimpleScope.javalangPrefix array
  103. // 2/ the advice is register thru its Bcel Method mirror
  104. @Before("execution(* test.loadtime5.AtAspectJTest.hello(String)) && args(s)")
  105. public void before(String s, JoinPoint.StaticPart sjp) {
  106. log("before-");
  107. log(s);
  108. assertEquals("hello", sjp.getSignature().getName());
  109. }
  110. }
  111. }