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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.harness.bridge;
  14. import org.aspectj.bridge.ICommand;
  15. import org.aspectj.bridge.IMessage;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.testing.run.IRunIterator;
  18. import org.aspectj.testing.run.IRunStatus;
  19. import org.aspectj.testing.run.WrappedRunIterator;
  20. import org.aspectj.testing.xml.SoftMessage;
  21. import org.aspectj.testing.xml.XMLWriter;
  22. import org.aspectj.util.FileUtil;
  23. import org.aspectj.util.LangUtil;
  24. import java.io.File;
  25. import java.io.FileFilter;
  26. import java.io.IOException;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.Collections;
  30. import java.util.List;
  31. /**
  32. * An incremental compiler run takes an existing compiler commmand
  33. * from the sandbox, updates the staging directory, and recompiles.
  34. * The staging directory is updated by prefix/suffix rules applied
  35. * to files found below Sandbox.testBaseSrcDir.
  36. * Files with suffix .{tag}.java are owned by this run
  37. * and are copied to the staging directory
  38. * unless they are prefixed "delete.", in which case the
  39. * corresponding file is deleted. Any "owned" file is passed to
  40. * the compiler as the list of changed files.
  41. * The files entry contains the expected files recompiled. XXX underinclusive
  42. * XXX prefer messages for expected files?
  43. * XXX later: also support specified paths, etc.
  44. */
  45. public class IncCompilerRun implements IAjcRun {
  46. final Spec spec; // nonfinal later to make re-runnable
  47. Sandbox sandbox;
  48. /**
  49. * @param handler must not be null, but may be reused in the same thread
  50. */
  51. public IncCompilerRun(Spec spec) {
  52. LangUtil.throwIaxIfNull(spec, "spec");
  53. this.spec = spec;
  54. }
  55. /**
  56. * Initialize this from the sandbox, using compiler and changedFiles.
  57. * @param sandbox the Sandbox setup for this test, including copying
  58. * any changed files, etc.
  59. * @see org.aspectj.testing.harness.bridge.AjcTest.IAjcRun#setup(File, File)
  60. * @throws AbortException containing IOException or IllegalArgumentException
  61. * if the staging operations fail
  62. */
  63. public boolean setupAjcRun(Sandbox sandbox, Validator validator) {
  64. LangUtil.throwIaxIfNull(validator, "validator");
  65. if (!validator.nullcheck(sandbox, "sandbox")
  66. || !validator.nullcheck(spec, "spec")
  67. || !validator.nullcheck(spec.tag, "fileSuffix")) {
  68. return false;
  69. }
  70. File srcDir = sandbox.getTestBaseSrcDir(this);
  71. File destDir = sandbox.stagingDir;
  72. if (!validator.canReadDir(srcDir, "testBaseSrcDir")
  73. || !validator.canReadDir(destDir, "stagingDir")) {
  74. return false;
  75. }
  76. this.sandbox = sandbox;
  77. return doStaging(validator);
  78. }
  79. /**
  80. * Handle copying and deleting of files per tag.
  81. * This returns false unless some file was copied or deleted successfully
  82. * and there were no failures copying or deleting files.
  83. * @return true if staging completed successfully
  84. */
  85. boolean doStaging(final Validator validator) {
  86. boolean result = false;
  87. try {
  88. //ArrayList changed = new ArrayList();
  89. final String toSuffix = ".java";
  90. final String fromSuffix = "." + spec.tag + toSuffix;
  91. // copy our tagged generation of files to the staging directory,
  92. // deleting any with ChangedFilesCollector.DELETE_SUFFIX
  93. class intHolder {
  94. int numCopies;
  95. int numDeletes;
  96. int numFails;
  97. }
  98. final intHolder holder = new intHolder();
  99. FileFilter deleteOrCount = new FileFilter() {
  100. final String clip = ".delete" + toSuffix;
  101. /** do copy unless file should be deleted */
  102. public boolean accept(File file) {
  103. boolean doCopy = true;
  104. String path = file.getAbsolutePath();
  105. if (!path.endsWith(clip)) {
  106. holder.numCopies++;
  107. } else {
  108. doCopy = false;
  109. path = path.substring(0, path.length()-clip.length()) + toSuffix;
  110. File toDelete = new File(path);
  111. if (toDelete.delete()) {
  112. holder.numDeletes++;
  113. } else {
  114. validator.fail("unable to delete file: " + path);
  115. holder.numFails++;
  116. }
  117. }
  118. return doCopy;
  119. }
  120. };
  121. File srcDir = sandbox.getTestBaseSrcDir(this);
  122. File destDir = sandbox.stagingDir;
  123. FileUtil.copyDir(srcDir, destDir, fromSuffix, toSuffix, deleteOrCount);
  124. if ((0 == holder.numCopies) && (0 == holder.numDeletes)) {
  125. validator.fail("no files changed??");
  126. } else {
  127. result = (0 == holder.numFails);
  128. }
  129. } catch (NullPointerException npe) {
  130. validator.fail("staging - input", npe);
  131. } catch (IOException e) {
  132. validator.fail("staging - operations", e);
  133. }
  134. return result;
  135. }
  136. /**
  137. * @see org.aspectj.testing.run.IRun#run(IRunStatus)
  138. */
  139. public boolean run(IRunStatus status) {
  140. ICommand compiler = sandbox.getCommand(this);
  141. if (null == compiler) {
  142. MessageUtil.abort(status, "null compiler");
  143. }
  144. // // This is a list of expected classes (in File-normal form
  145. // // relative to base class/src dir, without .class suffix
  146. // // -- like "org/aspectj/tools/ajc/Main")
  147. // // A preliminary list is generated in doStaging.
  148. // ArrayList expectedClasses = doStaging(status);
  149. // if (null == expectedClasses) {
  150. // return false;
  151. // }
  152. //
  153. // // now add any (additional) expected-class entries listed in the spec
  154. // // normalize to a similar file path (and do info messages for redundancies).
  155. //
  156. // List alsoChanged = spec.getPathsAsFile(sandbox.stagingDir);
  157. // for (Iterator iter = alsoChanged.iterator(); iter.hasNext();) {
  158. // File f = (File) iter.next();
  159. //
  160. // if (expectedClasses.contains(f)) {
  161. // // XXX remove old comment changed.contains() works b/c getPathsAsFile producing both File
  162. // // normalizes the paths, and File.equals(..) compares these lexically
  163. // String s = "specification of changed file redundant with tagged file: ";
  164. // MessageUtil.info(status, s + f);
  165. // } else {
  166. // expectedClasses.add(f);
  167. // }
  168. // }
  169. //
  170. // // now can create handler, use it for reporting
  171. // List errors = spec.getMessages(IMessage.ERROR);
  172. // List warnings = spec.getMessages(IMessage.WARNING);
  173. // AjcMessageHandler handler = new AjcMessageHandler(errors, warnings, expectedClasses);
  174. // same DirChanges handling for JavaRun, CompilerRun, IncCompilerRun
  175. // XXX around advice or template method/class
  176. DirChanges dirChanges = null;
  177. if (!LangUtil.isEmpty(spec.dirChanges)) {
  178. LangUtil.throwIaxIfFalse(1 == spec.dirChanges.size(), "expecting only 1 dirChanges");
  179. dirChanges = new DirChanges((DirChanges.Spec) spec.dirChanges.get(0));
  180. if (!dirChanges.start(status, sandbox.classesDir)) {
  181. return false; // setup failed
  182. }
  183. }
  184. List errors = spec.getMessages(IMessage.ERROR);
  185. List warnings = spec.getMessages(IMessage.WARNING);
  186. List expectRecompiled = Collections.EMPTY_LIST;
  187. AjcMessageHandler handler = new AjcMessageHandler(errors, warnings, expectRecompiled);
  188. boolean handlerResult = false;
  189. boolean commandResult = false;
  190. boolean result = false;
  191. boolean report = false;
  192. try {
  193. handler.init();
  194. final long startTime = System.currentTimeMillis();
  195. commandResult = compiler.repeatCommand(handler);
  196. // XXX disabled LangUtil.throwIaxIfNotAllAssignable(actualRecompiled, File.class, "recompiled");
  197. report = true;
  198. // handler does not verify sandbox...
  199. handlerResult = handler.passed();
  200. if (!handlerResult) {
  201. result = false;
  202. } else {
  203. result = (commandResult == handler.expectingCommandTrue());
  204. if (! result) {
  205. String m = commandResult
  206. ? "incremental compile command did not return false as expected"
  207. : "incremental compile command returned false unexpectedly";
  208. MessageUtil.fail(status, m);
  209. } else if (null != dirChanges) {
  210. result = dirChanges.end(status, sandbox.testBaseDir);
  211. }
  212. }
  213. } finally {
  214. if (!result || spec.runtime.isVerbose()) { // more debugging context in case of failure
  215. MessageUtil.info(handler, "spec: " + spec.toLongString());
  216. MessageUtil.info(handler, "sandbox: " + sandbox);
  217. String[] classes = FileUtil.listFiles(sandbox.classesDir);
  218. MessageUtil.info(handler, "sandbox.classes: " + Arrays.asList(classes));
  219. }
  220. // XXX weak - actual messages not reported in real-time, no fast-fail
  221. if (report) {
  222. handler.report(status);
  223. }
  224. }
  225. return result;
  226. }
  227. private boolean hasFile(ArrayList changed, File f) {
  228. return changed.contains(f); // d
  229. }
  230. public String toString() {
  231. return "" + spec;
  232. // return "IncCompilerRun(" + spec + ")"; // XXX
  233. }
  234. // /** @see IXmlWritable#writeXml(XMLWriter) */
  235. // public void writeXml(XMLWriter out) {
  236. // String elementName = "inc-compile";
  237. // String tagAttr = out.makeAttribute("tag", spec.tag);
  238. // List messages = spec.getMessages();
  239. // int nMessages = messages.size();
  240. // if (0 == nMessages) {
  241. // out.printElement(elementName, tagAttr);
  242. // } else {
  243. // out.startElement(elementName, tagAttr, true);
  244. // SoftMessage.writeXml(out, messages);
  245. // out.endElement(elementName);
  246. // }
  247. // }
  248. //
  249. /**
  250. * initializer/factory for IncCompilerRun.
  251. */
  252. public static class Spec extends AbstractRunSpec {
  253. public static final String XMLNAME = "inc-compile";
  254. protected ArrayList classesAdded;
  255. protected ArrayList classesRemoved;
  256. protected ArrayList classesUpdated;
  257. /**
  258. * skip description, skip sourceLocation,
  259. * do keywords, skip options, do paths as classes, do comment,
  260. * do dirChanges, do messages but skip children.
  261. */
  262. private static final XMLNames NAMES = new XMLNames(XMLNames.DEFAULT,
  263. "", "", null, "", "classes", null, false, false, true);
  264. /** identifies files this run owns, so {name}.{tag}.java maps to {name}.java */
  265. String tag;
  266. public Spec() {
  267. super(XMLNAME);
  268. setStaging(true);
  269. classesAdded = new ArrayList();
  270. classesRemoved = new ArrayList();
  271. classesUpdated = new ArrayList();
  272. }
  273. public void setTag(String input) {
  274. tag = input;
  275. }
  276. public String toString() {
  277. return "IncCompile.Spec(" + tag + ", " + super.toString() + ")";
  278. }
  279. /** override to set dirToken to Sandbox.CLASSES and default suffix to ".class" */
  280. public void addDirChanges(DirChanges.Spec spec) { // XXX copy/paste of CompilerRun.Spec...
  281. if (null == spec) {
  282. return;
  283. }
  284. spec.setDirToken(Sandbox.CLASSES_DIR);
  285. spec.setDefaultSuffix(".class");
  286. super.addDirChanges(spec);
  287. }
  288. /** @return a IncCompilerRun with this as spec if setup completes successfully. */
  289. public IRunIterator makeRunIterator(Sandbox sandbox, Validator validator) {
  290. IncCompilerRun run = new IncCompilerRun(this);
  291. if (run.setupAjcRun(sandbox, validator)) {
  292. // XXX need name
  293. return new WrappedRunIterator(this, run);
  294. }
  295. return null;
  296. }
  297. /**
  298. * Write this out as a compile element as defined in
  299. * AjcSpecXmlReader.DOCTYPE.
  300. * @see AjcSpecXmlReader#DOCTYPE
  301. * @see IXmlWritable#writeXml(XMLWriter)
  302. */
  303. public void writeXml(XMLWriter out) {
  304. String attr = XMLWriter.makeAttribute("tag", tag);
  305. out.startElement(xmlElementName, attr, false);
  306. super.writeAttributes(out);
  307. out.endAttributes();
  308. if (!LangUtil.isEmpty(dirChanges)) {
  309. DirChanges.Spec.writeXml(out, dirChanges);
  310. }
  311. List messages = getMessages();
  312. if (0 < messages.size()) {
  313. SoftMessage.writeXml(out, messages);
  314. }
  315. out.endElement(xmlElementName);
  316. }
  317. public void setClassesAdded(String items) {
  318. addItems(classesAdded, items);
  319. }
  320. public void setClassesUpdated(String items) {
  321. addItems(classesUpdated, items);
  322. }
  323. public void setClassesRemoved(String items) {
  324. addItems(classesRemoved, items);
  325. }
  326. private void addItems(ArrayList list, String items) {
  327. if (null != items) {
  328. String[] classes = XMLWriter.unflattenList(items);
  329. if (!LangUtil.isEmpty(classes)) {
  330. for (int i = 0; i < classes.length; i++) {
  331. if (!LangUtil.isEmpty(classes[i])) {
  332. list.add(classes[i]);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. } // class IncCompilerRun.Spec
  339. }
  340. // // XXX replaced with method-local class - revisit if useful
  341. //
  342. // /**
  343. // * This class collects the list of all changed files and
  344. // * deletes the corresponding file for those prefixed "delete."
  345. // */
  346. // static class ChangedFilesCollector implements FileFilter {
  347. // static final String DELETE_SUFFIX = ".delete.java";
  348. // static final String REPLACE_SUFFIX = ".java";
  349. // final ArrayList changed;
  350. // final Validator validator;
  351. // /** need this to generate paths by clipping */
  352. // final File destDir;
  353. //
  354. // /** @param changed the sink for all files changed (full paths) */
  355. // public ChangedFilesCollector(ArrayList changed, File destDir, Validator validator) {
  356. // LangUtil.throwIaxIfNull(validator, "ChangedFilesCollector - handler");
  357. // this.changed = changed;
  358. // this.validator = validator;
  359. // this.destDir = destDir;
  360. // }
  361. //
  362. // /**
  363. // * This converts the input File to normal String path form
  364. // * (without any source suffix) and adds it to the list changed.
  365. // * If the name of the file is suffixed ".delete..", then
  366. // * delete the corresponding file, and return false (no copy).
  367. // * Return true otherwise (copy file).
  368. // * @see java.io.FileFilter#accept(File)
  369. // */
  370. // public boolean accept(File file) {
  371. // final String aname = file.getAbsolutePath();
  372. // String name = file.getName();
  373. // boolean doCopy = true;
  374. // boolean failed = false;
  375. // if (name.endsWith(DELETE_SUFFIX)) {
  376. // name = name.substring(0,name.length()-DELETE_SUFFIX.length());
  377. // file = file.getParentFile();
  378. // file = new File(file, name + REPLACE_SUFFIX);
  379. // if (!file.canWrite()) {
  380. // validator.fail("file to delete is not writable: " + file);
  381. // failed = true;
  382. // } else if (!file.delete()) {
  383. // validator.fail("unable to delete file: " + file);
  384. // failed = true;
  385. // }
  386. // doCopy = false;
  387. // }
  388. // if (!failed && doCopy) {
  389. // int clip = FileUtil.sourceSuffixLength(file);
  390. // if (-1 != clip) {
  391. // name.substring(0, name.length()-clip);
  392. // }
  393. // if (null != destDir) {
  394. // String path = destDir.getPath();
  395. // if (!LangUtil.isEmpty(path)) {
  396. // // XXX incomplete
  397. // if (name.startsWith(path)) {
  398. // } else {
  399. // int loc = name.lastIndexOf(path);
  400. // if (-1 == loc) { // sigh
  401. //
  402. // } else {
  403. //
  404. // }
  405. // }
  406. // }
  407. // }
  408. // name = FileUtil.weakNormalize(name);
  409. // changed.add(file);
  410. // }
  411. // return doCopy;
  412. // }
  413. // };