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

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