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.9KB

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