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.

SingletonAspectBindingsTest.java 5.2KB

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