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.

TestHelper.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 junit.textui.TestRunner;
  14. import junit.framework.TestResult;
  15. import junit.framework.Assert;
  16. import junit.framework.TestFailure;
  17. import java.util.Enumeration;
  18. import org.aspectj.bridge.IMessageHandler;
  19. import org.aspectj.bridge.IMessage;
  20. import org.aspectj.bridge.AbortException;
  21. import org.aspectj.weaver.loadtime.DefaultMessageHandler;
  22. /**
  23. * Helper to run a test as a main class, but still throw exception and not just print on stderr
  24. * upon test failure.
  25. * <p/>
  26. * This is required for Ajc test case that are also designed to work with LTW.
  27. *
  28. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  29. */
  30. public class TestHelper extends DefaultMessageHandler {
  31. public static void runAndThrowOnFailure(junit.framework.Test test) {
  32. TestRunner r = new TestRunner();
  33. TestResult rr = r.doRun(test);
  34. if (!rr.wasSuccessful()) {
  35. StringBuffer sb = new StringBuffer("\n");
  36. Enumeration e = rr.failures();
  37. while (e.hasMoreElements()) {
  38. sb.append("JUnit Failure: ");
  39. sb.append(((TestFailure)e.nextElement()).thrownException().toString());
  40. sb.append("\n");
  41. }
  42. e = rr.errors();
  43. while (e.hasMoreElements()) {
  44. sb.append("JUnit Error: ");
  45. sb.append(((TestFailure)e.nextElement()).thrownException().toString());
  46. sb.append("\n");
  47. }
  48. throw new RuntimeException(sb.toString());
  49. }
  50. }
  51. public boolean handleMessage(IMessage message) throws AbortException {
  52. boolean ret = super.handleMessage(message);
  53. if (message.getKind().isSameOrLessThan(IMessage.INFO)) {
  54. ;
  55. } else {
  56. // we do exit here since Assert.fail will only trigger a runtime exception that might
  57. // be catched by the weaver anyway
  58. System.exit(-1);
  59. }
  60. return ret;
  61. }
  62. }