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.

PerClauseTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.JoinPoint;
  12. import org.aspectj.lang.Aspects;
  13. import org.aspectj.lang.NoAspectBoundException;
  14. import junit.framework.TestCase;
  15. /**
  16. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  17. */
  18. public class PerClauseTest extends TestCase {
  19. static StringBuffer s_log = new StringBuffer();
  20. static void log(String s) {
  21. s_log.append(s).append(" ");
  22. }
  23. public static void main(String[] args) {
  24. TestHelper.runAndThrowOnFailure(suite());
  25. }
  26. public static junit.framework.Test suite() {
  27. return new junit.framework.TestSuite(AfterXTest.class);
  28. }
  29. public void perTarget() {
  30. log("perTarget");
  31. }
  32. public void testPerTarget() {
  33. s_log = new StringBuffer();
  34. perTarget();
  35. assertEquals("AOP.perTarget perTarget ", s_log.toString());
  36. // singleton
  37. try {
  38. Aspects.aspectOf(TestAspectPerTarget.class);
  39. fail("should fail with NOABE");
  40. } catch (NoAspectBoundException e) {
  41. ;//ok
  42. }
  43. // this per
  44. try {
  45. TestAspectPerTarget aspect = (TestAspectPerTarget) Aspects.aspectOf(TestAspectPerTarget.class, this);
  46. assertNotNull(aspect);
  47. } catch (NoAspectBoundException e) {
  48. fail(e.toString());
  49. }
  50. // another per
  51. PerClauseTest me = new PerClauseTest();
  52. try {
  53. Aspects.aspectOf(TestAspectPerTarget.class, me);
  54. fail("should fail");
  55. } catch (NoAspectBoundException e) {
  56. ;//ok
  57. }
  58. me.perTarget();
  59. try {
  60. TestAspectPerTarget aspect = (TestAspectPerTarget) Aspects.aspectOf(TestAspectPerTarget.class, me);
  61. assertNotNull(aspect);
  62. } catch (NoAspectBoundException e) {
  63. fail(e.toString());
  64. }
  65. }
  66. @Aspect("pertarget(execution(* ataspectj.PerClauseTest.perTarget()))")
  67. public static class TestAspectPerTarget {
  68. public TestAspectPerTarget() {
  69. ;
  70. }
  71. @Before("execution(* ataspectj.PerClauseTest.perTarget()) && target(t)")
  72. public void before(JoinPoint jp, Object t) {
  73. log("AOP."+jp.getSignature().getName());
  74. assertTrue("perX match", this.equals(Aspects.aspectOf(getClass(), t)));
  75. }
  76. }
  77. public void perCflowEntry() {
  78. perCflow();
  79. }
  80. public void perCflow() {
  81. log("perCflow");
  82. }
  83. public void testPerCflow() {
  84. s_log = new StringBuffer();
  85. perCflow();
  86. assertEquals("perCflow ", s_log.toString());
  87. s_log = new StringBuffer();
  88. perCflowEntry();
  89. assertEquals("AOP.perCflow perCflow ", s_log.toString());
  90. }
  91. @Aspect("percflow(execution(* ataspectj.PerClauseTest.perCflowEntry()))")
  92. public static class TestAspectPerCflow {
  93. public TestAspectPerCflow() {
  94. ;
  95. }
  96. @Before("execution(* ataspectj.PerClauseTest.perCflow())")
  97. public void before(JoinPoint jp) {
  98. log("AOP."+jp.getSignature().getName());
  99. assertTrue("perX match", this.equals(Aspects.aspectOf(getClass(), Thread.currentThread())));
  100. }
  101. }
  102. }