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.

TesterTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.aspectj.bridge.IMessage;
  17. import org.aspectj.bridge.IMessageHandler;
  18. import junit.framework.TestCase;
  19. import junit.textui.TestRunner;
  20. /**
  21. * Test the Tester client API's.
  22. * See also tests/harness/*.java and tests/ajcHarnessTests.xml for harness-
  23. * driven Tester tests.
  24. * @author isberg
  25. */
  26. public class TesterTest extends TestCase {
  27. private static final String ME
  28. = "org.aspectj.testing.TesterTest";
  29. /** @param args ignored */
  30. public static void main(String[] args) {
  31. TestRunner.main(new String[] {ME});
  32. }
  33. /**
  34. * Constructor for TesterTest.
  35. * @param arg0
  36. */
  37. public TesterTest(String arg0) {
  38. super(arg0);
  39. }
  40. /**
  41. * Test the usage pattern
  42. * <pre>Tester.event("foo");
  43. * Tester.checkEvents(new String[] { "foo" }); </pre>
  44. */
  45. public void testEventArrayPattern() {
  46. MyTestReporter reporter = new MyTestReporter();
  47. Tester.setMessageHandler(reporter);
  48. //--------- positive test - got expected events
  49. reporter.clear();
  50. Tester.clear();
  51. Tester.event("one");
  52. Tester.event("two");
  53. Tester.checkEvents(new String[] { "one", "two"});
  54. reporter.assertSize(0);
  55. //--------- failed to get expected events
  56. reporter.clear();
  57. Tester.clear();
  58. Tester.checkEvents(new String[] { "one"});
  59. assertTrue(reporter.gotFail("one"));
  60. reporter.assertSize(1);
  61. //--------- got and didn't get expected events
  62. reporter.clear();
  63. Tester.clear();
  64. Tester.event("one");
  65. Tester.event("two");
  66. Tester.checkEvents(new String[] { "one", "two", "three"});
  67. reporter.assertSize(1);
  68. assertTrue(reporter.gotFail("three"));
  69. }
  70. /**
  71. * Test the usage pattern
  72. * <pre>Tester.event("foo");
  73. * Tester.expectEvent("foo");
  74. * Tester.checkAllEvents();</pre>
  75. */
  76. public void testEventStringPattern() {
  77. MyTestReporter reporter = new MyTestReporter();
  78. Tester.setMessageHandler(reporter);
  79. //--------- positive test - got expected events
  80. reporter.clear();
  81. Tester.clear();
  82. Tester.event("one");
  83. Tester.event("two");
  84. Tester.expectEvent("one");
  85. Tester.expectEvent("two");
  86. Tester.checkAllEvents();
  87. reporter.assertSize(0);
  88. //--------- failed to get expected events
  89. reporter.clear();
  90. Tester.clear();
  91. Tester.expectEvent("one");
  92. Tester.checkAllEvents();
  93. assertTrue(reporter.gotFail("one"));
  94. reporter.assertSize(1);
  95. //--------- got and didn't get expected events
  96. reporter.clear();
  97. Tester.clear();
  98. Tester.expectEvent("one");
  99. Tester.expectEvent("two");
  100. Tester.expectEvent("three");
  101. Tester.event("one");
  102. Tester.event("two");
  103. Tester.checkAllEvents();
  104. assertTrue(reporter.gotFail("three"));
  105. reporter.assertSize(1);
  106. }
  107. /**
  108. * Test the usage pattern
  109. * <pre>Tester.note("foo");
  110. * Tester.check("foo");</pre>
  111. */
  112. public void testNotePattern() {
  113. MyTestReporter reporter = new MyTestReporter();
  114. Tester.setMessageHandler(reporter);
  115. //--------- positive test - got expected events
  116. reporter.clear();
  117. Tester.clear();
  118. Tester.note("one");
  119. Tester.note("two");
  120. Tester.check("one");
  121. Tester.check("two");
  122. reporter.assertSize(0);
  123. //--------- failed to get expected events
  124. reporter.clear();
  125. Tester.clear();
  126. Tester.check("one");
  127. Tester.checkAllEvents();
  128. assertTrue(reporter.gotFail("one"));
  129. reporter.assertSize(1);
  130. //--------- got and didn't get expected events
  131. reporter.clear();
  132. Tester.clear();
  133. Tester.note("one");
  134. Tester.check("one");
  135. Tester.note("two");
  136. Tester.check("two");
  137. Tester.check("three");
  138. assertTrue(reporter.gotFail("three"));
  139. reporter.assertSize(1);
  140. }
  141. /**
  142. * Stub to record failures emitted by Tester.
  143. * @author isberg
  144. */
  145. public static class MyTestReporter implements IMessageHandler {
  146. public ArrayList<IMessage> failures = new ArrayList<>();
  147. public ArrayList<IMessage> passes = new ArrayList<>();
  148. public void clear() {
  149. failures.clear();
  150. passes.clear();
  151. }
  152. void assertSize(int size) {
  153. assertTrue(-1 < size);
  154. assertTrue("failures: " + failures, size == failures.size());
  155. }
  156. boolean gotPass(String substring) {
  157. return gotItem(passes, substring);
  158. }
  159. boolean gotFail(String substring) {
  160. return gotItem(failures, substring);
  161. }
  162. boolean gotItem(List<IMessage> list, String substring) {
  163. for (IMessage element: list) {
  164. String s = element.getMessage();
  165. if ((null != s) && (s.contains(substring))) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. public boolean isIgnoring(IMessage.Kind kind) {
  172. return false;
  173. }
  174. public void dontIgnore(IMessage.Kind kind) {
  175. }
  176. public void ignore(IMessage.Kind kind) {
  177. }
  178. public boolean handleMessage(IMessage message) {
  179. (message.isFailed() ? failures : passes).add(message);
  180. return true;
  181. }
  182. }
  183. }
  184. // /**
  185. // * @see ReporterI#abortWithFailure(String, Throwable)
  186. // */
  187. // public void abortWithFailure(String message, Throwable exception) {
  188. // if (null == exception) {
  189. // check(message, true);
  190. // } else {
  191. // String s = message + Util.unqualifiedClassName(exception)
  192. // + ": " + exception.getMessage();
  193. // check(s, false);
  194. // }
  195. // }
  196. //
  197. // /**
  198. // * @see ReporterI#check(String, boolean)
  199. // */
  200. // public boolean check(String message, boolean passed) {
  201. // (!passed ? failures : passes).add(message);
  202. // return passed;
  203. // }
  204. //