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.

CompilerRunTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* *******************************************************************
  2. * Copyright (c) 2003 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://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.harness.bridge;
  13. import java.io.File;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import org.aspectj.bridge.ICommand;
  17. import org.aspectj.bridge.IMessageHandler;
  18. import org.aspectj.bridge.IMessageHolder;
  19. import org.aspectj.bridge.MessageHandler;
  20. import org.aspectj.testing.run.IRunIterator;
  21. import org.aspectj.testing.run.RunStatus;
  22. import org.aspectj.testing.run.Runner;
  23. import org.aspectj.util.FileUtil;
  24. import org.aspectj.util.LangUtil;
  25. import junit.framework.TestCase;
  26. /**
  27. * Use a stub compiler/ICommand to verify command-line passed
  28. * to the compiler by the harness.
  29. */
  30. public class CompilerRunTest extends TestCase {
  31. /** String for each dummy report: {run|repeat}: [{args}] */
  32. private static ArrayList dummyReports = new ArrayList();
  33. private static void dummyRunning(String[] args) {
  34. dummyReports.add("run: " + Arrays.asList(args));
  35. }
  36. // private static void dummyRepeating(String[] args) {
  37. // dummyReports.add("repeat: " + Arrays.asList(args));
  38. // }
  39. private File testBaseDir;
  40. public CompilerRunTest(String name) {
  41. super(name);
  42. }
  43. public void setUp() {
  44. testBaseDir = new File("../testing/temp-CompilerRunTest");
  45. File f = new File(testBaseDir, "one");
  46. f.mkdirs();
  47. assertTrue(f.canRead());
  48. f = new File(testBaseDir, "two");
  49. f.mkdirs();
  50. assertTrue(f.canRead());
  51. f = new File(testBaseDir, "Foo.java");
  52. String foo = "public class Foo { public void main(String[] s) { System.out.println(\"Hello!\");}}";
  53. String err = FileUtil.writeAsString(f, foo);
  54. assertTrue(err, null == err);
  55. assertTrue(f.canRead());
  56. }
  57. public void tearDown() {
  58. FileUtil.deleteContents(testBaseDir);
  59. testBaseDir.delete();
  60. testBaseDir = null;
  61. }
  62. public void testExtDirs() {
  63. // String[] globals = null;
  64. CompilerRun.Spec spec = new CompilerRun.Spec();
  65. spec.setExtdirs("one,two");
  66. spec.setFiles("Foo.java");
  67. checkCommandLine(testBaseDir, spec, null, "-extdirs");
  68. }
  69. void checkCommandLine(
  70. File testBaseDir,
  71. CompilerRun.Spec spec,
  72. String[] globals,
  73. String expectedInCommand) {
  74. assertTrue(0 == dummyReports.size());
  75. assertTrue(checkCompilerRun(testBaseDir, spec, globals, null));
  76. assertTrue(dummyReports.toString(), 1 == dummyReports.size());
  77. String command = (String) dummyReports.remove(0);
  78. assertTrue(0 == dummyReports.size());
  79. if ((null == command)
  80. || (-1 == command.indexOf(expectedInCommand))) {
  81. assertTrue("expected "
  82. + expectedInCommand
  83. + "got "
  84. + command,
  85. false);
  86. }
  87. }
  88. /** run with dummy compiler */
  89. boolean checkCompilerRun(
  90. File testBaseDir,
  91. CompilerRun.Spec spec,
  92. String[] globals,
  93. MessageHandler handler) {
  94. LangUtil.throwIaxIfNull(spec, "spec");
  95. if (null == handler) {
  96. handler = new MessageHandler();
  97. }
  98. spec.setPermitAnyCompiler(true);
  99. spec.setCompiler(DummyCompiler.class.getName());
  100. AbstractRunSpec.RT parentRuntime = new AbstractRunSpec.RT();
  101. if (!LangUtil.isEmpty(globals)) {
  102. parentRuntime.setOptions(globals);
  103. }
  104. boolean adopted =
  105. spec.adoptParentValues(parentRuntime, handler);
  106. if (!adopted) {
  107. String s = "not adopted spec="
  108. + spec
  109. + " globals="
  110. + (LangUtil.isEmpty(globals)
  111. ? "[]"
  112. : Arrays.asList(globals).toString())
  113. + " -- "
  114. + handler;
  115. assertTrue(s, false);
  116. }
  117. if (0 != handler.numMessages(null, true)) {
  118. assertTrue("unexpected " + handler, false);
  119. }
  120. return run(testBaseDir, spec);
  121. }
  122. /** Run the compiler run specified by the spec */
  123. protected boolean run(File testBaseDir, CompilerRun.Spec spec) {
  124. // his is created using the Spec.</li>
  125. // * <li>setupAjcRun(Sandbox, Validator) is invoked,
  126. // * at which point this populates the shared sandbox
  127. // * with values derived from the spec and also
  128. // * sets up internal state based on both the sandbox
  129. // * and the spec.</li>
  130. // * <li>run(IRunStatus) is invoked,
  131. LangUtil.throwIaxIfNull(spec, "spec");
  132. Runner runner = new Runner();
  133. IMessageHolder holder = new MessageHandler();
  134. RunStatus status = new RunStatus(holder, runner);
  135. status.setIdentifier(spec);
  136. Validator validator = new Validator(status);
  137. validator.lock(this);
  138. Sandbox sandbox = null;
  139. try {
  140. sandbox = new Sandbox(testBaseDir, validator);
  141. IRunIterator test = spec.makeRunIterator(sandbox, validator);
  142. return runner.runIterator(test, status, null);
  143. } finally {
  144. validator.unlock(this);
  145. validator.deleteTempFiles(true);
  146. }
  147. }
  148. public static class DummyCompiler implements ICommand {
  149. private String[] command;
  150. public DummyCompiler() {
  151. }
  152. public boolean runCommand(
  153. String[] args,
  154. IMessageHandler handler) {
  155. command = (String[]) LangUtil.safeCopy(args, new String[0]);
  156. CompilerRunTest.dummyRunning(command);
  157. return true;
  158. }
  159. public boolean repeatCommand(IMessageHandler handler) {
  160. CompilerRunTest.dummyRunning(command);
  161. return true;
  162. }
  163. }
  164. }