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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.Before;
  15. import org.aspectj.lang.JoinPoint;
  16. import org.aspectj.lang.Aspects;
  17. import org.aspectj.lang.NoAspectBoundException;
  18. import junit.framework.TestCase;
  19. /**
  20. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  21. */
  22. public class PerClauseTest extends TestCase {
  23. static StringBuffer s_log = new StringBuffer();
  24. static void log(String s) {
  25. s_log.append(s).append(" ");
  26. }
  27. public static void main(String[] args) {
  28. TestHelper.runAndThrowOnFailure(suite());
  29. }
  30. public static junit.framework.Test suite() {
  31. return new junit.framework.TestSuite(PerClauseTest.class);
  32. }
  33. public void perSingleton() {
  34. log("perSingleton");
  35. }
  36. public void testPerSingleton() {
  37. s_log = new StringBuffer();
  38. // singleton is bound as soon as clinit
  39. try {
  40. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerSingleton.class));
  41. Aspects.aspectOf(PerClauseTestAspects.TestAspectPerSingleton.class);
  42. } catch (NoAspectBoundException e) {
  43. fail(e.toString());
  44. }
  45. perSingleton();
  46. assertEquals("AOP.perSingleton perSingleton ", s_log.toString());
  47. perSingleton();
  48. assertEquals(1, PerClauseTestAspects.TestAspectPerSingleton.s_count);
  49. }
  50. public void perTarget() {
  51. log("perTarget");
  52. }
  53. public void testPerTarget() {
  54. s_log = new StringBuffer();
  55. perTarget();
  56. assertEquals("AOP.perTarget perTarget ", s_log.toString());
  57. // calling singleton API will fail
  58. try {
  59. assertFalse(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerTarget.class));
  60. Aspects.aspectOf(PerClauseTestAspects.TestAspectPerTarget.class);
  61. fail("should fail with NOABE");
  62. } catch (NoAspectBoundException e) {
  63. ;//ok
  64. }
  65. // this per
  66. try {
  67. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerTarget.class, this));
  68. PerClauseTestAspects.TestAspectPerTarget aspect = (PerClauseTestAspects.TestAspectPerTarget) Aspects.aspectOf(PerClauseTestAspects.TestAspectPerTarget.class, this);
  69. assertNotNull(aspect);
  70. } catch (NoAspectBoundException e) {
  71. fail(e.toString());
  72. }
  73. // another per
  74. PerClauseTest me = new PerClauseTest();
  75. try {
  76. assertFalse(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerTarget.class, me));
  77. Aspects.aspectOf(PerClauseTestAspects.TestAspectPerTarget.class, me);
  78. fail("should fail");
  79. } catch (NoAspectBoundException e) {
  80. ;//ok
  81. }
  82. me.perTarget();
  83. try {
  84. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerTarget.class, me));
  85. PerClauseTestAspects.TestAspectPerTarget aspect = (PerClauseTestAspects.TestAspectPerTarget) Aspects.aspectOf(PerClauseTestAspects.TestAspectPerTarget.class, me);
  86. assertNotNull(aspect);
  87. } catch (NoAspectBoundException e) {
  88. fail(e.toString());
  89. }
  90. assertEquals(2, PerClauseTestAspects.TestAspectPerTarget.s_count);
  91. }
  92. public void perCflowEntry() {
  93. // the aspect is bound to the executing thread
  94. try {
  95. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerCflow.class));
  96. Aspects.aspectOf(PerClauseTestAspects.TestAspectPerCflow.class);
  97. } catch (NoAspectBoundException e) {
  98. fail(e.toString());
  99. }
  100. perCflow();
  101. }
  102. public void perCflow() {
  103. log("perCflow");
  104. }
  105. public void testPerCflow() throws Throwable {
  106. s_log = new StringBuffer();
  107. // no aspect bound yet
  108. try {
  109. assertFalse(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerCflow.class));
  110. Aspects.aspectOf(PerClauseTestAspects.TestAspectPerCflow.class);
  111. fail("No perCflow should be bound yet");
  112. } catch (NoAspectBoundException e) {
  113. ;//ok
  114. }
  115. perCflow();
  116. assertEquals("perCflow ", s_log.toString());
  117. // still no aspect bound yet
  118. try {
  119. assertFalse(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerCflow.class));
  120. Aspects.aspectOf(PerClauseTestAspects.TestAspectPerCflow.class);
  121. fail("No perCflow should be bound yet");
  122. } catch (NoAspectBoundException e) {
  123. ;//ok
  124. }
  125. s_log = new StringBuffer();
  126. perCflowEntry();
  127. assertEquals("AOP.perCflow perCflow ", s_log.toString());
  128. // no aspect bound anymore since went OUT of the per clause
  129. try {
  130. assertFalse(Aspects.hasAspect(PerClauseTestAspects.TestAspectPerCflow.class));
  131. Aspects.aspectOf(PerClauseTestAspects.TestAspectPerCflow.class);
  132. fail("No perCflow should be bound anymore");
  133. } catch (NoAspectBoundException e) {
  134. ;//ok
  135. }
  136. Runnable rok = new Runnable() {
  137. public void run() {
  138. perCflowEntry();
  139. }
  140. };
  141. Thread trok = new Thread(rok);
  142. trok.start();
  143. trok.join();
  144. Runnable rko = new Runnable() {
  145. public void run() {
  146. perCflow();
  147. }
  148. };
  149. Thread trko = new Thread(rko);
  150. trko.start();
  151. trko.join();
  152. assertEquals(2, PerClauseTestAspects.TestAspectPerCflow.s_count);
  153. }
  154. public void testPerTypeWithin() {
  155. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPTW.class, PTW1.class));
  156. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPTW.class, PTW2.class));
  157. assertFalse(Aspects.hasAspect(PerClauseTestAspects.TestAspectPTW.class, PTWNoMatch.class));
  158. PTW1.foo();
  159. PTW2.foo();
  160. PTWNoMatch.foo();
  161. assertEquals(2, PerClauseTestAspects.TestAspectPTW.s_count);
  162. try {
  163. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPTW.class, PTW1.class));
  164. assertTrue(Aspects.hasAspect(PerClauseTestAspects.TestAspectPTW.class, PTW2.class));
  165. Aspects.aspectOf(PerClauseTestAspects.TestAspectPTW.class, PTW1.class);
  166. Aspects.aspectOf(PerClauseTestAspects.TestAspectPTW.class, PTW2.class);
  167. } catch (NoAspectBoundException e) {
  168. fail(e.toString());
  169. }
  170. try {
  171. assertFalse(Aspects.hasAspect(PerClauseTestAspects.TestAspectPTW.class, PTWNoMatch.class));
  172. Aspects.aspectOf(PerClauseTestAspects.TestAspectPTW.class, PTWNoMatch.class);
  173. fail("should not have PTW aspect");
  174. } catch (NoAspectBoundException e) {
  175. ;//ok
  176. }
  177. }
  178. static class PTW1 {
  179. static void foo() {};
  180. }
  181. static class PTW2 {
  182. static void foo() {};
  183. }
  184. static class PTWNoMatch {
  185. static void foo() {};
  186. }
  187. }