您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Ajctest.java 68KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  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.internal.tools.ant.taskdefs;
  14. import java.beans.*;
  15. import java.io.*;
  16. import java.util.*;
  17. import java.util.List;
  18. import java.text.*;
  19. import org.apache.tools.ant.*;
  20. import org.apache.tools.ant.taskdefs.*;
  21. import org.apache.tools.ant.types.*;
  22. import org.aspectj.util.LangUtil;
  23. import java.awt.*;
  24. import javax.swing.*;
  25. import javax.swing.border.*;
  26. import javax.swing.event.*;
  27. import javax.swing.table.*;
  28. import javax.swing.text.*;
  29. public class Ajctest extends Task implements PropertyChangeListener {
  30. private static Ajctest CURRENT_AJCTEST;
  31. // todo shutdown hook assumes one task per VM
  32. public Ajctest() {
  33. super();
  34. CURRENT_AJCTEST = this;
  35. }
  36. private static boolean firstTime = true;
  37. public final PropertyChangeSupport bean = new PropertyChangeSupport(this);
  38. {
  39. bean.addPropertyChangeListener(this);
  40. }
  41. public void propertyChange(PropertyChangeEvent evt) {
  42. String name = evt.getPropertyName();
  43. if ("ajdoc.good".equals(name)) ajdocStats.goods++;
  44. else if ("ajc.good".equals(name)) ajcStats.goods++;
  45. else if ("run.good".equals(name)) runStats.goods++;
  46. if ("ajdoc.fail".equals(name)) ajdocStats.fails++;
  47. else if ("ajc.fail".equals(name)) ajcStats.fails++;
  48. else if ("run.fail".equals(name)) runStats.fails++;
  49. }
  50. private void fire(String prop, Object oldval, Object newval) {
  51. bean.firePropertyChange(prop, oldval, newval);
  52. }
  53. private void fire(String prop) {
  54. fire(prop, "dummy-old", "dummy-new");
  55. }
  56. private static boolean dumpresults = false;
  57. private Stats ajdocStats = new Stats();
  58. private Stats ajcStats = new Stats();
  59. private Stats runStats = new Stats();
  60. private Stats errorStats = new Stats();
  61. private static final String NO_TESTID = "NONE";
  62. private File workingdir = new File("ajworkingdir"); //XXX
  63. //fields
  64. private String testId = NO_TESTID;
  65. private List args = new Vector();
  66. private List testsets = new Vector();
  67. private Path classpath;
  68. private Path internalclasspath;
  69. private File destdir;
  70. private File dir;
  71. private File errorfile;
  72. private List testclasses = new Vector();
  73. private boolean nocompile;
  74. private Ajdoc ajdoc = null;
  75. private boolean noclean;
  76. private boolean noverify;
  77. private List depends = new Vector();
  78. //end-fields
  79. public Argfile createArgfile() {
  80. return createTestset().createArgfile();
  81. }
  82. public void setNoverify(boolean input) {
  83. if (input != noverify) noverify = input;
  84. }
  85. public void setOwningTarget(Target target) {
  86. super.setOwningTarget(target);
  87. if (null != target) {
  88. //setTestId(target.getName());
  89. }
  90. }
  91. public void setTestId(String str) {
  92. if ((null != str) && (0 < str.trim().length())) {
  93. testId = str;
  94. }
  95. }
  96. public void setArgs(String str) {
  97. if (str == null || str.length() < 1) return;
  98. StringTokenizer tok = new StringTokenizer(str, ",", false);
  99. while (tok.hasMoreTokens()) {
  100. String name = tok.nextToken().trim();
  101. if (0 < name.length()) {
  102. parse(name.startsWith("J") ? createJarg() : createArg(), name);
  103. }
  104. }
  105. }
  106. private void parse(Argument arg, String name) {
  107. int itilde = name.lastIndexOf("~");
  108. if (itilde != -1) {
  109. name = name.substring(0, itilde) + name.substring(itilde+1);
  110. }
  111. int ieq = name.lastIndexOf("=");
  112. int icolon = name.lastIndexOf(":");
  113. int ileft = name.lastIndexOf("[");
  114. int iright = name.lastIndexOf("]");
  115. boolean always = true;
  116. String rest = "";
  117. String newName = name;
  118. if (ieq != -1) {
  119. rest = name.substring(ieq+1);
  120. newName = name.substring(0, ieq);
  121. always = true;
  122. } else if (icolon != -1) {
  123. rest = name.substring(icolon+1);
  124. newName = name.substring(0, icolon);
  125. always = false;
  126. } else if (ileft != -1) {
  127. newName = name.substring(0, ileft);
  128. always = true;
  129. }
  130. String values = ileft == -1 ? rest :
  131. name.substring(ileft+1, iright > ileft ? iright : rest.length()-1);
  132. String value = null;
  133. if (itilde != -1) {
  134. String prop = project.getUserProperty(values);
  135. if (prop == null) {
  136. prop = project.getProperty(values);
  137. }
  138. if (prop != null) {
  139. value = prop;
  140. }
  141. }
  142. if (value != null) {
  143. arg.setValue(value);
  144. } else {
  145. arg.setValues(values);
  146. }
  147. arg.setName(newName);
  148. arg.setAlways(always);
  149. }
  150. public Argument createJarg() {
  151. Argument arg = new Argument(true);
  152. args.add(arg);
  153. return arg;
  154. }
  155. public Argument createArg() {
  156. Argument arg = new Argument(false);
  157. args.add(arg);
  158. return arg;
  159. }
  160. public void setClasspath(Path path) {
  161. if (classpath == null) {
  162. classpath = path;
  163. } else {
  164. classpath.append(path);
  165. }
  166. }
  167. public Path createClasspath() {
  168. if (classpath == null) {
  169. classpath = new Path(project);
  170. }
  171. return classpath.createPath();
  172. }
  173. public void setClasspathRef(Reference r) {
  174. createClasspath().setRefid(r);
  175. }
  176. public void setInternalclasspath(Path path) {
  177. if (internalclasspath == null) {
  178. internalclasspath = path;
  179. } else {
  180. internalclasspath.append(path);
  181. }
  182. }
  183. public Path createInternalclasspath() {
  184. if (internalclasspath == null) {
  185. internalclasspath = new Path(project);
  186. }
  187. return internalclasspath.createPath();
  188. }
  189. public void setInternalclasspathRef(Reference r) {
  190. createInternalclasspath().setRefid(r);
  191. }
  192. public void setDestdir(String destdir) {
  193. this.destdir = project.resolveFile(destdir);
  194. }
  195. public void setDir(File dir) {
  196. this.dir = dir;
  197. }
  198. public void setErrorfile(File errorfile) {
  199. this.errorfile = errorfile;
  200. }
  201. public Run createJava() {
  202. Run testclass = new Run(project);
  203. testclasses.add(testclass);
  204. return testclass;
  205. }
  206. public void setClasses(String str) {
  207. for (StringTokenizer t = new StringTokenizer(str, ", ", false);
  208. t.hasMoreTokens();) {
  209. createJava().setClassname(t.nextToken().trim());
  210. }
  211. }
  212. public void setTestclass(String testclass) {
  213. createJava().setClassname(testclass);
  214. }
  215. public void setAjdoc(boolean b) {
  216. if (b && ajdoc == null) {
  217. createAjdoc();
  218. } else if (!b) {
  219. ajdoc = null;
  220. }
  221. }
  222. public void setAjdocargs(String str) {
  223. createAjdoc();
  224. for (StringTokenizer t = new StringTokenizer(str, ", ", false);
  225. t.hasMoreTokens();) {
  226. ajdoc.createArg().setValue(t.nextToken().trim());
  227. }
  228. }
  229. public void addAjdoc(Ajdoc ajdoc) {
  230. this.ajdoc = ajdoc;
  231. }
  232. public Ajdoc createAjdoc() {
  233. return ajdoc = new Ajdoc();
  234. }
  235. public static class Argument {
  236. private String name;
  237. private List values = new Vector();
  238. private boolean always = true;
  239. final boolean isj;
  240. public Argument(boolean isj) {
  241. this.isj = isj;
  242. }
  243. public void setName(String str) {
  244. this.name = str.startsWith("-") ? str :
  245. ("-" + (str.startsWith("J") ? str.substring(1) : str));
  246. }
  247. public void setValues(String str) {
  248. values = new Vector();
  249. StringTokenizer tok = new StringTokenizer(str, ", ", false);
  250. while (tok.hasMoreTokens()) {
  251. values.add(tok.nextToken().trim());
  252. }
  253. }
  254. public void setValue(String value) {
  255. (values = new Vector()).add(value);
  256. }
  257. public void setAlways(boolean always) {
  258. this.always = always;
  259. }
  260. public String toString() { return name + ":" + values; }
  261. }
  262. public void setNocompile(boolean nocompile) {
  263. this.nocompile = nocompile;
  264. }
  265. private static class Stats {
  266. int goods = 0;
  267. int fails = 0;
  268. }
  269. private static class Arg {
  270. final String name;
  271. final String value;
  272. final boolean isj;
  273. Arg(String name, String value, boolean isj) {
  274. this.name = name;
  275. this.value = value;
  276. this.isj = isj;
  277. }
  278. public String toString() {
  279. return name + (!"".equals(value) ? " " + value : "");
  280. }
  281. }
  282. public Testset createTestset() {
  283. Testset testset = new Testset();
  284. testsets.add(testset);
  285. return testset;
  286. }
  287. public void setNoclean(boolean noclean) {
  288. this.noclean = noclean;
  289. }
  290. public void setDepends(String depends) {
  291. for (StringTokenizer t = new StringTokenizer(depends, ", ", false);
  292. t.hasMoreTokens();) {
  293. this.depends.add(t.nextToken().trim());
  294. }
  295. }
  296. //end-methods
  297. public static class Argfile {
  298. private String name;
  299. public void setName(String name) { this.name = name; }
  300. }
  301. public class Ajdoc {
  302. private Commandline cmd = new Commandline();
  303. public Commandline.Argument createArg() { return cmd.createArgument(); }
  304. public Commandline getCommandline() { return cmd; }
  305. public String toString() { return cmd + ""; }
  306. }
  307. public class Testset extends FileSet {
  308. private List argfileNames = new Vector();
  309. public List argfiles;
  310. public List files;
  311. public List args = new Vector();
  312. public String classname;
  313. private boolean havecludes = false;
  314. private List testclasses = new Vector();
  315. private Path classpath;
  316. private Path internalclasspath;
  317. private Ajdoc ajdoc = null;
  318. private boolean fork = false;
  319. private boolean noclean;
  320. private List depends = new Vector();
  321. public String toString() {
  322. String str = "";
  323. if (files.size() > 0) {
  324. str += "files:" + "\n";
  325. for (Iterator i = files.iterator(); i.hasNext();) {
  326. str += "\t" + i.next() + "\n";
  327. }
  328. }
  329. if (argfiles.size() > 0) {
  330. str += "argfiles:" + "\n";
  331. for (Iterator i = argfiles.iterator(); i.hasNext();) {
  332. str += "\t" + i.next() + "\n";
  333. }
  334. }
  335. if (args.size() > 0) {
  336. str += "args:" + "\n";
  337. for (Iterator i = args.iterator(); i.hasNext();) {
  338. str += "\t" + i.next() + "\n";
  339. }
  340. }
  341. if (testclasses.size() > 0) {
  342. str += "classes:" + "\n";
  343. for (Iterator i = testclasses.iterator(); i.hasNext();) {
  344. str += "\t" + i.next() + "\n";
  345. }
  346. }
  347. return str;
  348. }
  349. public void setIncludes(String includes) {
  350. super.setIncludes(includes);
  351. havecludes = true;
  352. }
  353. public void setExcludes(String excludes) {
  354. super.setExcludes(excludes);
  355. havecludes = true;
  356. }
  357. public void setIncludesfile(File includesfile) {
  358. super.setIncludesfile(includesfile);
  359. havecludes = true;
  360. }
  361. public void setExcludesfile(File excludesfile) {
  362. super.setExcludesfile(excludesfile);
  363. havecludes = true;
  364. }
  365. public void setArgfile(String name) {
  366. createArgfile().setName(name);
  367. }
  368. public void setArgfiles(String str) {
  369. StringTokenizer tok = new StringTokenizer(str, ", ", false);
  370. while (tok.hasMoreTokens()) {
  371. createArgfile().setName(tok.nextToken().trim());
  372. }
  373. }
  374. public Argfile createArgfile() {
  375. Argfile argfile = new Argfile();
  376. argfileNames.add(argfile);
  377. return argfile;
  378. }
  379. public Run createJava() {
  380. // See crashing note
  381. //Run testclass = new Run();
  382. Run testclass = new Run(project);
  383. this.testclasses.add(testclass);
  384. return testclass;
  385. }
  386. public void addJava(Run run) {
  387. this.testclasses.add(run);
  388. }
  389. public void setJava(String str) {
  390. StringTokenizer t = new StringTokenizer(str, " ");
  391. Run run = createJava();
  392. run.setClassname(t.nextToken().trim());
  393. while (t.hasMoreTokens()) {
  394. run.createArg().setValue(t.nextToken().trim());
  395. }
  396. }
  397. public void setTestclass(String testclass) {
  398. createJava().setClassname(testclass);
  399. }
  400. public void setClasses(String str) {
  401. for (StringTokenizer t = new StringTokenizer(str, ", ", false);
  402. t.hasMoreTokens();) {
  403. createJava().setClassname(t.nextToken().trim());
  404. }
  405. }
  406. public void setClasspath(Path path) {
  407. if (classpath == null) {
  408. classpath = path;
  409. } else {
  410. classpath.append(path);
  411. }
  412. }
  413. public Path createClasspath() {
  414. if (classpath == null) {
  415. classpath = new Path(project);
  416. }
  417. return classpath.createPath();
  418. }
  419. public void setClasspathRef(Reference r) {
  420. createClasspath().setRefid(r);
  421. }
  422. public void setInternalclasspath(Path path) {
  423. if (internalclasspath == null) {
  424. internalclasspath = path;
  425. } else {
  426. internalclasspath.append(path);
  427. }
  428. }
  429. public Path createInternalclasspath() {
  430. if (internalclasspath == null) {
  431. internalclasspath = new Path(project);
  432. }
  433. return internalclasspath.createPath();
  434. }
  435. public void setInternalclasspathRef(Reference r) {
  436. createInternalclasspath().setRefid(r);
  437. }
  438. public void setAjdoc(boolean b) {
  439. if (b && ajdoc == null) {
  440. createAjdoc();
  441. } else if (!b) {
  442. this.ajdoc = null;
  443. }
  444. }
  445. public Ajdoc getAjdoc() { return this.ajdoc; }
  446. public void setAjdocargs(String str) {
  447. createAjdoc();
  448. for (StringTokenizer t = new StringTokenizer(str, ", ", false);
  449. t.hasMoreTokens();) {
  450. this.ajdoc.createArg().setValue(t.nextToken().trim());
  451. }
  452. }
  453. public void addAjdoc(Ajdoc ajdoc) {
  454. this.ajdoc = ajdoc;
  455. }
  456. public Ajdoc createAjdoc() {
  457. return this.ajdoc = new Ajdoc();
  458. }
  459. public void setFork(boolean fork) {
  460. this.fork = fork;
  461. }
  462. public void setNoclean(boolean noclean) {
  463. this.noclean = noclean;
  464. }
  465. public void setDepends(String depends) {
  466. for (StringTokenizer t = new StringTokenizer(depends, ", ", false);
  467. t.hasMoreTokens();) {
  468. this.depends.add(t.nextToken().trim());
  469. }
  470. }
  471. //end-testset-methods
  472. public void resolve() throws BuildException {
  473. if (dir != null) this.setDir(dir);
  474. File src = getDir(project);
  475. argfiles = new Vector();
  476. files = new Vector();
  477. for(Iterator iter = argfileNames.iterator(); iter.hasNext();) {
  478. String name = ((Argfile)iter.next()).name;
  479. File argfile = new File(src, name);
  480. if (check(argfile, name, location)) argfiles.add(argfile);
  481. }
  482. if (havecludes || argfiles.size() <= 0) {
  483. String[] filenames =
  484. getDirectoryScanner(project).getIncludedFiles();
  485. for (int j = 0; j < filenames.length; j++) {
  486. String name = filenames[j];
  487. if (name.endsWith(".java")) {
  488. File file = new File(src, name);
  489. if (check(file, name, location)) files.add(file);
  490. }
  491. }
  492. }
  493. for (Iterator i = Ajctest.this.testclasses.iterator();
  494. i.hasNext();) {
  495. this.testclasses.add((Run)i.next());
  496. }
  497. if (this.classpath == null) {
  498. setClasspath(Ajctest.this.classpath);
  499. }
  500. if (this.internalclasspath == null) {
  501. setInternalclasspath(Ajctest.this.internalclasspath);
  502. }
  503. if (this.ajdoc == null) {
  504. this.ajdoc = Ajctest.this.ajdoc;
  505. }
  506. if (this.fork) {
  507. for (Iterator i = this.testclasses.iterator(); i.hasNext();) {
  508. ((Run)i.next()).setFork(fork);
  509. }
  510. }
  511. if (!this.noclean) {
  512. this.noclean = Ajctest.this.noclean;
  513. }
  514. this.depends.addAll(Ajctest.this.depends);
  515. }
  516. private boolean check(File file, String name, Location loc)
  517. throws BuildException {
  518. loc = loc != null ? loc : location;
  519. if (file == null) {
  520. throw new BuildException
  521. ("file " + name + " is null!", loc);
  522. }
  523. if (!file.exists()) {
  524. throw new BuildException
  525. ("file " + file + " with name " + name +
  526. " doesn't exist!", loc);
  527. }
  528. return true;
  529. }
  530. public void setArgs(String str) {
  531. if (str == null || str.length() < 1) return;
  532. StringTokenizer tok = new StringTokenizer(str, ",", false);
  533. while (tok.hasMoreTokens()) {
  534. String name = tok.nextToken().trim();
  535. parse(name.startsWith("J") ? createJarg() : createArg(), name);
  536. }
  537. }
  538. public Argument createJarg() {
  539. Argument arg = new Argument(true);
  540. args.add(arg);
  541. return arg;
  542. }
  543. public Argument createArg() {
  544. Argument arg = new Argument(false);
  545. args.add(arg);
  546. return arg;
  547. }
  548. }
  549. private void prepare() throws BuildException {
  550. }
  551. private void finish() throws BuildException {
  552. if (errors.size() > 0) {
  553. log("");
  554. log("There " + w(errors) + " " + errors.size() + " errors:");
  555. for (int i = 0; i < errors.size(); i++) {
  556. log(" ", (Failure)errors.get(i), i);
  557. }
  558. }
  559. allErrors.addAll(errors);
  560. }
  561. private void log(String space, Failure failure, int num) {
  562. String number = "[" + num + "] ";
  563. log(enough(number, 60, '-'));
  564. for (int i = number.length()-1; i > 0; i--) space += " ";
  565. log(space, failure.testset.files, "files:");
  566. log(space, failure.testset.argfiles, "argfiles:");
  567. log(space, failure.args, "args:");
  568. log(space + "msgs:" + failure.msgs);
  569. }
  570. private String enough(String str, int size, char filler) {
  571. while (str.length() < size) str += filler;
  572. return str;
  573. }
  574. private void log(String space, List list, String title) {
  575. if (list == null || list.size() < 1) return;
  576. log(space + title);
  577. for (Iterator i = list.iterator(); i.hasNext();) {
  578. log(space + " " + i.next());
  579. }
  580. }
  581. private void execute(Testset testset, List args) throws BuildException {
  582. if (testset.files.size() > 0) {
  583. log("\tfiles:");
  584. for (Iterator i = testset.files.iterator();
  585. i.hasNext();) {
  586. log("\t " + i.next());
  587. }
  588. }
  589. if (testset.argfiles.size() > 0) {
  590. log("\targfiles:");
  591. for (Iterator i = testset.argfiles.iterator();
  592. i.hasNext();) {
  593. log("\t " + i.next());
  594. }
  595. }
  596. if (args.size() > 0) {
  597. log("\targs:");
  598. for (Iterator i = args.iterator();
  599. i.hasNext();) {
  600. log("\t " + i.next());
  601. }
  602. }
  603. if (testset.testclasses.size() > 0) {
  604. log("\tclasses:");
  605. for (Iterator i = testset.testclasses.iterator();
  606. i.hasNext();) {
  607. log("\t " + i.next());
  608. }
  609. }
  610. if (!testset.noclean &&
  611. (!isSet("noclean") && !isSet("nocompile"))) {
  612. delete(destdir);
  613. make(destdir);
  614. }
  615. delete(workingdir);
  616. make(workingdir);
  617. for (Iterator i = testset.depends.iterator(); i.hasNext();) {
  618. String target = i.next()+"";
  619. // todo: capture failures here?
  620. project.executeTarget(target);
  621. }
  622. int exit;
  623. if (!isSet("nodoc") && testset.ajdoc != null) {
  624. log("\tdoc... " + testset.ajdoc);
  625. AjdocWrapper ajdoc = new AjdocWrapper(testset, args);
  626. if ((exit = ajdoc.run()) != 0) {
  627. post(testset, args, ajdoc.msgs, exit, "ajdoc");
  628. } else {
  629. fire("ajdoc.good");
  630. }
  631. fire("ajdoc.done");
  632. log("\tdone with ajdoc.");
  633. }
  634. boolean goodCompile = true;
  635. if (!isSet("nocompile") && !nocompile) {
  636. log("\tcompile" +
  637. (testset.noclean ? "(boostrapped)" : "") + "...");
  638. //AjcWrapper ajc = new AjcWrapper(testset, args);
  639. JavaCommandWrapper ajc;
  640. // XXX dependency on Ant property ajctest.compiler
  641. final String compiler = getAntProperty("ajctest.compiler");
  642. if ("eclipse".equals(compiler) || "eajc".equals(compiler)) {
  643. ajc = new EAjcWrapper(testset, args);
  644. } else if ((null == compiler) || "ajc".equals(compiler)) {
  645. ajc = new AjcWrapper(testset, args);
  646. } else if ("javac".equals(compiler)) {
  647. throw new Error("javac not supported");
  648. //ajc = new JavacWrapper(testset, args);
  649. } else {
  650. throw new Error("unknown compiler: " + compiler);
  651. }
  652. System.out.println("using compiler: " + ajc);
  653. try {
  654. if ((exit = ajc.run()) != 0) {
  655. post(testset, args, ajc.msgs, exit, "ajc");
  656. goodCompile = false;
  657. } else {
  658. fire("ajc.good");
  659. }
  660. fire("ajc.done");
  661. } catch (Throwable ___) {
  662. post(testset, args, ___+"", -1, "ajc");
  663. goodCompile = false;
  664. }
  665. }
  666. if (!goodCompile) {
  667. post(testset, new Vector(),
  668. "couldn't run classes " + testset.testclasses +
  669. "due to failed compile",
  670. -1, "run");
  671. } else if (!isSet("norun")) {
  672. for (Iterator i = testset.testclasses.iterator();
  673. i.hasNext();) {
  674. Run testclass = (Run)i.next();
  675. log("\ttest..." + testclass.classname());
  676. if (null != destdir) {
  677. testclass.setClassesDir(destdir.getAbsolutePath());
  678. }
  679. if ((exit = testclass.executeJava()) != 0) {
  680. post(testset, new Vector(), testclass.msgs, exit, "run");
  681. } else {
  682. fire("run.good");
  683. }
  684. fire("run.done");
  685. }
  686. }
  687. log("");
  688. }
  689. public void execute() throws BuildException {
  690. gui(this);
  691. dumpresults = isSet("dumpresults");
  692. prepare();
  693. log(testsets.size() + " testset" + s(testsets),
  694. Project.MSG_VERBOSE);
  695. Map testsetToArgcombo = new HashMap();
  696. List argcombos = new Vector();
  697. for (Iterator iter = testsets.iterator(); iter.hasNext();) {
  698. Testset testset = (Testset)iter.next();
  699. testset.resolve();
  700. List bothargs = new Vector(args);
  701. bothargs.addAll(testset.args);
  702. List argcombo = argcombo(bothargs);
  703. argcombos.add(new Integer(argcombo.size()));
  704. testsetToArgcombo.put(testset, argcombo);
  705. }
  706. while (!testsetToArgcombo.isEmpty()) {
  707. int _ = 1;
  708. for (Iterator iter = testsets.iterator(); iter.hasNext(); _++) {
  709. Testset testset = (Testset)iter.next();
  710. List argcombo = (List)testsetToArgcombo.get(testset);
  711. if (argcombo.size() == 0) {
  712. testsetToArgcombo.remove(testset);
  713. continue;
  714. }
  715. List args = (List)argcombo.remove(0);
  716. final String startStr = "Testset " + _ + " of " + testsets.size();
  717. String str = startStr + " / Combo " + _ + " of " + argcombos.size();
  718. log("---------- " + str + " ----------");
  719. execute(testset, args);
  720. }
  721. }
  722. // for (Iterator iter = testsets.iterator(); iter.hasNext(); _++) {
  723. // Testset testset = (Testset)iter.next();
  724. // testset.resolve();
  725. // List bothargs = new Vector(args);
  726. // bothargs.addAll(testset.args);
  727. // int __ = 1;
  728. // List argcombo = argcombo(bothargs);
  729. // log(argcombo.size() + " combination" + s(argcombo),
  730. // Project.MSG_VERBOSE);
  731. // final String startStr = "Testset " + _ + " of " + testsets.size();
  732. // for (Iterator comboiter = argcombo.iterator();
  733. // comboiter.hasNext(); __++) {
  734. // List args = (List)comboiter.next();
  735. // execute(testset, args);
  736. // log("");
  737. // }
  738. // }
  739. finish();
  740. }
  741. private void delete(File dir) throws BuildException {
  742. Delete delete = (Delete)project.createTask("delete");
  743. delete.setDir(dir);
  744. delete.execute();
  745. }
  746. private void make(File dir) throws BuildException {
  747. Mkdir mkdir = (Mkdir)project.createTask("mkdir");
  748. mkdir.setDir(dir);
  749. mkdir.execute();
  750. }
  751. private String getAntProperty(String name) {
  752. String uprop = project.getUserProperty(name);
  753. if (null == uprop) {
  754. uprop = project.getProperty(name);
  755. }
  756. return uprop;
  757. }
  758. private boolean isSet(String name) {
  759. String uprop = project.getUserProperty(name);
  760. if (uprop == null ||
  761. "no".equals(uprop) ||
  762. "false".equals(uprop)) return false;
  763. String prop = project.getProperty(name);
  764. if (prop == null ||
  765. "no".equals(prop) ||
  766. "false".equals(prop)) return false;
  767. return true;
  768. }
  769. /**
  770. * Interpose Wrapper class to catch and report exceptions
  771. * by setting a positive value for System.exit().
  772. * (In some cases it seems that Exceptions are not being reported
  773. * as errors in the tests.)
  774. * This forces the VM to fork. A forked VM is required for
  775. * two reasons:
  776. * (1) The wrapper class may have been defined by a different
  777. * class loader than the target class, so it would not be able
  778. * to load the target class;
  779. * <p>
  780. * (2) Since the wrapper class is generic, we have to pass in
  781. * the name of the target class. I choose to do this using
  782. * VM properties rather than hacking up the arguments.
  783. * <p>todo: relies on name/value of property "taskdef.jar"
  784. * to add jar with wrapper to invoking classpath.
  785. * <p>
  786. * It is beneficial for another reason:
  787. * (3) The wrapper class can be asked to test-load all classes
  788. * in a classes dir, by setting a VM property. This class
  789. * sets up the property if the value is defined using
  790. * <code>setClassesDir(String)</code>
  791. * <p>todo: if more tunnelling, generalize and parse.
  792. */
  793. public class RunWrapper extends Java {
  794. public final Class LINK_WRAPPER_CLASS = MainWrapper.class;
  795. /** tracked in MainWrapper.PROP_NAME */ // todo: since reflective, avoid direct
  796. public final String PROP_NAME = "MainWrapper.classname";
  797. /** tracked in MainWrapper.CLASSDIR_NAME */
  798. public final String CLASSDIR_NAME = "MainWrapper.classdir";
  799. public final String WRAPPER_CLASS
  800. = "org.aspectj.internal.tools.ant.taskdefs.MainWrapper";
  801. private String classname;
  802. protected String classesDir;
  803. /** capture classname here, replace with WRAPPER_CLASS */
  804. public void setClassname(String classname) {
  805. super.setClassname(WRAPPER_CLASS);
  806. this.classname = classname;
  807. }
  808. /**
  809. * Setup the requirements for the wrapper class:
  810. * <li>fork to get classpath and VM properties right</li>
  811. * <li>set VM property</li>
  812. * <li>add ${ajctest.wrapper.jar} (with wrapper class) to the classpath</li>
  813. */
  814. private void setup() {
  815. setFork(true);
  816. Commandline.Argument cname = createJvmarg();
  817. cname.setValue("-D"+PROP_NAME+"="+classname);
  818. if (!noverify) {
  819. cname = createJvmarg();
  820. cname.setValue("-Xfuture"); // todo: 1.2 or later..
  821. }
  822. if (null != classesDir) {
  823. cname = createJvmarg();
  824. cname.setValue("-D"+CLASSDIR_NAME+"="+classesDir);
  825. }
  826. // todo dependence on name/setting of ajctest.wrapper.jar
  827. String value = project.getProperty("ajctest.wrapper.jar");
  828. if (null != value) {
  829. Path wrapperPath = new Path(project, value);
  830. RunWrapper.this.createClasspath().append(wrapperPath);
  831. }
  832. }
  833. /** do setup, then super.execute() */
  834. public int executeJava() {
  835. setup();
  836. int result = super.executeJava();
  837. // snarf - also load all classes?
  838. return result;
  839. }
  840. /** set directory to scan for classes */
  841. public void setClassesDir(String dir) {
  842. classesDir = dir;
  843. }
  844. }
  845. public class Run extends RunWrapper {
  846. //public class Run extends Java
  847. private Path bootclasspath;
  848. public void setBootbootclasspath(Path path) {
  849. if (bootclasspath == null) {
  850. bootclasspath = path;
  851. } else {
  852. bootclasspath.append(path);
  853. }
  854. }
  855. public Path createBootbootclasspath() {
  856. if (bootclasspath == null) bootclasspath = new Path(this.project);
  857. return bootclasspath.createPath();
  858. }
  859. public void setBootbootclasspathRef(Reference r) {
  860. createBootbootclasspath().setRefid(r);
  861. }
  862. private Path bootclasspatha;
  863. public void setBootbootclasspatha(Path path) {
  864. if (bootclasspatha == null) {
  865. bootclasspatha = path;
  866. } else {
  867. bootclasspatha.append(path);
  868. }
  869. }
  870. public Path createBootbootclasspatha() {
  871. if (bootclasspatha == null) bootclasspatha = new Path(this.project);
  872. return bootclasspatha.createPath();
  873. }
  874. public void setBootbootclasspathaRef(Reference r) {
  875. createBootbootclasspatha().setRefid(r);
  876. }
  877. private Path bootclasspathp;
  878. public void setBootbootclasspathp(Path path) {
  879. if (bootclasspathp == null) {
  880. bootclasspathp = path;
  881. } else {
  882. bootclasspathp.append(path);
  883. }
  884. }
  885. public Path createBootbootclasspathp() {
  886. if (bootclasspathp == null) bootclasspathp = new Path(this.project);
  887. return bootclasspathp.createPath();
  888. }
  889. public void setBootbootclasspathpRef(Reference r) {
  890. createBootbootclasspathp().setRefid(r);
  891. }
  892. public Run(Project project) {
  893. super();
  894. //this.project = Ajctest.this.project;
  895. this.setTaskName("ajcjava");
  896. this.project = project;
  897. }
  898. public String msgs = "";
  899. public int executeJava() {
  900. Path cp = Ajctest.this.classpath != null ? Ajctest.this.classpath :
  901. new Path(this.project, destdir.getAbsolutePath());
  902. cp.append(Path.systemClasspath);
  903. this.setClasspath(cp);
  904. if (bootclasspath != null) {
  905. setFork(true);
  906. createJvmarg().setValue("-Xbootclasspath:" + bootclasspath);
  907. }
  908. if (bootclasspatha != null) {
  909. setFork(true);
  910. createJvmarg().setValue("-Xbootclasspath/a:" + bootclasspatha);
  911. }
  912. if (bootclasspathp != null) {
  913. setFork(true);
  914. createJvmarg().setValue("-Xbootclasspath/p:" + bootclasspathp);
  915. }
  916. int exit = -1;
  917. // todo: add timeout feature todo: this or below?
  918. try {
  919. exit = super.executeJava();
  920. } catch (Throwable t) {
  921. StringWriter sw = new StringWriter();
  922. PrintWriter out = new PrintWriter(sw);
  923. t.printStackTrace(out);
  924. msgs = sw.toString();
  925. out.close();
  926. // todo: return exit code
  927. }
  928. return exit;
  929. }
  930. public String _classname;
  931. public String classname() { return _classname; }
  932. public void setClassname(String classname) {
  933. super.setClassname(_classname = classname);
  934. }
  935. public String toString() { return _classname; }
  936. }
  937. // class Run
  938. // todo: need to run in a wrapper which report non-zero int on exception
  939. // todo: unused method? see executeJava above.
  940. private int java(String classname, Collection args) throws BuildException {
  941. Java java = (Java)project.createTask("java");
  942. java.setClassname(classname);
  943. for (Iterator i = args.iterator(); i.hasNext();) {
  944. Object o = i.next();
  945. Commandline.Argument arg = java.createArg();
  946. if (o instanceof File) {
  947. arg.setFile((File)o);
  948. } else if (o instanceof Path) {
  949. arg.setPath((Path)o);
  950. } else {
  951. arg.setValue(o+"");
  952. }
  953. }
  954. return java.executeJava();
  955. }
  956. private static List allErrors = new Vector();
  957. private List errors = new Vector();
  958. private void post(Testset testset, List args,
  959. String msgs, int exit, String type) {
  960. errors.add(new Failure(testset, args, msgs, exit, type, testId));
  961. fire(type + ".fail");
  962. }
  963. private static long startTime;
  964. private static long stopTime;
  965. private static String date(long time) {
  966. return DateFormat.getDateTimeInstance
  967. (DateFormat.FULL, DateFormat.FULL).
  968. format(new Date(time));
  969. }
  970. static {
  971. final PrintStream err = System.err;
  972. startTime = System.currentTimeMillis();
  973. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  974. private String ms(long start, long stop) {
  975. long rest = Math.abs(stop-start) / 1000;
  976. long days = rest / 86400;
  977. long hours = (rest -= days*86400) / 3600;
  978. long mins = (rest -= hours*3600) / 60;
  979. long secs = (rest -= mins*60);
  980. boolean req = false;
  981. String str = "";
  982. if (req || days > 0) {
  983. req = true;
  984. str += days + " day" + (days != 1 ? "s" : "") + " ";
  985. }
  986. if (req || hours > 0) {
  987. req = true;
  988. str += hours + " hour" + (hours != 1 ? "s" : "") + " ";
  989. }
  990. if (req || mins > 0) {
  991. req = true;
  992. str += mins + " minute" + (mins != 1 ? "s" : "") + " ";
  993. }
  994. str += secs + " second" + (secs != 1 ? "s" : "") + " ";
  995. return str;
  996. }
  997. public void run() {
  998. Ajctest current = CURRENT_AJCTEST;
  999. String oneLine = "warning: oneLine not set.";
  1000. String multiLine = "warning: multiLine not set.";
  1001. // setup oneLine
  1002. if (null == current) {
  1003. oneLine = "\nRESULT=\"ERROR\" null ACJTEST";
  1004. } else {
  1005. StringBuffer sb = new StringBuffer("\n");
  1006. int errs = Ajctest.allErrors.size();
  1007. int allFails = errs
  1008. + current.ajdocStats.fails
  1009. + current.ajcStats.fails
  1010. + current.runStats.fails;
  1011. if (1 > allFails) {
  1012. sb.append("RESULT=\"PASS\"\terrors=\"");
  1013. } else {
  1014. sb.append("RESULT=\"FAIL\"\terrors=\"");
  1015. }
  1016. sb.append(""+errs);
  1017. sb.append("\"\tajdoc.pass=\"");
  1018. sb.append(""+current.ajdocStats.goods);
  1019. sb.append("\"\tajdoc.fail=\"");
  1020. sb.append(""+current.ajdocStats.fails);
  1021. sb.append("\"\tajc.pass=\"");
  1022. sb.append(""+current.ajcStats.goods);
  1023. sb.append("\"\tajc.fail=\"");
  1024. sb.append(""+current.ajcStats.fails);
  1025. sb.append("\"\trun.pass=\"");
  1026. sb.append(""+current.runStats.goods);
  1027. sb.append("\"\trun.fail=\"");
  1028. sb.append(""+current.runStats.fails);
  1029. sb.append("\"\ttestId=\"");
  1030. sb.append(current.testId);
  1031. sb.append("\"\tproject=\"");
  1032. Project p = current.getProject();
  1033. if (null != p) sb.append(p.getName());
  1034. sb.append("\"\tfile=\"");
  1035. sb.append(""+current.getLocation());
  1036. sb.append("\"");
  1037. oneLine = sb.toString();
  1038. }
  1039. // setup multiLine
  1040. {
  1041. stopTime = System.currentTimeMillis();
  1042. String str = "";
  1043. str += "\n";
  1044. str +=
  1045. "===================================" +
  1046. "===================================" + "\n";
  1047. str += "Test started : " + date(startTime) + "\n";
  1048. str += "Test ended : " + date(stopTime) + "\n";
  1049. str += "Total time : " + ms(startTime, stopTime) + "\n";
  1050. str +=
  1051. "------------------------------" +
  1052. " Summary " +
  1053. "------------------------------" + "\n";
  1054. str += "Task\tPassed\tFailed" + "\n";
  1055. Object[] os = new Object[] {
  1056. "ajdoc", current.ajdocStats.goods+"", current.ajdocStats.fails+"",
  1057. "ajc", current.ajcStats.goods +"", current.ajcStats.fails +"",
  1058. "run", current.runStats.goods +"", current.runStats.fails +"",
  1059. };
  1060. for (int i = 0; i < os.length; i += 3) {
  1061. str += os[i] + "\t" + os[i+1] + "\t" + os[i+2] + "\n";
  1062. }
  1063. if (allErrors.size() > 0) {
  1064. str += "" + "\n";
  1065. str +=
  1066. "There " + w(allErrors) + " " +
  1067. allErrors.size() + " error" +
  1068. s(allErrors) + ":" + "\n";
  1069. for (int i = 0; i < allErrors.size(); i++) {
  1070. Failure failure = (Failure)allErrors.get(i);
  1071. str +=
  1072. "---------- Error " + i + " [" +
  1073. failure.testId + "]" +
  1074. " ------------------------------" + "\n";
  1075. str += " " + failure + "\n\n";
  1076. }
  1077. } else {
  1078. str += "No errors." + "\n";
  1079. }
  1080. str += "--------------------------" +
  1081. " End of Summary " +
  1082. "---------------------------" + "\n";
  1083. multiLine = str;
  1084. }
  1085. // print both multiLine and oneLine
  1086. err.println(multiLine);
  1087. err.println(oneLine);
  1088. if (dumpresults && (allErrors.size() +
  1089. current.ajdocStats.fails +
  1090. current.ajcStats.fails +
  1091. current.runStats.fails) > 0) {
  1092. String date = date(System.currentTimeMillis());
  1093. String filename = "ajc-errors";
  1094. for (StringTokenizer t = new StringTokenizer(date, ",: ");
  1095. t.hasMoreTokens();) {
  1096. filename += "-" + t.nextToken().trim();
  1097. }
  1098. filename += ".txt";
  1099. PrintWriter out = null;
  1100. File file = new File(filename);
  1101. System.err.println("dumping results to " + file);
  1102. try {
  1103. out = new PrintWriter(new FileWriter(file));
  1104. out.println(multiLine);
  1105. out.println(oneLine);
  1106. System.err.println("dumped results to " + file);
  1107. } catch (IOException ioe) {
  1108. if (out != null) out.close();
  1109. }
  1110. }
  1111. }
  1112. }));
  1113. }
  1114. private static String w(List list) { return a(list, "were", "was"); }
  1115. private static String s(List list) { return a(list, "s", ""); }
  1116. private static String a(List list, String some, String one) {
  1117. return list == null || list.size() != 1 ? some : one;
  1118. }
  1119. static class Failure {
  1120. public final Testset testset;
  1121. public final List args;
  1122. public final String msgs;
  1123. public final int exit;
  1124. public final String type;
  1125. public final long time;
  1126. public final String testId;
  1127. public Failure(Testset testset, List args,
  1128. String msgs, int exit, String type,
  1129. String testId) {
  1130. this.testset = testset;
  1131. this.args = args;
  1132. this.msgs = msgs;
  1133. this.exit = exit;
  1134. this.type = type;
  1135. this.time = System.currentTimeMillis();
  1136. this.testId = testId;
  1137. }
  1138. public String toString() {
  1139. String str = "testId:" + testId+ "\n";
  1140. str += "type:" + type + "\n";
  1141. str += testset + "\n";
  1142. if (args.size() > 0) {
  1143. str += " args: " + args + "\n";;
  1144. }
  1145. str += " msgs:" + msgs + "\n";
  1146. str += " exit:" + exit;
  1147. return str;
  1148. }
  1149. }
  1150. private List argcombo(List arguments) {
  1151. List combos = new Vector();
  1152. List always = new Vector();
  1153. for (Iterator iter = arguments.iterator(); iter.hasNext();) {
  1154. Argument arg = (Argument)iter.next();
  1155. if (arg.values.size() == 0) arg.values.add("");
  1156. if (!arg.always && !arg.values.contains(null)) arg.values.add(null);
  1157. if (arg.values.size() > 0) {
  1158. combos.add(arg);
  1159. } else if (arg.always) {
  1160. always.add(new Arg(arg.name, arg.values.get(0)+"", arg.isj));
  1161. }
  1162. }
  1163. List argcombo = combinations(combos);
  1164. for (Iterator iter = always.iterator(); iter.hasNext();) {
  1165. Arg arg = (Arg)iter.next();
  1166. for (Iterator comboiter = argcombo.iterator(); comboiter.hasNext();) {
  1167. ((List)comboiter.next()).add(arg);
  1168. }
  1169. }
  1170. return argcombo;
  1171. }
  1172. private abstract class ExecWrapper {
  1173. public String msgs;
  1174. public int run() {
  1175. return run(createCommandline());
  1176. }
  1177. protected abstract Commandline createCommandline();
  1178. protected final int run(Commandline cmd) {
  1179. Process process = null;
  1180. int exit = Integer.MIN_VALUE;
  1181. final StringBuffer buf = new StringBuffer();
  1182. Thread errPumper = null;
  1183. Thread outPumper = null;
  1184. try {
  1185. log("\tcalling " + cmd, Project.MSG_VERBOSE);
  1186. process = Runtime.getRuntime().exec(cmd.getCommandline());
  1187. OutputStream os = new OutputStream() {
  1188. StringBuffer sb = new StringBuffer();
  1189. public void write(int b) throws IOException {
  1190. final char c = (char)b;
  1191. buf.append(c);
  1192. if (c != '\n') {
  1193. sb.append(c);
  1194. } else {
  1195. System.err.println(sb.toString());
  1196. sb = new StringBuffer();
  1197. }
  1198. }
  1199. };
  1200. OutputStream los = new LogOutputStream(Ajctest.this,
  1201. Project.MSG_INFO);
  1202. outPumper = new Thread(new StreamPumper(process.getInputStream(),
  1203. los));
  1204. errPumper = new Thread(new StreamPumper(process.getErrorStream(),
  1205. os));
  1206. outPumper.setDaemon(true);
  1207. errPumper.setDaemon(true);
  1208. outPumper.start();
  1209. errPumper.start();
  1210. process.waitFor();
  1211. } catch (Exception e) {
  1212. e.printStackTrace();
  1213. //throw e;
  1214. } finally {
  1215. try {
  1216. if (outPumper != null) outPumper.join();
  1217. if (errPumper != null) errPumper.join();
  1218. } catch (InterruptedException ie) {
  1219. } finally {
  1220. outPumper = null;
  1221. errPumper = null;
  1222. }
  1223. exit = process.exitValue();
  1224. msgs = buf.toString();
  1225. if (exit != 0) {
  1226. log("Test failed with exit value: " + exit);
  1227. } else {
  1228. log("Success!", Project.MSG_VERBOSE);
  1229. }
  1230. if (process != null) process.destroy();
  1231. process = null;
  1232. System.err.flush();
  1233. System.out.flush();
  1234. }
  1235. return exit;
  1236. }
  1237. }
  1238. private class AjcWrapper extends JavaCommandWrapper {
  1239. public AjcWrapper(Testset testset, List args) {
  1240. super(testset, args, false);
  1241. if (testset.noclean) {
  1242. setExtraclasspath(new Path(project,
  1243. destdir.getAbsolutePath()));
  1244. }
  1245. }
  1246. String getMainClassName() {
  1247. return "org.aspectj.tools.ajc.Main";
  1248. }
  1249. }
  1250. private class EAjcWrapper extends JavaCommandWrapper {
  1251. public EAjcWrapper(Testset testset, List args) {
  1252. super(testset, args, false);
  1253. if (testset.noclean) {
  1254. setExtraclasspath(new Path(project,
  1255. destdir.getAbsolutePath()));
  1256. }
  1257. }
  1258. String getMainClassName() {
  1259. return "org.aspectj.ajdt.ajc.Main";
  1260. }
  1261. }
  1262. static List ajdocArgs(List args) {
  1263. List newargs = new Vector();
  1264. for (Iterator i = args.iterator(); i.hasNext();) {
  1265. String arg = i.next() + "";
  1266. if (arg.startsWith("-X")) {
  1267. newargs.add(arg);
  1268. } else if (arg.equals("-public") ||
  1269. arg.equals("-package") ||
  1270. arg.equals("-protected") ||
  1271. arg.equals("-private")) {
  1272. newargs.add(arg);
  1273. } else if (arg.equals("-d") ||
  1274. arg.equals("-classpath") ||
  1275. arg.equals("-cp") ||
  1276. arg.equals("-sourcepath") ||
  1277. arg.equals("-bootclasspath") ||
  1278. arg.equals("-argfile")) {
  1279. newargs.add(arg);
  1280. newargs.add(i.next()+"");
  1281. } else if (arg.startsWith("@")) {
  1282. newargs.add(arg);
  1283. }
  1284. }
  1285. return newargs;
  1286. }
  1287. private class AjdocWrapper extends JavaCommandWrapper {
  1288. public AjdocWrapper(Testset testset, List args) {
  1289. super(testset, ajdocArgs(args), true);
  1290. String[] cmds = testset.getAjdoc().getCommandline().getCommandline();
  1291. for (int i = 0; i < cmds.length; i++) {
  1292. this.args.add(cmds[i]);
  1293. }
  1294. }
  1295. String getMainClassName() {
  1296. return "org.aspectj.tools.ajdoc.Main";
  1297. }
  1298. }
  1299. private abstract class JavaCommandWrapper extends ExecWrapper {
  1300. abstract String getMainClassName();
  1301. protected Testset testset;
  1302. protected List args;
  1303. protected boolean needsClasspath;
  1304. protected Path extraClasspath;
  1305. public JavaCommandWrapper(Testset testset, List args,
  1306. boolean needsClasspath) {
  1307. this.testset = testset;
  1308. this.args = args;
  1309. this.needsClasspath = needsClasspath;
  1310. this.extraClasspath = testset.internalclasspath;
  1311. }
  1312. public void setExtraclasspath(Path extraClasspath) {
  1313. this.extraClasspath = extraClasspath;
  1314. }
  1315. public String toString() {
  1316. return LangUtil.unqualifiedClassName(getClass())
  1317. + "(" + getMainClassName() + ")";
  1318. }
  1319. protected Commandline createCommandline() {
  1320. Commandline cmd = new Commandline();
  1321. cmd.setExecutable("java");
  1322. cmd.createArgument().setValue("-classpath");
  1323. Path cp = null;
  1324. if (extraClasspath != null) {
  1325. cp = extraClasspath;
  1326. }
  1327. if (extraClasspath == null) {
  1328. Path aspectjBuildDir =
  1329. new Path(project,
  1330. project.getProperty("ajctest.pathelement"));
  1331. // todo: dependency on ant script variable name ajctest.pathelement
  1332. if (cp == null) cp = aspectjBuildDir;
  1333. else cp.append(aspectjBuildDir);
  1334. }
  1335. if (cp == null) {
  1336. cp = Path.systemClasspath;
  1337. } else {
  1338. cp.append(Path.systemClasspath);
  1339. }
  1340. cmd.createArgument().setPath(cp);
  1341. for (Iterator iter = args.iterator(); iter.hasNext();) {
  1342. Arg arg = (Arg)iter.next();
  1343. if (arg.isj) {
  1344. cmd.createArgument().setValue(arg.name);
  1345. if (!arg.value.equals("")) {
  1346. cmd.createArgument().setValue(arg.value);
  1347. }
  1348. }
  1349. }
  1350. cmd.createArgument().setValue(getMainClassName());
  1351. boolean alreadySetDestDir = false;
  1352. boolean alreadySetClasspath = false;
  1353. for (Iterator iter = args.iterator(); iter.hasNext();) {
  1354. Arg arg = (Arg)iter.next();
  1355. if (!arg.isj) {
  1356. cmd.createArgument().setValue(arg.name);
  1357. if (arg.name.equals("-d")) {
  1358. setDestdir(arg.value+"");
  1359. alreadySetDestDir = true;
  1360. }
  1361. if (arg.name.equals("-classpath")) {
  1362. alreadySetClasspath = true;
  1363. }
  1364. if (!arg.value.equals("")) {
  1365. cmd.createArgument().setValue(arg.value);
  1366. }
  1367. }
  1368. }
  1369. if (destdir == null) {
  1370. setDestdir(".");
  1371. }
  1372. if (!alreadySetDestDir) {
  1373. cmd.createArgument().setValue("-d");
  1374. cmd.createArgument().setFile(destdir);
  1375. }
  1376. if (!alreadySetClasspath && testset.classpath != null) {
  1377. cmd.createArgument().setValue("-classpath");
  1378. cmd.createArgument().setPath(testset.classpath);
  1379. } else if (needsClasspath) {
  1380. Path _cp = Ajctest.this.classpath != null ? Ajctest.this.classpath :
  1381. new Path(project, destdir.getAbsolutePath());
  1382. _cp.append(Path.systemClasspath);
  1383. cmd.createArgument().setValue("-classpath");
  1384. cmd.createArgument().setPath(_cp);
  1385. }
  1386. for (Iterator iter = testset.files.iterator(); iter.hasNext();) {
  1387. cmd.createArgument().setFile((File)iter.next());
  1388. }
  1389. for (Iterator iter = testset.argfiles.iterator(); iter.hasNext();) {
  1390. cmd.createArgument().setValue("-argfile");
  1391. cmd.createArgument().setFile((File)iter.next());
  1392. }
  1393. return cmd;
  1394. }
  1395. }
  1396. /** implement invocation of ajc */
  1397. private void java(Testset testset, List args) throws BuildException {
  1398. Java java = (Java)project.createTask("java");
  1399. java.setClassname("org.aspectj.tools.ajc.Main");
  1400. if (classpath != null) {
  1401. java.setClasspath(classpath);
  1402. }
  1403. for (Iterator iter = args.iterator(); iter.hasNext();) {
  1404. Arg arg = (Arg)iter.next();
  1405. if (arg.isj) {
  1406. java.createJvmarg().setValue(arg.name);
  1407. if (!arg.value.equals("")) {
  1408. java.createJvmarg().setValue(arg.value);
  1409. }
  1410. }
  1411. }
  1412. for (Iterator iter = args.iterator(); iter.hasNext();) {
  1413. Arg arg = (Arg)iter.next();
  1414. if (!arg.isj) {
  1415. java.createArg().setValue(arg.name);
  1416. if (!arg.value.equals("")) {
  1417. java.createArg().setValue(arg.value);
  1418. }
  1419. }
  1420. }
  1421. for (Iterator iter = testset.files.iterator(); iter.hasNext();) {
  1422. java.createArg().setFile((File)iter.next());
  1423. }
  1424. for (Iterator iter = testset.argfiles.iterator(); iter.hasNext();) {
  1425. java.createArg().setValue("-argfile");
  1426. java.createArg().setFile((File)iter.next());
  1427. }
  1428. java.setFork(true);
  1429. java.execute();
  1430. }
  1431. private void exec(Testset testset, List args) throws BuildException {
  1432. ExecTask exec = (ExecTask)project.createTask("exec");
  1433. exec.setExecutable("java");
  1434. if (classpath != null) {
  1435. exec.createArg().setValue("-classpath");
  1436. exec.createArg().setPath(classpath);
  1437. }
  1438. for (Iterator iter = args.iterator(); iter.hasNext();) {
  1439. Arg arg = (Arg)iter.next();
  1440. if (arg.isj) {
  1441. exec.createArg().setValue(arg.name);
  1442. if (!arg.value.equals("")) {
  1443. exec.createArg().setValue(arg.value);
  1444. }
  1445. }
  1446. }
  1447. exec.createArg().setValue("org.aspectj.tools.ajc.Main");
  1448. for (Iterator iter = args.iterator(); iter.hasNext();) {
  1449. Arg arg = (Arg)iter.next();
  1450. if (!arg.isj) {
  1451. exec.createArg().setValue(arg.name);
  1452. if (!arg.value.equals("")) {
  1453. exec.createArg().setValue(arg.value);
  1454. }
  1455. }
  1456. }
  1457. for (Iterator iter = testset.files.iterator(); iter.hasNext();) {
  1458. exec.createArg().setFile((File)iter.next());
  1459. }
  1460. for (Iterator iter = testset.argfiles.iterator(); iter.hasNext();) {
  1461. exec.createArg().setValue("-argfile");
  1462. exec.createArg().setFile((File)iter.next());
  1463. }
  1464. exec.execute();
  1465. }
  1466. public void handle(Throwable t) {
  1467. log("handling " + t);
  1468. if (t != null) t.printStackTrace();
  1469. log("done handling " + t);
  1470. }
  1471. private List combinations(List arglist) {
  1472. List result = new Vector();
  1473. result.add(new Vector());
  1474. for (Iterator iter = arglist.iterator(); iter.hasNext();) {
  1475. Argument arg = (Argument)iter.next();
  1476. int N = result.size();
  1477. for (int i = 0; i < N; i++) {
  1478. List to = (List)result.remove(0);
  1479. for (Iterator valiter = arg.values.iterator(); valiter.hasNext();) {
  1480. List newlist = new Vector(to);
  1481. Object val = valiter.next();
  1482. if (val != null) newlist.add(new Arg(arg.name, val+"", arg.isj));
  1483. result.add(newlist);
  1484. }
  1485. }
  1486. }
  1487. return result;
  1488. }
  1489. /////////////////////// GUI support //////////////////////////////
  1490. private static Gui gui;
  1491. private static void gui(Ajctest ajc) {
  1492. if (firstTime && ajc.isSet("gui")) {
  1493. JFrame f = new JFrame("AspectJ Test Suite");
  1494. f.getContentPane().add(BorderLayout.CENTER, gui = new Gui());
  1495. f.pack();
  1496. f.setVisible(true);
  1497. }
  1498. if (gui != null) {
  1499. ajc.bean.addPropertyChangeListener(gui);
  1500. }
  1501. firstTime = false;
  1502. }
  1503. private static class Gui extends JPanel implements PropertyChangeListener {
  1504. private FailurePanel fail = new FailurePanel();
  1505. private TablePanel table = new TablePanel();
  1506. private StatusPanel status = new StatusPanel();
  1507. public Gui() {
  1508. super(new BorderLayout());
  1509. JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  1510. split.setPreferredSize(new Dimension(500, 300));
  1511. split.add(JSplitPane.BOTTOM, fail);
  1512. split.add(JSplitPane.TOP, table);
  1513. split.setDividerLocation(200);
  1514. add(BorderLayout.CENTER, split);
  1515. add(BorderLayout.SOUTH, status);
  1516. setPreferredSize(new Dimension(640, 680));
  1517. }
  1518. public void propertyChange(PropertyChangeEvent evt) {
  1519. String name = evt.getPropertyName();
  1520. if ("ajdoc.good".equals(name)) {
  1521. status.ajc.goods.inc();
  1522. } else if ("ajc.good".equals(name)) {
  1523. status.ajc.goods.inc();
  1524. } else if ("run.good".equals(name)) {
  1525. status.runs.goods.inc();
  1526. }
  1527. if ("ajdoc.done".equals(name)) {
  1528. status.ajc.total.inc();
  1529. } else if ("ajc.done".equals(name)) {
  1530. status.ajc.total.inc();
  1531. } else if ("run.done".equals(name)) {
  1532. status.runs.total.inc();
  1533. }
  1534. if ("ajdoc.fail".equals(name)) {
  1535. status.ajc.fails.inc();
  1536. } else if ("ajc.fail".equals(name)) {
  1537. status.ajc.fails.inc();
  1538. } else if ("run.fail".equals(name)) {
  1539. status.runs.fails.inc();
  1540. }
  1541. }
  1542. private abstract static class TitledPanel extends JPanel {
  1543. public TitledPanel(LayoutManager layout, String title) {
  1544. super(layout);
  1545. setBorder(BorderFactory.createTitledBorder(title));
  1546. }
  1547. }
  1548. private static class StatusPanel extends TitledPanel {
  1549. StatusInnerPanel ajdoc = new StatusInnerPanel("Ajdoc");
  1550. StatusInnerPanel runs = new StatusInnerPanel("Runs");
  1551. StatusInnerPanel ajc = new StatusInnerPanel("Ajc");
  1552. public StatusPanel() {
  1553. super(new FlowLayout(), "Status");
  1554. add(ajdoc);
  1555. add(runs);
  1556. add(ajc);
  1557. }
  1558. private static class StatusInnerPanel extends TitledPanel {
  1559. IntField goods = new IntField(5, Color.green.darker());
  1560. IntField fails = new IntField(5, Color.red.darker());
  1561. IntField total = new IntField(5, Color.blue.darker());
  1562. public StatusInnerPanel(String str) {
  1563. super(null, str);
  1564. this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  1565. Object[] os = new Object[] {
  1566. "Passed", goods,
  1567. "Failed", fails,
  1568. "Totals", total,
  1569. };
  1570. for (int i = 0; i < os.length; i += 2) {
  1571. JPanel p = p();
  1572. p.add(new JLabel(os[i]+":"));
  1573. p.add((Component)os[i+1]);
  1574. this.add(p);
  1575. }
  1576. }
  1577. private JPanel p() {
  1578. JPanel p = new JPanel();
  1579. p.setLayout(new FlowLayout(FlowLayout.LEFT));
  1580. return p;
  1581. }
  1582. private class IntField extends JTextField {
  1583. public IntField(int i, Color fg) {
  1584. super("0", i);
  1585. this.setBackground(StatusInnerPanel.this.getBackground());
  1586. this.setForeground(fg);
  1587. this.setEditable(false);
  1588. this.setBorder(BorderFactory.createEmptyBorder());
  1589. }
  1590. public void add(int i) {
  1591. setText((Integer.parseInt(getText().trim())+i)+"");
  1592. }
  1593. public void inc() { add(1); }
  1594. }
  1595. }
  1596. }
  1597. private class TablePanel extends TitledPanel {
  1598. private DefaultTableModel model = new DefaultTableModel();
  1599. private TJable table;
  1600. private List failures = new Vector();
  1601. public TablePanel() {
  1602. super(new BorderLayout(), "Failures");
  1603. Object[] names = new String[] {
  1604. "Task", "Type", "Number", "Time"
  1605. };
  1606. for (int i = 0; i < names.length; i++) {
  1607. model.addColumn(names[i]);
  1608. }
  1609. table = new TJable(model, failures);
  1610. this.add(new JScrollPane(table), BorderLayout.CENTER);
  1611. }
  1612. private class TJable extends JTable {
  1613. private List list;
  1614. public TJable(TableModel model, List list) {
  1615. super(model);
  1616. this.list = list;
  1617. setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  1618. }
  1619. public void valueChanged(ListSelectionEvent e) {
  1620. super.valueChanged(e);
  1621. if (list == null) return;
  1622. int i = (e.getFirstIndex()-e.getLastIndex())/2;
  1623. if (list.size() > 0) {
  1624. Failure f = (Failure)list.get(i);
  1625. fail.setFailure(f);
  1626. }
  1627. }
  1628. }
  1629. public void add(Failure f, String taskname, String type,
  1630. int num, long time) {
  1631. model.addRow(new Object[]{taskname, type,
  1632. new Integer(num), date(time)});
  1633. failures.add(f);
  1634. }
  1635. }
  1636. private static class FailurePanel extends TitledPanel {
  1637. private JTextArea msgs = new JTextArea(10,50);
  1638. private InfoPanel info = new InfoPanel();
  1639. public FailurePanel() {
  1640. super(new BorderLayout(), "Failure");
  1641. msgs.setFont(StyleContext.getDefaultStyleContext().
  1642. getFont("SansSerif", Font.PLAIN, 10));
  1643. add(BorderLayout.NORTH, info);
  1644. JScrollPane sc = new JScrollPane(msgs);
  1645. sc.setBorder(BorderFactory.createTitledBorder("Messages"));
  1646. add(BorderLayout.CENTER, sc);
  1647. }
  1648. public void setText(String str) {
  1649. msgs.setText(str);
  1650. }
  1651. public void setFailure(Failure f) {
  1652. msgs.setText(f.msgs);
  1653. info.setText("Type" , f.type);
  1654. info.setText("Args" , f.args);
  1655. info.setText("Exit" , f.exit+"");
  1656. info.setText("Time" , date(f.time));
  1657. info.setText("Files" , f.testset.files);
  1658. info.setText("Classnames" , f.testset.testclasses);
  1659. }
  1660. private static class InfoPanel extends JPanel {
  1661. Map fields = new HashMap();
  1662. public void setText(String key, Object str) {
  1663. ((JTextField)fields.get(key)).setText(str+"");
  1664. }
  1665. public InfoPanel() {
  1666. super(new GridBagLayout());
  1667. LabelFieldGBC gbc = new LabelFieldGBC();
  1668. Object[] os = new Object[] {
  1669. "Type",
  1670. "Args",
  1671. "Exit",
  1672. "Time",
  1673. "Files",
  1674. "Classnames",
  1675. };
  1676. for (int i = 0; i < os.length; i++) {
  1677. String name = os[i]+"";
  1678. JLabel label = new JLabel(name+":");
  1679. JTextField comp = new JTextField(25);
  1680. comp.setEditable(false);
  1681. comp.setBackground(Color.white);
  1682. comp.setBorder(BorderFactory.
  1683. createBevelBorder(BevelBorder.LOWERED));
  1684. label.setLabelFor(comp);
  1685. fields.put(name, comp);
  1686. add(label, gbc.forLabel());
  1687. add(comp, gbc.forField());
  1688. }
  1689. add(new JLabel(), gbc.forLastLabel());
  1690. }
  1691. }
  1692. private static class LabelFieldGBC extends GridBagConstraints {
  1693. public LabelFieldGBC() {
  1694. insets = new Insets(1,3,1,3);
  1695. gridy = RELATIVE;
  1696. gridheight = 1;
  1697. gridwidth = 1;
  1698. }
  1699. public LabelFieldGBC forLabel() {
  1700. fill = NONE;
  1701. gridx = 0;
  1702. anchor = NORTHEAST;
  1703. weightx = 0.0;
  1704. return this;
  1705. }
  1706. public LabelFieldGBC forLastLabel() {
  1707. forLabel();
  1708. fill = VERTICAL;
  1709. weighty = 1.0;
  1710. return this;
  1711. }
  1712. public LabelFieldGBC forField() {
  1713. fill = HORIZONTAL;
  1714. gridx = 1;
  1715. anchor = CENTER;
  1716. weightx = 1.0;
  1717. return this;
  1718. }
  1719. public LabelFieldGBC forLastField() {
  1720. forField();
  1721. fill = BOTH;
  1722. weighty = 1.0;
  1723. return this;
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }