Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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