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.

AroundHandler.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import org.aspectj.testing.Tester;
  2. import org.aspectj.testing.Tester;
  3. /*
  4. These test advice on handlers:
  5. 1) can use before advice to skip handler by throwing Error
  6. 2) can use after advice after running handler and throw Error
  7. 3) can use around advice [skip | run] X [ throw Error or complete normally ]
  8. a) skip without throwing error
  9. b) skip with throwing error
  10. c) run without throwing error
  11. d) run with throwing error
  12. Rather than overload advice at join points,
  13. there is one method and one advice for each of the 6 test cases.
  14. */
  15. /** @testcase VerifyError after around advice falls off end of tryCatch */
  16. public class AroundHandler {
  17. /** if true, then run the around test cases */
  18. public static final boolean TEST_AROUND = true ;
  19. public static void main(String[] args) {
  20. Target target = new Target();
  21. /** @testcase before advice skips handler by throwing Error */
  22. Tester.check(target.skipBeforeErrorHandler(), "target.skipBeforeErrorHandler()");
  23. /** @testcase after advice runs handler, throws Error */
  24. Tester.check(target.runAfterErrorHandler(), "target.runAfterErrorHandler()");
  25. if (TEST_AROUND) {
  26. /** @testcase around advice skips handler, no Error thrown */
  27. Tester.check(target.skipErrorHandler(), "target.skipErrorHandler()");
  28. /** @testcase around advice runs handler, no Error thrown */
  29. Tester.check(target.runErrorHandler(), "target.runErrorHandler()");
  30. /** @testcase around advice skips handler, throws exception from around advice */
  31. Tester.expectEvent("skipErrorHandlerGotError");
  32. try {
  33. target.skipErrorHandlerThrowError();
  34. Tester.check(false, "expecting Error thrown by around");
  35. } catch (Error e) {
  36. Target.isERR(e);
  37. Tester.event("skipErrorHandlerGotError");
  38. }
  39. /** @testcase around advice runs handler, throws exception from around advice */
  40. Tester.expectEvent("runErrorHandlerThrowError");
  41. Tester.expectEvent("runErrorHandlerGotError");
  42. try {
  43. target.runErrorHandlerThrowError();
  44. Tester.check(false, "expecting Error thrown by around");
  45. } catch (Error e) {
  46. Target.isERR(e);
  47. Tester.event("runErrorHandlerGotError");
  48. }
  49. } // TEST_AROUND
  50. }
  51. }
  52. class OuterError extends Error {
  53. public OuterError(String s) { super(s); }
  54. }
  55. class Target {
  56. public static String ERR = "goto Error";
  57. public static void isERR(Throwable throwable) {
  58. String message = (null == throwable ? "" : throwable.getMessage());
  59. Tester.check(Target.ERR.equals(message),
  60. "\"" + ERR + "\".equals(\"" + message + "\")");
  61. }
  62. /** advised by before */
  63. public boolean skipBeforeErrorHandler() {
  64. boolean ranHandler = false;
  65. boolean ranOuterHandler = false;
  66. try {
  67. try { throw new Error(ERR); }
  68. catch (Error t) { ranHandler = true; }
  69. } catch (OuterError t) { ranOuterHandler = true; }
  70. Tester.check(!ranHandler, "!ranHandler");
  71. Tester.check(ranOuterHandler, "ranOuterHandler");
  72. return (!ranHandler && ranOuterHandler);
  73. }
  74. /** advised by after */
  75. public boolean runAfterErrorHandler() {
  76. boolean ranHandler = false;
  77. boolean ranOuterHandler = false;
  78. try {
  79. try { throw new Error(ERR); }
  80. catch (Error t) { ranHandler = true; }
  81. } catch (OuterError t) { ranOuterHandler = true; }
  82. Tester.check(ranHandler, "!ranHandler");
  83. Tester.check(ranOuterHandler, "ranOuterHandler");
  84. return (ranHandler && ranOuterHandler);
  85. }
  86. //---------------- remainder all advised using around
  87. public boolean skipErrorHandler() {
  88. boolean ranHandler = false;
  89. try { throw new Error(ERR); }
  90. catch (Error t) { ranHandler = true; }
  91. Tester.check(!ranHandler, "!ranHandler");
  92. return !ranHandler;
  93. }
  94. public boolean runErrorHandler() {
  95. boolean ranHandler = false;
  96. try { throw new Error(ERR); }
  97. catch (Error t) { ranHandler = true; }
  98. Tester.check(ranHandler, "ranHandler");
  99. return ranHandler;
  100. }
  101. public boolean skipErrorHandlerThrowError() {
  102. try { throw new Error(ERR); }
  103. catch (Error t) {
  104. Tester.check(false, "skipErrorHandlerThrowError ran handler");
  105. }
  106. return false; // should never get here - Error thrown
  107. }
  108. public boolean runErrorHandlerThrowError() {
  109. try { throw new Error(ERR); }
  110. catch (Error t) {
  111. Tester.event("runErrorHandlerThrowError");
  112. }
  113. return false; // should never get here - Error thrown
  114. }
  115. }
  116. aspect A {
  117. /** @testcase use before to skip handler by throwing Error from advice */
  118. before(Error error)
  119. : withincode(boolean Target.skipBeforeErrorHandler())
  120. && handler(Error) && args(error)
  121. {
  122. Target.isERR(error);
  123. throw new OuterError(Target.ERR);
  124. }
  125. /** @testcase use after to run handler but throw Error from advice */
  126. after(Error error)
  127. : withincode(boolean Target.runAfterErrorHandler())
  128. && handler(Error) && args(error)
  129. {
  130. Target.isERR(error);
  131. throw new OuterError(Target.ERR);
  132. }
  133. // -------------------- around advice
  134. /** @testcase use around, run handler */
  135. Object around(Error error)
  136. : withincode(boolean Target.runErrorHandler())
  137. && handler(Error) && args(error)
  138. && if(AroundHandler.TEST_AROUND)
  139. {
  140. Target.isERR(error);
  141. return proceed(error);
  142. }
  143. /** @testcase use around to skip handler, throw no Error from around */
  144. Object around(Error error)
  145. : withincode(boolean Target.skipErrorHandler())
  146. && handler(Error) && args(error)
  147. && if(AroundHandler.TEST_AROUND)
  148. {
  149. Target.isERR(error);
  150. //Object ignore = proceed(error);
  151. //throw new Error(Target.ERR);
  152. return null;
  153. }
  154. /** @testcase use around to skip handler, but throw Error from around */
  155. Object around(Error error)
  156. : withincode(boolean Target.skipErrorHandlerThrowError())
  157. && handler(Error) && args(error)
  158. && if(AroundHandler.TEST_AROUND)
  159. {
  160. Target.isERR(error);
  161. //Object ignore = proceed(error);
  162. throw new OuterError(Target.ERR);
  163. //return null;
  164. }
  165. /** @testcase use around, run handler, but throw Error from around */
  166. Object around(Error error)
  167. : withincode(boolean Target.runErrorHandlerThrowError())
  168. && handler(Error) && args(error)
  169. && if(AroundHandler.TEST_AROUND)
  170. {
  171. Target.isERR(error);
  172. Object result = proceed(error);
  173. throw new OuterError(Target.ERR);
  174. // return result;
  175. }
  176. }