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

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