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.

CommandTestCase.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  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://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.batch;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import junit.framework.TestCase;
  20. import org.aspectj.ajdt.ajc.AjdtAjcTests;
  21. import org.aspectj.ajdt.ajc.AjdtCommand;
  22. import org.aspectj.bridge.ICommand;
  23. import org.aspectj.bridge.IMessage;
  24. import org.aspectj.bridge.IMessageHandler;
  25. import org.aspectj.bridge.IMessageHolder;
  26. import org.aspectj.bridge.MessageHandler;
  27. import org.aspectj.testing.util.TestUtil;
  28. import org.aspectj.tools.ajc.Ajc;
  29. import org.aspectj.tools.ajc.AjcTests;
  30. import org.aspectj.weaver.bcel.LazyClassGen;
  31. public abstract class CommandTestCase extends TestCase {
  32. /**
  33. * Constructor for CommandTestCase.
  34. * @param name
  35. */
  36. public CommandTestCase(String name) {
  37. super(name);
  38. }
  39. public static final int[] NO_ERRORS = new int[0];
  40. public static final int[] TOP_ERROR = new int[0];
  41. private File sandbox;
  42. public void checkCompile(String source, int[] expectedErrors) {
  43. checkCompile(source, new String[0], expectedErrors, getSandboxName());
  44. }
  45. protected void runMain(String className) {
  46. TestUtil.runMain(getSandboxName(), className);
  47. }
  48. public static void checkCompile(String source, String[] extraArgs, int[] expectedErrors, String sandboxName) {
  49. List args = new ArrayList();
  50. args.add("-verbose");
  51. args.add("-d");
  52. args.add(sandboxName);
  53. args.add("-classpath");
  54. args.add(getRuntimeClasspath() + File.pathSeparator +
  55. "../lib/junit/junit.jar");
  56. args.add("-g"); //XXX need this to get sourcefile and line numbers, shouldn't
  57. for (int i = 0; i < extraArgs.length; i++) {
  58. args.add(extraArgs[i]);
  59. }
  60. args.add(AjdtAjcTests.TESTDATA_PATH + "/" + source);
  61. runCompiler(args, expectedErrors);
  62. }
  63. public void testEmptyForAntJUnitSupport() {}
  64. public void checkMultipleCompile(String source) throws InterruptedException {
  65. List args = new ArrayList();
  66. args.add("-verbose");
  67. args.add("-d");
  68. args.add(getSandboxName());
  69. args.add("-classpath");
  70. args.add(getRuntimeClasspath());
  71. args.add(AjdtAjcTests.TESTDATA_PATH + "/" + source);
  72. ICommand compiler = runCompiler(args, NO_ERRORS);
  73. Thread.sleep(100);
  74. rerunCompiler(compiler);
  75. }
  76. public void rerunCompiler(ICommand command) {
  77. MessageHandler myHandler = new MessageHandler();
  78. // List recompiledFiles = new ArrayList();
  79. if (!command.repeatCommand(myHandler)) {
  80. assertTrue("recompile failed", false);
  81. }
  82. assertEquals(0, myHandler.numMessages(IMessage.ERROR, true));
  83. }
  84. public static ICommand runCompiler(List args, int[] expectedErrors) {
  85. ICommand command = new AjdtCommand();
  86. MessageHandler myHandler = new MessageHandler();
  87. myHandler.setInterceptor(org.aspectj.tools.ajc.Main.MessagePrinter.TERSE);
  88. boolean result = command.runCommand((String[])args.toArray(new String[args.size()]), myHandler);
  89. System.out.println("result: " + result);
  90. // System.out.println("errors: " + Arrays.asList(myHandler.getErrors()));
  91. // System.out.println("warnings: " + Arrays.asList(myHandler.getWarnings()));
  92. int nErrors = myHandler.numMessages(IMessage.ERROR, IMessageHolder.EQUAL);
  93. if (expectedErrors == NO_ERRORS) {
  94. if (0 != nErrors) {
  95. String s = ""+Arrays.asList(myHandler.getErrors());
  96. assertTrue("unexpected errors: " + s, false);
  97. }
  98. } else if (expectedErrors == TOP_ERROR) { // ?? what is this?
  99. assertTrue("expected error", nErrors > 0);
  100. } else {
  101. List errors = new ArrayList(Arrays.asList(myHandler.getErrors()));
  102. for (int i=0, len=expectedErrors.length; i < len; i++) {
  103. int line = expectedErrors[i];
  104. boolean found = false;
  105. for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
  106. IMessage m = (IMessage)iter.next();
  107. if (m.getSourceLocation() != null && m.getSourceLocation().getLine() == line) {
  108. found = true;
  109. iter.remove();
  110. }
  111. }
  112. assertTrue("didn't find error on line " + line, found);
  113. }
  114. if (errors.size() > 0) {
  115. assertTrue("didn't expect errors: " + errors, false);
  116. }
  117. }
  118. return command;
  119. }
  120. public static void printGenerated(String path, String name) throws IOException {
  121. String fullpath = AjdtAjcTests.TESTDATA_PATH + "/" + path;
  122. LazyClassGen.disassemble(fullpath, name, System.out);
  123. }
  124. /** incremental test case adapter to JUnit */
  125. public class IncCase extends IncrementalCase {
  126. protected void fail(IMessageHandler handler, String mssg) {
  127. assertTrue(mssg, false);
  128. }
  129. protected void message(
  130. IMessage.Kind kind,
  131. String mssg,
  132. IMessageHandler handler) {
  133. if ((kind == IMessage.FAIL) || (kind == IMessage.ABORT)) {
  134. assertTrue(mssg, false);
  135. } else {
  136. System.err.println("IncCase " + kind + ": " + mssg); // XXX
  137. }
  138. super.message(kind, mssg, handler);
  139. }
  140. }
  141. /** get the location of the org.aspectj.lang & runtime classes */
  142. protected static String getRuntimeClasspath() {
  143. return AjcTests.aspectjrtClasspath();
  144. }
  145. protected String getSandboxName () {
  146. return sandbox.getAbsolutePath();
  147. }
  148. protected void setUp() throws Exception {
  149. super.setUp();
  150. sandbox = Ajc.createEmptySandbox();
  151. }
  152. }