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.

AfterXTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * initial implementation Alexandre Vasseur
  11. *******************************************************************************/
  12. package ataspectj;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.AfterReturning;
  15. import org.aspectj.lang.annotation.AfterThrowing;
  16. import org.aspectj.lang.annotation.After;
  17. import org.aspectj.lang.annotation.After;
  18. import org.aspectj.lang.annotation.AfterReturning;
  19. import org.aspectj.lang.annotation.AfterThrowing;
  20. import org.aspectj.lang.annotation.AfterThrowing;
  21. import org.aspectj.lang.annotation.Aspect;
  22. import org.aspectj.lang.JoinPoint;
  23. import junit.framework.TestCase;
  24. /**
  25. * AfterXXX tests
  26. *
  27. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  28. */
  29. public class AfterXTest extends TestCase {
  30. static StringBuffer s_log = new StringBuffer();
  31. static void log(String s) {
  32. s_log.append(s).append(" ");
  33. }
  34. public static void main(String[] args) {
  35. TestHelper.runAndThrowOnFailure(suite());
  36. }
  37. public static junit.framework.Test suite() {
  38. return new junit.framework.TestSuite(AfterXTest.class);
  39. }
  40. public int dupPos(int i) throws NoSuchMethodException {
  41. if (i > 0)
  42. return i*2;
  43. else
  44. throw new NoSuchMethodException("arg is negative");
  45. }
  46. public void testAfterReturningAndThrowing() {
  47. AfterXTest me = new AfterXTest();
  48. try {
  49. s_log = new StringBuffer();
  50. int dup2 = me.dupPos(2);
  51. // see after advice precedence here..
  52. //TODO really weird after finally comes first
  53. // see BcelWeaver fix on sorting of shadowMungerList
  54. //assertEquals("after after2- 2 4 afterRet- 2 ", s_log.toString());
  55. assertEquals("afterRet- 2 after2- 2 4 after ", s_log.toString());
  56. } catch (Exception e) {
  57. fail("Should not fail " + e.toString());
  58. }
  59. s_log = new StringBuffer();
  60. try {
  61. int dupm2 = me.dupPos(-2);
  62. fail("should not be reached");
  63. } catch (NoSuchMethodException e) {
  64. //TODO really weird after finally comes first
  65. // see BcelWeaver fix in sorting of shadowMungerList
  66. assertEquals("after afterThrowing afterThrowing3- [arg is negative] ", s_log.toString());
  67. }
  68. }
  69. @Aspect
  70. public static class TestAspect {
  71. @AfterReturning("execution(int ataspectj.AfterXTest.dupPos(..)) && args(arg)")
  72. public void afterRet(int arg) {
  73. log("afterRet-");
  74. log(""+arg);
  75. }
  76. @AfterReturning(returning="ret", pointcut="execution(int ataspectj.AfterXTest.dupPos(..)) && args(arg)")
  77. public void after2(int arg, int ret) {//CORRECT
  78. //public void after2(int ret, int arg) {//INCORRECT
  79. log("after2-");
  80. log(""+arg);
  81. log(""+ret);
  82. }
  83. @After("execution(int ataspectj.AfterXTest.dupPos(..))")
  84. public void after() {
  85. log("after");
  86. }
  87. @AfterThrowing("execution(int ataspectj.AfterXTest.dupPos(..))")
  88. public void afterThrowing(JoinPoint jp) {
  89. log("afterThrowing");
  90. }
  91. // formal binding is mandatory in AJ
  92. //@AfterThrowing(throwned="java.lang.RuntimeException", pointcut="execution(int alex.test.ReturnAndThrowHelloWorld.dupPos(..))")
  93. //public void afterThrowing2() {
  94. @AfterThrowing(throwing= "e", pointcut="execution(int ataspectj.AfterXTest.dupPos(..))")
  95. public void afterThrowing2(RuntimeException e) {
  96. fail("should not be bounded");
  97. }
  98. @AfterThrowing(throwing= "e", value="execution(int ataspectj.AfterXTest.dupPos(..))")
  99. public void afterThrowing3(NoSuchMethodException e) {
  100. log("afterThrowing3-");
  101. log("["+e.getMessage()+"]");
  102. }
  103. }
  104. }